tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: moving junit specific rules to junit package + adding scope rule
Date Fri, 02 Jan 2015 07:24:49 GMT
Repository: tomee
Updated Branches:
  refs/heads/develop 8bd634432 -> fd990a263


moving junit specific rules to junit package + adding scope rule


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

Branch: refs/heads/develop
Commit: fd990a26395838f42acccc38fe73c0ef837181c5
Parents: 8bd6344
Author: Romain Manni-Bucau <rmannibucau@apache.org>
Authored: Fri Jan 2 08:24:18 2015 +0100
Committer: Romain Manni-Bucau <rmannibucau@apache.org>
Committed: Fri Jan 2 08:24:18 2015 +0100

----------------------------------------------------------------------
 .../openejb/junit/BeanContextBaseRule.java      | 47 ++++++++++
 .../org/apache/openejb/junit/CdiScopes.java     | 30 +++++++
 .../org/apache/openejb/junit/RunAsRule.java     | 89 +++++++++++++++++++
 .../org/apache/openejb/junit/ScopesRule.java    | 69 +++++++++++++++
 .../apache/openejb/junit/TransactionRule.java   | 93 ++++++++++++++++++++
 .../openejb/testing/ApplicationComposers.java   | 18 ++--
 .../openejb/testing/BeanContextBaseRule.java    | 47 ----------
 .../org/apache/openejb/testing/RunAsRule.java   | 89 -------------------
 .../apache/openejb/testing/TransactionRule.java | 93 --------------------
 .../apache/openejb/junit/ScopesRuleTest.java    | 33 +++++++
 .../apache/openejb/testing/RunAsRuleTest.java   |  1 +
 .../openejb/testing/TransactionRuleTest.java    |  1 +
 12 files changed, 376 insertions(+), 234 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/junit/BeanContextBaseRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/junit/BeanContextBaseRule.java
b/container/openejb-core/src/main/java/org/apache/openejb/junit/BeanContextBaseRule.java
new file mode 100644
index 0000000..e0d4483
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/junit/BeanContextBaseRule.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.openejb.junit;
+
+import org.apache.openejb.BeanContext;
+import org.apache.openejb.Injector;
+import org.apache.openejb.core.ThreadContext;
+
+public abstract class BeanContextBaseRule {
+    protected final Object instance;
+
+    protected BeanContextBaseRule(final Object instance) {
+        this.instance = instance;
+    }
+
+    protected BeanContext getBeanContext() {
+        BeanContext beanContext = null;
+        if (instance != null) {
+            beanContext = Injector.resolve(instance.getClass());
+        }
+        if (beanContext == null) {
+            final ThreadContext threadContext = ThreadContext.getThreadContext();
+            if (threadContext != null) {
+                beanContext = threadContext.getBeanContext();
+            }
+        }
+
+        if (beanContext == null) {
+            throw new IllegalStateException("Can't find test BeanContext");
+        }
+        return beanContext;
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/junit/CdiScopes.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/junit/CdiScopes.java
b/container/openejb-core/src/main/java/org/apache/openejb/junit/CdiScopes.java
new file mode 100644
index 0000000..b0eb4d4
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/junit/CdiScopes.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.openejb.junit;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ ElementType.METHOD, ElementType.TYPE })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface CdiScopes {
+    Class<? extends Annotation>[] value();
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/junit/RunAsRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/junit/RunAsRule.java
b/container/openejb-core/src/main/java/org/apache/openejb/junit/RunAsRule.java
new file mode 100644
index 0000000..b771f2c
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/junit/RunAsRule.java
@@ -0,0 +1,89 @@
+/*
+ * 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.openejb.junit;
+
+import org.apache.openejb.BeanContext;
+import org.apache.openejb.core.ThreadContext;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import javax.annotation.security.RunAs;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+public class RunAsRule extends BeanContextBaseRule implements TestRule {
+    private final ThreadLocal<String> role = new ThreadLocal<>();
+
+    public RunAsRule() {
+        this(null);
+    }
+
+    public RunAsRule(final Object o) {
+        super(o);
+    }
+
+    public void role(final String role) {
+        this.role.set(role);
+    }
+
+    @Override
+    public Statement apply(final Statement base, final Description description) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                final RunAs annotation = description.getAnnotation(RunAs.class);
+                final As as = description.getAnnotation(As.class);
+                String currentRole = role.get();
+                role.remove(); // no more needed
+                if (annotation == null && as == null && currentRole == null)
{
+                    base.evaluate();
+                    return;
+                }
+
+                final BeanContext beanContext = getBeanContext();
+                if (currentRole == null) {
+                    if (annotation == null) {
+                        currentRole = as.value();
+                    } else {
+                        currentRole = annotation.value();
+                    }
+                }
+                final String runAs = beanContext.getRunAs();
+                final String runAsUser = beanContext.getRunAsUser();
+                beanContext.setRunAs(currentRole);
+                final ThreadContext old = ThreadContext.enter(new ThreadContext(beanContext,
null));
+                try {
+                    base.evaluate();
+                } finally {
+                    // reset for next test
+                    ThreadContext.exit(old);
+                    beanContext.setRunAs(runAs);
+                    beanContext.setRunAsUser(runAsUser);
+                }
+            }
+        };
+    }
+
+    @Target({ElementType.METHOD, ElementType.TYPE}) // cause @RunAs doesn't support method
+    @Retention(RetentionPolicy.RUNTIME)
+    public @interface As {
+        String value();
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/junit/ScopesRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/junit/ScopesRule.java
b/container/openejb-core/src/main/java/org/apache/openejb/junit/ScopesRule.java
new file mode 100644
index 0000000..406faef
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/junit/ScopesRule.java
@@ -0,0 +1,69 @@
+/*
+ * 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.openejb.junit;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.web.lifecycle.test.MockHttpSession;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import javax.enterprise.context.SessionScoped;
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ScopesRule implements TestRule {
+    @Override
+    public Statement apply(final Statement base, final Description description) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                final CdiScopes annotation = description.getAnnotation(CdiScopes.class);
+                if (annotation == null) {
+                    base.evaluate();
+                    return;
+                }
+
+                final WebBeansContext wbc = WebBeansContext.currentInstance();
+                final Map<Class<?>, Object> init = new HashMap<>();
+                Class<? extends Annotation>[] scopes = annotation.value();
+                for (final Class<? extends Annotation> c : scopes) {
+                    final Object o = param(c);
+                    if (o != null) {
+                        init.put(c, o);
+                    }
+                    wbc.getContextsService().startContext(c, o);
+                }
+                try {
+                    base.evaluate();
+                } finally {
+                    for (final Class<? extends Annotation> c : scopes) {
+                        wbc.getContextsService().endContext(c, init.get(c));
+                    }
+                }
+            }
+        };
+    }
+
+    private Object param(final Class<?> c) {
+        if (SessionScoped.class == c) {
+            return new MockHttpSession();
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/junit/TransactionRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/junit/TransactionRule.java
b/container/openejb-core/src/main/java/org/apache/openejb/junit/TransactionRule.java
new file mode 100644
index 0000000..72a6b65
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/junit/TransactionRule.java
@@ -0,0 +1,93 @@
+/*
+ * 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.openejb.junit;
+
+import org.apache.openejb.BeanContext;
+import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.core.transaction.EjbTransactionUtil;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TransactionType;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.transaction.Transactional;
+import java.lang.reflect.Method;
+
+public class TransactionRule extends BeanContextBaseRule implements TestRule {
+    private final boolean rollback;
+
+    public TransactionRule() {
+        this(null, true);
+    }
+
+    public TransactionRule(final boolean rollback) {
+        this(null, rollback);
+    }
+
+    public TransactionRule(final Object o, final boolean rollback) {
+        super(o);
+        this.rollback = rollback;
+    }
+
+    @Override
+    public Statement apply(final Statement base, final Description description) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
+                final Transactional annotation2 = description.getAnnotation(Transactional.class);
+                if (annotation == null && annotation2 == null) {
+                    base.evaluate();
+                    return;
+                }
+
+                final BeanContext beanContext = getBeanContext();
+                final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
+
+                final TransactionType transactionType = TransactionType.get(annotation ==
null ?
+                        TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
+                beanContext.getMethodContext(method)
+                    .setTransactionType(
+                            transactionType);
+
+                ThreadContext tc = ThreadContext.getThreadContext();
+                final boolean tcCreated;
+                if (tc == null) {
+                    tcCreated = true;
+                    tc = ThreadContext.enter(new ThreadContext(beanContext, null));
+                } else {
+                    tcCreated = false;
+                }
+                final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType,
tc);
+                try {
+                    base.evaluate();
+                } finally {
+                    if (rollback) {
+                        policy.setRollbackOnly();
+                    }
+                    EjbTransactionUtil.afterInvoke(policy, tc);
+                    if (tcCreated) {
+                        ThreadContext.exit(tc);
+                    }
+                }
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
index 63aaa3c..2745999 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
@@ -802,7 +802,9 @@ public final class ApplicationComposers {
 
         appContext = assembler.createApplication(appInfo);
 
-        ScopeHelper.startContexts(appContext.getWebBeansContext().getContextsService(), servletContext,
session);
+        if (mockCdiContexts()) {
+            ScopeHelper.startContexts(appContext.getWebBeansContext().getContextsService(),
servletContext, session);
+        }
 
         final BeanContext context = containerSystem.getBeanContext(testClass.getName());
 
@@ -846,6 +848,10 @@ public final class ApplicationComposers {
         testClassFinders.put(this, testClassFinder);
     }
 
+    private boolean mockCdiContexts() {
+        return "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.testing.start-cdi-contexts",
"true"));
+    }
+
     private void addWebApp(final AppModule appModule, final ManagedBean testBean,
                           final Map<String, URL> additionalDescriptors,
                           final Descriptors descriptors,
@@ -1084,10 +1090,12 @@ public final class ApplicationComposers {
 
             globalJndiEntries.clear();
 
-            try {
-                ScopeHelper.stopContexts(contextsService, servletContext, session);
-            } catch (final Exception e) {
-                // no-op
+            if (mockCdiContexts()) {
+                try {
+                    ScopeHelper.stopContexts(contextsService, servletContext, session);
+                } catch (final Exception e) {
+                    // no-op
+                }
             }
         }
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/testing/BeanContextBaseRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/BeanContextBaseRule.java
b/container/openejb-core/src/main/java/org/apache/openejb/testing/BeanContextBaseRule.java
deleted file mode 100644
index ecfa1e2..0000000
--- a/container/openejb-core/src/main/java/org/apache/openejb/testing/BeanContextBaseRule.java
+++ /dev/null
@@ -1,47 +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.openejb.testing;
-
-import org.apache.openejb.BeanContext;
-import org.apache.openejb.Injector;
-import org.apache.openejb.core.ThreadContext;
-
-public abstract class BeanContextBaseRule {
-    protected final Object instance;
-
-    protected BeanContextBaseRule(final Object instance) {
-        this.instance = instance;
-    }
-
-    protected BeanContext getBeanContext() {
-        BeanContext beanContext = null;
-        if (instance != null) {
-            beanContext = Injector.resolve(instance.getClass());
-        }
-        if (beanContext == null) {
-            final ThreadContext threadContext = ThreadContext.getThreadContext();
-            if (threadContext != null) {
-                beanContext = threadContext.getBeanContext();
-            }
-        }
-
-        if (beanContext == null) {
-            throw new IllegalStateException("Can't find test BeanContext");
-        }
-        return beanContext;
-    }
-}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java
b/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java
deleted file mode 100644
index 173f079..0000000
--- a/container/openejb-core/src/main/java/org/apache/openejb/testing/RunAsRule.java
+++ /dev/null
@@ -1,89 +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.openejb.testing;
-
-import org.apache.openejb.BeanContext;
-import org.apache.openejb.core.ThreadContext;
-import org.junit.rules.TestRule;
-import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
-
-import javax.annotation.security.RunAs;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-public class RunAsRule extends BeanContextBaseRule implements TestRule {
-    private final ThreadLocal<String> role = new ThreadLocal<>();
-
-    public RunAsRule() {
-        this(null);
-    }
-
-    public RunAsRule(final Object o) {
-        super(o);
-    }
-
-    public void role(final String role) {
-        this.role.set(role);
-    }
-
-    @Override
-    public Statement apply(final Statement base, final Description description) {
-        return new Statement() {
-            @Override
-            public void evaluate() throws Throwable {
-                final RunAs annotation = description.getAnnotation(RunAs.class);
-                final As as = description.getAnnotation(As.class);
-                String currentRole = role.get();
-                role.remove(); // no more needed
-                if (annotation == null && as == null && currentRole == null)
{
-                    base.evaluate();
-                    return;
-                }
-
-                final BeanContext beanContext = getBeanContext();
-                if (currentRole == null) {
-                    if (annotation == null) {
-                        currentRole = as.value();
-                    } else {
-                        currentRole = annotation.value();
-                    }
-                }
-                final String runAs = beanContext.getRunAs();
-                final String runAsUser = beanContext.getRunAsUser();
-                beanContext.setRunAs(currentRole);
-                final ThreadContext old = ThreadContext.enter(new ThreadContext(beanContext,
null));
-                try {
-                    base.evaluate();
-                } finally {
-                    // reset for next test
-                    ThreadContext.exit(old);
-                    beanContext.setRunAs(runAs);
-                    beanContext.setRunAsUser(runAsUser);
-                }
-            }
-        };
-    }
-
-    @Target({ElementType.METHOD, ElementType.TYPE}) // cause @RunAs doesn't support method
-    @Retention(RetentionPolicy.RUNTIME)
-    public @interface As {
-        String value();
-    }
-}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/main/java/org/apache/openejb/testing/TransactionRule.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/TransactionRule.java
b/container/openejb-core/src/main/java/org/apache/openejb/testing/TransactionRule.java
deleted file mode 100644
index 79b19a7..0000000
--- a/container/openejb-core/src/main/java/org/apache/openejb/testing/TransactionRule.java
+++ /dev/null
@@ -1,93 +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.openejb.testing;
-
-import org.apache.openejb.BeanContext;
-import org.apache.openejb.core.ThreadContext;
-import org.apache.openejb.core.transaction.EjbTransactionUtil;
-import org.apache.openejb.core.transaction.TransactionPolicy;
-import org.apache.openejb.core.transaction.TransactionType;
-import org.junit.rules.TestRule;
-import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
-
-import javax.ejb.TransactionAttribute;
-import javax.ejb.TransactionAttributeType;
-import javax.transaction.Transactional;
-import java.lang.reflect.Method;
-
-public class TransactionRule extends BeanContextBaseRule implements TestRule {
-    private final boolean rollback;
-
-    public TransactionRule() {
-        this(null, true);
-    }
-
-    public TransactionRule(final boolean rollback) {
-        this(null, rollback);
-    }
-
-    public TransactionRule(final Object o, final boolean rollback) {
-        super(o);
-        this.rollback = rollback;
-    }
-
-    @Override
-    public Statement apply(final Statement base, final Description description) {
-        return new Statement() {
-            @Override
-            public void evaluate() throws Throwable {
-                final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
-                final Transactional annotation2 = description.getAnnotation(Transactional.class);
-                if (annotation == null && annotation2 == null) {
-                    base.evaluate();
-                    return;
-                }
-
-                final BeanContext beanContext = getBeanContext();
-                final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
-
-                final TransactionType transactionType = TransactionType.get(annotation ==
null ?
-                        TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
-                beanContext.getMethodContext(method)
-                    .setTransactionType(
-                            transactionType);
-
-                ThreadContext tc = ThreadContext.getThreadContext();
-                final boolean tcCreated;
-                if (tc == null) {
-                    tcCreated = true;
-                    tc = ThreadContext.enter(new ThreadContext(beanContext, null));
-                } else {
-                    tcCreated = false;
-                }
-                final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType,
tc);
-                try {
-                    base.evaluate();
-                } finally {
-                    if (rollback) {
-                        policy.setRollbackOnly();
-                    }
-                    EjbTransactionUtil.afterInvoke(policy, tc);
-                    if (tcCreated) {
-                        ThreadContext.exit(tc);
-                    }
-                }
-            }
-        };
-    }
-}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/test/java/org/apache/openejb/junit/ScopesRuleTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/junit/ScopesRuleTest.java
b/container/openejb-core/src/test/java/org/apache/openejb/junit/ScopesRuleTest.java
new file mode 100644
index 0000000..659622a
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/junit/ScopesRuleTest.java
@@ -0,0 +1,33 @@
+package org.apache.openejb.junit;
+
+import org.apache.openejb.testing.ContainerProperties;
+import org.apache.webbeans.config.WebBeansContext;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.enterprise.context.RequestScoped;
+
+import static org.junit.Assert.assertTrue;
+
+@ContainerProperties({
+        @ContainerProperties.Property(name = "openejb.testing.start-cdi-contexts", value
= "false")
+})
+@org.apache.openejb.testing.Classes(cdi = true, innerClassesAsBean = true)
+@RunWith(ApplicationComposer.class)
+public class ScopesRuleTest {
+    @Rule
+    public final ScopesRule rule = new ScopesRule();
+
+    public static class Foo {
+        public void touch() {
+            // ok
+        }
+    }
+
+    @Test
+    @CdiScopes(RequestScoped.class)
+    public void scopeExists() {
+        assertTrue(WebBeansContext.currentInstance().getContextsService().getCurrentContext(RequestScoped.class).isActive());
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
b/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
index deb838c..8baab65 100644
--- a/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
+++ b/container/openejb-core/src/test/java/org/apache/openejb/testing/RunAsRuleTest.java
@@ -19,6 +19,7 @@ package org.apache.openejb.testing;
 import org.apache.openejb.jee.EnterpriseBean;
 import org.apache.openejb.jee.SingletonBean;
 import org.apache.openejb.junit.ApplicationComposerRule;
+import org.apache.openejb.junit.RunAsRule;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.RuleChain;

http://git-wip-us.apache.org/repos/asf/tomee/blob/fd990a26/container/openejb-core/src/test/java/org/apache/openejb/testing/TransactionRuleTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/testing/TransactionRuleTest.java
b/container/openejb-core/src/test/java/org/apache/openejb/testing/TransactionRuleTest.java
index 17ad522..177e3d4 100644
--- a/container/openejb-core/src/test/java/org/apache/openejb/testing/TransactionRuleTest.java
+++ b/container/openejb-core/src/test/java/org/apache/openejb/testing/TransactionRuleTest.java
@@ -19,6 +19,7 @@ package org.apache.openejb.testing;
 import org.apache.openejb.OpenEJB;
 import org.apache.openejb.jee.EjbJar;
 import org.apache.openejb.junit.ApplicationComposerRule;
+import org.apache.openejb.junit.TransactionRule;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.RuleChain;


Mime
View raw message