deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject git commit: DELTASPIKE-602 forward manual scope-handling
Date Wed, 21 May 2014 21:36:25 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 3c16c913e -> 0f18eccc4


DELTASPIKE-602 forward manual scope-handling


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

Branch: refs/heads/master
Commit: 0f18eccc46b6e45578015027a505092bf687fb40
Parents: 3c16c91
Author: gpetracek <gpetracek@apache.org>
Authored: Wed May 21 23:34:26 2014 +0200
Committer: gpetracek <gpetracek@apache.org>
Committed: Wed May 21 23:34:26 2014 +0200

----------------------------------------------------------------------
 .../testcontrol/api/junit/CdiTestRunner.java    | 18 +++++++
 .../impl/jsf/MockedJsf2TestContainer.java       | 20 --------
 .../impl/jsf/MockedJsfTestContainerAdapter.java | 20 --------
 .../impl/jsf/MyFacesContainerAdapter.java       | 20 --------
 .../MyFacesContainerPerTestMethodAdapter.java   | 21 --------
 .../impl/request/ContextControlDecorator.java   | 51 +++++++++++++++++---
 .../request/ManuallyHandledRequestEvent.java    | 39 ---------------
 7 files changed, 61 insertions(+), 128 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0f18eccc/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 1c0a427..c91bafc 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
@@ -82,6 +82,8 @@ public class CdiTestRunner extends BlockJUnit4ClassRunner
 
     private static ThreadLocal<Boolean> automaticScopeHandlingActive = new ThreadLocal<Boolean>();
 
+    private static ThreadLocal<CdiTestRunner> currentTestRunner = new ThreadLocal<CdiTestRunner>();
+
     private List<TestStatementDecoratorFactory> statementDecoratorFactories;
 
     private ContainerAwareTestContext testContext;
@@ -153,6 +155,8 @@ public class CdiTestRunner extends BlockJUnit4ClassRunner
     @Override
     protected void runChild(FrameworkMethod method, RunNotifier notifier)
     {
+        currentTestRunner.set(this);
+
         TestControl testControl = method.getAnnotation(TestControl.class);
 
         ContainerAwareTestContext currentTestContext =
@@ -587,6 +591,8 @@ public class CdiTestRunner extends BlockJUnit4ClassRunner
                 setCurrentTestMethod(null);
                 ProjectStageProducer.setProjectStage(previousProjectStage);
                 previousProjectStage = null;
+                currentTestRunner.remove();
+                currentTestRunner.set(null);
             }
         }
 
@@ -779,4 +785,16 @@ public class CdiTestRunner extends BlockJUnit4ClassRunner
     {
         return automaticScopeHandlingActive.get();
     }
+
+    public static List<ExternalContainer> getActiveExternalContainers()
+    {
+        CdiTestRunner cdiTestRunner = currentTestRunner.get();
+
+        if (cdiTestRunner == null)
+        {
+            return Collections.emptyList();
+        }
+
+        return Collections.unmodifiableList(cdiTestRunner.testContext.externalContainers);
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0f18eccc/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java
index ab38bb7..dce9dae 100644
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsf2TestContainer.java
@@ -19,7 +19,6 @@
 package org.apache.deltaspike.testcontrol.impl.jsf;
 
 import org.apache.deltaspike.core.api.config.ConfigResolver;
-import org.apache.deltaspike.testcontrol.impl.request.ManuallyHandledRequestEvent;
 import org.apache.deltaspike.testcontrol.spi.ExternalContainer;
 import org.apache.myfaces.test.mock.MockApplicationFactory;
 import org.apache.myfaces.test.mock.MockExceptionHandlerFactory;
@@ -41,8 +40,6 @@ import javax.el.ExpressionFactory;
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.context.SessionScoped;
-import javax.enterprise.event.Observes;
-import javax.enterprise.event.Reception;
 import javax.faces.FactoryFinder;
 import javax.faces.application.Application;
 import javax.faces.application.ApplicationFactory;
@@ -290,21 +287,4 @@ public class MockedJsf2TestContainer implements ExternalContainer
     {
         return LifecycleFactory.DEFAULT_LIFECYCLE;
     }
-
-    public void onManuallyHandledRequest(
-        @Observes(notifyObserver = Reception.IF_EXISTS) ManuallyHandledRequestEvent manuallyHandledRequestEvent)
-    {
-        switch (manuallyHandledRequestEvent.getManualAction())
-        {
-            case STARTED:
-                startScope(RequestScoped.class);
-                break;
-            case STOPPED:
-                stopScope(RequestScoped.class);
-                break;
-            default:
-                throw new IllegalArgumentException("unsupported action: " +
-                    manuallyHandledRequestEvent.getManualAction().name());
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0f18eccc/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java
index b68634a..48c09b9 100644
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MockedJsfTestContainerAdapter.java
@@ -18,15 +18,12 @@
  */
 package org.apache.deltaspike.testcontrol.impl.jsf;
 
-import org.apache.deltaspike.testcontrol.impl.request.ManuallyHandledRequestEvent;
 import org.apache.deltaspike.testcontrol.spi.ExternalContainer;
 import org.apache.myfaces.test.mock.MockedJsfTestContainer;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.context.RequestScoped;
 import javax.enterprise.context.SessionScoped;
-import javax.enterprise.event.Observes;
-import javax.enterprise.event.Reception;
 import java.lang.annotation.Annotation;
 
 /**
@@ -79,21 +76,4 @@ public class MockedJsfTestContainerAdapter implements ExternalContainer
     {
         return 1000; //default in ds
     }
-
-    public void onManuallyHandledRequest(
-            @Observes(notifyObserver = Reception.IF_EXISTS) ManuallyHandledRequestEvent manuallyHandledRequestEvent)
-    {
-        switch (manuallyHandledRequestEvent.getManualAction())
-        {
-            case STARTED:
-                startScope(RequestScoped.class);
-                break;
-            case STOPPED:
-                stopScope(RequestScoped.class);
-                break;
-            default:
-                throw new IllegalArgumentException("unsupported action: " +
-                        manuallyHandledRequestEvent.getManualAction().name());
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0f18eccc/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java
index f8dc224..315fecf 100644
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerAdapter.java
@@ -20,7 +20,6 @@ package org.apache.deltaspike.testcontrol.impl.jsf;
 
 import org.apache.deltaspike.core.api.config.ConfigResolver;
 import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider;
-import org.apache.deltaspike.testcontrol.impl.request.ManuallyHandledRequestEvent;
 import org.apache.deltaspike.testcontrol.spi.ExternalContainer;
 import org.apache.deltaspike.testcontrol.spi.TestAware;
 import org.apache.myfaces.mc.test.core.annotation.TestConfig;
@@ -30,8 +29,6 @@ import org.junit.runners.model.TestClass;
 import javax.el.ExpressionFactory;
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.context.RequestScoped;
-import javax.enterprise.event.Observes;
-import javax.enterprise.event.Reception;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.HashMap;
@@ -164,21 +161,4 @@ public class MyFacesContainerAdapter implements TestAware, ExternalContainer
     {
         //not needed
     }
-
-    public void onManuallyHandledRequest(
-            @Observes(notifyObserver = Reception.IF_EXISTS) ManuallyHandledRequestEvent manuallyHandledRequestEvent)
-    {
-        switch (manuallyHandledRequestEvent.getManualAction())
-        {
-            case STARTED:
-                startScope(RequestScoped.class);
-                break;
-            case STOPPED:
-                stopScope(RequestScoped.class);
-                break;
-            default:
-                throw new IllegalArgumentException("unsupported action: " +
-                        manuallyHandledRequestEvent.getManualAction().name());
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0f18eccc/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerPerTestMethodAdapter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerPerTestMethodAdapter.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerPerTestMethodAdapter.java
index 07fe368..f954497 100644
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerPerTestMethodAdapter.java
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/jsf/MyFacesContainerPerTestMethodAdapter.java
@@ -18,14 +18,10 @@
  */
 package org.apache.deltaspike.testcontrol.impl.jsf;
 
-import org.apache.deltaspike.testcontrol.impl.request.ManuallyHandledRequestEvent;
 import org.apache.deltaspike.testcontrol.spi.ExternalContainer;
 import org.apache.deltaspike.testcontrol.spi.TestAware;
 
 import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.context.RequestScoped;
-import javax.enterprise.event.Observes;
-import javax.enterprise.event.Reception;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 
@@ -87,21 +83,4 @@ public class MyFacesContainerPerTestMethodAdapter implements TestAware,
External
             myFacesContainerAdapterThreadLocal.get().shutdown();
         }
     }
-
-    public void onManuallyHandledRequest(
-            @Observes(notifyObserver = Reception.IF_EXISTS) ManuallyHandledRequestEvent manuallyHandledRequestEvent)
-    {
-        switch (manuallyHandledRequestEvent.getManualAction())
-        {
-            case STARTED:
-                startScope(RequestScoped.class);
-                break;
-            case STOPPED:
-                stopScope(RequestScoped.class);
-                break;
-            default:
-                throw new IllegalArgumentException("unsupported action: " +
-                        manuallyHandledRequestEvent.getManualAction().name());
-        }
-    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0f18eccc/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ContextControlDecorator.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ContextControlDecorator.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ContextControlDecorator.java
index 21534f2..5c3ad7f 100644
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ContextControlDecorator.java
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ContextControlDecorator.java
@@ -20,12 +20,16 @@ package org.apache.deltaspike.testcontrol.impl.request;
 
 import org.apache.deltaspike.cdise.api.ContextControl;
 import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
+import org.apache.deltaspike.testcontrol.spi.ExternalContainer;
 
 import javax.decorator.Decorator;
 import javax.decorator.Delegate;
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.ConversationScoped;
 import javax.enterprise.context.RequestScoped;
-import javax.enterprise.event.Event;
+import javax.enterprise.context.SessionScoped;
 import javax.inject.Inject;
+import javax.inject.Singleton;
 import java.lang.annotation.Annotation;
 
 /**
@@ -44,18 +48,39 @@ public class ContextControlDecorator implements ContextControl
     @Delegate
     private ContextControl wrapped;
 
-    @Inject
-    private Event<ManuallyHandledRequestEvent> manualRequestEvent;
-
     @Override
     public void startContexts()
     {
         wrapped.startContexts();
+
+        if (isManualScopeHandling())
+        {
+            for (ExternalContainer externalContainer : CdiTestRunner.getActiveExternalContainers())
+            {
+                externalContainer.startScope(Singleton.class);
+                externalContainer.startScope(ApplicationScoped.class);
+                externalContainer.startScope(RequestScoped.class);
+                externalContainer.startScope(SessionScoped.class);
+                externalContainer.startScope(ConversationScoped.class);
+            }
+        }
     }
 
     @Override
     public void stopContexts()
     {
+        if (isManualScopeHandling())
+        {
+            for (ExternalContainer externalContainer : CdiTestRunner.getActiveExternalContainers())
+            {
+                externalContainer.stopScope(ConversationScoped.class);
+                externalContainer.stopScope(SessionScoped.class);
+                externalContainer.stopScope(RequestScoped.class);
+                externalContainer.stopScope(ApplicationScoped.class);
+                externalContainer.stopScope(Singleton.class);
+            }
+        }
+
         wrapped.stopContexts();
     }
 
@@ -66,7 +91,10 @@ public class ContextControlDecorator implements ContextControl
 
         if (isManuallyHandledRequest(scopeClass))
         {
-            manualRequestEvent.fire(new ManuallyHandledRequestEvent(ManuallyHandledRequestEvent.ManualAction.STARTED));
+            for (ExternalContainer externalContainer : CdiTestRunner.getActiveExternalContainers())
+            {
+                externalContainer.startScope(scopeClass);
+            }
         }
     }
 
@@ -77,13 +105,20 @@ public class ContextControlDecorator implements ContextControl
 
         if (isManuallyHandledRequest(scopeClass))
         {
-            manualRequestEvent.fire(new ManuallyHandledRequestEvent(ManuallyHandledRequestEvent.ManualAction.STOPPED));
+            for (ExternalContainer externalContainer : CdiTestRunner.getActiveExternalContainers())
+            {
+                externalContainer.stopScope(scopeClass);
+            }
         }
     }
 
     private boolean isManuallyHandledRequest(Class<? extends Annotation> scopeClass)
     {
-        return RequestScoped.class.equals(scopeClass) &&
-            !Boolean.TRUE.equals(CdiTestRunner.isAutomaticScopeHandlingActive());
+        return RequestScoped.class.equals(scopeClass) && isManualScopeHandling();
+    }
+
+    private boolean isManualScopeHandling()
+    {
+        return !Boolean.TRUE.equals(CdiTestRunner.isAutomaticScopeHandlingActive());
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/0f18eccc/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ManuallyHandledRequestEvent.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ManuallyHandledRequestEvent.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ManuallyHandledRequestEvent.java
deleted file mode 100644
index c0700b1..0000000
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/request/ManuallyHandledRequestEvent.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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.request;
-
-public class ManuallyHandledRequestEvent
-{
-    private final ManualAction manualAction;
-
-    public ManuallyHandledRequestEvent(ManualAction manualAction)
-    {
-        this.manualAction = manualAction;
-    }
-
-    public ManualAction getManualAction()
-    {
-        return manualAction;
-    }
-
-    public static enum ManualAction
-    {
-        STARTED, STOPPED
-    }
-}


Mime
View raw message