19
19
20
20
/*
21
21
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22
- * Portions Copyright (c) 2018, Chris Fraire <[email protected] >.
22
+ * Portions Copyright (c) 2018-2019 , Chris Fraire <[email protected] >.
23
23
*/
24
24
package org .opengrok .indexer .util ;
25
25
26
+ import static org .junit .Assert .assertFalse ;
26
27
import static org .junit .Assert .assertNotNull ;
27
28
import static org .junit .Assert .assertTrue ;
28
29
41
42
*/
42
43
public class TestRepository {
43
44
45
+ private final RuntimeEnvironment env ;
44
46
private File sourceRoot ;
45
47
private File dataRoot ;
48
+ private File externalRoot ;
49
+
50
+ public TestRepository () {
51
+ env = RuntimeEnvironment .getInstance ();
52
+ }
46
53
47
54
public void createEmpty () throws IOException {
48
- RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
49
55
sourceRoot = Files .createTempDirectory ("source" ).toFile ();
50
56
dataRoot = Files .createTempDirectory ("data" ).toFile ();
51
57
env .setSourceRoot (sourceRoot .getAbsolutePath ());
52
58
env .setDataRoot (dataRoot .getAbsolutePath ());
53
59
}
54
60
55
61
public void create (InputStream inputBundle ) throws IOException {
56
- File sourceBundle = null ;
57
- try {
58
- sourceRoot = Files .createTempDirectory ("source" ).toFile ();
59
- dataRoot = Files .createTempDirectory ("data" ).toFile ();
60
- sourceBundle = File .createTempFile ("srcbundle" , ".zip" );
61
-
62
- if (sourceBundle .exists ()) {
63
- assertTrue (sourceBundle .delete ());
64
- }
62
+ createEmpty ();
63
+ extractBundle (sourceRoot , inputBundle );
64
+ }
65
65
66
- assertNotNull (inputBundle );
67
- FileOutputStream out = new FileOutputStream (sourceBundle );
68
- FileUtilities .copyFile (inputBundle , out );
69
- out .close ();
70
- FileUtilities .extractArchive (sourceBundle , sourceRoot );
71
- RuntimeEnvironment .getInstance ().setSourceRoot (sourceRoot .getAbsolutePath ());
72
- RuntimeEnvironment .getInstance ().setDataRoot (dataRoot .getAbsolutePath ());
73
- } finally {
74
- if (sourceBundle != null ) {
75
- sourceBundle .delete ();
76
- }
77
- }
66
+ public void createExternal (InputStream inputBundle ) throws IOException {
67
+ createEmpty ();
68
+ externalRoot = Files .createTempDirectory ("external" ).toFile ();
69
+ extractBundle (externalRoot , inputBundle );
78
70
}
79
71
80
72
public void destroy () {
81
73
if (sourceRoot != null ) {
82
74
FileUtilities .removeDirs (sourceRoot );
83
75
}
84
- purgeData ();
76
+ if (externalRoot != null ) {
77
+ FileUtilities .removeDirs (externalRoot );
78
+ }
79
+ if (dataRoot != null ) {
80
+ FileUtilities .removeDirs (dataRoot );
81
+ }
85
82
}
86
83
84
+ /**
85
+ * Deletes the directory tree of {@link #getDataRoot()}, and then recreates
86
+ * the empty directory afterward.
87
+ */
87
88
public void purgeData () {
88
89
if (dataRoot != null ) {
89
- FileUtilities .removeDirs (dataRoot );
90
+ assertTrue ("should delete dataRoot" , FileUtilities .removeDirs (dataRoot ));
91
+ assertFalse ("dataRoot should not exist" , dataRoot .exists ());
92
+ assertTrue ("should recreate dataRoot" , dataRoot .mkdir ());
90
93
}
91
94
}
92
95
@@ -98,6 +101,10 @@ public String getDataRoot() {
98
101
return dataRoot .getAbsolutePath ();
99
102
}
100
103
104
+ public String getExternalRoot () {
105
+ return externalRoot == null ? null : externalRoot .getAbsolutePath ();
106
+ }
107
+
101
108
private final static String dummyFilename = "dummy.txt" ;
102
109
103
110
public File addDummyFile (String project ) throws IOException {
@@ -145,4 +152,24 @@ public File addAdhocFile(String filename, InputStream in, String project)
145
152
}
146
153
return adhoc ;
147
154
}
155
+
156
+ private void extractBundle (File target , InputStream inputBundle ) throws IOException {
157
+ File sourceBundle = null ;
158
+ try {
159
+ sourceBundle = File .createTempFile ("srcbundle" , ".zip" );
160
+ if (sourceBundle .exists ()) {
161
+ assertTrue (sourceBundle .delete ());
162
+ }
163
+
164
+ assertNotNull ("inputBundle should not be null" , inputBundle );
165
+ FileOutputStream out = new FileOutputStream (sourceBundle );
166
+ FileUtilities .copyFile (inputBundle , out );
167
+ out .close ();
168
+ FileUtilities .extractArchive (sourceBundle , target );
169
+ } finally {
170
+ if (sourceBundle != null ) {
171
+ sourceBundle .delete ();
172
+ }
173
+ }
174
+ }
148
175
}
0 commit comments