Repository: deltaspike Updated Branches: refs/heads/master aabbc879e -> 6475f49ce
DELTASPIKE-1338 tests for @TestControl#classFilter Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/6475f49c Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/6475f49c Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/6475f49c Branch: refs/heads/master Commit: 6475f49ce82cf568430792b81b58117e41423b17 Parents: 1c18704 Author: gpetracek <gpetra...@apache.org> Authored: Fri Apr 20 15:13:44 2018 +0200 Committer: gpetracek <gpetra...@apache.org> Committed: Fri Apr 20 15:22:40 2018 +0200 ---------------------------------------------------------------------- .../testcontrol/uc019/DefaultTestService.java | 31 ++++++++++ .../testcontrol/uc019/TestBeanClassFilter.java | 65 ++++++++++++++++++++ .../test/testcontrol/uc019/TestLabeled.java | 34 ++++++++++ .../uc019/TestLabeledAlternativeFilter.java | 56 +++++++++++++++++ .../test/testcontrol/uc019/TestService.java | 24 ++++++++ .../testcontrol/uc019/TestServiceLabelX.java | 34 ++++++++++ .../uc019/TestServiceLabelXTest.java | 61 ++++++++++++++++++ .../testcontrol/uc019/TestServiceLabelY.java | 34 ++++++++++ .../uc019/TestServiceLabelYTest.java | 61 ++++++++++++++++++ .../META-INF/apache-deltaspike.properties | 3 + .../impl/src/test/resources/META-INF/beans.xml | 4 ++ 11 files changed, 407 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/DefaultTestService.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/DefaultTestService.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/DefaultTestService.java new file mode 100644 index 0000000..ea153a2 --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/DefaultTestService.java @@ -0,0 +1,31 @@ +/* + * 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.testcontrol.uc019; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class DefaultTestService implements TestService +{ + @Override + public String getValue() + { + return "default-result"; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestBeanClassFilter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestBeanClassFilter.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestBeanClassFilter.java new file mode 100644 index 0000000..4953f3e --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestBeanClassFilter.java @@ -0,0 +1,65 @@ +/* + * 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.testcontrol.uc019; + +import org.apache.deltaspike.core.api.config.ConfigResolver; +import org.apache.deltaspike.core.spi.filter.ClassFilter; +import org.apache.deltaspike.core.util.ClassUtils; +import org.apache.deltaspike.testcontrol.api.TestControl; + +//!!!not!!! needed with cdi 1.1+ and @Priority (which is the target of this use-case) +//only needed because our test-suite is based on cdi v1.0 + +//also useful to test DELTASPIKE-1337 +public class TestBeanClassFilter implements ClassFilter +{ + @Override + public boolean isFiltered(Class<?> targetClass) + { + if (!targetClass.getName().startsWith("org.apache.deltaspike.test.")) + { + return false; + } + + String currentTestOrigin = ConfigResolver.getPropertyValue(TestControl.class.getName()); + + if (currentTestOrigin == null) //no known origin (no @TestControl is used) + { + //filter all classes which are located in packages using tests with class-filters + //(since we test the feature with ambiguous beans which isn't valid without filtering) + return getClass().getPackage().getName().equals(targetClass.getPackage().getName()); + } + else + { + Class<?> currentOrigin = ClassUtils.tryToLoadClassForName(currentTestOrigin); + //origin is in one of the packages for class-filtering tests + if (getClass().getPackage().getName().equals(currentOrigin.getPackage().getName())) + { + TestControl testControl = currentOrigin.getAnnotation(TestControl.class); + return ClassUtils.tryToInstantiateClass(testControl.classFilter()).isFiltered(targetClass); + } + return isInSamePackage(targetClass); + } + } + + private boolean isInSamePackage(Class<?> targetClass) + { + return targetClass.getPackage().getName().equals(getClass().getPackage().getName()); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeled.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeled.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeled.java new file mode 100644 index 0000000..fdabf91 --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeled.java @@ -0,0 +1,34 @@ +/* + * 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.testcontrol.uc019; + +import org.apache.deltaspike.testcontrol.api.TestControl; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Retention(RUNTIME) +@Target(TYPE) +public @interface TestLabeled +{ + Class<? extends TestControl.Label> value(); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeledAlternativeFilter.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeledAlternativeFilter.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeledAlternativeFilter.java new file mode 100644 index 0000000..1f25d7b --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestLabeledAlternativeFilter.java @@ -0,0 +1,56 @@ +/* + * 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.testcontrol.uc019; + +import org.apache.deltaspike.core.spi.filter.ClassFilter; +import org.apache.deltaspike.testcontrol.api.TestControl; + +import javax.enterprise.inject.Alternative; + +public abstract class TestLabeledAlternativeFilter implements ClassFilter +{ + private final Class<? extends TestControl.Label> activeLabel; + + protected TestLabeledAlternativeFilter(Class<? extends TestControl.Label> activeLabel) + { + this.activeLabel = activeLabel; + } + + @Override + public boolean isFiltered(Class<?> targetClass) + { + if (!targetClass.isAnnotationPresent(Alternative.class)) + { + return false; + } + + TestLabeled testLabeled = targetClass.getAnnotation(TestLabeled.class); + + if (testLabeled == null) + { + return false; + } + + if (testLabeled.value().equals(activeLabel)) + { + return false; + } + return true; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestService.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestService.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestService.java new file mode 100644 index 0000000..049d07e --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestService.java @@ -0,0 +1,24 @@ +/* + * 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.testcontrol.uc019; + +public interface TestService +{ + String getValue(); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelX.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelX.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelX.java new file mode 100644 index 0000000..6adf4b2 --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelX.java @@ -0,0 +1,34 @@ +/* + * 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.testcontrol.uc019; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Alternative; + +@Alternative +@TestLabeled(TestServiceLabelXTest.TestLabelX.class) +@ApplicationScoped +public class TestServiceLabelX implements TestService +{ + @Override + public String getValue() + { + return "result-x"; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelXTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelXTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelXTest.java new file mode 100644 index 0000000..b3ec01d --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelXTest.java @@ -0,0 +1,61 @@ +/* + * 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.testcontrol.uc019; + +import org.apache.deltaspike.test.category.SeCategory; +import org.apache.deltaspike.testcontrol.api.TestControl; +import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +//Usually NOT needed! Currently only needed due to our arquillian-setup +@Category(SeCategory.class) + +@RunWith(CdiTestRunner.class) +@TestControl( + activeAlternativeLabel = TestServiceLabelXTest.TestLabelX.class, + classFilter = TestServiceLabelXTest.LabelXFilter.class) +public class TestServiceLabelXTest +{ + @Inject + private TestService testService; + + @Test + public void resultX() + { + Assert.assertEquals("result-x", testService.getValue()); + } + + public static class TestLabelX implements TestControl.Label + { + } + + //replaces the text based config of labeled-alternatives + public static class LabelXFilter extends TestLabeledAlternativeFilter + { + public LabelXFilter() + { + super(TestLabelX.class); + } + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelY.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelY.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelY.java new file mode 100644 index 0000000..1d4f9eb --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelY.java @@ -0,0 +1,34 @@ +/* + * 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.testcontrol.uc019; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Alternative; + +@Alternative +@TestLabeled(TestServiceLabelYTest.TestLabelY.class) +@ApplicationScoped +public class TestServiceLabelY implements TestService +{ + @Override + public String getValue() + { + return "result-y"; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelYTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelYTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelYTest.java new file mode 100644 index 0000000..db45c8a --- /dev/null +++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc019/TestServiceLabelYTest.java @@ -0,0 +1,61 @@ +/* + * 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.testcontrol.uc019; + +import org.apache.deltaspike.test.category.SeCategory; +import org.apache.deltaspike.testcontrol.api.TestControl; +import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +//Usually NOT needed! Currently only needed due to our arquillian-setup +@Category(SeCategory.class) + +@RunWith(CdiTestRunner.class) +@TestControl( + activeAlternativeLabel = TestServiceLabelYTest.TestLabelY.class, + classFilter = TestServiceLabelYTest.LabelYFilter.class) +public class TestServiceLabelYTest +{ + @Inject + private TestService testService; + + @Test + public void resultY() + { + Assert.assertEquals("result-y", testService.getValue()); + } + + public static class TestLabelY implements TestControl.Label + { + } + + //replaces the text based config of labeled-alternatives + public static class LabelYFilter extends TestLabeledAlternativeFilter + { + public LabelYFilter() + { + super(TestServiceLabelYTest.TestLabelY.class); + } + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/resources/META-INF/apache-deltaspike.properties ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/resources/META-INF/apache-deltaspike.properties b/deltaspike/modules/test-control/impl/src/test/resources/META-INF/apache-deltaspike.properties index 357e2b6..d86331a 100644 --- a/deltaspike/modules/test-control/impl/src/test/resources/META-INF/apache-deltaspike.properties +++ b/deltaspike/modules/test-control/impl/src/test/resources/META-INF/apache-deltaspike.properties @@ -31,3 +31,6 @@ labeledAlternatives[lbl].org.apache.deltaspike.test.testcontrol.uc015.TestServic labeledAlternatives[label].org.apache.deltaspike.test.testcontrol.uc017.DefaultTestServiceProducer=org.apache.deltaspike.test.testcontrol.uc017.LabeledTestServiceProducer labeledAlternatives[label].org.apache.deltaspike.test.testcontrol.uc018.InterceptedTestServiceProducer=org.apache.deltaspike.test.testcontrol.uc018.LabeledTestServiceProducer + +#only needed because our test-suite is based on cdi v1.0. with v1.1+ and @Priority (which is the target of this use-case) the following part isn't needed: +org.apache.deltaspike.core.spi.filter.ClassFilter=org.apache.deltaspike.test.testcontrol.uc019.TestBeanClassFilter \ No newline at end of file http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6475f49c/deltaspike/modules/test-control/impl/src/test/resources/META-INF/beans.xml ---------------------------------------------------------------------- diff --git a/deltaspike/modules/test-control/impl/src/test/resources/META-INF/beans.xml b/deltaspike/modules/test-control/impl/src/test/resources/META-INF/beans.xml index d1b25a7..d22d695 100644 --- a/deltaspike/modules/test-control/impl/src/test/resources/META-INF/beans.xml +++ b/deltaspike/modules/test-control/impl/src/test/resources/META-INF/beans.xml @@ -28,5 +28,9 @@ <alternatives> <class>org.apache.deltaspike.test.testcontrol.CustomMockManager</class> + + <!-- with cdi 1.1+ the following can be replaced with @Priority(...)--> + <class>org.apache.deltaspike.test.testcontrol.uc019.TestServiceLabelX</class> + <class>org.apache.deltaspike.test.testcontrol.uc019.TestServiceLabelY</class> </alternatives> </beans>