Author: struberg
Date: Sat Oct 19 09:02:44 2013
New Revision: 1533716

URL: http://svn.apache.org/r1533716
Log:
add docs for testing application with OWB and DeltaSpike CdiCtrl

Added:
    openwebbeans/cms-site/trunk/content/testing_cdictrl.mdtext   (with props)

Added: openwebbeans/cms-site/trunk/content/testing_cdictrl.mdtext
URL: 
http://svn.apache.org/viewvc/openwebbeans/cms-site/trunk/content/testing_cdictrl.mdtext?rev=1533716&view=auto
==============================================================================
--- openwebbeans/cms-site/trunk/content/testing_cdictrl.mdtext (added)
+++ openwebbeans/cms-site/trunk/content/testing_cdictrl.mdtext Sat Oct 19 
09:02:44 2013
@@ -0,0 +1,170 @@
+Title: OpenWebBeans CdiCtrl 
+Notice:    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.
+
+# Testing your application with Apache DeltaSpike CdiCtrl 
+
+## About CdiCtrl
+
+``CdiCtrl`` is *not* part of Apache OpenWebBeans but a module of 
+[Apache DeltaSpike][1]. 
+
+The ``CdiCtrl`` interface abstracts away all the logic to boot a CDI Container
+and controls the lifecycle of it's Contexts (Request Context, Session Context, 
etc).
+
+The actual CDI Container is determined by using the 
``java.util.ServiceLoader``.
+There are a few different implementations available. Besides Apache 
OpenWebBeans
+there are also plugins for JBoss Weld and [Apache TomEE][2]. 
+
+## Why use CdiCtrl for your unit tests?
+
+Whenever you need to write unit tests for a full application, then you will 
need to 
+have a CDI container scann all your classes, create ``Bean<T>`` from it and 
provide 
+them for injection. All this can be done by either using JUnits ``@RunWith`` 
or 
+by simply creating a common base class for your unit tests which boots up the 
+container on your test classpath.
+
+There is no need to restart the container for each and every of your unit tests
+as this would cause a big performance loss. Instead it is usually sufficient 
to 
+use the CdiCtrls ``ContextControl`` mechanism to just stop and restart the 
+respective CDI Contexts.
+
+Such a base class could look roughly like the following:
+
+    :::java
+    public abstract class ContainerTest {
+    
+        protected static volatile CdiContainer cdiContainer;
+        // nice to know, since testng executes tests in parallel.
+        protected static int containerRefCount = 0;
+    
+        private static final Logger logger = 
LoggerFactory.getLogger(ContainerTest.class);
+    
+        protected ProjectStage runInProjectStage() {
+            return ProjectStage.UnitTest;
+        }
+        
+        /**
+         * Starts container
+         * @throws Exception in case of severe problem
+         */
+        @BeforeMethod
+        public final void beforeMethod() throws Exception {
+            containerRefCount++;
+    
+            if (cdiContainer == null) {
+                // setting up the Apache DeltaSpike ProjectStage
+                ProjectStage projectStage = runInProjectStage();
+                ProjectStageProducer.setProjectStage(projectStage);
+    
+                cdiContainer = CdiContainerLoader.getCdiContainer();
+    
+                cdiContainer.boot();
+                cdiContainer.getContextControl().startContexts();
+            }
+            else {
+                cleanInstances();
+            }
+        }
+    
+    
+        public static CdiContainer getCdiContainer() {
+            return cdiContainer;
+        }
+    
+        /**
+         * This will fill all the InjectionPoints of the current test class 
for you
+         */
+        @BeforeClass
+        public final void beforeClass() throws Exception {
+            beforeMethod();
+    
+            // perform injection into the very own test class
+            BeanManager beanManager = cdiContainer.getBeanManager();
+    
+            CreationalContext creationalContext = 
beanManager.createCreationalContext(null);
+    
+            AnnotatedType annotatedType = 
beanManager.createAnnotatedType(this.getClass());
+            InjectionTarget injectionTarget = 
beanManager.createInjectionTarget(annotatedType);
+            injectionTarget.inject(this, creationalContext);
+    
+            // this is a trick we use to have proper DB transactions when 
using the entitymanager-per-request pattern
+            cleanInstances();
+            cleanUpDb();
+            cleanInstances();
+        }
+    
+        /**
+         * Shuts down container.
+         * @throws Exception in case of severe problem
+         */
+        @AfterMethod
+        public final void afterMethod() throws Exception {
+            if (cdiContainer != null) {
+                cleanInstances();
+                containerRefCount--;
+            }
+        }
+    
+        /**
+         * clean the NormalScoped contextual instances by stopping and 
restarting
+         * some contexts. You could also restart the ApplicationScoped context
+         * if you have some caches in your classes. 
+         */
+        public final void cleanInstances() throws Exception {
+            cdiContainer.getContextControl().stopContext(RequestScoped.class);
+            cdiContainer.getContextControl().startContext(RequestScoped.class);
+            cdiContainer.getContextControl().stopContext(SessionScoped.class);
+            cdiContainer.getContextControl().startContext(SessionScoped.class);
+        }
+    
+        @AfterSuite
+        public synchronized void shutdownContainer() throws Exception {
+            if (cdiContainer != null) {
+                cdiContainer.shutdown();
+                cdiContainer = null;
+            }
+        }
+    
+        public void finalize() throws Throwable {
+            shutdownContainer();
+            super.finalize();
+        }
+    
+    
+        /**
+         * Override this method for database clean up.
+         *
+         * @throws Exception in case of severe problem
+         */
+        protected void cleanUpDb() throws Exception {
+            //Override in subclasses when needed
+        }
+    
+        protected <T> T getInstance(Class<T> type, Qualifier... qualifiers) {
+            return BeanProvider.getContextualReference(type, qualifiers);
+        }
+    
+    }
+
+ 
+
+
+
+  [1]: http://deltaspike.apache.org
+  [2]: http://tomee.apache.org
+

Propchange: openwebbeans/cms-site/trunk/content/testing_cdictrl.mdtext
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to