From 59233334144dca83fb017795d54d99636cccee81 Mon Sep 17 00:00:00 2001 From: Jiri Vanek Date: Wed, 27 Nov 2013 14:11:13 +0100 Subject: Added null check when getting manifest attributes for case of jar without manifest * netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java: (getManifestAttribute) added check fo null manifest to prevent npe. * /tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java: added test for npe from getManifestAttribute * tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java: (createJarWithContents) enhanced to be able to create jar without manifest. --- .../jnlp/runtime/JNLPClassLoaderTest.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tests/netx') diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java index e713512..26de7b6 100644 --- a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java @@ -51,6 +51,7 @@ import net.sourceforge.jnlp.cache.UpdatePolicy; import net.sourceforge.jnlp.mock.DummyJNLPFileWithJar; import net.sourceforge.jnlp.util.FileTestUtils; import net.sourceforge.jnlp.util.logging.NoStdOutErrTest; +import org.junit.Assert; import org.junit.Test; @@ -259,4 +260,38 @@ public class JNLPClassLoaderTest extends NoStdOutErrTest{ } }); } + + + @Test + public void tryNullManifest() throws Exception { + File tempDirectory = FileTestUtils.createTempDirectory(); + File jarLocation = new File(tempDirectory, "test-npe.jar"); + File dummyContent = File.createTempFile("dummy", "context", tempDirectory); + jarLocation.deleteOnExit(); + + /* Test with-out any attribute specified specified */ + FileTestUtils.createJarWithoutManifestContents(jarLocation, dummyContent); + + final Exception[] exs = new Exception[2]; + final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation); + try { + final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS); + assertNoFileLeak(new Runnable() { + @Override + public void run() { + try { + assertEquals(null, classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.MAIN_CLASS)); + assertEquals(null, classLoader.getManifestAttribute(jnlpFile.getJarLocation(), Attributes.Name.IMPLEMENTATION_TITLE)); + } catch (Exception e) { + exs[0] = e; + } + } + }); + } catch (Exception e) { + exs[1] = e; + } + Assert.assertNotNull(exs); + Assert.assertNull(exs[0]); + Assert.assertNull(exs[1]); + } } -- cgit v1.2.3