FileSystem { public static IDirectory getFSRoot(File fs) { return FileSystemImpl.getFSRoot(fs, null); }  static IDirectory getFSRoot(File fs); static ICloseableDirectory getFSRoot(InputStream is);  }
@Test(expected=UnsupportedOperationException.class) public void basicRootDirTestsWithFiles() throws IOException { File baseDir = new File(getTestResourceDir(), "/app1"); File manifest = new File(baseDir, "META-INF/APPLICATION.MF"); IDirectory dir = FileSystem.getFSRoot(baseDir); runBasicRootDirTests(dir, baseDir.length(), manifest.lastModified()); }
@Test public void basicDirTestsWithFiles() throws IOException { File baseDir = new File(getTestResourceDir(), "/app1"); IDirectory dir = FileSystem.getFSRoot(baseDir); File desiredFile = new File(baseDir, "META-INF/APPLICATION.MF"); runBasicDirTest(dir, desiredFile.length(), desiredFile.lastModified()); runBasicDirTest(dir.toCloseable(), desiredFile.length(), desiredFile.lastModified()); }
@Test(expected=UnsupportedOperationException.class) public void basicRootDirTestsWithZip() throws IOException { File baseDir = new File("fileSystemTest/app2.zip"); IDirectory dir = FileSystem.getFSRoot(baseDir); runBasicRootDirTests(dir, baseDir.length(), baseDir.lastModified()); }
@Test(expected=UnsupportedOperationException.class) public void basicRootDirTestsWithZipInputStream() throws IOException { File baseDir = new File("fileSystemTest/app2.zip"); ICloseableDirectory dir = FileSystem.getFSRoot(new FileInputStream(baseDir)); try { runBasicRootDirTests(dir, baseDir.length(), baseDir.lastModified()); } finally { dir.close(); } }
@Test public void testInvalidFSRoot() throws IOException { File baseDir = new File(getTestResourceDir(), "/app1"); File manifest = new File(baseDir, "META-INF/APPLICATION.MF"); try { FileSystem.getFSRoot(manifest); fail("Should have thrown an IORuntimeException"); } catch (IORuntimeException e) { } }
@Test public void nestedZipInDirectory() throws IOException { IDirectory dir = FileSystem.getFSRoot(new File("").getAbsoluteFile()); IDirectory zip = dir.getFile("fileSystemTest/app2.zip").convert(); assertNull(zip); zip = dir.getFile("fileSystemTest/app2.zip").convertNested(); assertNotNull(zip); assertNotNull(zip.getParent()); assertEquals("fileSystemTest", zip.getParent().getName()); assertEquals("fileSystemTest/app2.zip", zip.getName()); IFile appMf = zip.getFile("META-INF/APPLICATION.MF"); assertNotNull(appMf); assertEquals("fileSystemTest/app2.zip/META-INF/APPLICATION.MF", appMf.getName()); checkManifest(appMf.open()); assertFalse(zip.isRoot()); assertEquals(dir, zip.getRoot()); assertEquals(dir, appMf.getRoot()); checkManifest(appMf.toURL().openStream()); runBasicDirTest(zip, "fileSystemTest/app2.zip/", appMf.getSize(), appMf.getLastModified()); }
@Test public void nestedZipInZip() throws IOException { IDirectory outer = FileSystem.getFSRoot(new File("fileSystemTest/outer.zip")); IFile innerFile = outer.getFile("app2.zip"); assertNotNull(innerFile); IDirectory inner = innerFile.convertNested(); assertNotNull(inner); File desiredFile = new File(new File(getTestResourceDir(), "/app1"), "META-INF/APPLICATION.MF"); runBasicDirTest(inner, "app2.zip/", -1, desiredFile.lastModified()); runBasicDirTest(inner.toCloseable(), "app2.zip/", desiredFile.length(), desiredFile.lastModified()); }
@Test public void nestedZipInZipInputStream() throws Exception { ICloseableDirectory outer = FileSystem.getFSRoot(new FileInputStream("fileSystemTest/outer.zip")); try { IFile innerFile = outer.getFile("app2.zip"); assertNotNull(innerFile); IDirectory inner = innerFile.convertNested(); assertNotNull(inner); File desiredFile = new File(new File(getTestResourceDir(), "/app1"), "META-INF/APPLICATION.MF"); runBasicDirTest(inner, "app2.zip/", -1, desiredFile.lastModified()); runBasicDirTest(inner.toCloseable(), "app2.zip/", desiredFile.length(), desiredFile.lastModified()); } finally { outer.close(); Field f = outer.getClass().getDeclaredField("tempFile"); f.setAccessible(true); assertFalse(((File)f.get(outer)).exists()); } }
@Test public void basicDirTestsWithZip() throws IOException { File baseDir = new File("fileSystemTest/app2.zip"); IDirectory dir = FileSystem.getFSRoot(baseDir); assertTrue(dir.toString(), dir.toString().endsWith("app2.zip")); File desiredFile = new File(new File(getTestResourceDir(), "/app1"), "META-INF/APPLICATION.MF"); runBasicDirTest(dir, desiredFile.length(), desiredFile.lastModified()); runBasicDirTest(dir.toCloseable(), desiredFile.length(), desiredFile.lastModified()); }
@Test public void basicDirTestsWithZipInputStream() throws IOException { File baseDir = new File("fileSystemTest/app2.zip"); ICloseableDirectory dir = FileSystem.getFSRoot(new FileInputStream(baseDir)); try { File desiredFile = new File(new File(getTestResourceDir(), "/app1"), "META-INF/APPLICATION.MF"); runBasicDirTest(dir, desiredFile.length(), desiredFile.lastModified()); runBasicDirTest(dir.toCloseable(), desiredFile.length(), desiredFile.lastModified()); } finally { dir.close(); } }
@Test public void zipCloseableZipSimplePerformanceTest() throws IOException { int N = 100000; File baseDir = new File("fileSystemTest/app2.zip"); ZipFile zip = new ZipFile(baseDir); long start = System.currentTimeMillis(); for (int i=0; i<N; i++) { ZipEntry ze = zip.getEntry("META-INF/APPLICATION.MF"); InputStream is = zip.getInputStream(ze); is.close(); } zip.close(); long duration = System.currentTimeMillis() - start; ICloseableDirectory dir = FileSystem.getFSRoot(baseDir).toCloseable(); start = System.currentTimeMillis(); for (int i=0; i<N; i++) { IFile appMf = dir.getFile("META-INF/APPLICATION.MF"); InputStream is = appMf.open(); is.close(); } long duration2 = System.currentTimeMillis() - start; dir.close(); assertTrue("ZipFile: "+duration+", IDirectory: "+duration2 , duration2 < 10*duration ); IDirectory outer = FileSystem.getFSRoot(new File("fileSystemTest/outer.zip")); IFile innerFile = outer.getFile("app2.zip"); dir = innerFile.convertNested().toCloseable(); start = System.currentTimeMillis(); for (int i=0; i<N; i++) { IFile appMf = dir.getFile("META-INF/APPLICATION.MF"); InputStream is = appMf.open(); is.close(); } long duration3 = System.currentTimeMillis() - start; dir.close(); assertTrue("ZipFile: "+duration+", IDirectory: "+duration3 , duration3 < 10*duration ); }