Github user chtyim commented on a diff in the pull request:
https://github.com/apache/twill/pull/1#discussion_r75600610
--- Diff:
twill-core/src/test/java/org/apache/twill/internal/utils/ApplicationBundlerTest.java
---
@@ -68,6 +71,56 @@ public void testFindDependencies() throws IOException,
ClassNotFoundException {
Assert.assertNotSame(classLoader, clz.getClassLoader());
}
+ @Test
+ public void testSameJar() throws IOException, ClassNotFoundException {
+ File dir1 = tmpDir.newFolder();
+ File dir2 = tmpDir.newFolder();
+ File j1 = new File(dir1, "samename.jar");
+ File j2 = new File(dir2, "samename.jar");
+
+ createJar(Class1.class, j1);
+ createJar(Class2.class, j2);
+
+ ClassLoader currentClassLoader =
Thread.currentThread().getContextClassLoader();
+ Location location;
+
+ try {
+ URL[] urls = new URL[] { j1.toURI().toURL(), j2.toURI().toURL() };
+ Thread.currentThread().setContextClassLoader(new
URLClassLoader(urls, null));
+
+ // create bundle
+ location = new
LocalLocationFactory(tmpDir.newFolder()).create("test.jar");
+ ApplicationBundler bundler = new
ApplicationBundler(ImmutableList.<String> of());
+ bundler.createBundle(location, Class1.class, Class2.class);
+
+ } finally {
+ Thread.currentThread().setContextClassLoader(currentClassLoader);
+ }
+
+ File targetDir = tmpDir.newFolder();
+ unjar(new File(location.toURI()), targetDir);
+
+ // should be able to load both classes
+ ClassLoader classLoader = createClassLoader(targetDir);
+ Class<?> c1 = classLoader.loadClass(Class1.class.getName());
+ Class<?> c2 = classLoader.loadClass(Class2.class.getName());
+
+ // make sure we are loading them from the correct class loader (not
defaulting back to some classloader
+ // from the test
+ Assert.assertSame(classLoader, c1.getClassLoader());
+ Assert.assertSame(classLoader, c2.getClassLoader());
+ }
+
+ private void createJar(Class<?> clazz, File jarFile) throws IOException {
+ try (JarOutputStream jos = new JarOutputStream(new
FileOutputStream(jarFile))) {
+ String pathname = clazz.getName().replace(".", "/") + ".class";
+ JarEntry entry = new JarEntry(pathname);
+ jos.putNextEntry(entry);
+ IOUtils.copy(clazz.getClassLoader().getResourceAsStream(pathname),
jos);
--- End diff --
And that also make sure the proper close of the resource stream.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---