deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject git commit: DELTASPIKE-342 basic support for @Before and @After
Date Mon, 06 Jan 2014 22:27:11 GMT
Updated Branches:
  refs/heads/master 50fc96e41 -> ed454cef5


DELTASPIKE-342 basic support for @Before and @After


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

Branch: refs/heads/master
Commit: ed454cef5e0da6a29e6e28d2b83de1599963587f
Parents: 50fc96e
Author: gpetracek <gpetracek@apache.org>
Authored: Mon Jan 6 23:22:05 2014 +0100
Committer: gpetracek <gpetracek@apache.org>
Committed: Mon Jan 6 23:26:05 2014 +0100

----------------------------------------------------------------------
 .../testcontrol/api/junit/CdiTestRunner.java    | 23 +++++++
 .../uc008/BeforeAndAfterInjectionTest.java      | 67 ++++++++++++++++++++
 2 files changed, 90 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ed454cef/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/junit/CdiTestRunner.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/junit/CdiTestRunner.java
b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/junit/CdiTestRunner.java
index 5e8ede8..33564a6 100644
--- a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/junit/CdiTestRunner.java
+++ b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/api/junit/CdiTestRunner.java
@@ -154,6 +154,29 @@ public class CdiTestRunner extends BlockJUnit4ClassRunner
         }
     }
 
+    @Override
+    protected Object createTest() throws Exception
+    {
+        BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
+
+        Class<?> type = getTestClass().getJavaClass();
+        Set<Bean<?>> beans = beanManager.getBeans(type);
+
+        Object result;
+        if (beans == null || beans.isEmpty())
+        {
+            result = super.createTest();
+            BeanProvider.injectFields(result); //fallback to simple injection
+        }
+        else
+        {
+            Bean<Object> bean = (Bean<Object>) beanManager.resolve(beans);
+            CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean);
+            result = beanManager.getReference(bean, type, creationalContext);
+        }
+        return result;
+    }
+
     //TODO use Rules instead
     @Override
     protected Statement withBefores(FrameworkMethod method, Object target, Statement statement)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/ed454cef/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc008/BeforeAndAfterInjectionTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc008/BeforeAndAfterInjectionTest.java
b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc008/BeforeAndAfterInjectionTest.java
new file mode 100644
index 0000000..007c6a2
--- /dev/null
+++ b/deltaspike/modules/test-control/impl/src/test/java/org/apache/deltaspike/test/testcontrol/uc008/BeforeAndAfterInjectionTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.test.testcontrol.uc008;
+
+import junit.framework.Assert;
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.testcontrol.shared.ApplicationScopedBean;
+import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
+import org.junit.After;
+import org.junit.Before;
+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 BeforeAndAfterInjectionTest
+{
+    @Inject
+    private ApplicationScopedBean applicationScopedBean;
+
+    @Before
+    public void before()
+    {
+        if (this.applicationScopedBean == null)
+        {
+            throw new IllegalStateException("injection failed");
+        }
+    }
+
+    @Test
+    public void injectionTest()
+    {
+        Assert.assertNotNull(this.applicationScopedBean);
+    }
+
+    @After
+    public void after()
+    {
+        if (this.applicationScopedBean == null)
+        {
+            throw new IllegalStateException("injection failed");
+        }
+    }
+}


Mime
View raw message