Skip to content

Commit 9a38683

Browse files
committed
Make use of the new createTemporaryDirectory() method in our own tests
The benefits are obvious: should Jenkins report failures in one of these tests, we can easily inspect those temporary directories because they are now written into the target/ directory (which Jenkins makes visible, in contrast to /tmp/). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent e8622af commit 9a38683

File tree

3 files changed

+11
-48
lines changed

3 files changed

+11
-48
lines changed

src/test/java/org/scijava/annotations/EclipseHelperTest.java

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static org.junit.Assert.assertEquals;
3535
import static org.junit.Assert.assertFalse;
3636
import static org.junit.Assert.assertTrue;
37+
import static org.scijava.test.TestUtils.createTemporaryDirectory;
3738

3839
import java.io.File;
3940
import java.io.FileOutputStream;
@@ -54,7 +55,7 @@ public class EclipseHelperTest {
5455

5556
@Test
5657
public void testSkipIndexGeneration() throws Exception {
57-
final File dir = createTempDirectory();
58+
final File dir = createTemporaryDirectory("eclipse-test-");
5859
copyClasses(dir, Complex.class, Simple.class);
5960
final File jsonDir = new File(dir, Index.INDEX_PREFIX);
6061
assertFalse(jsonDir.exists());
@@ -68,7 +69,7 @@ public void testSkipIndexGeneration() throws Exception {
6869

6970
@Test
7071
public void testIndexing() throws Exception {
71-
final File dir = createTempDirectory();
72+
final File dir = createTemporaryDirectory("eclipse-test-");
7273
copyClasses(dir, Complex.class, Simple.class, Fruit.class,
7374
AnnotatedA.class, AnnotatedB.class, AnnotatedC.class);
7475
final File jsonDir = new File(dir, Index.INDEX_PREFIX);
@@ -144,46 +145,4 @@ private void copyClasses(final File dir, final Class<?>... classes)
144145
}
145146
}
146147

147-
private File createTempDirectory() throws IOException {
148-
// if running from .../target/test-classes/, let's make a directory next to
149-
// it
150-
final String classPath =
151-
"/" + DirectoryIndexerTest.getResourcePath(getClass());
152-
final String url = getClass().getResource(classPath).toString();
153-
if (url.startsWith("file:") && url.endsWith(classPath)) {
154-
final String directory =
155-
url.substring(5, url.length() - classPath.length());
156-
if (directory.endsWith("/target/test-classes")) {
157-
final File testClassesDirectory = new File(directory);
158-
if (testClassesDirectory.isDirectory()) {
159-
final File result =
160-
new File(testClassesDirectory.getParentFile(), "eclipse test");
161-
if (result.exists()) {
162-
rmRF(result);
163-
}
164-
return result;
165-
}
166-
}
167-
}
168-
// fall back to /tmp/
169-
final File result = File.createTempFile("eclipse test", "");
170-
result.delete();
171-
result.mkdir();
172-
return result;
173-
}
174-
175-
private static boolean rmRF(final File directory) {
176-
final File[] list = directory.listFiles();
177-
if (list != null) {
178-
for (final File file : list) {
179-
if (file.isFile() && !file.delete()) {
180-
return false;
181-
}
182-
else if (file.isDirectory() && !rmRF(file)) {
183-
return false;
184-
}
185-
}
186-
}
187-
return directory.delete();
188-
}
189148
}

src/test/java/org/scijava/util/ClassUtilsTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.junit.Assert.assertNull;
3636
import static org.junit.Assert.assertSame;
3737
import static org.junit.Assert.assertTrue;
38+
import static org.scijava.test.TestUtils.createTemporaryDirectory;
3839

3940
import java.io.File;
4041
import java.io.FileOutputStream;
@@ -171,7 +172,7 @@ public void testGetGenericType() {
171172

172173
@Test
173174
public void testUnpackedClass() throws IOException {
174-
final File tmpDir = FileUtils.createTemporaryDirectory("class-utils-test", "");
175+
final File tmpDir = createTemporaryDirectory("class-utils-test-");
175176
final String path = getClass().getName().replace('.', '/') + ".class";
176177
final File classFile = new File(tmpDir, path);
177178
assertTrue(classFile.getParentFile().exists() ||
@@ -189,7 +190,8 @@ public void testUnpackedClass() throws IOException {
189190

190191
@Test
191192
public void testClassInJar() throws IOException {
192-
final File jar = File.createTempFile("class-utils-test", ".jar");
193+
final File tmpDir = createTemporaryDirectory("class-utils-test-");
194+
final File jar = new File(tmpDir, "test.jar");
193195
final JarOutputStream out = new JarOutputStream(new FileOutputStream(jar));
194196
final String path = getClass().getName().replace('.', '/') + ".class";
195197
out.putNextEntry(new ZipEntry(path));

src/test/java/org/scijava/util/FileUtilsTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.junit.Assert.assertNotNull;
3737
import static org.junit.Assert.assertTrue;
3838
import static org.junit.Assert.fail;
39+
import static org.scijava.test.TestUtils.createTemporaryDirectory;
3940

4041
import java.io.File;
4142
import java.io.FileNotFoundException;
@@ -201,7 +202,8 @@ public void testListContents() throws IOException, URISyntaxException {
201202
// write some items to a temporary .jar file
202203
final String subDir = "sub 𝄞directory/";
203204
final String subSubDir = "more 𝄢stuff/";
204-
final File jarFile = File.createTempFile("listContentsTest", ".jar");
205+
final File tmpDir = createTemporaryDirectory("file-utils-test-");
206+
final File jarFile = new File(tmpDir, "listContentsTest.jar");
205207
final FileOutputStream out = new FileOutputStream(jarFile);
206208
final JarOutputStream jarOut = new JarOutputStream(out);
207209
try {
@@ -292,7 +294,7 @@ public void testGetAllVersions() throws IOException {
292294
final String withClassifier = "miglayout-3.7.3.1-swing.jar";
293295
final String withoutClassifier = "miglayout-3.7.3.1.jar";
294296

295-
final File tmp = FileUtils.createTemporaryDirectory("delete-other-", "");
297+
final File tmp = createTemporaryDirectory("delete-other-");
296298
try {
297299
writeEmptyFile(new File(tmp, withClassifier));
298300
writeEmptyFile(new File(tmp, withoutClassifier));

0 commit comments

Comments
 (0)