/** Tests {@link Types#location} with a class in a JAR file. */ @Test public void testLocationClassInJar() throws IOException { 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)); copy(getClass().getResource("/" + path).openStream(), out, true); final ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }, null); final Class<?> c = Types.load(getClass().getName(), classLoader); final URL location = Types.location(c); assertEquals(jar, FileUtils.urlToFile(location)); jar.deleteOnExit(); }
/** Tests {@link Types#location} with a class on the file system. */ @Test public void testLocationUnpackedClass() throws IOException { 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() || classFile.getParentFile().mkdirs()); copy(getClass().getResource("/" + path).openStream(), new FileOutputStream(classFile), true); final ClassLoader classLoader = new URLClassLoader(new URL[] { tmpDir.toURI().toURL() }, null); final Class<?> c = Types.load(getClass().getName(), classLoader); final URL location = Types.location(c); assertEquals(tmpDir, FileUtils.urlToFile(location)); FileUtils.deleteRecursively(tmpDir); }