You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/10/31 21:14:12 UTC

git commit: TOMEE-1437 TransactionRule

Repository: tomee
Updated Branches:
  refs/heads/develop 5cede8c77 -> c130214b9


TOMEE-1437 TransactionRule


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

Branch: refs/heads/develop
Commit: c130214b9c5e55112a33191e2f71080a8e6ffaa2
Parents: 5cede8c
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Fri Oct 31 21:13:02 2014 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Fri Oct 31 21:13:02 2014 +0100

----------------------------------------------------------------------
 .../main/java/org/apache/openejb/Injector.java  |  2 +-
 .../openejb/testing/BeanContextBaseRule.java    | 47 ++++++++++
 .../org/apache/openejb/testing/RunAsRule.java   | 25 +++--
 .../apache/openejb/testing/TransactionRule.java | 96 ++++++++++++++++++++
 .../openejb/testing/TransactionRuleTest.java    | 56 ++++++++++++
 5 files changed, 216 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/c130214b/container/openejb-core/src/main/java/org/apache/openejb/Injector.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/Injector.java b/container/openejb-core/src/main/java/org/apache/openejb/Injector.java
index 3d8b6bb..b00e99e 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/Injector.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/Injector.java
@@ -91,7 +91,7 @@ public class Injector {
         return false;
     }
 
-    private static BeanContext resolve(Class<?> clazz) {
+    public static BeanContext resolve(Class<?> clazz) {
 
         final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
 

http://git-wip-us.apache.org/repos/asf/tomee/blob/c130214b/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
new file mode 100644
index 0000000..ecfa1e2
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/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.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/c130214b/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
index ce86b81..173f079 100644
--- 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
@@ -28,9 +28,17 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
-public class RunAsRule implements TestRule {
+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);
     }
@@ -43,18 +51,13 @@ public class RunAsRule implements TestRule {
                 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) {
-                    role.remove();
                     base.evaluate();
                     return;
                 }
 
-                final ThreadContext threadContext = ThreadContext.getThreadContext();
-                if (threadContext == null) {
-                    throw new IllegalStateException("No context arounding RunAs rule, start ApplicationComposerRule before please");
-                }
-
-                final BeanContext beanContext = threadContext.getBeanContext();
+                final BeanContext beanContext = getBeanContext();
                 if (currentRole == null) {
                     if (annotation == null) {
                         currentRole = as.value();
@@ -62,13 +65,17 @@ public class RunAsRule implements TestRule {
                         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 {
-                    role.remove(); // reset for next test
+                    // reset for next test
                     ThreadContext.exit(old);
+                    beanContext.setRunAs(runAs);
+                    beanContext.setRunAsUser(runAsUser);
                 }
             }
         };

http://git-wip-us.apache.org/repos/asf/tomee/blob/c130214b/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
new file mode 100644
index 0000000..5cfb346
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/TransactionRule.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.openejb.testing;
+
+import org.apache.openejb.BeanContext;
+import org.apache.openejb.OpenEJB;
+import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.core.transaction.EjbTransactionUtil;
+import org.apache.openejb.core.transaction.JtaTransactionPolicyFactory;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TransactionPolicyFactory;
+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/c130214b/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
new file mode 100644
index 0000000..17ad522
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/testing/TransactionRuleTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.OpenEJB;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.junit.ApplicationComposerRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.naming.NamingException;
+import javax.transaction.SystemException;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class TransactionRuleTest {
+    @Rule
+    public final TestRule container = RuleChain
+            .outerRule(new ApplicationComposerRule(this))
+            .around(new TransactionRule());
+
+    @Module
+    public EjbJar jar() {
+        return new EjbJar();
+    }
+
+    @Test
+    public void no() throws NamingException, SystemException {
+        assertNull(OpenEJB.getTransactionManager().getTransaction());
+    }
+
+    @Test
+    @TransactionAttribute(TransactionAttributeType.REQUIRED)
+    public void yes() throws NamingException, SystemException {
+        assertNotNull(OpenEJB.getTransactionManager().getTransaction());
+    }
+}