You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2011/12/28 16:08:23 UTC

git commit: DELTASPIKE-17 add an Arquillian unit test for it

Updated Branches:
  refs/heads/master 1f9fb71d7 -> d8d3965e2


DELTASPIKE-17 add an Arquillian unit test for it

This also adds the OWB profile basic unit testing setup.


Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/d8d3965e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/d8d3965e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/d8d3965e

Branch: refs/heads/master
Commit: d8d3965e2dff60ebff0b27d32effe6ab663aaa5b
Parents: 1f9fb71
Author: Mark Struberg <st...@apache.org>
Authored: Wed Dec 28 16:05:51 2011 +0100
Committer: Mark Struberg <st...@apache.org>
Committed: Wed Dec 28 16:07:27 2011 +0100

----------------------------------------------------------------------
 .../services/javax.enterprise.inject.spi.Extension |    2 +-
 .../test/api/provider/BeanManagerProviderTest.java |   68 ++++
 .../core/test/api/provider/TestBean.java           |   39 ++
 .../api/temptestutil/ShrinkWrapArchiveUtil.java    |  292 +++++++++++++++
 .../impl/src/test/resources/META-INF/beans.xml     |    5 -
 5 files changed, 400 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d8d3965e/deltaspike/core/api/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/deltaspike/core/api/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
index 04b9fc9..4e945d9 100644
--- a/deltaspike/core/api/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
+++ b/deltaspike/core/api/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -18,4 +18,4 @@
 #####################################################################################
 
 # DeltaSpike BeanManager provider
-org.apache.deltaspike.api.provider.BeanManagerProvider
+org.apache.deltaspike.core.api.provider.BeanManagerProvider

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d8d3965e/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/BeanManagerProviderTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/BeanManagerProviderTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/BeanManagerProviderTest.java
new file mode 100644
index 0000000..ee1a1a7
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/BeanManagerProviderTest.java
@@ -0,0 +1,68 @@
+/*
+* 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.deltaspike.core.test.api.provider;
+
+
+import javax.enterprise.inject.spi.BeanManager;
+
+import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.test.api.temptestutil.ShrinkWrapArchiveUtil;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.Assert;
+import org.junit.runner.RunWith;
+
+@RunWith(Arquillian.class)
+public class BeanManagerProviderTest
+{
+    /**
+     *X TODO creating a WebArchive is only a workaround because JavaArchive cannot contain other archives.
+     */
+    @Deployment
+    public static WebArchive deploy()
+    {
+        return ShrinkWrap.create(WebArchive.class)
+                .addAsLibraries(ShrinkWrapArchiveUtil.getArchives(null,
+                                                  "META-INF/beans.xml",
+                                                  new String[]{"org.apache.deltaspike"},
+                                                  null))
+                .addClass(TestBean.class)
+                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+    }
+    
+    @Test
+    public void testBeanManagerProvider() throws Exception {
+            BeanManagerProvider bmp = BeanManagerProvider.getInstance();
+            Assert.assertNotNull(bmp);
+
+            BeanManager bm = bmp.getBeanManager();
+            Assert.assertNotNull(bm);
+
+            TestBean tb1 = BeanProvider.getContextualReference(TestBean.class, false);
+            Assert.assertNotNull(tb1);
+
+            TestBean tb2 = BeanProvider.getContextualReference(TestBean.class, false, "extraNameBean");
+            Assert.assertNotNull(tb2);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d8d3965e/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/TestBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/TestBean.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/TestBean.java
new file mode 100644
index 0000000..6f88e53
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/provider/TestBean.java
@@ -0,0 +1,39 @@
+/*
+* 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.deltaspike.core.test.api.provider;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+
+@Named("extraNameBean")
+@ApplicationScoped
+public class TestBean
+{
+    int i=4711;
+
+    public int getI()
+    {
+        return i;
+    }
+
+    public void setI(int i)
+    {
+        this.i = i;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d8d3965e/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/temptestutil/ShrinkWrapArchiveUtil.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/temptestutil/ShrinkWrapArchiveUtil.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/temptestutil/ShrinkWrapArchiveUtil.java
new file mode 100644
index 0000000..24eb054
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/core/test/api/temptestutil/ShrinkWrapArchiveUtil.java
@@ -0,0 +1,292 @@
+/*
+ * 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.deltaspike.core.test.api.temptestutil;
+
+
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.jar.JarInputStream;
+import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+
+
+/**
+ * Lots of neat little helpers to more easily create JavaArchives from marker files on the classpath.
+ * This should finally get moved to ShrinkWrap core!
+ *
+ * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
+ * @deprecated This class should get moved to ShrinkWrap itself!
+ */
+public class ShrinkWrapArchiveUtil
+{
+    private static final Logger LOG = Logger.getLogger(ShrinkWrapArchiveUtil.class.getName());
+    /**
+     * Resolve all markerFiles from the current ClassPath and package the root nodes
+     * into a JavaArchive.
+     * @param classLoader to use
+     * @param markerFile
+     * @param includeIfPackageExists if not null, we will only create JavaArchives if the given package exists
+     * @param excludeIfPackageExists if not null, we will <b>not</b> create JavaArchives if the given package exists.
+     *                               This has a higher precedence than includeIfPackageExists.
+     * @return
+     */
+    public static JavaArchive[] getArchives(ClassLoader classLoader,
+                                            String markerFile,
+                                            String[] includeIfPackageExists,
+                                            String[] excludeIfPackageExists)
+    {
+        if (classLoader == null) {
+            classLoader = ShrinkWrapArchiveUtil.class.getClassLoader();
+        }
+
+        try {
+            Enumeration<URL> foundFiles = classLoader.getResources(markerFile);
+    
+            List<JavaArchive> archives = new ArrayList<JavaArchive>();
+    
+            while (foundFiles.hasMoreElements()) {
+                URL foundFile = foundFiles.nextElement();
+                LOG.fine("Evaluating Java ClassPath URL " + foundFile.toExternalForm());
+    
+                JavaArchive archive = createArchive(foundFile, markerFile, includeIfPackageExists, excludeIfPackageExists);
+                if (archive != null) {
+                    LOG.info("Adding Java ClassPath URL as JavaArchive " + foundFile.toExternalForm());
+                    archives.add(archive);
+                }
+            }
+
+            return archives.toArray(new JavaArchive[archives.size()]);
+        }
+        catch (IOException ioe) {
+            throw new RuntimeException(ioe);
+        }
+
+    }
+
+    private static JavaArchive createArchive(URL foundFile, String markerFile,
+                                             String[] includeIfPackageExists, String[] excludeIfPackageExists)
+            throws IOException {
+        String urlString = foundFile.toString();
+        int idx = urlString.lastIndexOf(markerFile);
+        urlString = urlString.substring(0, idx);
+
+        String jarUrlPath = isJarUrl(urlString);
+        if (jarUrlPath != null)
+        {
+            return addJarArchive((new URL(ensureCorrectUrlFormat(jarUrlPath))).openStream(),
+                    includeIfPackageExists, excludeIfPackageExists);
+        }
+        else
+        {
+            File f = new File( (new URL(ensureCorrectUrlFormat(urlString))).getFile() );
+            if (!f.exists())
+            {
+                // try a fallback if the URL contains %20 -> spaces
+                if (jarUrlPath.contains("%20"))
+                {
+                    jarUrlPath = jarUrlPath.replaceAll("%20", " ");
+                    f = new File( (new URL(ensureCorrectUrlFormat(jarUrlPath))).getFile() );
+                }
+
+            }
+
+            return addFileArchive(f, includeIfPackageExists, excludeIfPackageExists);
+        }
+    }
+
+    private static JavaArchive addJarArchive(InputStream inputStream,
+                                             String[] includeIfPackageExists,
+                                             String[] excludeIfPackageExists)
+            throws IOException {
+        JavaArchive ret = null;
+        JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class);
+
+        if (includeIfPackageExists == null) {
+            // no include rule, thus add it immediately
+            ret = javaArchive;
+        }
+
+        JarInputStream jar = new JarInputStream(inputStream);
+        try {
+            for (ZipEntry jarEntry = jar.getNextEntry(); jarEntry != null; jarEntry = jar.getNextEntry()) {
+                String entryName = jarEntry.getName();
+
+                if (jarEntry.isDirectory()) {
+                    // exclude rule
+                    if (excludeIfPackageExists(entryName, excludeIfPackageExists)) {
+                        return null;
+                    }
+
+                    if (ret == null && includeIfPackageExists(entryName, includeIfPackageExists)) {
+                        ret = javaArchive;
+                    }
+
+                    continue;
+                }
+
+                if (entryName.endsWith(".class")) {
+                    String className = entryName.substring(0, entryName.length()-(".class".length())).replace('/', '.');
+                    javaArchive.addClass(className);
+                }
+                else {
+                    javaArchive.addAsResource(entryName);
+                }
+            }
+        }
+        finally {
+            try {
+                jar.close();
+            }
+            catch (IOException ignored) {
+                // all fine
+            }
+        }
+
+        return ret;
+    }
+
+    private static JavaArchive addFileArchive(File archiveBasePath,
+                                              String[] includeIfPackageExists,
+                                              String[] excludeIfPackageExists)
+            throws IOException {
+        if (!archiveBasePath.exists()) {
+            return null;
+        }
+
+        JavaArchive ret = null;
+        JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class);
+
+        if (includeIfPackageExists == null) {
+            // no include rule, thus add it immediately
+            ret = javaArchive;
+        }
+
+        int basePathLength = archiveBasePath.getAbsolutePath().length() + 1;
+
+        for (File archiveEntry : collectArchiveEntries(archiveBasePath) ) {
+            String entryName = archiveEntry.getAbsolutePath().substring(basePathLength);
+
+            // exclude rule
+            if (excludeIfPackageExists(entryName, excludeIfPackageExists)) {
+                return null;
+            }
+
+            // include rule
+            if (ret == null && includeIfPackageExists(entryName, includeIfPackageExists)) {
+                ret = javaArchive;
+            }
+
+            if (entryName.endsWith(".class")) {
+                String className = entryName.substring(0, entryName.length()-(".class".length())).replace('/', '.');
+                javaArchive.addClass(className);
+            }
+            else {
+                javaArchive.addAsResource(entryName);
+            }
+        }
+
+        return ret;
+    }
+
+    private static List<File> collectArchiveEntries(File archiveBasePath)
+    {
+        if (archiveBasePath.isDirectory()) {
+            List<File> archiveEntries = new ArrayList<File>();
+            File[] files = archiveBasePath.listFiles();
+
+            for (File file : files) {
+                if (file.isDirectory()) {
+                    archiveEntries.addAll(collectArchiveEntries(file));
+                }
+                else {
+                    archiveEntries.add(file);
+                }
+            }
+
+            return archiveEntries;
+        }
+
+        return Collections.EMPTY_LIST;
+    }
+
+
+    private static boolean excludeIfPackageExists(String jarEntryName, String[] excludeOnPackages) {
+        if (excludeOnPackages != null) {
+            String packageName = jarEntryName.replace('/' ,'.');
+
+            for (String excludeOnPackage : excludeOnPackages) {
+                if (packageName.startsWith(excludeOnPackage)) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    private static boolean includeIfPackageExists(String jarEntryName, String[] includeOnPackages) {
+        if (includeOnPackages == null ) {
+            return true;
+        }
+
+        String packageName = jarEntryName.replace('/' ,'.');
+
+        for (String includeOnPackage : includeOnPackages) {
+            if (packageName.startsWith(includeOnPackage)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * check if the given url path is a Jar
+     * @param urlPath
+     * @return
+     */
+    private static String isJarUrl(String urlPath) {
+        // common prefixes of the url are: jar: (tomcat), zip: (weblogic) and wsjar: (websphere)
+        final int jarColon = urlPath.indexOf(':');
+        if (urlPath.endsWith("!/") && jarColon > 0) {
+            urlPath = urlPath.substring(jarColon + 1, urlPath.length() - 2);
+            return urlPath;
+        }
+
+        return null;
+    }
+
+    private static String ensureCorrectUrlFormat(String url) {
+        //fix for wls
+        if(!url.startsWith("file:/")) {
+            url = "file:/" + url;
+        }
+        return url;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d8d3965e/deltaspike/core/impl/src/test/resources/META-INF/beans.xml
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/resources/META-INF/beans.xml b/deltaspike/core/impl/src/test/resources/META-INF/beans.xml
index adaf2c3..4070730 100644
--- a/deltaspike/core/impl/src/test/resources/META-INF/beans.xml
+++ b/deltaspike/core/impl/src/test/resources/META-INF/beans.xml
@@ -20,9 +20,4 @@
 <beans xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
-    <alternatives>
-        <class>org.apache.myfaces.extensions.cdi.core.test.impl.activation.testbeans.MyMailServiceMockImpl</class>
-        <class>org.apache.myfaces.extensions.cdi.core.test.impl.activation.testbeans.ExpressionActivatedTestBeanMockImpl</class>
-        <class>org.apache.myfaces.extensions.cdi.core.test.impl.activation.testbeans.ExpressionActivatedTestInfoBeanMockImpl</class>
-    </alternatives>
 </beans>