[jboss-cvs] JBossAS SVN: r72304 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/plugins/context/jar and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 16 12:59:46 EDT 2008


Author: alesj
Date: 2008-04-16 12:59:46 -0400 (Wed, 16 Apr 2008)
New Revision: 72304

Added:
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/UnpackTestCase.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java
Log:
Unpack tests.

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java	2008-04-16 16:45:50 UTC (rev 72303)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/VFSUtils.java	2008-04-16 16:59:46 UTC (rev 72304)
@@ -30,6 +30,8 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -43,6 +45,7 @@
 
 import org.jboss.logging.Logger;
 import org.jboss.util.StringPropertyReplacer;
+import org.jboss.util.id.GUID;
 import org.jboss.virtual.plugins.context.file.FileSystemContext;
 import org.jboss.virtual.plugins.context.jar.AbstractJarHandler;
 import org.jboss.virtual.plugins.context.jar.NestedJarHandler;
@@ -73,6 +76,27 @@
    public static final String FORCE_COPY_KEY = "jboss.vfs.forceCopy";
    public static final String USE_COPY_QUERY = "useCopyJarHandler";
 
+   private static File tempDir;
+
+   private static class GetTempDir implements PrivilegedAction<File>
+   {
+      public File run()
+      {
+         String tempDirKey = System.getProperty("vfs.temp.dir", "jboss.server.temp.dir");
+         return new File(System.getProperty(tempDirKey, System.getProperty("java.io.tmpdir")));
+      }
+   }
+
+   private synchronized static File getTempDirectory()
+   {
+      if (tempDir == null)
+      {
+         tempDir = AccessController.doPrivileged(new GetTempDir());
+         log.info("VFS temp dir: " + tempDir);
+      }
+      return tempDir;
+   }
+
    /**
     * Get the paths string for a collection of virtual files
     * 
@@ -470,13 +494,19 @@
 
       VirtualFileHandler handler = file.getHandler();
       // already unpacked
-      if (handler instanceof NestedJarHandler)
+      if (handler instanceof NestedJarHandler || handler instanceof AbstractJarHandler == false)
+      {
+         if (log.isTraceEnabled())
+            log.trace("Should already be unpacked: " + file);
          return file;
-      if (handler instanceof AbstractJarHandler == false)
-         throw new IllegalArgumentException("Can only handle jar files: " + file);
+      }
 
-      File unpacked = File.createTempFile("nestedjar", null);
-      unpack(handler, unpacked);
+      File unpacked = new File(getTempDirectory(), GUID.asString());
+      if (unpacked.mkdir() == false)
+         throw new IllegalArgumentException("Cannot create directory: " + unpacked);
+      unpacked.deleteOnExit();
+
+      unpack(handler, unpacked, false);
       FileSystemContext fileSystemContext = new FileSystemContext(unpacked);
       VirtualFileHandler newHandler = fileSystemContext.getRoot();
       VirtualFileHandler parent = handler.getParent();
@@ -492,18 +522,26 @@
     *
     * @param root the root
     * @param file the file
+    * @param writeRoot do we write root
     * @throws IOException for any error
     */
-   private static void unpack(VirtualFileHandler root, File file) throws IOException
+   private static void unpack(VirtualFileHandler root, File file, boolean writeRoot) throws IOException
    {
-      rewrite(root, file);
+      // should we write trhe root
+      if (writeRoot)
+         rewrite(root, file);
+
       List<VirtualFileHandler> children = root.getChildren(true);
       if (children != null && children.isEmpty() == false)
       {
          for (VirtualFileHandler handler : children)
          {
             File next = new File(file, handler.getName());
-            unpack(handler, next);
+            if (handler.isLeaf() == false && next.mkdir() == false)
+               throw new IllegalArgumentException("Problems creating new file: " + next);
+            next.deleteOnExit();
+
+            unpack(handler, next, handler.isLeaf());
          }
       }
    }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java	2008-04-16 16:45:50 UTC (rev 72303)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java	2008-04-16 16:59:46 UTC (rev 72304)
@@ -51,9 +51,6 @@
    /** The temporary file */
    private transient File temp;
 
-   /** TODO WHAT DOES THIS DO? It is unused */
-   private transient URL original;
-
    /**
     * Create a temporary jar
     * 
@@ -146,8 +143,7 @@
       }
 
       this.temp = temp;
-      this.original = original;
-      
+
       try
       {
          initJarFile();

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java	2008-04-16 16:45:50 UTC (rev 72303)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java	2008-04-16 16:59:46 UTC (rev 72304)
@@ -220,5 +220,4 @@
          in.close();
       }
    }
-
 }
\ No newline at end of file

Copied: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/UnpackTestCase.java (from rev 72283, projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/PathQueryTestCase.java)
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/UnpackTestCase.java	                        (rev 0)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/UnpackTestCase.java	2008-04-16 16:59:46 UTC (rev 72304)
@@ -0,0 +1,165 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.virtual.test;
+
+import java.net.URL;
+import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+import junit.framework.Test;
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VFSUtils;
+import org.jboss.virtual.VirtualFile;
+
+/**
+ * Unpack tests.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class UnpackTestCase extends AbstractVFSTest
+{
+   public UnpackTestCase(String s)
+   {
+      super(s);
+   }
+
+   public static Test suite()
+   {
+      return suite(UnpackTestCase.class);
+   }
+
+   protected void assertNoReplacement(VFS vfs, String name) throws Throwable
+   {
+      VirtualFile original = vfs.findChild(name);
+      VirtualFile replacement = VFSUtils.unpack(original);
+      assertSame(original, replacement);
+   }
+
+   public void testNoReplacement() throws Throwable
+   {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getVFS(rootURL);
+
+      assertNoReplacement(vfs, "unpacked-outer.jar");
+      assertNoReplacement(vfs, "jar1-filesonly.mf");
+      assertNoReplacement(vfs, "unpacked-with-metadata.jar/META-INF");
+   }
+
+   public void testUnpackOuter() throws Throwable
+   {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getVFS(rootURL);
+      VirtualFile original;
+      VirtualFile replacement;
+
+      original = vfs.findChild("outer.jar");
+      replacement = VFSUtils.unpack(original);
+      assertEquals(original.getParent(), replacement.getParent());
+
+      VirtualFile child = replacement.findChild("jar1.jar");
+      assertNotNull(child);
+      assertNotNull(child.findChild("META-INF/MANIFEST.MF"));
+      assertNotNull(replacement.findChild("jar2.jar"));
+   }
+
+   public void testUnpackTopLevel() throws Throwable
+   {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getVFS(rootURL);
+      VirtualFile original;
+      VirtualFile replacement;
+
+      original = vfs.findChild("level1.zip");
+      replacement = VFSUtils.unpack(original);
+      assertEquals(original.getParent(), replacement.getParent());
+      VirtualFile parent = original.getParent();
+      VirtualFile child = parent.findChild("level1.zip");
+      assertEquals(replacement, child);
+
+      VirtualFile textOne = replacement.findChild("test1.txt");
+      testText(textOne);
+      VirtualFile two = replacement.findChild("level2.zip");
+      VirtualFile textTwo = two.findChild("test2.txt");
+      testText(textTwo);
+      VirtualFile three = two.findChild("level3.zip");
+      VirtualFile textThree = three.findChild("test3.txt");
+      testText(textThree);
+   }
+
+   public void testUnpack2ndLevel() throws Throwable
+   {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getVFS(rootURL);
+      VirtualFile original;
+      VirtualFile replacement;
+
+      original = vfs.findChild("level1.zip/level2.zip");
+      replacement = VFSUtils.unpack(original);
+      assertEquals(original.getParent(), replacement.getParent());
+      VirtualFile parent = original.getParent();
+      VirtualFile child = parent.findChild("level2.zip");
+      assertEquals(replacement, child);
+
+      VirtualFile textTwo = replacement.findChild("test2.txt");
+      testText(textTwo);
+      VirtualFile three = replacement.findChild("level3.zip");
+      VirtualFile textThree = three.findChild("test3.txt");
+      testText(textThree);
+   }
+
+   public void testUnpackDeepLevel() throws Throwable
+   {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getVFS(rootURL);
+      VirtualFile original;
+      VirtualFile replacement;
+
+      original = vfs.findChild("level1.zip/level2.zip/level3.zip");
+      replacement = VFSUtils.unpack(original);
+      assertEquals(original.getParent(), replacement.getParent());
+      VirtualFile parent = original.getParent();
+      VirtualFile child = parent.findChild("level3.zip");
+      assertEquals(replacement, child);
+
+      VirtualFile textThree = replacement.findChild("test3.txt");
+      testText(textThree);
+   }
+
+   protected void testText(VirtualFile file) throws Exception
+   {
+      InputStream in = file.openStream();
+      try
+      {
+         BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+         String line;
+         while ((line = reader.readLine()) != null)
+         {
+            assertEquals("Some test.", line);
+         }
+      }
+      finally
+      {
+         in.close();
+      }
+   }
+}
\ No newline at end of file

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java	2008-04-16 16:45:50 UTC (rev 72303)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java	2008-04-16 16:59:46 UTC (rev 72304)
@@ -70,6 +70,8 @@
       suite.addTest(SundryVFSUnitTestCase.suite());
       // options / policy
       suite.addTest(PathQueryTestCase.suite());
+      // unpack
+      suite.addTest(UnpackTestCase.suite());
 
       return suite;
    }




More information about the jboss-cvs-commits mailing list