Skip to content

Commit c6987dc

Browse files
committed
Add TestRepository.createExternal() and bolster its purgeData()
1 parent 8ba3bf8 commit c6987dc

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/util/FileUtilities.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ public static void extractArchive(File sourceBundle, File root) throws IOExcepti
8181
}
8282
}
8383

84-
public static void removeDirs(File root) {
84+
public static boolean removeDirs(File root) {
8585
for (File f : root.listFiles()) {
8686
if (f.isDirectory()) {
8787
removeDirs(f);
8888
} else {
8989
f.delete();
9090
}
9191
}
92-
root.delete();
92+
return root.delete();
9393
}
9494

9595
public static void copyFile(InputStream in, OutputStream out) throws IOException {

opengrok-indexer/src/test/java/org/opengrok/indexer/util/TestRepository.java

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
/*
2121
* 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]>.
2323
*/
2424
package org.opengrok.indexer.util;
2525

26+
import static org.junit.Assert.assertFalse;
2627
import static org.junit.Assert.assertNotNull;
2728
import static org.junit.Assert.assertTrue;
2829

@@ -41,52 +42,54 @@
4142
*/
4243
public class TestRepository {
4344

45+
private final RuntimeEnvironment env;
4446
private File sourceRoot;
4547
private File dataRoot;
48+
private File externalRoot;
49+
50+
public TestRepository() {
51+
env = RuntimeEnvironment.getInstance();
52+
}
4653

4754
public void createEmpty() throws IOException {
48-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
4955
sourceRoot = Files.createTempDirectory("source").toFile();
5056
dataRoot = Files.createTempDirectory("data").toFile();
5157
env.setSourceRoot(sourceRoot.getAbsolutePath());
5258
env.setDataRoot(dataRoot.getAbsolutePath());
5359
}
5460

5561
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+
}
6565

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);
7870
}
7971

8072
public void destroy() {
8173
if (sourceRoot != null) {
8274
FileUtilities.removeDirs(sourceRoot);
8375
}
84-
purgeData();
76+
if (externalRoot != null) {
77+
FileUtilities.removeDirs(externalRoot);
78+
}
79+
if (dataRoot != null) {
80+
FileUtilities.removeDirs(dataRoot);
81+
}
8582
}
8683

84+
/**
85+
* Deletes the directory tree of {@link #getDataRoot()}, and then recreates
86+
* the empty directory afterward.
87+
*/
8788
public void purgeData() {
8889
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());
9093
}
9194
}
9295

@@ -98,6 +101,10 @@ public String getDataRoot() {
98101
return dataRoot.getAbsolutePath();
99102
}
100103

104+
public String getExternalRoot() {
105+
return externalRoot == null ? null : externalRoot.getAbsolutePath();
106+
}
107+
101108
private final static String dummyFilename = "dummy.txt";
102109

103110
public File addDummyFile(String project) throws IOException {
@@ -145,4 +152,24 @@ public File addAdhocFile(String filename, InputStream in, String project)
145152
}
146153
return adhoc;
147154
}
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+
}
148175
}

0 commit comments

Comments
 (0)