aboutsummaryrefslogtreecommitdiffstats
path: root/tests/netx/unit/net/sourceforge/jnlp
diff options
context:
space:
mode:
authorAdam Domurad <[email protected]>2013-06-03 10:51:47 -0400
committerAdam Domurad <[email protected]>2013-06-03 10:51:47 -0400
commit0fa3a1ac219c218d2c489b10c8ce70a108c50b10 (patch)
tree1a0f76ff7f7413e12328a2f6be1ab7df44bfe413 /tests/netx/unit/net/sourceforge/jnlp
parent58464afe42ef3f0558da034bece3a7800f9104ff (diff)
Add NativeLibraryStorageTEst
Diffstat (limited to 'tests/netx/unit/net/sourceforge/jnlp')
-rw-r--r--tests/netx/unit/net/sourceforge/jnlp/cache/NativeLibraryStorageTest.java172
-rw-r--r--tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java106
2 files changed, 204 insertions, 74 deletions
diff --git a/tests/netx/unit/net/sourceforge/jnlp/cache/NativeLibraryStorageTest.java b/tests/netx/unit/net/sourceforge/jnlp/cache/NativeLibraryStorageTest.java
new file mode 100644
index 0000000..9caf866
--- /dev/null
+++ b/tests/netx/unit/net/sourceforge/jnlp/cache/NativeLibraryStorageTest.java
@@ -0,0 +1,172 @@
+/*
+Copyright (C) 2013 Red Hat, Inc.
+
+This file is part of IcedTea.
+
+IcedTea is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 2.
+
+IcedTea is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with IcedTea; see the file COPYING. If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version.
+ */
+
+package net.sourceforge.jnlp.cache;
+
+import static net.sourceforge.jnlp.util.FileTestUtils.assertNoFileLeak;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sourceforge.jnlp.Version;
+import net.sourceforge.jnlp.util.FileTestUtils;
+
+import org.junit.Test;
+
+public class NativeLibraryStorageTest {
+
+ /**************************************************************************
+ * Test helpers *
+ **************************************************************************/
+
+ /* Associates an extension with whether it represents a native library */
+ static class FileExtension {
+ public FileExtension(String extension, boolean isNative) {
+ this.extension = extension;
+ this.isNative = isNative;
+ }
+ final String extension;
+ final boolean isNative;
+ }
+
+ static private List<FileExtension> makeExtensionsToTest() {
+ List<FileExtension> exts = new ArrayList<FileExtension>();
+ exts.add(new FileExtension(".foobar", false)); /* Dummy non-native test extension */
+ for (String ext : NativeLibraryStorage.NATIVE_LIBRARY_EXTENSIONS) {
+ exts.add(new FileExtension(ext, true));
+ }
+ return exts;
+ }
+
+ /* All the native library types we support, as well as one negative test */
+ static final List<FileExtension> extensionsToTest = makeExtensionsToTest();
+
+ /* Creates a NativeLibraryStorage object, caching the given URLs */
+ static NativeLibraryStorage nativeLibraryStorageWithCache(URL... urlsToCache) {
+ ResourceTracker tracker = new ResourceTracker();
+ for (URL urlToCache : urlsToCache) {
+ tracker.addResource(urlToCache, new Version("1.0"), null, UpdatePolicy.ALWAYS);
+ }
+
+ return new NativeLibraryStorage(tracker);
+ }
+
+ /**************************************************************************
+ * Test cases *
+ **************************************************************************/
+
+ /* Tests searching for native libraries in jars */
+ @Test
+ public void testJarFileSearch() throws Exception {
+ /* Create a temporary directory to create jars in */
+ File tempDirectory = FileTestUtils.createTempDirectory();
+
+ for (FileExtension ext : extensionsToTest) {
+ /* Create empty file to search for */
+ String testFileName = "foobar" + ext.extension;
+ File testFile = new File(tempDirectory, testFileName);
+ FileTestUtils.createFileWithContents(testFile, "");
+
+ /* Create jar to search in */
+ File jarLocation = new File(tempDirectory, "test.jar");
+ FileTestUtils.createJarWithContents(jarLocation, testFile);
+
+ final URL tempJarUrl = jarLocation.toURI().toURL();
+ final NativeLibraryStorage storage = nativeLibraryStorageWithCache(tempJarUrl);
+
+ assertNoFileLeak( new Runnable () {
+ @Override
+ public void run() {
+ storage.addSearchJar(tempJarUrl);
+ }
+ });
+
+ /* This check isn't critical, but ensures we do not accidentally add jars as search directories */
+ assertFalse(storage.getSearchDirectories().contains(tempJarUrl));
+
+ /* If the file we added is native, it should be found
+ * Due to an implementation detail, non-native files will not be found */
+ boolean testFileWasFound = storage.findLibrary(testFileName) != null;
+ assertEquals(ext.isNative, testFileWasFound);
+ }
+ }
+
+ /* Tests searching for native libraries in directories */
+ @Test
+ public void testDirectorySearch() throws Exception {
+ /* Create a temporary directory to search in */
+ File tempDirectory = FileTestUtils.createTempDirectory();
+
+ for (FileExtension ext : extensionsToTest) {
+ /* Create empty file in the directory */
+ String testFileName = "foobar" + ext.extension;
+ FileTestUtils.createFileWithContents(new File(tempDirectory, testFileName), "");
+
+ /* Add the directory to the search list */
+ NativeLibraryStorage storage = nativeLibraryStorageWithCache(/* None needed */);
+ storage.addSearchDirectory(tempDirectory);
+
+ /* Ensure directory is in our search list */
+ assertTrue(storage.getSearchDirectories().contains(tempDirectory));
+
+ /* The file should be found, regardless if it was native */
+ boolean testFileWasFound = storage.findLibrary(testFileName) != null;
+ assertTrue(testFileWasFound);
+ }
+ }
+
+ @Test
+ public void testCleanupTemporaryFolder() throws Exception {
+ NativeLibraryStorage storage = nativeLibraryStorageWithCache(/* None needed */);
+ storage.ensureNativeStoreDirectory();
+
+ /* The temporary native store directory should be our only search folder */
+ assertTrue(storage.getSearchDirectories().size() == 1);
+
+ File searchDirectory = storage.getSearchDirectories().get(0);
+ assertTrue(searchDirectory.exists());
+
+ /* Test that it has been deleted */
+ storage.cleanupTemporaryFolder();
+ assertFalse(searchDirectory.exists());
+ }
+} \ No newline at end of file
diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
index 6e33464..169af7b 100644
--- a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
+++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
@@ -36,92 +36,34 @@ exception statement from your version.
package net.sourceforge.jnlp.runtime;
+import static net.sourceforge.jnlp.util.FileTestUtils.assertNoFileLeak;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import java.io.File;
-import java.lang.management.ManagementFactory;
-import java.net.URL;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import net.sourceforge.jnlp.JARDesc;
import net.sourceforge.jnlp.LaunchException;
-import net.sourceforge.jnlp.ServerAccess;
-import net.sourceforge.jnlp.Version;
import net.sourceforge.jnlp.cache.UpdatePolicy;
import net.sourceforge.jnlp.mock.DummyJNLPFileWithJar;
-import net.sourceforge.jnlp.util.StreamUtils;
+import net.sourceforge.jnlp.util.FileTestUtils;
import org.junit.Test;
public class JNLPClassLoaderTest {
- /* Get the open file-descriptor count for the process.
- * Note that this is specific to Unix-like operating systems.
- * As well, it relies on */
- static public long getOpenFileDescriptorCount() {
- MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
- try {
- return (Long) beanServer.getAttribute(
- new ObjectName("java.lang:type=OperatingSystem"),
- "OpenFileDescriptorCount"
- );
- } catch (Exception e) {
- // Effectively disables leak tests
- ServerAccess.logErrorReprint("Warning: Cannot get file descriptors for this platform!");
- return 0;
- }
- }
-
- /* Check the amount of file descriptors before and after a Runnable */
- static private void assertNoFileLeak(Runnable runnable) {
- long filesOpenBefore = getOpenFileDescriptorCount();
- runnable.run();
- long filesLeaked = getOpenFileDescriptorCount() - filesOpenBefore;
- assertEquals(0, filesLeaked);
- }
-
- static private String cleanExec(File directory, String... command) throws Exception {
- Process p = Runtime.getRuntime().exec(command, new String[]{}, directory);
-
- String stdOut = StreamUtils.readStreamAsString(p.getInputStream());
- String stdErr = StreamUtils.readStreamAsString(p.getErrorStream());
-
- ServerAccess.logNoReprint("Running " + Arrays.toString(command));
- ServerAccess.logNoReprint("Standard output was: \n" + stdOut);
- ServerAccess.logNoReprint("Standard error was: \n" + stdErr);
-
- p.getInputStream().close();
- p.getErrorStream().close();
- p.getOutputStream().close();
-
- return stdOut;
-
- }
-
- /* Creates a jar in a temporary directory, with the given name & manifest contents. */
- static private File createTempJar(String jarName, String manifestContents) throws Exception {
- File dir = new File(cleanExec(null /* current working dir */, "mktemp", "-d"));
- cleanExec(dir, "/bin/bash", "-c", "echo '" + manifestContents + "' > Manifest.txt");
- cleanExec(dir, "jar", "-cfm", jarName, "Manifest.txt");
- return new File(dir.getAbsolutePath() + "/" + jarName);
- }
-
- /* Creates a jar in a temporary directory, with the given name & an empty manifest. */
- static private File createTempJar(String jarName) throws Exception {
- return createTempJar(jarName, "");
- }
-
/* Note: Only does file leak testing for now. */
@Test
public void constructorFileLeakTest() throws Exception {
- final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(createTempJar("test.jar"));
+ File tempDirectory = FileTestUtils.createTempDirectory();
+ File jarLocation = new File(tempDirectory, "test.jar");
+ FileTestUtils.createJarWithContents(jarLocation /* no contents*/);
+
+ final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation);
assertNoFileLeak( new Runnable () {
@Override
@@ -139,7 +81,11 @@ public class JNLPClassLoaderTest {
* However, it is tricky without it erroring-out. */
@Test
public void isInvalidJarTest() throws Exception {
- final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(createTempJar("test.jar"));
+ File tempDirectory = FileTestUtils.createTempDirectory();
+ File jarLocation = new File(tempDirectory, "test.jar");
+ FileTestUtils.createJarWithContents(jarLocation /* no contents*/);
+
+ final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation);
final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS);
assertNoFileLeak( new Runnable () {
@@ -148,13 +94,19 @@ public class JNLPClassLoaderTest {
assertFalse(classLoader.isInvalidJar(jnlpFile.jarDesc));
}
});
-
}
@Test
public void getMainClassNameTest() throws Exception {
- /* Test with main-class */{
- final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(createTempJar("test.jar", "Main-Class: DummyClass\n"));
+ File tempDirectory = FileTestUtils.createTempDirectory();
+ File jarLocation = new File(tempDirectory, "test.jar");
+
+ /* Test with main-class in manifest */ {
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "DummyClass");
+ FileTestUtils.createJarWithContents(jarLocation, manifest);
+
+ final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation);
final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS);
assertNoFileLeak(new Runnable() {
@@ -164,8 +116,10 @@ public class JNLPClassLoaderTest {
}
});
}
- /* Test with-out main-class */{
- final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(createTempJar("test.jar", ""));
+ /* Test with-out any main-class specified */ {
+ FileTestUtils.createJarWithContents(jarLocation /* No contents */);
+
+ final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation);
final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS);
assertNoFileLeak(new Runnable() {
@@ -188,7 +142,11 @@ public class JNLPClassLoaderTest {
/* Note: Although it does a basic check, this mainly checks for file-descriptor leak */
@Test
public void checkForMainFileLeakTest() throws Exception {
- final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(createTempJar("test.jar", ""));
+ File tempDirectory = FileTestUtils.createTempDirectory();
+ File jarLocation = new File(tempDirectory, "test.jar");
+ FileTestUtils.createJarWithContents(jarLocation /* No contents */);
+
+ final DummyJNLPFileWithJar jnlpFile = new DummyJNLPFileWithJar(jarLocation);
final JNLPClassLoader classLoader = new JNLPClassLoader(jnlpFile, UpdatePolicy.ALWAYS);
assertNoFileLeak(new Runnable() {
@Override