openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1786813 - in /openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src: main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java test/java/org/apache/webbeans/arquillian/test/OwbArquillianWebJarDeploymentTest.java
Date Mon, 13 Mar 2017 21:31:16 GMT
Author: struberg
Date: Mon Mar 13 21:31:16 2017
New Revision: 1786813

URL: http://svn.apache.org/viewvc?rev=1786813&view=rev
Log:
OWB-1172 fix WebArchive handling in the OWB-Arquillian adapter

Before that getResources() did not find anything in jars inside WEB-INF/lib/*.jar

Modified:
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianWebJarDeploymentTest.java

Modified: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java?rev=1786813&r1=1786812&r2=1786813&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbSWClassLoader.java
Mon Mar 13 21:31:16 2017
@@ -21,8 +21,11 @@ package org.apache.webbeans.arquillian.s
 import org.jboss.shrinkwrap.api.Archive;
 import org.jboss.shrinkwrap.api.ArchivePath;
 import org.jboss.shrinkwrap.api.ArchivePaths;
+import org.jboss.shrinkwrap.api.Filters;
 import org.jboss.shrinkwrap.api.Node;
+import org.jboss.shrinkwrap.api.asset.ArchiveAsset;
 import org.jboss.shrinkwrap.api.asset.Asset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
 import org.jboss.shrinkwrap.api.spec.WebArchive;
 
 import java.io.Closeable;
@@ -38,7 +41,10 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class OwbSWClassLoader extends URLClassLoader implements Closeable
 {
@@ -128,12 +134,12 @@ public class OwbSWClassLoader extends UR
     @Override
     public URL findResource(final String name)
     {
-        final Node node = findNode(name);
-        if (node != null)
+        Set<String> nodes = findNodes(archive, name);
+        if (!nodes.isEmpty())
         {
             try
             {
-                return new URL(null, "archive:" + archive.getName() + "/" + name, new ArchiveStreamHandler());
+                return new URL(null, "archive:" + nodes.iterator().next());
             }
             catch (final MalformedURLException e)
             {
@@ -150,31 +156,65 @@ public class OwbSWClassLoader extends UR
     @Override
     public Enumeration<URL> findResources(final String name) throws IOException
     {
-        final Node node = findNode(name);
-        if (node != null)
+        final Set<String> nodes = findNodes(archive, name);
+        List<URL> urls = new ArrayList<>(nodes.size());
+        for (String node : nodes)
         {
-            return Collections.enumeration(Collections.singleton(new URL(null, "archive:"
+ archive.getName() + "/" + name, new ArchiveStreamHandler())));
+            urls.add(new URL(null, "archive:" + node, new ArchiveStreamHandler()));
         }
-        if (useOnlyArchiveResources)
+
+        if (!useOnlyArchiveResources)
         {
-            return EMPTY_ENUMERATION;
+            Enumeration<URL> parentResources = getParent().getResources(name);
+            while (parentResources.hasMoreElements())
+            {
+                urls.add(parentResources.nextElement());
+            }
         }
 
-        return super.findResources(name);
+        return Collections.enumeration(urls);
     }
 
-    private Node findNode(final String name)
+    private Set<String> findNodes(Archive arch, final String name)
     {
-        ArchivePath path = ArchivePaths.create(path(prefix, name));
-        Node node = archive.get(path);
-        if (node == null)
+        Set<String> nodes = new HashSet<>();
+
+        if (arch instanceof WebArchive)
         {
-            path = ArchivePaths.create(name);
-            node = archive.get(path);
+            // first check WEB-INF/classes
+            ArchivePath path = ArchivePaths.create(path(prefix, name));
+            Node node = arch.get(path);
+            if (node != null)
+            {
+                nodes.add(path.get());
+            }
 
+            Map<ArchivePath, Node> jarLibs = ((WebArchive) arch).getContent(Filters.include("/WEB-INF/lib/.*\\.jar"));
+            for (Node jarLib : jarLibs.values())
+            {
+                if (jarLib.getAsset() instanceof ArchiveAsset && ((ArchiveAsset)
jarLib.getAsset()).getArchive() instanceof JavaArchive)
+                {
+                    Set<String> jarNodes = findNodes(((ArchiveAsset) jarLib.getAsset()).getArchive(),
name);
+                    for (String jarNode : jarNodes)
+                    {
+                        path = ArchivePaths.create(path("WEB-INF/lib", ((ArchiveAsset) jarLib.getAsset()).getArchive().getName(),
jarNode));
+                        nodes.add(path.get());
+                    }
 
+                }
+            }
         }
-        return node;
+        else
+        {
+            ArchivePath path = ArchivePaths.create(name);
+            Node node = arch.get(path);
+            if (node != null)
+            {
+                nodes.add(path.get());
+            }
+        }
+
+        return nodes;
     }
 
     private String path(final String... parts)

Modified: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianWebJarDeploymentTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianWebJarDeploymentTest.java?rev=1786813&r1=1786812&r2=1786813&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianWebJarDeploymentTest.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianWebJarDeploymentTest.java
Mon Mar 13 21:31:16 2017
@@ -18,6 +18,14 @@
  */
 package org.apache.webbeans.arquillian.test;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
+
 import javax.inject.Inject;
 
 import org.apache.webbeans.arquillian.test.beans.SampleUser;
@@ -26,6 +34,7 @@ import org.jboss.arquillian.junit.Arquil
 import org.jboss.shrinkwrap.api.Archive;
 import org.jboss.shrinkwrap.api.ShrinkWrap;
 import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
 import org.jboss.shrinkwrap.api.spec.WebArchive;
 import org.junit.Assert;
@@ -39,6 +48,7 @@ import org.junit.runner.RunWith;
 @RunWith(Arquillian.class)
 public class OwbArquillianWebJarDeploymentTest
 {
+    private static final String RESOURCE_NAME = "some/resource.properties";
 
     @Inject
     private SampleUser sampleUser;
@@ -47,27 +57,47 @@ public class OwbArquillianWebJarDeployme
     @Deployment
     public static Archive deploy()
     {
-        JavaArchive testJar = ShrinkWrap
-                .create(JavaArchive.class, "sampleUserTest.jar")
+        JavaArchive testJar1 = ShrinkWrap
+                .create(JavaArchive.class, "sampleUserTest1.jar")
                 .addClass(OwbArquillianWebJarDeploymentTest.class)
                 .addPackage(SampleUser.class.getPackage())
+                .addAsResource(new StringAsset("hello1"), RESOURCE_NAME)
                 .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
 
+        JavaArchive testJar2 = ShrinkWrap
+                .create(JavaArchive.class, "sampleUserTest2.jar")
+                .addAsResource(new StringAsset("hello2"), RESOURCE_NAME);
+
 
         WebArchive testWar = ShrinkWrap
                 .create(WebArchive.class, "sampleUserTest.war")
-                .addAsLibrary(testJar);
+                .addAsResource(new StringAsset("hello3"), RESOURCE_NAME)
+                .addAsLibrary(testJar1)
+                .addAsLibrary(testJar2);
 
         return testWar;
     }
 
     @Test
-    public void testOwbArqContainerStartup()
-    {
+    public void testOwbArqContainerStartup() throws IOException {
         Assert.assertNotNull(sampleUser);
         Assert.assertEquals("Hans", sampleUser.getFirstName());
 
         sampleUser.setFirstName("Karl");
         Assert.assertEquals("Karl", sampleUser.getFirstName());
+
+        Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(RESOURCE_NAME);
+        Set<String> contents = new HashSet<>();
+
+        while (resources.hasMoreElements())
+        {
+            URL url = resources.nextElement();
+            contents.add(new BufferedReader(new InputStreamReader(url.openStream())).readLine());
+        }
+
+        Assert.assertEquals(3, contents.size());
+        Assert.assertTrue(contents.contains("hello1"));
+        Assert.assertTrue(contents.contains("hello2"));
+        Assert.assertTrue(contents.contains("hello3"));
     }
 }



Mime
View raw message