@@ -0,0 +1,2428 @@
+package imagej.util;%0A%0Aimport static org.junit.Assert.*;%0A%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0Aimport java.net.URL;%0Aimport java.net.URLClassLoader;%0Aimport java.util.jar.JarOutputStream;%0Aimport java.util.zip.ZipEntry;%0A%0Aimport org.junit.Test;%0A%0Apublic class ClassUtilsTest %7B%0A%0A%09@Test%0A%09public void testUnpackedClass() throws IOException %7B%0A%09%09File tmpDir = createTempDirectory(%22class-utils-test%22);%0A%09%09String path = getClass().getName().replace('.', '/') + %22.class%22;%0A%09%09File classFile = new File(tmpDir, path);%0A%09%09assertTrue(classFile.getParentFile().exists() %7C%7C classFile.getParentFile().mkdirs());%0A%09%09copy(getClass().getResource(%22/%22 + path).openStream(), new FileOutputStream(classFile), true);%0A%0A%09%09ClassLoader classLoader = new URLClassLoader(new URL%5B%5D %7B tmpDir.toURI().toURL() %7D, null);%0A%09%09assertEquals(tmpDir, ClassUtils.getLocation(getClass().getName(), classLoader));%0A%09%7D%0A%0A%09@Test%0A%09public void testClassInJar() throws IOException %7B%0A%09%09File jar = File.createTempFile(%22class-utils-test%22, %22.jar%22);%0A%09%09JarOutputStream out = new JarOutputStream(new FileOutputStream(jar));%0A%09%09String path = getClass().getName().replace('.', '/') + %22.class%22;%0A%09%09out.putNextEntry(new ZipEntry(path));%0A%09%09copy(getClass().getResource(%22/%22 + path).openStream(), out, true);%0A%0A%09%09ClassLoader classLoader = new URLClassLoader(new URL%5B%5D %7B jar.toURI().toURL() %7D, null);%0A%09%09assertEquals(jar, ClassUtils.getLocation(getClass().getName(), classLoader));%0A%09%7D%0A%0A%09/**%0A%09 * Create a temporary directory%0A%09 * %0A%09 * @param prefix the prefix as for %7B@link File#createTempFile(String, String)%7D%0A%09 * @return the File object describing the directory%0A%09 * @throws IOException%0A%09 */%0A%09protected static File createTempDirectory(final String prefix) throws IOException %7B%0A%09%09final File file = File.createTempFile(prefix, %22%22);%0A%09%09file.delete();%0A%09%09file.mkdir();%0A%09%09return file;%0A%09%7D%0A%0A%09/**%0A%09 * Copies bytes from an %7B@link InputStream%7D to an %7B@link OutputStream%7D%0A%09 * %0A%09 * @param in the source%0A%09 * @param out the sink%0A%09 * @param closeOut whether to close the sink after we're done%0A%09 * @throws IOException%0A%09 */%0A%09protected static void copy(final InputStream in, final OutputStream out, boolean closeOut) throws IOException %7B%0A%09%09byte%5B%5D buffer = new byte%5B16384%5D;%0A%09%09for (;;) %7B%0A%09%09%09int count = in.read(buffer);%0A%09%09%09if (count %3C 0) break;%0A%09%09%09out.write(buffer, 0, count);%0A%09%09%7D%0A%09%09in.close();%0A%09%09if (closeOut) out.close();%0A%09%7D%0A%0A%7D%0A

Add unit tests for ClassUtils#getLocation(String)