diff options
author | Jiri Vanek <[email protected]> | 2013-11-27 14:11:13 +0100 |
---|---|---|
committer | Jiri Vanek <[email protected]> | 2013-11-27 14:11:13 +0100 |
commit | 59233334144dca83fb017795d54d99636cccee81 (patch) | |
tree | a28c68f6af5745527ecd66e3620a440764ca2115 | |
parent | b8da03fd7a7aac183acebf7ccd26196ccafca9bc (diff) |
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.
4 files changed, 64 insertions, 4 deletions
@@ -1,3 +1,12 @@ +2013-11-26 Jiri Vanek <[email protected]> + + * netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java: (getManifestAttribute) + added check for 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. + 2013-11-25 Jiri Vanek <[email protected]> * netx/net/sourceforge/jnlp/JNLPFile.java: (TITLE_NOT_FOUND) new constant diff --git a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java index 3be3623..324bbc6 100644 --- a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java +++ b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java @@ -900,8 +900,12 @@ public class JNLPClassLoader extends URLClassLoader { JarFile mainJar = null; try { mainJar = new JarFile(f); - attributeValue = mainJar.getManifest(). - getMainAttributes().getValue(attribute); + Manifest manifest = mainJar.getManifest(); + if (manifest == null || manifest.getMainAttributes() == null){ + //yes, jars without manifest exists + return null; + } + attributeValue = manifest.getMainAttributes().getValue(attribute); } catch (IOException ioe) { attributeValue = null; } finally { 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]); + } } diff --git a/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java b/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java index 828d6a5..1ce4935 100644 --- a/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java +++ b/tests/test-extensions/net/sourceforge/jnlp/util/FileTestUtils.java @@ -89,13 +89,25 @@ public class FileTestUtils { } /* Creates a jar in a temporary directory, with the given name & file contents */ + static public void createJarWithoutManifestContents(File jarFile, File... fileContents) throws Exception{ + createJarWithContents(jarFile, null, fileContents); + } + + /* Creates a jar in a temporary directory, with the given name & file contents */ static public void createJarWithContents(File jarFile, Manifest manifestContents, File... fileContents) throws Exception { /* Manifest quite evilly ignores all attributes if we don't specify a version! * Make sure it's set here. */ - manifestContents.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + if (manifestContents != null){ + manifestContents.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); + } - JarOutputStream jarWriter = new JarOutputStream(new FileOutputStream(jarFile), manifestContents); + JarOutputStream jarWriter; + if (manifestContents == null){ + jarWriter = new JarOutputStream(new FileOutputStream(jarFile)); + } else { + jarWriter = new JarOutputStream(new FileOutputStream(jarFile), manifestContents); + } for (File file : fileContents) { jarWriter.putNextEntry(new JarEntry(file.getName())); FileInputStream fileReader = new FileInputStream(file); |