Updated Branches: refs/heads/master a95c1e6e3 -> 7c8039a82
DELTASPIKE-399 filter test-execution in case of cdi 1.0 (currently only in case of owb) Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/7c8039a8 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/7c8039a8 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/7c8039a8 Branch: refs/heads/master Commit: 7c8039a82b00d4f39e2aaaad3ba41d7975cc025b Parents: a95c1e6 Author: gpetracek <[email protected]> Authored: Sat Dec 28 14:26:02 2013 +0100 Committer: gpetracek <[email protected]> Committed: Sat Dec 28 14:34:02 2013 +0100 ---------------------------------------------------------------------- .../core/impl/resourceloader/Cdi10Bean.java | 50 +++++++ .../core/impl/resourceloader/Cdi11Bean.java | 67 +++++++++ .../resourceloader/ClasspathResourceTest.java | 50 ++++--- .../impl/resourceloader/FileResourceTest.java | 24 +++- .../impl/resourceloader/TestResourceHolder.java | 35 +++++ .../test/utils/CdiContainerUnderTest.java | 140 +++++++++++++++++++ .../test/utils/CdiImplementation.java | 48 +++++++ 7 files changed, 393 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c8039a8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java new file mode 100644 index 0000000..df5be1b --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi10Bean.java @@ -0,0 +1,50 @@ +/* + * 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.test.core.impl.resourceloader; + +import javax.enterprise.inject.Instance; +import java.io.InputStream; +import java.util.Properties; + +public class Cdi10Bean implements TestResourceHolder +{ + @Override + public InputStream getInputStream() + { + throw new UnsupportedOperationException(); + } + + @Override + public Properties getProperties() + { + throw new UnsupportedOperationException(); + } + + @Override + public Instance<InputStream> getInputStreamInstance() + { + throw new UnsupportedOperationException(); + } + + @Override + public Instance<Properties> getPropertiesInstance() + { + throw new UnsupportedOperationException(); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c8039a8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java new file mode 100644 index 0000000..908cc38 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/Cdi11Bean.java @@ -0,0 +1,67 @@ +/* + * 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.test.core.impl.resourceloader; + +import org.apache.deltaspike.core.api.resourceloader.ClasspathStorage; +import org.apache.deltaspike.core.api.resourceloader.ExternalResource; + +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Instance; +import javax.inject.Inject; +import java.io.InputStream; +import java.util.Properties; + +public class Cdi11Bean implements TestResourceHolder +{ + @Inject + @ExternalResource(storage = ClasspathStorage.class,location="testconfig.properties") + private InputStream inputStream; + + @Inject + @ExternalResource(storage = ClasspathStorage.class,location="testconfig.properties") + private Properties properties; + + @Inject + @Any + private Instance<InputStream> inputStreamInstance; + + @Inject + @Any + private Instance<Properties> propertiesInstance; + + public InputStream getInputStream() + { + return inputStream; + } + + public Properties getProperties() + { + return properties; + } + + public Instance<InputStream> getInputStreamInstance() + { + return inputStreamInstance; + } + + public Instance<Properties> getPropertiesInstance() + { + return propertiesInstance; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c8039a8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java index 8f2e469..a95f6c6 100644 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/ClasspathResourceTest.java @@ -21,8 +21,9 @@ package org.apache.deltaspike.test.core.impl.resourceloader; import org.apache.deltaspike.core.api.literal.ExternalResourceLiteral; import org.apache.deltaspike.core.api.resourceloader.ClasspathStorage; -import org.apache.deltaspike.core.api.resourceloader.ExternalResource; import org.apache.deltaspike.test.util.ArchiveUtils; +import org.apache.deltaspike.test.utils.CdiContainerUnderTest; +import org.apache.deltaspike.test.utils.CdiImplementation; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.Archive; @@ -31,22 +32,29 @@ import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Assert; +import org.junit.Assume; import org.junit.Test; import org.junit.runner.RunWith; -import javax.enterprise.inject.Any; -import javax.enterprise.inject.Instance; import javax.inject.Inject; import java.io.IOException; -import java.io.InputStream; import java.util.Properties; @RunWith(Arquillian.class) -public class ClasspathResourceTest { +public class ClasspathResourceTest +{ @Deployment public static Archive<?> createResourceLoaderArchive() { + Class versionDependentImplementation = Cdi11Bean.class; + if (isOwbForCdi10()) + { + versionDependentImplementation = Cdi10Bean.class; + } + Archive<?> arch = ShrinkWrap.create(WebArchive.class, ClasspathResourceTest.class.getSimpleName() + ".war") + .addClass(TestResourceHolder.class) + .addClass(versionDependentImplementation) .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") .add(new StringAsset("some.propertykey = somevalue"), "WEB-INF/classes/testconfig.properties") .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive()); @@ -54,35 +62,39 @@ public class ClasspathResourceTest { } @Inject - @ExternalResource(storage = ClasspathStorage.class,location="testconfig.properties") - private InputStream inputStream; - - @Inject - @ExternalResource(storage = ClasspathStorage.class,location="testconfig.properties") - private Properties props; - - @Inject - @Any - private Instance<InputStream> inputStreams; + private TestResourceHolder testResourceHolder; @Test public void testInputStream() throws IOException { - Assert.assertNotNull(inputStream); + Assume.assumeTrue(!isOwbForCdi10()); + + Assert.assertNotNull(testResourceHolder.getInputStream()); Properties p = new Properties(); - p.load(inputStream); + p.load(testResourceHolder.getInputStream()); Assert.assertEquals("somevalue", p.getProperty("some.propertykey", "wrong answer")); } @Test public void testProperties() { - Assert.assertEquals("somevalue", props.getProperty("some.propertykey", "wrong answer")); + Assume.assumeTrue(!isOwbForCdi10()); + + Assert.assertEquals("somevalue", + testResourceHolder.getProperties().getProperty("some.propertykey", "wrong answer")); } @Test(expected = RuntimeException.class) public void testAmbiguousFileLookup() { - inputStreams.select(new ExternalResourceLiteral(ClasspathStorage.class, "META-INF/beans.xml")).get(); + Assume.assumeTrue(!isOwbForCdi10()); + + testResourceHolder.getInputStreamInstance() + .select(new ExternalResourceLiteral(ClasspathStorage.class, "META-INF/beans.xml")).get(); + } + + private static boolean isOwbForCdi10() + { + return CdiContainerUnderTest.isCdiVersion(CdiImplementation.OWB11) || CdiContainerUnderTest.isCdiVersion(CdiImplementation.OWB12); } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c8039a8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java index 1a19b9a..bb3e229 100644 --- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/FileResourceTest.java @@ -21,7 +21,10 @@ package org.apache.deltaspike.test.core.impl.resourceloader; import org.apache.deltaspike.core.api.literal.ExternalResourceLiteral; import org.apache.deltaspike.core.api.resourceloader.FileSystemStorage; +import org.apache.deltaspike.core.util.ExceptionUtils; import org.apache.deltaspike.test.util.ArchiveUtils; +import org.apache.deltaspike.test.utils.CdiContainerUnderTest; +import org.apache.deltaspike.test.utils.CdiImplementation; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.Archive; @@ -29,8 +32,8 @@ import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Assert; +import org.junit.Assume; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -64,14 +67,22 @@ public class FileResourceTest } catch (IOException e) { - + throw ExceptionUtils.throwAsRuntimeException(e); } } @Deployment public static Archive<?> createResourceLoaderArchive() { + Class versionDependentImplementation = Cdi11Bean.class; + if (isOwbForCdi10()) + { + versionDependentImplementation = Cdi10Bean.class; + } + Archive<?> arch = ShrinkWrap.create(WebArchive.class, FileResourceTest.class.getSimpleName() + ".war") + .addClass(TestResourceHolder.class) + .addClass(versionDependentImplementation) .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive()); return arch; @@ -88,6 +99,8 @@ public class FileResourceTest @Test public void testInputStream() throws IOException { + Assume.assumeTrue(!isOwbForCdi10()); + InputStream inputStream = inputStreamInst .select(new ExternalResourceLiteral(FileSystemStorage.class, tempFileName)).get(); Assert.assertNotNull(inputStream); @@ -99,8 +112,15 @@ public class FileResourceTest @Test public void testProperties() { + Assume.assumeTrue(!isOwbForCdi10()); + Properties props = this.propsInst.select(new ExternalResourceLiteral(FileSystemStorage.class,tempFileName)) .get(); Assert.assertEquals("somevalue", props.getProperty("some.propertykey", "wrong answer")); } + + private static boolean isOwbForCdi10() + { + return CdiContainerUnderTest.isCdiVersion(CdiImplementation.OWB11) || CdiContainerUnderTest.isCdiVersion(CdiImplementation.OWB12); + } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c8039a8/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java new file mode 100644 index 0000000..cf059d2 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/resourceloader/TestResourceHolder.java @@ -0,0 +1,35 @@ +/* + * 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.test.core.impl.resourceloader; + +import javax.enterprise.inject.Instance; +import java.io.InputStream; +import java.util.Properties; + +//just needed to build different archives - otherwise injection would fail with owb implementing cdi 1.0 +public interface TestResourceHolder +{ + InputStream getInputStream(); + + Properties getProperties(); + + Instance<InputStream> getInputStreamInstance(); + + Instance<Properties> getPropertiesInstance(); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c8039a8/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiContainerUnderTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiContainerUnderTest.java b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiContainerUnderTest.java index ddbf711..a9c7cd2 100644 --- a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiContainerUnderTest.java +++ b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiContainerUnderTest.java @@ -18,6 +18,12 @@ */ package org.apache.deltaspike.test.utils; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + /** * A small helper class which checks if the container * which is currently being tested matches the given version RegExp @@ -53,4 +59,138 @@ public class CdiContainerUnderTest return false; } + + //TODO discuss merge with #is (there 'cdicontainer.version' isn't available in all cases) + public static boolean isCdiVersion(CdiImplementation cdiImplementation) + { + Class implementationClass = tryToLoadClassForName(cdiImplementation.getImplementationClassName()); + + if (implementationClass == null) + { + return false; + } + + String containerVersion = getJarVersion(implementationClass); + return containerVersion != null && containerVersion.matches(cdiImplementation.getVersionRegex()); + } + + private static Class tryToLoadClassForName(String name) + { + try + { + return loadClassForName(name); + } + catch (ClassNotFoundException e) + { + //do nothing - it's just a try + return null; + } + } + + private static Class loadClassForName(String name) throws ClassNotFoundException + { + try + { + // Try WebApp ClassLoader first + return Class.forName(name, false, // do not initialize for faster startup + getClassLoader(null)); + } + catch (ClassNotFoundException ignore) + { + // fallback: Try ClassLoader for ClassUtils (i.e. the myfaces.jar lib) + return Class.forName(name, false, // do not initialize for faster startup + CdiContainerUnderTest.class.getClassLoader()); + } + } + + private static ClassLoader getClassLoader(Object o) + { + if (System.getSecurityManager() != null) + { + return AccessController.doPrivileged(new GetClassLoaderAction(o)); + } + else + { + return getClassLoaderInternal(o); + } + } + + private static ClassLoader getClassLoaderInternal(Object o) + { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + + if (loader == null && o != null) + { + loader = o.getClass().getClassLoader(); + } + + if (loader == null) + { + loader = CdiContainerUnderTest.class.getClassLoader(); + } + + return loader; + } + + private static String getJarVersion(Class targetClass) + { + String manifestFileLocation = getManifestFileLocationOfClass(targetClass); + + try + { + return new Manifest(new URL(manifestFileLocation).openStream()) + .getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); + } + catch (Exception e) + { + return null; + } + } + + private static String getManifestFileLocationOfClass(Class targetClass) + { + String manifestFileLocation; + + try + { + manifestFileLocation = getManifestLocation(targetClass); + } + catch (Exception e) + { + //in this case we have a proxy + manifestFileLocation = getManifestLocation(targetClass.getSuperclass()); + } + return manifestFileLocation; + } + + private static String getManifestLocation(Class targetClass) + { + String classFilePath = targetClass.getCanonicalName().replace('.', '/') + ".class"; + String manifestFilePath = "/META-INF/MANIFEST.MF"; + + String classLocation = targetClass.getResource(targetClass.getSimpleName() + ".class").toString(); + return classLocation.substring(0, classLocation.indexOf(classFilePath) - 1) + manifestFilePath; + } + + private static class GetClassLoaderAction implements PrivilegedAction<ClassLoader> + { + private Object object; + GetClassLoaderAction(Object object) + { + this.object = object; + } + + @Override + public ClassLoader run() + { + try + { + return getClassLoaderInternal(object); + } + catch (Exception e) + { + return null; + } + } + } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/7c8039a8/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiImplementation.java ---------------------------------------------------------------------- diff --git a/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiImplementation.java b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiImplementation.java new file mode 100644 index 0000000..b11b747 --- /dev/null +++ b/deltaspike/test-utils/src/main/java/org/apache/deltaspike/test/utils/CdiImplementation.java @@ -0,0 +1,48 @@ +/* + * 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.test.utils; + +public enum CdiImplementation +{ + OWB11 ("org.apache.webbeans.container.BeanManagerImpl", "1\\.1\\..*"), + OWB12 ("org.apache.webbeans.container.BeanManagerImpl", "1\\.2\\..*"), + + WELD11("org.jboss.weld.manager.BeanManagerImpl", "1\\.1\\..*"), + WELD12("org.jboss.weld.manager.BeanManagerImpl", "1\\.2\\..*"), + WELD20("org.jboss.weld.manager.BeanManagerImpl", "2\\.0\\..*"); + + private final String implementationClassName; + private final String versionRegex; + + CdiImplementation(String implementationClassName, String versionRegex) + { + this.implementationClassName = implementationClassName; + this.versionRegex = versionRegex; + } + + public String getImplementationClassName() + { + return implementationClassName; + } + + public String getVersionRegex() + { + return versionRegex; + } +}
