geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r1085868 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/archive/ test/java/org/apache/xbean/finder/ test/java/org/apache/xbean/finder/archive/ test/java/org/apache/xbean/finder/util/
Date Sun, 27 Mar 2011 04:16:24 GMT
Author: dblevins
Date: Sun Mar 27 04:16:23 2011
New Revision: 1085868

URL: http://svn.apache.org/viewvc?rev=1085868&view=rev
Log:
 XBEAN-167:  Archive Interface for cleaner integration of Classpath and OSGi based systems
  XBEAN-168: ClassesArchive for static class list finder support
  XBEAN-169: ClasspathArchive for traditional java classpath finder support


Finished ClasspathArchive.  It is now made from JarArchive and FileArchive joined by CompositeArchive.
All fully tested.
Many other new Archive tests


Added:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/CompositeArchive.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/FileArchive.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClassesArchiveTest.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClasspathArchiveTest.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/CompositeArchiveTest.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FilteredArchiveTest.java   (contents, props changed)
      - copied, changed from r1085846, geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java   (with props)
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/MockArchive.java   (contents, props changed)
      - copied, changed from r1085846, geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/util/IOUtil.java   (with props)
Removed:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java
Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/ClasspathArchive.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/ClasspathArchive.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/ClasspathArchive.java?rev=1085868&r1=1085867&r2=1085868&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/ClasspathArchive.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/ClasspathArchive.java Sun Mar 27 04:16:23 2011
@@ -18,18 +18,18 @@ package org.apache.xbean.finder.archive;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.JarURLConnection;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.Iterator;
 import java.util.List;
 
 /**
- * TODO Unfinished
+ * Supports JarArchive and FileArchive URLs
+ *
  * @version $Rev$ $Date$
  */
-public class ClasspathArchive implements Archive {
+public class ClasspathArchive extends CompositeArchive {
 
     private final List<URL> urls = new ArrayList<URL>();
     private final ClassLoader loader;
@@ -39,16 +39,46 @@ public class ClasspathArchive implements
     }
 
     public ClasspathArchive(ClassLoader loader, Iterable<URL> urls) {
+        super(archives(loader, urls));
         this.loader = loader;
-        for (URL url : urls) {
-            this.urls.add(url);
-        }
+
     }
 
-    @Override
-    public Iterator<String> iterator() {
-        // TODO
-        return Collections.EMPTY_LIST.iterator();
+    public static List<Archive> archives(ClassLoader loader, Iterable<URL> urls) {
+        List<Archive> archives = new ArrayList<Archive>();
+
+        for (URL location : urls) {
+            try {
+
+                if (location.getProtocol().equals("jar")) {
+
+                    archives.add(new JarArchive(loader, location));
+
+                } else if (location.getProtocol().equals("file")) {
+
+                    try {
+
+                        // See if it's actually a jar
+
+                        URL jarUrl = new URL("jar", "", location.toExternalForm() + "!/");
+                        JarURLConnection juc = (JarURLConnection) jarUrl.openConnection();
+                        juc.getJarFile();
+
+                        archives.add(new JarArchive(loader, jarUrl));
+
+                    } catch (IOException e) {
+
+                        archives.add(new FileArchive(loader, location));
+
+                    }
+                }
+            } catch (Exception e) {
+                // TODO This is what we did before, so not too urgent to change, but not ideal
+                e.printStackTrace();
+            }
+        }
+
+        return archives;
     }
 
     @Override

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/CompositeArchive.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/CompositeArchive.java?rev=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/CompositeArchive.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/CompositeArchive.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,109 @@
+/**
+ * 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.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class CompositeArchive implements Archive {
+
+    private final List<Archive> archives = new ArrayList<Archive>();
+
+    public CompositeArchive(Archive... archives) {
+        this(Arrays.asList(archives));
+    }
+
+    public CompositeArchive(Iterable<Archive> archives) {
+        for (Archive archive : archives) {
+            this.archives.add(archive);
+        }
+    }
+
+    @Override
+    public InputStream getBytecode(String className) throws IOException, ClassNotFoundException {
+        for (Archive archive : archives) {
+            try {
+                return archive.getBytecode(className);
+            } catch (ClassNotFoundException e) {
+            }
+        }
+
+        throw new ClassNotFoundException(className);
+    }
+
+    @Override
+    public Class<?> loadClass(String className) throws ClassNotFoundException {
+        for (Archive archive : archives) {
+            try {
+                return archive.loadClass(className);
+            } catch (ClassNotFoundException e) {
+            }
+        }
+
+        throw new ClassNotFoundException(className);
+    }
+
+    @Override
+    public Iterator<String> iterator() {
+        return new CompositeIterator(archives);
+    }
+
+    private static class CompositeIterator implements Iterator<String> {
+
+        private Iterator<Archive> archives;
+        private Iterator<String> current;
+
+        private CompositeIterator(Iterable<Archive> archives) {
+            this.archives = archives.iterator();
+            if (this.archives.hasNext()) {
+                current = this.archives.next().iterator();
+            }
+        }
+
+        @Override
+        public boolean hasNext() {
+            if (current == null) return false;
+            if (current.hasNext()) return true;
+            
+            if (archives.hasNext()) {
+                current = archives.next().iterator();
+                return hasNext();
+            }
+            return false;
+        }
+
+        @Override
+        public String next() {
+            if (!hasNext()) throw new NoSuchElementException();
+
+            return current.next();
+        }
+
+        @Override
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+}

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

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/FileArchive.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/FileArchive.java?rev=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/FileArchive.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/FileArchive.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,150 @@
+/**
+ * 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.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class FileArchive implements Archive {
+
+    private final ClassLoader loader;
+    private final File dir;
+
+    public FileArchive(ClassLoader loader, URL url) {
+        this.loader = loader;
+        this.dir = toFile(url);
+    }
+
+    public FileArchive(ClassLoader loader, File dir) {
+        this.loader = loader;
+        this.dir = dir;
+    }
+
+    @Override
+    public InputStream getBytecode(String className) throws IOException, ClassNotFoundException {
+        int pos = className.indexOf("<");
+        if (pos > -1) {
+            className = className.substring(0, pos);
+        }
+        pos = className.indexOf(">");
+        if (pos > -1) {
+            className = className.substring(0, pos);
+        }
+        if (!className.endsWith(".class")) {
+            className = className.replace('.', '/') + ".class";
+        }
+
+        URL resource = loader.getResource(className);
+        if (resource != null) return resource.openStream();
+
+        throw new ClassNotFoundException(className);
+    }
+
+
+    @Override
+    public Class<?> loadClass(String className) throws ClassNotFoundException {
+        return loader.loadClass(className);
+    }
+
+    @Override
+    public Iterator<String> iterator() {
+        return file(dir).iterator();
+    }
+
+    private List<String> file(File dir) {
+        List<String> classNames = new ArrayList<String>();
+        if (dir.isDirectory()) {
+            scanDir(dir, classNames, "");
+        }
+        return classNames;
+    }
+
+    private void scanDir(File dir, List<String> classNames, String packageName) {
+        File[] files = dir.listFiles();
+        for (File file : files) {
+            if (file.isDirectory()) {
+                scanDir(file, classNames, packageName + file.getName() + ".");
+            } else if (file.getName().endsWith(".class")) {
+                String name = file.getName();
+                name = name.replaceFirst(".class$", "");
+                if (name.contains(".")) continue;
+                classNames.add(packageName + name);
+            }
+        }
+    }
+
+    private static File toFile(URL url) {
+        if (!"file".equals(url.getProtocol())) throw new IllegalArgumentException("not a file url: " + url);
+        String path = url.getFile();
+        File dir = new File(decode(path));
+        if (dir.getName().equals("META-INF")) {
+            dir = dir.getParentFile(); // Scrape "META-INF" off
+        }
+        return dir;
+    }
+
+    public static String decode(String fileName) {
+        if (fileName.indexOf('%') == -1) return fileName;
+
+        StringBuilder result = new StringBuilder(fileName.length());
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+        for (int i = 0; i < fileName.length();) {
+            char c = fileName.charAt(i);
+
+            if (c == '%') {
+                out.reset();
+                do {
+                    if (i + 2 >= fileName.length()) {
+                        throw new IllegalArgumentException("Incomplete % sequence at: " + i);
+                    }
+
+                    int d1 = Character.digit(fileName.charAt(i + 1), 16);
+                    int d2 = Character.digit(fileName.charAt(i + 2), 16);
+
+                    if (d1 == -1 || d2 == -1) {
+                        throw new IllegalArgumentException("Invalid % sequence (" + fileName.substring(i, i + 3) + ") at: " + String.valueOf(i));
+                    }
+
+                    out.write((byte) ((d1 << 4) + d2));
+
+                    i += 3;
+
+                } while (i < fileName.length() && fileName.charAt(i) == '%');
+
+
+                result.append(out.toString());
+
+                continue;
+            } else {
+                result.append(c);
+            }
+
+            i++;
+        }
+        return result.toString();
+    }
+}
\ No newline at end of file

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

Added: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java?rev=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/archive/JarArchive.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,110 @@
+/**
+ * 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.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JarArchive implements Archive {
+
+    private final ClassLoader loader;
+    private final URL url;
+
+    public JarArchive(ClassLoader loader, URL url) {
+        if (!"jar".equals(url.getProtocol())) throw new IllegalArgumentException("not a file url: " + url);
+        this.loader = loader;
+        this.url = url;
+    }
+
+    @Override
+    public InputStream getBytecode(String className) throws IOException, ClassNotFoundException {
+        int pos = className.indexOf("<");
+        if (pos > -1) {
+            className = className.substring(0, pos);
+        }
+        pos = className.indexOf(">");
+        if (pos > -1) {
+            className = className.substring(0, pos);
+        }
+        if (!className.endsWith(".class")) {
+            className = className.replace('.', '/') + ".class";
+        }
+
+        URL resource = loader.getResource(className);
+        if (resource != null) return resource.openStream();
+
+        throw new ClassNotFoundException(className);
+    }
+
+
+    @Override
+    public Class<?> loadClass(String className) throws ClassNotFoundException {
+        return loader.loadClass(className);
+    }
+
+    @Override
+    public Iterator<String> iterator() {
+        try {
+            return jar(url).iterator();
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+    private List<String> jar(URL location) throws IOException {
+        String jarPath = location.getFile();
+        if (jarPath.indexOf("!") > -1){
+            jarPath = jarPath.substring(0, jarPath.indexOf("!"));
+        }
+        URL url = new URL(jarPath);
+        InputStream in = url.openStream();
+        try {
+            JarInputStream jarStream = new JarInputStream(in);
+            return jar(jarStream);
+        } finally {
+            in.close();
+        }
+    }
+
+    private List<String> jar(JarInputStream jarStream) throws IOException {
+        List<String> classNames = new ArrayList<String>();
+
+        JarEntry entry;
+        while ((entry = jarStream.getNextJarEntry()) != null) {
+            if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
+                continue;
+            }
+            String className = entry.getName();
+            className = className.replaceFirst(".class$", "");
+            if (className.contains(".")) continue;
+            className = className.replace('/', '.');
+            classNames.add(className);
+        }
+
+        return classNames;
+    }
+
+}

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

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClassesArchiveTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClassesArchiveTest.java?rev=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClassesArchiveTest.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClassesArchiveTest.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,84 @@
+/**
+ * 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 junit.framework.TestCase;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClassesArchiveTest extends TestCase {
+    private ClassesArchive archive;
+
+    @Override
+    protected void setUp() throws Exception {
+        archive = new ClassesArchive(Red.class, Green.class, Blue.class);
+    }
+
+    public void testGetBytecode() throws Exception {
+
+        assertNotNull(archive.getBytecode(Blue.class.getName()));
+        assertNotNull(archive.getBytecode(Green.class.getName()));
+        assertNotNull(archive.getBytecode(Red.class.getName()));
+
+        try {
+            archive.getBytecode("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    public void testLoadClass() throws Exception {
+        assertEquals(Blue.class, archive.loadClass(Blue.class.getName()));
+        assertEquals(Green.class, archive.loadClass(Green.class.getName()));
+        assertEquals(Red.class, archive.loadClass(Red.class.getName()));
+
+        try {
+            archive.loadClass("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    public void testIterator() throws Exception {
+        List<String> classes = new ArrayList<String>();
+        for (String classname : archive) {
+            classes.add(classname);
+        }
+
+        assertFalse(0 == classes.size());
+        assertTrue(classes.contains(Blue.class.getName()));
+        assertTrue(classes.contains(Red.class.getName()));
+        assertTrue(classes.contains(Green.class.getName()));
+        assertEquals(3, classes.size());
+    }
+
+    public static class Red {
+    }
+
+    public static class Green {
+    }
+
+    public static class Blue {
+    }
+
+}

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

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClasspathArchiveTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClasspathArchiveTest.java?rev=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClasspathArchiveTest.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/ClasspathArchiveTest.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,115 @@
+/**
+ * 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 junit.framework.TestCase;
+import org.acme.foo.Blue;
+import org.acme.foo.Green;
+import org.acme.foo.Red;
+import org.apache.xbean.finder.UrlSet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClasspathArchiveTest extends TestCase {
+
+
+    private ClasspathArchive archive;
+    private final Class[] classes = {Blue.class, Blue.Navy.class, Blue.Sky.class, Green.class, Green.Emerald.class, Red.class, Red.CandyApple.class, Red.Pink.class};
+
+    @Override
+    protected void setUp() throws Exception {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+
+        UrlSet urlSet = new UrlSet(classLoader);
+
+        if (classLoader.getParent() != null) {
+            urlSet = urlSet.exclude(classLoader.getParent());
+        }
+
+        urlSet = urlSet.excludeJavaHome();
+
+        archive = new ClasspathArchive(classLoader, urlSet.getUrls());
+    }
+
+    public void testGetBytecode() throws Exception {
+
+        for (Class clazz : classes) {
+            assertNotNull(clazz.getName(), archive.getBytecode(clazz.getName()));
+        }
+
+        try {
+            archive.getBytecode("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    public void testLoadClass() throws Exception {
+        for (Class clazz : classes) {
+            assertEquals(clazz.getName(), clazz, archive.loadClass(clazz.getName()));
+        }
+
+        try {
+            archive.loadClass("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    public void testArchives() throws Exception {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+
+        UrlSet urlSet = new UrlSet(classLoader);
+
+        if (classLoader.getParent() != null) {
+            urlSet = urlSet.exclude(classLoader.getParent());
+        }
+
+        urlSet = urlSet.excludeJavaHome();
+
+        List<Archive> list = ClasspathArchive.archives(classLoader, urlSet.getUrls());
+
+
+        // At least classes, test-classes and junit.jar should be here
+        assertTrue(list.size() >= 3);
+
+        // target/classes/ and target/test-classes/
+        assertTrue(sublist(list, FileArchive.class).size() >= 2);
+
+        // junit
+        assertTrue(sublist(list, JarArchive.class).size() >= 1);
+    }
+
+    private <T> List<T> sublist(List<Archive> list, Class<? extends T> type) {
+        List<T> ts = new ArrayList<T>();
+        for (Archive archive : list) {
+
+            if (type.isAssignableFrom(archive.getClass())) {
+                T t = (T) archive;
+                ts.add(t);
+            }
+        }
+        return ts;
+    }
+
+}

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

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/CompositeArchiveTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/CompositeArchiveTest.java?rev=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/CompositeArchiveTest.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/CompositeArchiveTest.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,89 @@
+/**
+ * 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 junit.framework.TestCase;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class CompositeArchiveTest extends TestCase {
+    private CompositeArchive archive;
+    Class[] classes = {Blue.class, Green.class, Red.class};
+
+    @Override
+    protected void setUp() throws Exception {
+        archive = new CompositeArchive(
+                new ClassesArchive(Red.class, Green.class),
+                new ClassesArchive(Blue.class)
+        );
+    }
+
+    public void testGetBytecode() throws Exception {
+
+        for (Class clazz : classes) {
+            assertNotNull(clazz.getName(), archive.getBytecode(clazz.getName()));
+        }
+
+        try {
+            archive.getBytecode("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    public void testLoadClass() throws Exception {
+        for (Class clazz : classes) {
+            assertEquals(clazz.getName(), clazz, archive.loadClass(clazz.getName()));
+        }
+
+        try {
+            archive.loadClass("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    public void testIterator() throws Exception {
+        List<String> classes = new ArrayList<String>();
+        for (String classname : archive) {
+            classes.add(classname);
+        }
+
+        assertFalse(0 == classes.size());
+        assertTrue(classes.contains(Blue.class.getName()));
+        assertTrue(classes.contains(Red.class.getName()));
+        assertTrue(classes.contains(Green.class.getName()));
+        assertEquals(3, classes.size());
+    }
+
+
+    public static class Red {
+    }
+
+    public static class Green {
+    }
+
+    public static class Blue {
+    }
+
+}

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

Added: 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=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FileArchiveTest.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,139 @@
+/**
+ * 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 org.acme.foo.Blue;
+import org.acme.foo.Green;
+import org.acme.foo.Red;
+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 static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class FileArchiveTest {
+
+    private static final Class[] classes = {Blue.class, Blue.Navy.class, Blue.Sky.class, Green.class, Green.Emerald.class, Red.class, Red.CandyApple.class, Red.Pink.class};
+    private static File classpath;
+    private FileArchive archive;
+
+    @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();
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        URL[] urls = {classpath.toURI().toURL()};
+
+        archive = new FileArchive(new URLClassLoader(urls), urls[0]);
+    }
+
+
+    @Test
+    public void testGetBytecode() throws Exception {
+
+        for (Class clazz : classes) {
+            assertNotNull(clazz.getName(), archive.getBytecode(clazz.getName()));
+        }
+
+        try {
+            archive.getBytecode("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    @Test
+    public void testLoadClass() throws Exception {
+        for (Class clazz : classes) {
+            assertEquals(clazz.getName(), clazz, archive.loadClass(clazz.getName()));
+        }
+
+        try {
+            archive.loadClass("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    @Test
+    public void testIterator() throws Exception {
+        List<String> actual = new ArrayList<String>();
+        for (String classname : archive) {
+            actual.add(classname);
+        }
+
+        assertFalse(0 == actual.size());
+
+        for (Class clazz : classes) {
+            assertTrue(clazz.getName(), actual.contains(Blue.class.getName()));
+        }
+
+        assertEquals(classes.length, actual.size());
+    }
+
+
+}

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

Copied: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FilteredArchiveTest.java (from r1085846, geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java)
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FilteredArchiveTest.java?p2=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FilteredArchiveTest.java&p1=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java&r1=1085846&r2=1085868&rev=1085868&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/FilteredArchiveTest.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/FilteredArchiveTest.java Sun Mar 27 04:16:23 2011
@@ -14,10 +14,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
-package org.apache.xbean.finder;
+package org.apache.xbean.finder.archive;
 
 import junit.framework.TestCase;
-import org.apache.xbean.finder.archive.FilteredArchive;
 import org.apache.xbean.finder.filter.Filter;
 
 import java.util.ArrayList;

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

Added: 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=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/JarArchiveTest.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,142 @@
+/**
+ * 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 org.acme.foo.Blue;
+import org.acme.foo.Green;
+import org.acme.foo.Red;
+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;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JarArchiveTest {
+
+    private static final Class[] classes = {Blue.class, Blue.Navy.class, Blue.Sky.class, Green.class, Green.Emerald.class, Red.class, Red.CandyApple.class, Red.Pink.class};
+    private static File classpath;
+    private JarArchive archive;
+
+    @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();
+    }
+
+    @Before
+    public void setUp() throws Exception {
+
+        URL[] urls = {new URL("jar:" + classpath.toURI().toURL() + "!/")};
+
+        archive = new JarArchive(new URLClassLoader(urls), urls[0]);
+    }
+
+
+    @Test
+    public void testGetBytecode() throws Exception {
+
+        for (Class clazz : classes) {
+            assertNotNull(clazz.getName(), archive.getBytecode(clazz.getName()));
+        }
+
+        try {
+            archive.getBytecode("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    @Test
+    public void testLoadClass() throws Exception {
+        for (Class clazz : classes) {
+            assertEquals(clazz.getName(), clazz, archive.loadClass(clazz.getName()));
+        }
+
+        try {
+            archive.loadClass("Fake");
+            fail("ClassNotFoundException should have been thrown");
+        } catch (ClassNotFoundException e) {
+            // pass
+        }
+    }
+
+    @Test
+    public void testIterator() throws Exception {
+        List<String> actual = new ArrayList<String>();
+        for (String classname : archive) {
+            actual.add(classname);
+        }
+
+        assertFalse(0 == actual.size());
+
+        for (Class clazz : classes) {
+            assertTrue(clazz.getName(), actual.contains(Blue.class.getName()));
+        }
+
+        assertEquals(classes.length, actual.size());
+    }
+
+
+}
\ No newline at end of file

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

Copied: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/MockArchive.java (from r1085846, geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java)
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/MockArchive.java?p2=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/MockArchive.java&p1=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java&r1=1085846&r2=1085868&rev=1085868&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/MockArchive.java (original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/archive/MockArchive.java Sun Mar 27 04:16:23 2011
@@ -14,7 +14,7 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
-package org.apache.xbean.finder;
+package org.apache.xbean.finder.archive;
 
 import org.apache.xbean.finder.archive.Archive;
 

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

Added: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/util/IOUtil.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/util/IOUtil.java?rev=1085868&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/util/IOUtil.java (added)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/util/IOUtil.java Sun Mar 27 04:16:23 2011
@@ -0,0 +1,109 @@
+/**
+ * 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.util;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.Flushable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.URL;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class IOUtil {
+    public static String readString(URL url) throws IOException {
+        InputStream in = url.openStream();
+        try {
+            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+            return reader.readLine();
+        } finally {
+            close(in);
+        }
+    }
+
+    public static String readString(File file) throws IOException {
+        FileReader in = new FileReader(file);
+        try {
+            BufferedReader reader = new BufferedReader(in);
+            return reader.readLine();
+        } finally {
+            close(in);
+        }
+    }
+
+    public static void writeString(File file, String string) throws IOException {
+        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
+        bufferedWriter.write(string);
+        bufferedWriter.newLine();
+        close(bufferedWriter);
+    }
+
+    public static void copy(File from, OutputStream to) throws IOException {
+        copy(read(from), to);
+    }
+
+    public static void copy(InputStream from, OutputStream to) throws IOException {
+        try {
+            byte[] buffer = new byte[1024];
+            int length = 0;
+            while ((length = from.read(buffer)) != -1) {
+                to.write(buffer, 0, length);
+            }
+        } finally {
+            close(from);
+            close(to);
+        }
+
+    }
+
+    public static void close(Closeable closeable) throws IOException {
+        if (closeable == null) return;
+        try {
+            if (closeable instanceof Flushable) {
+                ((Flushable) closeable).flush();
+            }
+        } catch (IOException e) {
+        }
+        try {
+            closeable.close();
+        } catch (IOException e) {
+        }
+    }
+
+    public static OutputStream write(File destination) throws FileNotFoundException {
+        OutputStream out = new FileOutputStream(destination);
+        return new BufferedOutputStream(out, 32768);
+    }
+
+    public static InputStream read(File source) throws FileNotFoundException {
+        InputStream in = new FileInputStream(source);
+        return new BufferedInputStream(in, 32768);
+    }
+}

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



Mime
View raw message