You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2014/05/15 16:11:37 UTC
[2/2] git commit: DELTASPIKE-593 support for mocked cdi-beans
DELTASPIKE-593 support for mocked cdi-beans
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/b47375ca
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/b47375ca
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/b47375ca
Branch: refs/heads/master
Commit: b47375cacf51805356fa6fad4c0a50da53b03c49
Parents: 59f8c0c
Author: gpetracek <gp...@apache.org>
Authored: Thu May 15 15:51:05 2014 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Thu May 15 16:11:03 2014 +0200
----------------------------------------------------------------------
.../api/mock/ApplicationMockManager.java | 29 ++
.../api/mock/DynamicMockManager.java | 24 ++
.../testcontrol/api/mock/TypedMock.java | 36 ++
.../testcontrol/spi/mock/MockFilter.java | 28 ++
deltaspike/modules/test-control/impl/pom.xml | 5 +
.../impl/mock/AbstractMockManager.java | 96 +++++
.../testcontrol/impl/mock/BeanCacheKey.java | 423 +++++++++++++++++++
.../impl/mock/DefaultMockFilter.java | 53 +++
.../mock/MockAwareInjectionTargetWrapper.java | 103 +++++
.../impl/mock/MockAwareProducerWrapper.java | 86 ++++
.../testcontrol/impl/mock/MockExtension.java | 158 +++++++
.../impl/mock/SimpleApplicationMockManager.java | 30 ++
.../impl/mock/SimpleMockManager.java | 47 +++
.../impl/src/main/resources/META-INF/beans.xml | 23 +
.../javax.enterprise.inject.spi.Extension | 18 +
...e.deltaspike.testcontrol.spi.mock.MockFilter | 18 +
.../testcontrol/InternalTestMockFilter.java | 32 ++
.../mock/shared/ApplicationScopedBean.java | 37 ++
.../testcontrol/mock/shared/MyQualifier.java | 35 ++
.../mock/shared/RequestScopedBean.java | 37 ++
.../mock/shared/SessionScopedBean.java | 39 ++
.../mock/uc001/MockedRequestScopedBeanTest.java | 77 ++++
.../mock/uc002/MockedSessionScopedBeanTest.java | 77 ++++
.../mock/uc003/MockedSessionScopedBean.java | 32 ++
...ockedSessionScopedBeanAcrossMethodsTest.java | 77 ++++
.../mock/uc004/MockedApplicationScopedBean.java | 33 ++
.../uc004/MockedApplicationScopedBeanTest.java | 58 +++
.../mock/uc005/MockedProducedBeanTest.java | 74 ++++
.../testcontrol/mock/uc005/ProducedBean.java | 37 ++
.../mock/uc005/ProducedBeanProducer.java | 34 ++
.../MockedRequestScopedQualifiedBeanTest.java | 62 +++
.../testcontrol/mock/uc006/QualifiedBean.java | 40 ++
.../uc007/MockedProducedQualifiedBeanTest.java | 77 ++++
.../testcontrol/mock/uc007/ProducedBean.java | 37 ++
.../mock/uc007/ProducedBeanProducer.java | 37 ++
.../mock/uc008/MockedTypedBeanTest.java | 95 +++++
.../test/testcontrol/mock/uc008/T1.java | 24 ++
.../test/testcontrol/mock/uc008/T2.java | 24 ++
.../test/testcontrol/mock/uc008/T3.java | 24 ++
.../testcontrol/mock/uc008/TypedBean1and2.java | 39 ++
.../test/testcontrol/mock/uc008/TypedBean3.java | 39 ++
.../mock/uc009/MockedTypedProducedBeanTest.java | 96 +++++
.../test/testcontrol/mock/uc009/T1.java | 24 ++
.../test/testcontrol/mock/uc009/T2.java | 24 ++
.../test/testcontrol/mock/uc009/T3.java | 24 ++
.../testcontrol/mock/uc009/TypedBean1and2.java | 37 ++
.../test/testcontrol/mock/uc009/TypedBean3.java | 37 ++
.../mock/uc009/TypedBeanProducer.java | 43 ++
.../mock/uc010/MockedRequestScopedBeanTest.java | 69 +++
.../MockedRequestScopedBeanWithInjection.java | 38 ++
...ockedRequestScopedBeanWithInjectionTest.java | 63 +++
.../MockedRequestScopedBeanWithInjection.java | 38 ++
...ockedRequestScopedBeanWithInjectionTest.java | 88 ++++
...e.deltaspike.testcontrol.spi.mock.MockFilter | 18 +
deltaspike/parent/pom.xml | 7 +
55 files changed, 2960 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/ApplicationMockManager.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/ApplicationMockManager.java b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/ApplicationMockManager.java
new file mode 100644
index 0000000..b59ae2c
--- /dev/null
+++ b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/ApplicationMockManager.java
@@ -0,0 +1,29 @@
+/*
+ * 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.testcontrol.api.mock;
+
+import java.lang.annotation.Annotation;
+
+//needed e.g. due to caches for application-scoped,... beans
+public interface ApplicationMockManager
+{
+ void addMock(Object mockInstance, Annotation... qualifiers);
+
+ <T> T getMock(Class<T> beanClass, Annotation... qualifiers);
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/DynamicMockManager.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/DynamicMockManager.java b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/DynamicMockManager.java
new file mode 100644
index 0000000..e9ec339
--- /dev/null
+++ b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/DynamicMockManager.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.testcontrol.api.mock;
+
+public interface DynamicMockManager extends ApplicationMockManager
+{
+ void reset();
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/TypedMock.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/TypedMock.java b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/TypedMock.java
new file mode 100644
index 0000000..3fd444d
--- /dev/null
+++ b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/mock/TypedMock.java
@@ -0,0 +1,36 @@
+/*
+ * 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.testcontrol.api.mock;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+//equivalent to @Typed for mocks to avoid conflicts with cdi
+
+@Target({ FIELD, METHOD, TYPE })
+@Retention(RUNTIME)
+public @interface TypedMock
+{
+ Class<?>[] value() default { };
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
new file mode 100644
index 0000000..23cfbc3
--- /dev/null
+++ b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
@@ -0,0 +1,28 @@
+/*
+ * 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.testcontrol.spi.mock;
+
+import org.apache.deltaspike.core.spi.activation.Deactivatable;
+
+import javax.enterprise.inject.spi.Annotated;
+
+public interface MockFilter extends Deactivatable
+{
+ boolean isMockedImplementationSupported(Annotated annotated);
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/pom.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/pom.xml b/deltaspike/modules/test-control/impl/pom.xml
index cad1a3f..8edb707 100644
--- a/deltaspike/modules/test-control/impl/pom.xml
+++ b/deltaspike/modules/test-control/impl/pom.xml
@@ -101,6 +101,11 @@
<artifactId>commons-logging</artifactId>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ </dependency>
</dependencies>
<build>
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/AbstractMockManager.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/AbstractMockManager.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/AbstractMockManager.java
new file mode 100644
index 0000000..f672ea2
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/AbstractMockManager.java
@@ -0,0 +1,96 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+import org.apache.deltaspike.testcontrol.api.mock.TypedMock;
+
+import javax.enterprise.inject.Typed;
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+
+public abstract class AbstractMockManager implements DynamicMockManager
+{
+ private Map<BeanCacheKey, Object> registeredMocks = new HashMap<BeanCacheKey, Object>();
+
+ @Override
+ public void addMock(Object mockInstance, Annotation... qualifiers)
+ {
+ Class<?> mockClass = mockInstance.getClass();
+ Class<?> beanClass = mockClass.getSuperclass();
+
+ if (beanClass == null)
+ {
+ beanClass = mockClass;
+ }
+ if (Object.class.equals(beanClass))
+ {
+ throw new IllegalArgumentException(mockInstance.getClass().getName() +
+ " isn't a supported approach for mocking -> please extend from the original class.");
+ }
+
+ TypedMock typedMock = mockClass.getAnnotation(TypedMock.class);
+
+ if (typedMock == null)
+ {
+ typedMock = beanClass.getAnnotation(TypedMock.class);
+ }
+
+ Class[] specifiedTypes = null;
+
+ if (typedMock != null)
+ {
+ specifiedTypes = typedMock.value();
+ }
+ else
+ {
+ Typed typed = beanClass.getAnnotation(Typed.class);
+
+ if (typed != null && typed.value().length > 0)
+ {
+ specifiedTypes = typed.value();
+ }
+ }
+
+ if (specifiedTypes != null)
+ {
+ for (Class typedClass : specifiedTypes)
+ {
+ this.registeredMocks.put(new BeanCacheKey(typedClass, qualifiers), mockInstance);
+ }
+ }
+ else
+ {
+ this.registeredMocks.put(new BeanCacheKey(beanClass, qualifiers), mockInstance);
+ }
+ }
+
+ @Override
+ public <T> T getMock(Class<T> beanClass, Annotation... qualifiers)
+ {
+ return (T)this.registeredMocks.get(new BeanCacheKey(beanClass, qualifiers));
+ }
+
+ @Override
+ public void reset()
+ {
+ this.registeredMocks.clear();
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/BeanCacheKey.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/BeanCacheKey.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/BeanCacheKey.java
new file mode 100644
index 0000000..1b29cc1
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/BeanCacheKey.java
@@ -0,0 +1,423 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import javax.enterprise.util.Nonbinding;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.util.Arrays;
+import java.util.Comparator;
+
+//class from OWB
+public class BeanCacheKey
+{
+ private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
+ private static final Comparator<Annotation> ANNOTATION_COMPARATOR = new AnnotationComparator();
+
+ private final Type type;
+ private final Annotation qualifier;
+ private final Annotation qualifiers[];
+ private final int hashCode;
+
+ public BeanCacheKey(Type type, Annotation... qualifiers)
+ {
+ this.type = type;
+ final int length = qualifiers != null ? qualifiers.length : 0;
+ if (length == 0)
+ {
+ qualifier = null;
+ this.qualifiers = null;
+ }
+ else if (length == 1)
+ {
+ qualifier = qualifiers[0];
+ this.qualifiers = null;
+ }
+ else
+ {
+ qualifier = null;
+ // to save array creations, we only create an array, if we have more than one annotation
+ this.qualifiers = new Annotation[length];
+ System.arraycopy(qualifiers, 0, this.qualifiers, 0, length);
+ Arrays.sort(this.qualifiers, ANNOTATION_COMPARATOR);
+ }
+
+ // this class is directly used in ConcurrentHashMap.get() so simply init the hasCode here
+ hashCode = computeHashCode();
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass())
+ {
+ return false;
+ }
+
+ BeanCacheKey cacheKey = (BeanCacheKey) o;
+
+ if (!type.equals(cacheKey.type))
+ {
+ return false;
+ }
+ if (qualifier != null ? !qualifierEquals(qualifier, cacheKey.qualifier) : cacheKey.qualifier != null)
+ {
+ return false;
+ }
+ if (!qualifierArrayEquals(qualifiers, cacheKey.qualifiers))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ private boolean qualifierArrayEquals(Annotation[] qualifiers1, Annotation[] qualifiers2)
+ {
+ if (qualifiers1 == qualifiers2)
+ {
+ return true;
+ }
+ else if (qualifiers1 == null || qualifiers2 == null)
+ {
+ return false;
+ }
+ if (qualifiers1.length != qualifiers2.length)
+ {
+ return false;
+ }
+ for (int i = 0; i < qualifiers1.length; i++)
+ {
+ Annotation a1 = qualifiers1[i];
+ Annotation a2 = qualifiers2[i];
+ if (a1 == null ? a2 != null : !qualifierEquals(a1, a2))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return hashCode;
+ }
+
+ /**
+ * We need this method as some weird JVMs return 0 as hashCode for classes.
+ * In that case we return the hashCode of the String.
+ */
+ private int getTypeHashCode(Type type)
+ {
+ int typeHash = type.hashCode();
+ if (typeHash == 0 && type instanceof Class)
+ {
+ return ((Class)type).getName().hashCode();
+ // the type.toString() is always the same: "java.lang.Class@<hexid>"
+ // was: return type.toString().hashCode();
+ }
+
+ return typeHash;
+ }
+
+ /**
+ * Compute the HashCode. This should be called only in the constructor.
+ */
+ private int computeHashCode()
+ {
+ int computedHashCode = 31 * getTypeHashCode(type);
+ if (qualifier != null)
+ {
+ computedHashCode = 31 * computedHashCode + getQualifierHashCode(qualifier);
+ }
+ if (qualifiers != null)
+ {
+ for (int i = 0; i < qualifiers.length; i++)
+ {
+ computedHashCode = 31 * computedHashCode + getQualifierHashCode(qualifiers[i]);
+ }
+ }
+ return computedHashCode;
+ }
+
+ /**
+ * Calculate the hashCode() of a qualifier, which ignores {@link javax.enterprise.util.Nonbinding} members.
+ */
+ private int getQualifierHashCode(Annotation a)
+ {
+ Class annotationClass = getAnnotationClass(a.getClass());
+
+ if (annotationClass == null)
+ {
+ return getTypeHashCode(a.getClass());
+ }
+
+ // the hashCode of an Annotation is calculated solely via the hashCodes
+ // of it's members. If there are no members, it is 0.
+ // thus we first need to get the annotation-class hashCode
+ int hashCode = getTypeHashCode(annotationClass);
+
+ // and now add the hashCode of all it's Nonbinding members
+ // the following algorithm is defined by the Annotation class definition
+ // see the JavaDoc for Annotation!
+ // we only change it so far that we skip evaluating @Nonbinding members
+ final Method[] members = annotationClass.getDeclaredMethods();
+
+ for (Method member : members)
+ {
+ if (member.isAnnotationPresent(Nonbinding.class))
+ {
+ // ignore the non binding
+ continue;
+ }
+
+ // Member value
+ final Object object = callMethod(a, member);
+ final int value;
+ if (object.getClass().isArray())
+ {
+ Class<?> type = object.getClass().getComponentType();
+ if (type.isPrimitive())
+ {
+ if (Long.TYPE == type)
+ {
+ value = Arrays.hashCode((long[]) object);
+ }
+ else if (Integer.TYPE == type)
+ {
+ value = Arrays.hashCode((int[])object);
+ }
+ else if (Short.TYPE == type)
+ {
+ value = Arrays.hashCode((short[])object);
+ }
+ else if (Double.TYPE == type)
+ {
+ value = Arrays.hashCode((double[])object);
+ }
+ else if (Float.TYPE == type)
+ {
+ value = Arrays.hashCode((float[])object);
+ }
+ else if (Boolean.TYPE == type)
+ {
+ value = Arrays.hashCode((boolean[])object);
+ }
+ else if (Byte.TYPE == type)
+ {
+ value = Arrays.hashCode((byte[])object);
+ }
+ else if (Character.TYPE == type)
+ {
+ value = Arrays.hashCode((char[])object);
+ }
+ else
+ {
+ value = 0;
+ }
+ }
+ else
+ {
+ value = Arrays.hashCode((Object[])object);
+ }
+ }
+ else
+ {
+ value = object.hashCode();
+ }
+
+ hashCode = 29 * hashCode + value;
+ hashCode = 29 * hashCode + member.getName().hashCode();
+ }
+
+ return hashCode;
+ }
+
+ /**
+ * Implements the equals() method for qualifiers, which ignores {@link javax.enterprise.util.Nonbinding} members.
+ */
+ private boolean qualifierEquals(Annotation qualifier1, Annotation qualifier2)
+ {
+ return ANNOTATION_COMPARATOR.compare(qualifier1, qualifier2) == 0;
+ }
+
+ private static Class getAnnotationClass(Class a)
+ {
+ for (Class i : a.getInterfaces())
+ {
+ if (i.isAnnotation())
+ {
+ return i;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Helper method for calculating the hashCode of an annotation.
+ */
+ private static Object callMethod(Object instance, Method method)
+ {
+ try
+ {
+ if (!method.isAccessible())
+ {
+ method.setAccessible(true);
+ }
+
+ return method.invoke(instance, EMPTY_OBJECT_ARRAY);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Exception in method call : " + method.getName(), e);
+ }
+
+ }
+
+ /**
+ * for debugging ...
+ */
+ @Override
+ public String toString()
+ {
+ return "BeanCacheKey{" + "type=" + type + ", qualifiers="
+ + (qualifiers == null ? qualifier : Arrays.asList(qualifiers)) + ", hashCode=" + hashCode + '}';
+ }
+
+ /**
+ * to keep the annotations ordered.
+ */
+ private static class AnnotationComparator implements Comparator<Annotation>
+ {
+
+ // Notice: Sorting is a bit costly, but the use of this code is very rar.
+ @Override
+ public int compare(Annotation annotation1, Annotation annotation2)
+ {
+ final Class<? extends Annotation> type1 = annotation1.annotationType();
+ final Class<? extends Annotation> type2 = annotation2.annotationType();
+ final int temp = type1.getName().compareTo(type2.getName());
+ if (temp != 0)
+ {
+ return temp;
+ }
+ final Method[] member1 = type1.getDeclaredMethods();
+ final Method[] member2 = type2.getDeclaredMethods();
+
+ // TBD: the order of the list of members seems to be deterministic
+
+ int i = 0;
+ int j = 0;
+ final int length1 = member1.length;
+ final int length2 = member2.length;
+
+ // find next nonbinding
+ for (;; i++, j++)
+ {
+ while (i < length1 && member1[i].isAnnotationPresent(Nonbinding.class))
+ {
+ i++;
+ }
+ while (j < length2 && member2[j].isAnnotationPresent(Nonbinding.class))
+ {
+ j++;
+ }
+ if (i >= length1 && j >= length2)
+ { // both ended
+ return 0;
+ }
+ else if (i >= length1)
+ { // #1 ended
+ return 1;
+ }
+ else if (j >= length2)
+ { // #2 ended
+ return -1;
+ }
+ else
+ { // not ended
+ int c = member1[i].getName().compareTo(member2[j].getName());
+ if (c != 0)
+ {
+ return c;
+ }
+ final Object value1 = callMethod(annotation1, member1[i]);
+ final Object value2 = callMethod(annotation2, member2[j]);
+ assert value1.getClass().equals(value2.getClass());
+
+ if (value1 instanceof Comparable)
+ {
+ c = ((Comparable)value1).compareTo(value2);
+ if (c != 0)
+ {
+ return c;
+ }
+ }
+ else if (value1.getClass().isArray())
+ {
+ c = value1.getClass().getComponentType().getName()
+ .compareTo(value2.getClass().getComponentType().getName());
+ if (c != 0)
+ {
+ return c;
+ }
+
+ final int length = Array.getLength(value1);
+ c = length - Array.getLength(value2);
+ if (c != 0)
+ {
+ return c;
+ }
+ for (int k = 0; k < length; k++)
+ {
+ c = ((Comparable)Array.get(value1, k)).compareTo(Array.get(value2, k));
+ if (c != 0)
+ {
+ return c;
+ }
+ }
+
+ }
+ else if (value1 instanceof Class)
+ {
+
+ c = ((Class)value1).getName().compareTo(((Class) value2).getName());
+ if (c != 0)
+ {
+ return c;
+ }
+ }
+ else
+ {
+ // valid types for members are only Comparable, Arrays, or Class
+ assert false;
+ }
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
new file mode 100644
index 0000000..68c9f1e
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
@@ -0,0 +1,53 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import org.apache.deltaspike.testcontrol.spi.mock.MockFilter;
+
+import javax.enterprise.inject.spi.Annotated;
+import javax.enterprise.inject.spi.AnnotatedMember;
+import javax.enterprise.inject.spi.AnnotatedType;
+
+public class DefaultMockFilter implements MockFilter
+{
+ private static final String OWB_BASE_PACKAGE = "org.apache.webbeans.";
+ private static final String WELD_BASE_PACKAGE = "org.jboss.weld.";
+
+ @Override
+ public boolean isMockedImplementationSupported(Annotated annotated)
+ {
+ String packageName = null;
+
+ if (annotated instanceof AnnotatedType)
+ {
+ packageName = ((AnnotatedType)annotated).getJavaClass().getPackage().getName();
+ }
+ else if (annotated instanceof AnnotatedMember)
+ {
+ packageName = ((AnnotatedMember)annotated).getJavaMember().getDeclaringClass().getPackage().getName();
+ }
+
+ return packageName != null && !isInternalPackage(packageName);
+ }
+
+ protected boolean isInternalPackage(String packageName)
+ {
+ return packageName.startsWith(OWB_BASE_PACKAGE) || packageName.startsWith(WELD_BASE_PACKAGE);
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareInjectionTargetWrapper.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareInjectionTargetWrapper.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareInjectionTargetWrapper.java
new file mode 100644
index 0000000..6bb77a0
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareInjectionTargetWrapper.java
@@ -0,0 +1,103 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.Set;
+
+public class MockAwareInjectionTargetWrapper<T> implements InjectionTarget<T>
+{
+ private BeanManager beanManager;
+ private final InjectionTarget<T> wrapped;
+ private final List<Type> beanTypes;
+ private final List<Annotation> qualifiers;
+
+ public MockAwareInjectionTargetWrapper(BeanManager beanManager,
+ InjectionTarget<T> wrapped,
+ List<Type> beanTypes,
+ List<Annotation> qualifiers)
+ {
+ this.beanManager = beanManager;
+ this.wrapped = wrapped;
+ this.beanTypes = beanTypes;
+ this.qualifiers = qualifiers;
+ }
+
+ @Override
+ public T produce(CreationalContext<T> creationalContext)
+ {
+ DynamicMockManager mockManager =
+ BeanProvider.getContextualReference(this.beanManager, DynamicMockManager.class, false);
+
+ for (Type beanType : this.beanTypes)
+ {
+ Object mockInstance = mockManager.getMock(
+ (Class)beanType, this.qualifiers.toArray(new Annotation[this.qualifiers.size()]));
+
+ if (mockInstance != null)
+ {
+ return (T)mockInstance;
+ }
+ }
+ return wrapped.produce(creationalContext);
+ }
+
+ /*
+ * generated
+ */
+
+ @Override
+ public void inject(T instance, CreationalContext<T> ctx)
+ {
+ wrapped.inject(instance, ctx);
+ }
+
+ @Override
+ public void postConstruct(T instance)
+ {
+ wrapped.postConstruct(instance);
+ }
+
+ @Override
+ public void preDestroy(T instance)
+ {
+ wrapped.preDestroy(instance);
+ }
+
+ @Override
+ public void dispose(T instance)
+ {
+ wrapped.dispose(instance);
+ }
+
+ @Override
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return wrapped.getInjectionPoints();
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareProducerWrapper.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareProducerWrapper.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareProducerWrapper.java
new file mode 100644
index 0000000..c8ee51d
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockAwareProducerWrapper.java
@@ -0,0 +1,86 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.Producer;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.Set;
+
+public class MockAwareProducerWrapper<T> implements Producer<T>
+{
+ private final BeanManager beanManager;
+ private final Producer<T> wrapped;
+ private final List<Type> beanTypes;
+ private final List<Annotation> qualifiers;
+
+ public MockAwareProducerWrapper(BeanManager beanManager,
+ Producer<T> wrapped,
+ List<Type> beanTypes,
+ List<Annotation> qualifiers)
+ {
+ this.beanManager = beanManager;
+ this.wrapped = wrapped;
+ this.beanTypes = beanTypes;
+ this.qualifiers = qualifiers;
+ }
+
+ @Override
+ public T produce(CreationalContext<T> creationalContext)
+ {
+ DynamicMockManager mockManager =
+ BeanProvider.getContextualReference(this.beanManager, DynamicMockManager.class, false);
+
+ for (Type beanType : this.beanTypes)
+ {
+ Object mockInstance = mockManager.getMock(
+ (Class)beanType, this.qualifiers.toArray(new Annotation[this.qualifiers.size()]));
+
+ if (mockInstance != null)
+ {
+ return (T)mockInstance;
+ }
+ }
+
+ return wrapped.produce(creationalContext);
+ }
+
+ /*
+ * generated
+ */
+
+ @Override
+ public void dispose(T instance)
+ {
+ wrapped.dispose(instance);
+ }
+
+ @Override
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return wrapped.getInjectionPoints();
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
new file mode 100644
index 0000000..a708ca1
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
@@ -0,0 +1,158 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import org.apache.deltaspike.core.spi.activation.Deactivatable;
+import org.apache.deltaspike.core.util.ClassDeactivationUtils;
+import org.apache.deltaspike.core.util.ServiceUtils;
+import org.apache.deltaspike.testcontrol.spi.mock.MockFilter;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Typed;
+import javax.enterprise.inject.spi.AnnotatedMember;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.inject.spi.ProcessInjectionTarget;
+import javax.enterprise.inject.spi.ProcessProducer;
+import javax.enterprise.inject.spi.Producer;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class MockExtension implements Extension, Deactivatable
+{
+ private Boolean isActivated = true;
+ private List<MockFilter> mockFilters;
+
+ protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery)
+ {
+ isActivated = ClassDeactivationUtils.isActivated(getClass());
+ mockFilters = ServiceUtils.loadServiceImplementations(MockFilter.class);
+ }
+
+ public <X> void onProcessInjectionTarget(@Observes ProcessInjectionTarget<X> processInjectionTarget,
+ BeanManager beanManager)
+ {
+ if (!isActivated)
+ {
+ return;
+ }
+
+ for (MockFilter mockFilter : mockFilters)
+ {
+ if (!mockFilter.isMockedImplementationSupported(processInjectionTarget.getAnnotatedType()))
+ {
+ return;
+ }
+ }
+
+ List<Annotation> qualifiers = new ArrayList<Annotation>();
+ for (Annotation annotation : processInjectionTarget.getAnnotatedType().getAnnotations())
+ {
+ if (beanManager.isQualifier(annotation.annotationType()))
+ {
+ qualifiers.add(annotation);
+ }
+ }
+
+ Typed typed = processInjectionTarget.getAnnotatedType().getAnnotation(Typed.class);
+
+ List<Type> foundTypes = new ArrayList<Type>();
+ if (typed != null)
+ {
+ Collections.addAll(foundTypes, typed.value());
+ }
+ else
+ {
+ foundTypes.addAll(extractTypes(processInjectionTarget.getAnnotatedType().getJavaClass()));
+ }
+
+ if (foundTypes.isEmpty())
+ {
+ return;
+ }
+
+ final InjectionTarget<X> originalInjectionTarget = processInjectionTarget.getInjectionTarget();
+ processInjectionTarget.setInjectionTarget(new MockAwareInjectionTargetWrapper<X>(
+ beanManager, originalInjectionTarget, foundTypes, qualifiers));
+ }
+
+ public <X, T> void onProcessProducer(@Observes ProcessProducer<X, T> processProducer, BeanManager beanManager)
+ {
+ if (!isActivated)
+ {
+ return;
+ }
+
+ for (MockFilter mockFilter : mockFilters)
+ {
+ if (!mockFilter.isMockedImplementationSupported(processProducer.getAnnotatedMember()))
+ {
+ return;
+ }
+ }
+
+ final Producer<T> originalProducer = processProducer.getProducer();
+ AnnotatedMember<X> annotatedMember = processProducer.getAnnotatedMember();
+ List<Annotation> qualifiers = new ArrayList<Annotation>();
+ for (Annotation annotation : annotatedMember.getAnnotations())
+ {
+ if (beanManager.isQualifier(annotation.annotationType()))
+ {
+ qualifiers.add(annotation);
+ }
+ }
+
+ Typed typed = annotatedMember.getAnnotation(Typed.class);
+
+ List<Type> foundTypes = new ArrayList<Type>();
+ if (typed != null)
+ {
+ Collections.addAll(foundTypes, typed.value());
+ }
+ else if (annotatedMember.getBaseType() instanceof Class)
+ {
+ foundTypes.addAll(extractTypes((Class)annotatedMember.getBaseType()));
+ }
+
+ if (foundTypes.isEmpty())
+ {
+ return;
+ }
+
+ processProducer.setProducer(new MockAwareProducerWrapper<T>(
+ beanManager, originalProducer, foundTypes, qualifiers));
+ }
+
+ //logic from org.apache.deltaspike.core.util.bean.BeanBuilder
+ protected List<Type> extractTypes(Class currentClass)
+ {
+ List<Type> result = new ArrayList<Type>();
+ for (Class<?> c = currentClass; c != Object.class && c != null; c = c.getSuperclass())
+ {
+ result.add(c);
+ }
+ Collections.addAll(result, currentClass.getInterfaces());
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleApplicationMockManager.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleApplicationMockManager.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleApplicationMockManager.java
new file mode 100644
index 0000000..053475d
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleApplicationMockManager.java
@@ -0,0 +1,30 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import org.apache.deltaspike.testcontrol.api.mock.ApplicationMockManager;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Typed;
+
+@ApplicationScoped
+@Typed(ApplicationMockManager.class)
+public class SimpleApplicationMockManager extends AbstractMockManager implements ApplicationMockManager
+{
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleMockManager.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleMockManager.java b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleMockManager.java
new file mode 100644
index 0000000..8190ab0
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/SimpleMockManager.java
@@ -0,0 +1,47 @@
+/*
+ * 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.testcontrol.impl.mock;
+
+import org.apache.deltaspike.testcontrol.api.mock.ApplicationMockManager;
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.Typed;
+import javax.inject.Inject;
+import java.lang.annotation.Annotation;
+
+@RequestScoped
+@Typed(DynamicMockManager.class)
+public class SimpleMockManager extends AbstractMockManager
+{
+ @Inject
+ private ApplicationMockManager applicationMockManager;
+
+ @Override
+ public <T> T getMock(Class<T> beanClass, Annotation... qualifiers)
+ {
+ T result = applicationMockManager.getMock(beanClass, qualifiers);
+
+ if (result != null)
+ {
+ return result;
+ }
+ return super.getMock(beanClass, qualifiers);
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/resources/META-INF/beans.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/resources/META-INF/beans.xml b/deltaspike/modules/test-control/impl/src/main/resources/META-INF/beans.xml
new file mode 100644
index 0000000..4070730
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/resources/META-INF/beans.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<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">
+</beans>
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
new file mode 100644
index 0000000..23cea62
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -0,0 +1,18 @@
+# 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.
+
+org.apache.deltaspike.testcontrol.impl.mock.MockExtension
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.mock.MockFilter
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.mock.MockFilter b/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.mock.MockFilter
new file mode 100644
index 0000000..a59065d
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/main/resources/META-INF/services/org.apache.deltaspike.testcontrol.spi.mock.MockFilter
@@ -0,0 +1,18 @@
+# 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.
+
+org.apache.deltaspike.testcontrol.impl.mock.DefaultMockFilter
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/InternalTestMockFilter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/InternalTestMockFilter.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/InternalTestMockFilter.java
new file mode 100644
index 0000000..1478293
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/InternalTestMockFilter.java
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+import org.apache.deltaspike.testcontrol.impl.mock.DefaultMockFilter;
+
+public class InternalTestMockFilter extends DefaultMockFilter
+{
+ private static final String DS_BASE_PACKAGE = "org.apache.deltaspike.";
+ private static final String DS_TEST_BASE_PACKAGE = "org.apache.deltaspike.test.testcontrol.mock.";
+
+ protected boolean isInternalPackage(String packageName)
+ {
+ return (packageName.startsWith(DS_BASE_PACKAGE) && !packageName.startsWith(DS_TEST_BASE_PACKAGE));
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/ApplicationScopedBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/ApplicationScopedBean.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/ApplicationScopedBean.java
new file mode 100644
index 0000000..69a7f63
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/ApplicationScopedBean.java
@@ -0,0 +1,37 @@
+/*
+ * 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.mock.shared;
+
+import javax.enterprise.context.ApplicationScoped;
+
+@ApplicationScoped
+public class ApplicationScopedBean
+{
+ private int count = 0;
+
+ public int getCount()
+ {
+ return count;
+ }
+
+ public void increaseCount()
+ {
+ this.count++;
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/MyQualifier.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/MyQualifier.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/MyQualifier.java
new file mode 100644
index 0000000..ff5a0b6
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/MyQualifier.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.testcontrol.mock.shared;
+
+import javax.inject.Qualifier;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.*;
+
+@Target( { TYPE, METHOD, PARAMETER, FIELD })
+@Retention(value = RetentionPolicy.RUNTIME)
+@Documented
+@Qualifier
+public @interface MyQualifier
+{
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/RequestScopedBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/RequestScopedBean.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/RequestScopedBean.java
new file mode 100644
index 0000000..e0d7cd8
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/RequestScopedBean.java
@@ -0,0 +1,37 @@
+/*
+ * 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.mock.shared;
+
+import javax.enterprise.context.RequestScoped;
+
+@RequestScoped
+public class RequestScopedBean
+{
+ private int count = 0;
+
+ public int getCount()
+ {
+ return count;
+ }
+
+ public void increaseCount()
+ {
+ this.count++;
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/SessionScopedBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/SessionScopedBean.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/SessionScopedBean.java
new file mode 100644
index 0000000..f7bee7f
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/shared/SessionScopedBean.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.test.testcontrol.mock.shared;
+
+import javax.enterprise.context.SessionScoped;
+import java.io.Serializable;
+
+@SessionScoped
+public class SessionScopedBean implements Serializable
+{
+ private static final long serialVersionUID = -6055362670706159152L;
+ protected int count = 0;
+
+ public int getCount()
+ {
+ return count;
+ }
+
+ public void increaseCount()
+ {
+ this.count++;
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc001/MockedRequestScopedBeanTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc001/MockedRequestScopedBeanTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc001/MockedRequestScopedBeanTest.java
new file mode 100644
index 0000000..3551c7b
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc001/MockedRequestScopedBeanTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.mock.uc001;
+
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.testcontrol.mock.shared.RequestScopedBean;
+import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+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)
+public class MockedRequestScopedBeanTest
+{
+ @Inject
+ private RequestScopedBean requestScopedBean;
+
+ @Inject
+ private DynamicMockManager mockManager;
+
+ @Test
+ public void manualMock1()
+ {
+ mockManager.addMock(new RequestScopedBean()
+ {
+ @Override
+ public int getCount()
+ {
+ return 7;
+ }
+ });
+
+ Assert.assertEquals(7, requestScopedBean.getCount());
+ requestScopedBean.increaseCount();
+ Assert.assertEquals(7, requestScopedBean.getCount());
+ }
+
+ @Test
+ public void manualMock2() //same test with different mock
+ {
+ mockManager.addMock(new RequestScopedBean()
+ {
+ @Override
+ public int getCount()
+ {
+ return 14;
+ }
+ });
+
+ Assert.assertEquals(14, requestScopedBean.getCount());
+ requestScopedBean.increaseCount();
+ Assert.assertEquals(14, requestScopedBean.getCount());
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc002/MockedSessionScopedBeanTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc002/MockedSessionScopedBeanTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc002/MockedSessionScopedBeanTest.java
new file mode 100644
index 0000000..5908807
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc002/MockedSessionScopedBeanTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.mock.uc002;
+
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.testcontrol.mock.shared.SessionScopedBean;
+import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+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)
+public class MockedSessionScopedBeanTest
+{
+ @Inject
+ private SessionScopedBean sessionScopedBean;
+
+ @Inject
+ private DynamicMockManager mockManager;
+
+ @Test
+ public void manualMock1()
+ {
+ mockManager.addMock(new SessionScopedBean()
+ {
+ @Override
+ public int getCount()
+ {
+ return 7;
+ }
+ });
+
+ Assert.assertEquals(7, sessionScopedBean.getCount());
+ sessionScopedBean.increaseCount();
+ Assert.assertEquals(7, sessionScopedBean.getCount());
+ }
+
+ @Test
+ public void manualMock2() //same test with different mock
+ {
+ mockManager.addMock(new SessionScopedBean()
+ {
+ @Override
+ public int getCount()
+ {
+ return 14;
+ }
+ });
+
+ Assert.assertEquals(14, sessionScopedBean.getCount());
+ sessionScopedBean.increaseCount();
+ Assert.assertEquals(14, sessionScopedBean.getCount());
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBean.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBean.java
new file mode 100644
index 0000000..6cd8360
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBean.java
@@ -0,0 +1,32 @@
+/*
+ * 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.mock.uc003;
+
+import org.apache.deltaspike.test.testcontrol.mock.shared.SessionScopedBean;
+
+import javax.enterprise.inject.Typed;
+
+@Typed() //exclude it for the cdi type-check
+public class MockedSessionScopedBean extends SessionScopedBean
+{
+ public void setCount(int count)
+ {
+ this.count = count;
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBeanAcrossMethodsTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBeanAcrossMethodsTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBeanAcrossMethodsTest.java
new file mode 100644
index 0000000..f4f1400
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc003/MockedSessionScopedBeanAcrossMethodsTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.mock.uc003;
+
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.testcontrol.mock.shared.SessionScopedBean;
+import org.apache.deltaspike.testcontrol.api.TestControl;
+import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.inject.Inject;
+
+//Usually NOT needed! Currently only needed due to our arquillian-setup
+@Category(SeCategory.class)
+
+@RunWith(CdiTestRunner.class)
+@TestControl(startScopes = {ApplicationScoped.class, SessionScoped.class})
+public class MockedSessionScopedBeanAcrossMethodsTest
+{
+ @Inject
+ private SessionScopedBean sessionScopedBean;
+
+ @Inject
+ private DynamicMockManager mockManager;
+
+ //static is needed here, because the session spans across all test-methods
+ private static MockedSessionScopedBean mockedSessionScopedBean = new MockedSessionScopedBean();
+
+ @Before
+ public void init()
+ {
+ mockManager.addMock(mockedSessionScopedBean);
+ }
+
+ @Test
+ public void manualMock1()
+ {
+ mockedSessionScopedBean.setCount(7);
+
+ Assert.assertEquals(7, sessionScopedBean.getCount());
+ sessionScopedBean.increaseCount();
+ Assert.assertEquals(8, sessionScopedBean.getCount());
+ }
+
+ @Test
+ public void manualMock2()
+ {
+ mockedSessionScopedBean.setCount(14);
+
+ Assert.assertEquals(14, sessionScopedBean.getCount());
+ sessionScopedBean.increaseCount();
+ Assert.assertEquals(15, sessionScopedBean.getCount());
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBean.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBean.java
new file mode 100644
index 0000000..a68a164
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBean.java
@@ -0,0 +1,33 @@
+/*
+ * 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.mock.uc004;
+
+import org.apache.deltaspike.test.testcontrol.mock.shared.ApplicationScopedBean;
+
+import javax.enterprise.inject.Typed;
+
+@Typed() //exclude it for the cdi type-check
+public class MockedApplicationScopedBean extends ApplicationScopedBean
+{
+ @Override
+ public int getCount()
+ {
+ return 14;
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBeanTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBeanTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBeanTest.java
new file mode 100644
index 0000000..8186881
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc004/MockedApplicationScopedBeanTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.mock.uc004;
+
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.testcontrol.mock.shared.ApplicationScopedBean;
+import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
+import org.apache.deltaspike.testcontrol.api.mock.ApplicationMockManager;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+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)
+public class MockedApplicationScopedBeanTest
+{
+ @Inject
+ private ApplicationScopedBean applicationScopedBean;
+
+ //e.g. application-scoped beans are cached very early -> usage of ApplicationMockManager to register mocks for them
+ @BeforeClass
+ public static void init()
+ {
+ ApplicationMockManager applicationMockManager = BeanProvider.getContextualReference(ApplicationMockManager.class);
+ applicationMockManager.addMock(new MockedApplicationScopedBean());
+ }
+
+ @Test
+ public void manualMock1()
+ {
+ Assert.assertEquals(14, applicationScopedBean.getCount());
+ applicationScopedBean.increaseCount();
+ Assert.assertEquals(14, applicationScopedBean.getCount());
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/MockedProducedBeanTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/MockedProducedBeanTest.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/MockedProducedBeanTest.java
new file mode 100644
index 0000000..1826cab
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/MockedProducedBeanTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.mock.uc005;
+
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
+import org.apache.deltaspike.testcontrol.api.mock.DynamicMockManager;
+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)
+public class MockedProducedBeanTest
+{
+ @Inject
+ private ProducedBean producedBean;
+
+ @Inject
+ private DynamicMockManager mockManager;
+
+ @Test
+ public void manualMockForProducedBean1()
+ {
+ mockManager.addMock(new ProducedBean()
+ {
+ @Override
+ public int getCount()
+ {
+ return 7;
+ }
+ });
+ Assert.assertEquals(7, producedBean.getCount());
+ producedBean.increaseCount();
+ Assert.assertEquals(7, producedBean.getCount());
+ }
+
+ @Test
+ public void manualMockForProducedBean2()
+ {
+ mockManager.addMock(new ProducedBean()
+ {
+ @Override
+ public int getCount()
+ {
+ return 14;
+ }
+ });
+ Assert.assertEquals(14, producedBean.getCount());
+ producedBean.increaseCount();
+ Assert.assertEquals(14, producedBean.getCount());
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b47375ca/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/ProducedBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/ProducedBean.java b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/ProducedBean.java
new file mode 100644
index 0000000..c55e726
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/mock/uc005/ProducedBean.java
@@ -0,0 +1,37 @@
+/*
+ * 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.mock.uc005;
+
+import javax.enterprise.inject.Typed;
+
+@Typed() //exclude it for the cdi type-check
+public class ProducedBean
+{
+ private int count = 0;
+
+ public int getCount()
+ {
+ return count;
+ }
+
+ public void increaseCount()
+ {
+ this.count++;
+ }
+}