You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xbean-scm@geronimo.apache.org by db...@apache.org on 2011/05/05 08:27:56 UTC

svn commit: r1099698 - in /geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder: ResourceFinderTest.java archive/Archives.java archive/FileArchiveTest.java archive/JarArchiveTest.java

Author: dblevins
Date: Thu May  5 06:27:55 2011
New Revision: 1099698

URL: http://svn.apache.org/viewvc?rev=1099698&view=rev
Log:
only changes are to tests.  was using it to drive out a jar xbean-finder related issue.  still unsolved.

Added:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/Archives.java   (with props)
Modified:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java?rev=1099698&r1=1099697&r2=1099698&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java Thu May  5 06:27:55 2011
@@ -20,11 +20,18 @@ package org.apache.xbean.finder;
  * @version $Rev$ $Date$
  */
 
+import java.io.File;
+import java.io.IOException;
+import java.net.JarURLConnection;
 import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
 import junit.framework.TestCase;
 import org.acme.BarUrlHandler;
@@ -35,6 +42,7 @@ import org.acme.Two;
 import org.acme.javaURLContextFactory;
 import org.acme.kernelURLContextFactory;
 import org.acme.ldapURLContextFactory;
+import org.apache.xbean.finder.archive.Archives;
 
 public class ResourceFinderTest extends TestCase {
     ResourceFinder resourceFinder = new ResourceFinder("META-INF/");
@@ -350,6 +358,50 @@ public class ResourceFinderTest extends 
         assertEquals("year", "2005", properties.getProperty("year"));
     }
 
+
+    public void testWebinfJar() throws Exception {
+
+        Map<String, String> map = new HashMap<String, String>();
+        map.put("WEB-INF/beans.xml", "<beans/>");
+
+        final File jarFile = Archives.jarArchive(map);
+
+        final URL jarFileUrl = jarFile.toURI().toURL();
+        final ResourceFinder finder = new ResourceFinder(jarFileUrl);
+
+        final URL beansXmlUrl = finder.find("WEB-INF/beans.xml");
+
+        assertNotNull(beansXmlUrl);
+    }
+
+
+    private static void readJarEntries(URL location, String basePath, Map<String, URL> resources) throws IOException {
+        JarURLConnection conn = (JarURLConnection) location.openConnection();
+        JarFile jarfile = null;
+        jarfile = conn.getJarFile();
+
+        Enumeration<JarEntry> entries = jarfile.entries();
+        while (entries != null && entries.hasMoreElements()) {
+            JarEntry entry = entries.nextElement();
+            String name = entry.getName();
+
+            if (entry.isDirectory() || !name.startsWith(basePath) || name.length() == basePath.length()) {
+                continue;
+            }
+
+            name = name.substring(basePath.length());
+
+            if (name.contains("/")) {
+                continue;
+            }
+
+            URL resource = new URL(location, name);
+            resources.put(name, resource);
+        }
+    }
+
+
+
     private void validateSimpsons(Properties properties) {
         assertEquals("props size", 6, properties.size());
         assertEquals("creator", "Matt Groening", properties.getProperty("creator"));

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/Archives.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/Archives.java?rev=1099698&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/Archives.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/Archives.java Thu May  5 06:27:55 2011
@@ -0,0 +1,140 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.finder.archive;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class Archives {
+
+    public static File fileArchive(Class[] classes) throws IOException {
+        return fileArchive(new HashMap<String, String>(), classes);
+    }
+
+    public static File fileArchive(Map<String, String> entries, Class... classes) throws IOException {
+
+        ClassLoader loader = Archives.class.getClassLoader();
+
+        File classpath = File.createTempFile("path with spaces", "classes");
+
+        assertTrue(classpath.delete());
+        assertTrue(classpath.mkdirs());
+
+
+        for (Class clazz : classes) {
+            String name = clazz.getName().replace('.', File.separatorChar) + ".class";
+            File file = new File(classpath, name);
+
+            File d = file.getParentFile();
+
+            if (!d.exists()) assertTrue(d.getAbsolutePath(), d.mkdirs());
+
+            URL resource = loader.getResource(name);
+            assertNotNull(resource);
+
+            InputStream in = new BufferedInputStream(resource.openStream());
+            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
+
+            int i = -1;
+            while ((i = in.read()) != -1) {
+                out.write(i);
+            }
+
+            out.close();
+            in.close();
+        }
+
+        for (Map.Entry<String, String> entry : entries.entrySet()) {
+
+            final String key = entry.getKey().replace('/', File.separatorChar);
+
+            final File file = new File(classpath, key);
+
+            File d = file.getParentFile();
+
+            if (!d.exists()) assertTrue(d.getAbsolutePath(), d.mkdirs());
+
+            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
+
+            out.write(entry.getValue().getBytes());
+
+            out.close();
+        }
+
+        return classpath;
+    }
+
+    public static File jarArchive(Class[] classes) throws IOException {
+        return jarArchive(new HashMap<String, String>(), classes);
+    }
+
+    public static File jarArchive(Map<String, String> entries, Class... classes) throws IOException {
+
+        ClassLoader loader = Archives.class.getClassLoader();
+
+        File classpath = File.createTempFile("path with spaces", ".jar");
+
+        // Create the ZIP file
+        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(classpath)));
+
+        for (Class clazz : classes) {
+            String name = clazz.getName().replace('.', File.separatorChar) + ".class";
+
+            URL resource = loader.getResource(name);
+            assertNotNull(resource);
+
+            // Add ZIP entry to output stream.
+            out.putNextEntry(new ZipEntry(name));
+
+            InputStream in = new BufferedInputStream(resource.openStream());
+
+            int i = -1;
+            while ((i = in.read()) != -1) {
+                out.write(i);
+            }
+
+            // Complete the entry
+            out.closeEntry();
+        }
+
+        for (Map.Entry<String, String> entry : entries.entrySet()) {
+
+            out.putNextEntry(new ZipEntry(entry.getKey()));
+
+            out.write(entry.getValue().getBytes());
+        }
+
+        // Complete the ZIP file
+        out.close();
+        return classpath;
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/Archives.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java?rev=1099698&r1=1099697&r2=1099698&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java Thu May  5 06:27:55 2011
@@ -23,11 +23,7 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
@@ -51,35 +47,7 @@ public class FileArchiveTest {
     @BeforeClass
     public static void classSetUp() throws Exception {
 
-        ClassLoader loader = FileArchiveTest.class.getClassLoader();
-
-        classpath = File.createTempFile("path with spaces", "classes");
-
-        assertTrue(classpath.delete());
-        assertTrue(classpath.mkdirs());
-
-        for (Class clazz : classes) {
-            String name = clazz.getName().replace('.', File.separatorChar) + ".class";
-            File file = new File(classpath, name);
-
-            File d = file.getParentFile();
-
-            if (!d.exists()) assertTrue(d.getAbsolutePath(), d.mkdirs());
-
-            URL resource = loader.getResource(name);
-            assertNotNull(resource);
-
-            InputStream in = new BufferedInputStream(resource.openStream());
-            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
-
-            int i = -1;
-            while ((i = in.read()) != -1) {
-                out.write(i);
-            }
-
-            out.close();
-            in.close();
-        }
+        classpath = Archives.fileArchive(classes);
     }
 
     @Before

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java?rev=1099698&r1=1099697&r2=1099698&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java Thu May  5 06:27:55 2011
@@ -23,17 +23,11 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertFalse;
@@ -53,35 +47,7 @@ public class JarArchiveTest {
     @BeforeClass
     public static void classSetUp() throws Exception {
 
-        ClassLoader loader = JarArchiveTest.class.getClassLoader();
-
-        classpath = File.createTempFile("path with spaces", "classes");
-
-        // Create the ZIP file
-        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(classpath)));
-
-        for (Class clazz : classes) {
-            String name = clazz.getName().replace('.', File.separatorChar) + ".class";
-
-            URL resource = loader.getResource(name);
-            assertNotNull(resource);
-
-            // Add ZIP entry to output stream.
-            out.putNextEntry(new ZipEntry(name));
-
-            InputStream in = new BufferedInputStream(resource.openStream());
-
-            int i = -1;
-            while ((i = in.read()) != -1) {
-                out.write(i);
-            }
-
-            // Complete the entry
-            out.closeEntry();
-        }
-
-        // Complete the ZIP file
-        out.close();
+        classpath = Archives.jarArchive(classes);
     }
 
     @Before