diff options
author | Jiri Vanek <[email protected]> | 2013-11-01 11:08:06 +0100 |
---|---|---|
committer | Jiri Vanek <[email protected]> | 2013-11-01 11:08:06 +0100 |
commit | 113381d62d1eabb685f248573beff4195076740b (patch) | |
tree | 078043bf535410c68762b57b6648dacf96d52994 /tests | |
parent | 5c1af20ae6967095c67d2859824af90ea1bf22d5 (diff) |
tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java: added tests for custom attributes (getCustomAtributes), (getCustomAtributesEmpty) and test to ensure order during searching for attributes in manifests (checkOrderWhenReadingAttributes).
tests/test-extensions/net/sourceforge/jnlp/mock/DummyJNLPFileWithJar.java: can now handle multiple source jars, and set main jar (new constructors), (jarFiles) and (jarDescs) redeclared to arrays.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java | 117 | ||||
-rw-r--r-- | tests/test-extensions/net/sourceforge/jnlp/mock/DummyJNLPFileWithJar.java | 62 |
2 files changed, 163 insertions, 16 deletions
diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java index c3ff3a7..49da8d5 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java @@ -92,7 +92,7 @@ public class JNLPClassLoaderTest { assertNoFileLeak( new Runnable () { @Override public void run() { - assertFalse(classLoader.isInvalidJar(jnlpFile.jarDesc)); + assertFalse(classLoader.isInvalidJar(jnlpFile.getJarDesc())); } }); } @@ -113,11 +113,17 @@ public class JNLPClassLoaderTest { assertNoFileLeak(new Runnable() { @Override public void run() { - assertEquals("DummyClass", classLoader.getMainClassName(jnlpFile.jarLocation)); + assertEquals("DummyClass", classLoader.getMainClassName(jnlpFile.getJarLocation())); } }); } + } + + @Test + public void getMainClassNameTestEmpty() throws Exception { /* Test with-out any main-class specified */ { + File tempDirectory = FileTestUtils.createTempDirectory(); + File jarLocation = new File(tempDirectory, "test.jar"); FileTestUtils.createJarWithContents(jarLocation /* No contents */); final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation); @@ -126,7 +132,7 @@ public class JNLPClassLoaderTest { assertNoFileLeak(new Runnable() { @Override public void run() { - assertEquals(null, classLoader.getMainClassName(jnlpFile.jarLocation)); + assertEquals(null, classLoader.getMainClassName(jnlpFile.getJarLocation())); } }); } @@ -145,7 +151,7 @@ public class JNLPClassLoaderTest { @Override public void run() { try { - classLoader.checkForMain(Arrays.asList(jnlpFile.jarDesc)); + classLoader.checkForMain(Arrays.asList(jnlpFile.getJarDesc())); } catch (LaunchException e) { fail(e.toString()); } @@ -153,4 +159,105 @@ public class JNLPClassLoaderTest { }); assertFalse(classLoader.hasMainJar()); } -}
\ No newline at end of file + + @Test + public void getCustomAtributes() throws Exception { + File tempDirectory = FileTestUtils.createTempDirectory(); + File jarLocation = new File(tempDirectory, "testX.jar"); + + /* Test with attributes in manifest */ + Manifest manifest = new Manifest(); + manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "DummyClass"); + manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_TITLE, "it"); + manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR, "rh"); + FileTestUtils.createJarWithContents(jarLocation, manifest); + + final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation); + final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS); + + assertNoFileLeak(new Runnable() { + @Override + public void run() { + assertEquals("rh", classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.IMPLEMENTATION_VENDOR)); + assertEquals("DummyClass", classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.MAIN_CLASS)); + assertEquals("it", classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.IMPLEMENTATION_TITLE)); + } + }); + } + + @Test + public void getCustomAtributesEmpty() throws Exception { + File tempDirectory = FileTestUtils.createTempDirectory(); + File jarLocation = new File(tempDirectory, "testX.jar"); + + /* Test with-out any attribute specified specified */ + FileTestUtils.createJarWithContents(jarLocation /* No contents */); + + final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation); + final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS); + + assertNoFileLeak(new Runnable() { + @Override + public void run() { + assertEquals(null, classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.IMPLEMENTATION_VENDOR)); + assertEquals(null, classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.MAIN_CLASS)); + assertEquals(null, classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.IMPLEMENTATION_TITLE)); + } + }); + } + + @Test + public void checkOrderWhenReadingAttributes() throws Exception { + File tempDirectory = FileTestUtils.createTempDirectory(); + File jarLocation1 = new File(tempDirectory, "test1.jar"); + File jarLocation2 = new File(tempDirectory, "test2.jar"); + File jarLocation3 = new File(tempDirectory, "test3.jar"); + File jarLocation4 = new File(tempDirectory, "test4.jar"); + File jarLocation5 = new File(tempDirectory, "test5.jar"); + + /* Test with various attributes in manifest!s! */ + Manifest manifest1 = new Manifest(); + manifest1.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "DummyClass1"); //two times, but one in main jar, see DummyJNLPFileWithJar constructor with int + + Manifest manifest2 = new Manifest(); + manifest2.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR, "rh1"); //two times, both in not main jar, see DummyJNLPFileWithJar constructor with int + + Manifest manifest3 = new Manifest(); + manifest3.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_TITLE, "it"); //jsut once in not main jar, see DummyJNLPFileWithJar constructor with int + manifest3.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR, "rh2"); + + Manifest manifest4 = new Manifest(); + manifest4.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "DummyClass2"); //see jnlpFile.setMainJar(3); + manifest4.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_URL, "some url2"); //see DummyJNLPFileWithJar constructor with int + + //first jar + Manifest manifest5 = new Manifest(); + manifest5.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_URL, "some url1"); //see DummyJNLPFileWithJar constructor with int + + + FileTestUtils.createJarWithContents(jarLocation1, manifest1); + FileTestUtils.createJarWithContents(jarLocation2, manifest2); + FileTestUtils.createJarWithContents(jarLocation3, manifest3); + FileTestUtils.createJarWithContents(jarLocation4, manifest4); + FileTestUtils.createJarWithContents(jarLocation5, manifest5); + + final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(3, jarLocation5, jarLocation3, jarLocation4, jarLocation1, jarLocation2); //jar 1 should be main + final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS); + + assertNoFileLeak(new Runnable() { + @Override + public void run() { + //defined twice + assertEquals(null, classLoader.checkForAttributeInJars(Arrays.asList(jnlpFile.getJarDescs()), Attributes.Name.IMPLEMENTATION_VENDOR)); + //defined twice, but one in main jar + assertEquals("DummyClass1", classLoader.checkForAttributeInJars(Arrays.asList(jnlpFile.getJarDescs()), Attributes.Name.MAIN_CLASS)); + //defined not in main jar + assertEquals("it", classLoader.checkForAttributeInJars(Arrays.asList(jnlpFile.getJarDescs()), Attributes.Name.IMPLEMENTATION_TITLE)); + //not deffined + assertEquals(null, classLoader.checkForAttributeInJars(Arrays.asList(jnlpFile.getJarDescs()), Attributes.Name.IMPLEMENTATION_VENDOR_ID)); + //deffined in first jar + assertEquals("some url1", classLoader.checkForAttributeInJars(Arrays.asList(jnlpFile.getJarDescs()), Attributes.Name.IMPLEMENTATION_URL)); + } + }); + } +} diff --git a/tests/test-extensions/net/sourceforge/jnlp/mock/DummyJNLPFileWithJar.java b/tests/test-extensions/net/sourceforge/jnlp/mock/DummyJNLPFileWithJar.java index dd3ea84..0ef4e10 100644 --- a/tests/test-extensions/net/sourceforge/jnlp/mock/DummyJNLPFileWithJar.java +++ b/tests/test-extensions/net/sourceforge/jnlp/mock/DummyJNLPFileWithJar.java @@ -17,25 +17,65 @@ import net.sourceforge.jnlp.Version; public class DummyJNLPFileWithJar extends JNLPFile { /* Create a JARDesc for the given URL location */ - static JARDesc makeJarDesc(URL jarLocation) { - return new JARDesc(jarLocation, new Version("1"), null, false,false, false,false); + static JARDesc makeJarDesc(URL jarLocation, boolean main) { + return new JARDesc(jarLocation, new Version("1"), null, false,main, false,false); } - public URL codeBase, jarLocation; - public JARDesc jarDesc; + private final URL codeBase; + private final JARDesc[] jarDescs; + private final File[] jarFiles; - public DummyJNLPFileWithJar(File jarFile) throws MalformedURLException { - codeBase = jarFile.getParentFile().toURI().toURL(); - jarLocation = jarFile.toURI().toURL(); - jarDesc = makeJarDesc(jarLocation); + public DummyJNLPFileWithJar(File... jarFiles) throws MalformedURLException { + this(-1, jarFiles); + } + public DummyJNLPFileWithJar(int main, File... jarFiles) throws MalformedURLException { + codeBase = jarFiles[0].getParentFile().toURI().toURL(); + this.jarFiles = jarFiles; + jarDescs = new JARDesc[jarFiles.length]; + + for (int i = 0; i < jarFiles.length; i++) { + jarDescs[i] = makeJarDesc(jarFiles[i].toURI().toURL(), i==main); + + } info = new ArrayList<InformationDesc>(); } + public URL getJarLocation() { + try { + return jarFiles[0].toURI().toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + + public URL getJarLocation(int i) { + try { + return jarFiles[i].toURI().toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + + public JARDesc[] getJarDescs() { + return jarDescs; + } + + public JARDesc getJarDesc() { + return jarDescs[0]; + } + + public JARDesc getJarDesc(int i) { + return jarDescs[i]; + } + + @Override public ResourcesDesc getResources() { - ResourcesDesc resources = new ResourcesDesc(null, new Locale[0], new String[0], new String[0]); - resources.addResource(jarDesc); - return resources; + ResourcesDesc localResources = new ResourcesDesc(null, new Locale[0], new String[0], new String[0]); + for (JARDesc j : jarDescs) { + localResources.addResource(j); + } + return localResources; } @Override public ResourcesDesc[] getResourcesDescs(final Locale locale, final String os, final String arch) { |