Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.AbstractMap;
import java.util.Map;

import org.scijava.util.ClassUtils;
import org.scijava.util.FileUtils;
Expand All @@ -58,7 +60,8 @@ public class TestUtils {
* @throws IOException
*/
public static File createTemporaryDirectory(final String prefix) throws IOException {
return createTemporaryDirectory(prefix, getCallingClass(null));
final Map.Entry<Class<?>, String> calling = getCallingCodeLocation(null);
return createTemporaryDirectory(prefix, calling.getKey(), calling.getValue());
}

/**
Expand All @@ -77,18 +80,51 @@ public static File createTemporaryDirectory(final String prefix) throws IOExcept
*/
public static File createTemporaryDirectory(final String prefix,
final Class<?> forClass) throws IOException
{
return createTemporaryDirectory(prefix, forClass, "");
}

/**
* Makes a temporary directory for use with unit tests.
* <p>
* When the unit test runs in a Maven context, the temporary directory will be
* created in the corresponding <i>target/</i> directory instead of
* <i>/tmp/</i>.
* </p>
*
* @param prefix the prefix for the directory's name
* @param forClass the class for context (to determine whether there's a
* <i>target/<i> directory)
* @param suffix the suffix for the directory's name
* @return the reference to the newly-created temporary directory
* @throws IOException
*/
public static File createTemporaryDirectory(final String prefix,
final Class<?> forClass, final String suffix) throws IOException
{
final URL directory = ClassUtils.getLocation(forClass);
if (directory != null && "file".equals(directory.getProtocol())) {
final String path = directory.getPath();
if (path != null && path.endsWith("/target/test-classes/")) {
final File baseDirectory =
new File(path.substring(0, path.length() - 13));
final File file = File.createTempFile(prefix, "", baseDirectory);
if (file.delete() && file.mkdir()) return file;
}
if (directory == null) {
throw new IllegalArgumentException("No location for class " + forClass);
}
if (!"file".equals(directory.getProtocol())) {
throw new IllegalArgumentException("Invalid directory: " + directory);
}
final String path = directory.getPath();
if (path == null) throw new IllegalArgumentException("Directory has null path");
final File baseDirectory;
if (path.endsWith("/target/test-classes/")) {
baseDirectory = new File(path).getParentFile();
} else {
baseDirectory = new File(path);
}

final File file = new File(baseDirectory, prefix + suffix);
if (file.isDirectory()) FileUtils.deleteRecursively(file);
else if (file.exists() && !file.delete()) {
throw new IOException("Could not remove " + file);
}
return FileUtils.createTemporaryDirectory(prefix, "");
if (!file.mkdir()) throw new IOException("Could not make directory " + file);
return file;
}

/**
Expand All @@ -104,6 +140,22 @@ public static File createTemporaryDirectory(final String prefix,
* @return the class of the caller
*/
public static Class<?> getCallingClass(final Class<?> excluding) {
return getCallingCodeLocation(excluding).getKey();
}

/**
* Returns the class and the method/line number of the caller (excluding the specified class).
* <p>
* Sometimes it is convenient to determine the caller's context, e.g. to
* determine whether running in a maven-surefire-plugin context (in which case
* the location of the caller's class would end in
* <i>target/test-classes/</i>).
* </p>
*
* @param excluding the class to exclude (or null)
* @return the class of the caller and the method and line number
*/
public static Map.Entry<Class<?>, String> getCallingCodeLocation(final Class<?> excluding) {
final String thisClassName = TestUtils.class.getName();
final String thisClassName2 = excluding == null ? null : excluding.getName();
final Thread currentThread = Thread.currentThread();
Expand All @@ -115,14 +167,17 @@ public static Class<?> getCallingClass(final Class<?> excluding) {
continue;
}
final ClassLoader loader = currentThread.getContextClassLoader();
final Class<?> clazz;
try {
return loader.loadClass(element.getClassName());
clazz = loader.loadClass(element.getClassName());
}
catch (ClassNotFoundException e) {
throw new UnsupportedOperationException("Could not load " +
element.getClassName() + " with the current context class loader (" +
loader + ")!");
}
final String suffix = element.getMethodName() + "-L" + element.getLineNumber();
return new AbstractMap.SimpleEntry<Class<?>, String>(clazz, suffix);
}
throw new UnsupportedOperationException("No calling class outside " + thisClassName + " found!");
}
Expand Down
47 changes: 3 additions & 44 deletions src/test/java/org/scijava/annotations/EclipseHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.scijava.test.TestUtils.createTemporaryDirectory;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -54,7 +55,7 @@ public class EclipseHelperTest {

@Test
public void testSkipIndexGeneration() throws Exception {
final File dir = createTempDirectory();
final File dir = createTemporaryDirectory("eclipse-test-");
copyClasses(dir, Complex.class, Simple.class);
final File jsonDir = new File(dir, Index.INDEX_PREFIX);
assertFalse(jsonDir.exists());
Expand All @@ -68,7 +69,7 @@ public void testSkipIndexGeneration() throws Exception {

@Test
public void testIndexing() throws Exception {
final File dir = createTempDirectory();
final File dir = createTemporaryDirectory("eclipse-test-");
copyClasses(dir, Complex.class, Simple.class, Fruit.class,
AnnotatedA.class, AnnotatedB.class, AnnotatedC.class);
final File jsonDir = new File(dir, Index.INDEX_PREFIX);
Expand Down Expand Up @@ -144,46 +145,4 @@ private void copyClasses(final File dir, final Class<?>... classes)
}
}

private File createTempDirectory() throws IOException {
// if running from .../target/test-classes/, let's make a directory next to
// it
final String classPath =
"/" + DirectoryIndexerTest.getResourcePath(getClass());
final String url = getClass().getResource(classPath).toString();
if (url.startsWith("file:") && url.endsWith(classPath)) {
final String directory =
url.substring(5, url.length() - classPath.length());
if (directory.endsWith("/target/test-classes")) {
final File testClassesDirectory = new File(directory);
if (testClassesDirectory.isDirectory()) {
final File result =
new File(testClassesDirectory.getParentFile(), "eclipse test");
if (result.exists()) {
rmRF(result);
}
return result;
}
}
}
// fall back to /tmp/
final File result = File.createTempFile("eclipse test", "");
result.delete();
result.mkdir();
return result;
}

private static boolean rmRF(final File directory) {
final File[] list = directory.listFiles();
if (list != null) {
for (final File file : list) {
if (file.isFile() && !file.delete()) {
return false;
}
else if (file.isDirectory() && !rmRF(file)) {
return false;
}
}
}
return directory.delete();
}
}
6 changes: 4 additions & 2 deletions src/test/java/org/scijava/util/ClassUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.scijava.test.TestUtils.createTemporaryDirectory;

import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -171,7 +172,7 @@ public void testGetGenericType() {

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

@Test
public void testClassInJar() throws IOException {
final File jar = File.createTempFile("class-utils-test", ".jar");
final File tmpDir = createTemporaryDirectory("class-utils-test-");
final File jar = new File(tmpDir, "test.jar");
final JarOutputStream out = new JarOutputStream(new FileOutputStream(jar));
final String path = getClass().getName().replace('.', '/') + ".class";
out.putNextEntry(new ZipEntry(path));
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/org/scijava/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.scijava.test.TestUtils.createTemporaryDirectory;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -201,7 +202,8 @@ public void testListContents() throws IOException, URISyntaxException {
// write some items to a temporary .jar file
final String subDir = "sub 𝄞directory/";
final String subSubDir = "more 𝄢stuff/";
final File jarFile = File.createTempFile("listContentsTest", ".jar");
final File tmpDir = createTemporaryDirectory("file-utils-test-");
final File jarFile = new File(tmpDir, "listContentsTest.jar");
final FileOutputStream out = new FileOutputStream(jarFile);
final JarOutputStream jarOut = new JarOutputStream(out);
try {
Expand Down Expand Up @@ -292,7 +294,7 @@ public void testGetAllVersions() throws IOException {
final String withClassifier = "miglayout-3.7.3.1-swing.jar";
final String withoutClassifier = "miglayout-3.7.3.1.jar";

final File tmp = FileUtils.createTemporaryDirectory("delete-other-", "");
final File tmp = createTemporaryDirectory("delete-other-");
try {
writeEmptyFile(new File(tmp, withClassifier));
writeEmptyFile(new File(tmp, withoutClassifier));
Expand Down