Skip to content

Commit 9fbcad0

Browse files
committed
Merge pull request #70 from scijava/test-utils
Provide a TestUtils class in the main artifact
2 parents 13613ee + 5a114fe commit 9fbcad0

File tree

4 files changed

+77
-59
lines changed

4 files changed

+77
-59
lines changed

src/test/java/org/scijava/test/TestUtils.java renamed to src/main/java/org/scijava/test/TestUtils.java

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.io.File;
3535
import java.io.IOException;
3636
import java.net.URL;
37+
import java.util.AbstractMap;
38+
import java.util.Map;
3739

3840
import org.scijava.util.ClassUtils;
3941
import org.scijava.util.FileUtils;
@@ -58,7 +60,8 @@ public class TestUtils {
5860
* @throws IOException
5961
*/
6062
public static File createTemporaryDirectory(final String prefix) throws IOException {
61-
return createTemporaryDirectory(prefix, getCallingClass(null));
63+
final Map.Entry<Class<?>, String> calling = getCallingCodeLocation(null);
64+
return createTemporaryDirectory(prefix, calling.getKey(), calling.getValue());
6265
}
6366

6467
/**
@@ -77,18 +80,51 @@ public static File createTemporaryDirectory(final String prefix) throws IOExcept
7780
*/
7881
public static File createTemporaryDirectory(final String prefix,
7982
final Class<?> forClass) throws IOException
83+
{
84+
return createTemporaryDirectory(prefix, forClass, "");
85+
}
86+
87+
/**
88+
* Makes a temporary directory for use with unit tests.
89+
* <p>
90+
* When the unit test runs in a Maven context, the temporary directory will be
91+
* created in the corresponding <i>target/</i> directory instead of
92+
* <i>/tmp/</i>.
93+
* </p>
94+
*
95+
* @param prefix the prefix for the directory's name
96+
* @param forClass the class for context (to determine whether there's a
97+
* <i>target/<i> directory)
98+
* @param suffix the suffix for the directory's name
99+
* @return the reference to the newly-created temporary directory
100+
* @throws IOException
101+
*/
102+
public static File createTemporaryDirectory(final String prefix,
103+
final Class<?> forClass, final String suffix) throws IOException
80104
{
81105
final URL directory = ClassUtils.getLocation(forClass);
82-
if (directory != null && "file".equals(directory.getProtocol())) {
83-
final String path = directory.getPath();
84-
if (path != null && path.endsWith("/target/test-classes/")) {
85-
final File baseDirectory =
86-
new File(path.substring(0, path.length() - 13));
87-
final File file = File.createTempFile(prefix, "", baseDirectory);
88-
if (file.delete() && file.mkdir()) return file;
89-
}
106+
if (directory == null) {
107+
throw new IllegalArgumentException("No location for class " + forClass);
108+
}
109+
if (!"file".equals(directory.getProtocol())) {
110+
throw new IllegalArgumentException("Invalid directory: " + directory);
111+
}
112+
final String path = directory.getPath();
113+
if (path == null) throw new IllegalArgumentException("Directory has null path");
114+
final File baseDirectory;
115+
if (path.endsWith("/target/test-classes/")) {
116+
baseDirectory = new File(path).getParentFile();
117+
} else {
118+
baseDirectory = new File(path);
119+
}
120+
121+
final File file = new File(baseDirectory, prefix + suffix);
122+
if (file.isDirectory()) FileUtils.deleteRecursively(file);
123+
else if (file.exists() && !file.delete()) {
124+
throw new IOException("Could not remove " + file);
90125
}
91-
return FileUtils.createTemporaryDirectory(prefix, "");
126+
if (!file.mkdir()) throw new IOException("Could not make directory " + file);
127+
return file;
92128
}
93129

94130
/**
@@ -104,6 +140,22 @@ public static File createTemporaryDirectory(final String prefix,
104140
* @return the class of the caller
105141
*/
106142
public static Class<?> getCallingClass(final Class<?> excluding) {
143+
return getCallingCodeLocation(excluding).getKey();
144+
}
145+
146+
/**
147+
* Returns the class and the method/line number of the caller (excluding the specified class).
148+
* <p>
149+
* Sometimes it is convenient to determine the caller's context, e.g. to
150+
* determine whether running in a maven-surefire-plugin context (in which case
151+
* the location of the caller's class would end in
152+
* <i>target/test-classes/</i>).
153+
* </p>
154+
*
155+
* @param excluding the class to exclude (or null)
156+
* @return the class of the caller and the method and line number
157+
*/
158+
public static Map.Entry<Class<?>, String> getCallingCodeLocation(final Class<?> excluding) {
107159
final String thisClassName = TestUtils.class.getName();
108160
final String thisClassName2 = excluding == null ? null : excluding.getName();
109161
final Thread currentThread = Thread.currentThread();
@@ -115,14 +167,17 @@ public static Class<?> getCallingClass(final Class<?> excluding) {
115167
continue;
116168
}
117169
final ClassLoader loader = currentThread.getContextClassLoader();
170+
final Class<?> clazz;
118171
try {
119-
return loader.loadClass(element.getClassName());
172+
clazz = loader.loadClass(element.getClassName());
120173
}
121174
catch (ClassNotFoundException e) {
122175
throw new UnsupportedOperationException("Could not load " +
123176
element.getClassName() + " with the current context class loader (" +
124177
loader + ")!");
125178
}
179+
final String suffix = element.getMethodName() + "-L" + element.getLineNumber();
180+
return new AbstractMap.SimpleEntry<Class<?>, String>(clazz, suffix);
126181
}
127182
throw new UnsupportedOperationException("No calling class outside " + thisClassName + " found!");
128183
}

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)