You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2013/03/13 04:43:17 UTC

git commit: DELTASPIKE-320 improved default handling

Updated Branches:
  refs/heads/master 4a8ab4863 -> d860a471e


DELTASPIKE-320 improved default handling


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

Branch: refs/heads/master
Commit: d860a471e7ebbfbfd89815dd26ccb96944d61dd3
Parents: 4a8ab48
Author: gpetracek <gp...@apache.org>
Authored: Wed Mar 13 04:31:59 2013 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Wed Mar 13 04:42:46 2013 +0100

----------------------------------------------------------------------
 .../jpa/api/transaction/Transactional.java         |   12 ++-
 .../transaction/TransactionStrategyHelper.java     |    7 +
 ...ggregatedDefaultEntityManagerInjectionTest.java |  101 +++++++++++++++
 .../jpa/api/transactional/aggregation/BeanA.java   |   34 +++++
 .../jpa/api/transactional/aggregation/BeanB.java   |   34 +++++
 .../aggregation/TestEntityManagerProducer.java     |   42 ++++++
 .../aggregation/TransactionalBean.java             |   41 ++++++
 7 files changed, 269 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d860a471/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
index 03ae24d..e993d16 100644
--- a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
+++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/api/transaction/Transactional.java
@@ -21,13 +21,13 @@ package org.apache.deltaspike.jpa.api.transaction;
 import javax.enterprise.inject.Any;
 import javax.enterprise.util.Nonbinding;
 import javax.interceptor.InterceptorBinding;
+import java.lang.annotation.Annotation;
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Inherited;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
-import java.lang.annotation.Annotation;
 
 /**
  * If it isn't possible to use EJBs, this interceptor adds transaction support to methods or a class.
@@ -49,7 +49,15 @@ public @interface Transactional
      * for the injected {@link javax.persistence.EntityManager}s.
      * Default-value is {@link Any} which means any injected {@link javax.persistence.EntityManager}s
      * should be detected automatically and transactions for all injected {@link javax.persistence.EntityManager}s
-     * will be started
+     * will be started. Or the {@link javax.enterprise.inject.Default} {@link javax.persistence.EntityManager}
+     * will be used, if no qualifier and no {@link javax.persistence.EntityManager} was found (in the annotated class)
+     * (see DELTASPIKE-320).
+     *
+     * This qualifier can also be used for integrating other frameworks,
+     * which follow a different style (see DELTASPIKE-319) as well as the usage of
+     * {@link javax.persistence.EntityManager}s with qualifiers in a called method (of a different bean)
+     * which isn't {@link Transactional} itself.
+     *
      * @return target persistence-unit identifier
      */
     @Nonbinding Class<? extends Annotation>[] qualifier() default Any.class;

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d860a471/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
index bea8322..3bf92d1 100644
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
@@ -41,6 +41,8 @@ import java.util.Set;
 @Dependent
 public class TransactionStrategyHelper implements Serializable
 {
+    private static final long serialVersionUID = -6272327391611428125L;
+
     @Inject
     private BeanManager beanManager;
 
@@ -80,6 +82,11 @@ public class TransactionStrategyHelper implements Serializable
             Collections.addAll(emQualifiers, qualifierClasses);
         }
 
+        //see DELTASPIKE-320
+        if (emQualifiers.isEmpty())
+        {
+            emQualifiers.add(Default.class);
+        }
         return emQualifiers;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d860a471/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/AggregatedDefaultEntityManagerInjectionTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/AggregatedDefaultEntityManagerInjectionTest.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/AggregatedDefaultEntityManagerInjectionTest.java
new file mode 100644
index 0000000..064f6ef
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/AggregatedDefaultEntityManagerInjectionTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.jpa.api.transactional.aggregation;
+
+import org.apache.deltaspike.core.api.projectstage.ProjectStage;
+import org.apache.deltaspike.core.util.ProjectStageProducer;
+import org.apache.deltaspike.jpa.impl.transaction.context.TransactionBeanStorage;
+import org.apache.deltaspike.jpa.impl.transaction.context.TransactionContextExtension;
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.jpa.api.shared.TestEntityManager;
+import org.apache.deltaspike.test.jpa.api.shared.TestEntityTransaction;
+import org.apache.deltaspike.test.util.ArchiveUtils;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import javax.enterprise.inject.spi.Extension;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+
+@RunWith(Arquillian.class)
+@Category(SeCategory.class)
+public class AggregatedDefaultEntityManagerInjectionTest
+{
+    @Inject
+    private TransactionalBean transactionalBean;
+
+    @Inject
+    private TestEntityManagerProducer entityManagerProducer;
+
+    @Deployment
+    public static WebArchive deploy()
+    {
+        JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "aggregatedDefaultInjectionTest.jar")
+                .addPackage(ArchiveUtils.SHARED_PACKAGE)
+                .addPackage(AggregatedDefaultEntityManagerInjectionTest.class.getPackage().getName())
+                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+
+        return ShrinkWrap.create(WebArchive.class)
+                .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndJpaArchive())
+                .addAsLibraries(testJar)
+                .addAsServiceProvider(Extension.class, TransactionContextExtension.class)
+                .addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml");
+    }
+
+    @Before
+    public void init()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.UnitTest);
+    }
+
+    @Test
+    public void defaultEntityManagerInjection()
+    {
+        EntityManager injectedEntityManager = entityManagerProducer.getDefaultEntityManager();
+
+        Assert.assertNotNull(injectedEntityManager);
+        Assert.assertTrue(injectedEntityManager instanceof TestEntityManager);
+        TestEntityTransaction testTransaction = (TestEntityTransaction) (injectedEntityManager).getTransaction();
+
+        Assert.assertEquals(false, ((TestEntityManager) injectedEntityManager).isFlushed());
+        Assert.assertEquals(false, testTransaction.isActive());
+        Assert.assertEquals(false, testTransaction.isStarted());
+        Assert.assertEquals(false, testTransaction.isCommitted());
+        Assert.assertEquals(false, testTransaction.isRolledBack());
+
+        transactionalBean.executeInTransaction();
+
+        Assert.assertEquals(true, ((TestEntityManager) injectedEntityManager).isFlushed());
+        Assert.assertEquals(false, testTransaction.isActive());
+        Assert.assertEquals(true, testTransaction.isStarted());
+        Assert.assertEquals(true, testTransaction.isCommitted());
+        Assert.assertEquals(false, testTransaction.isRolledBack());
+
+        Assert.assertEquals(false, TransactionBeanStorage.isOpen());
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d860a471/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanA.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanA.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanA.java
new file mode 100644
index 0000000..d8764a0
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanA.java
@@ -0,0 +1,34 @@
+/*
+ * 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.jpa.api.transactional.aggregation;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+
+@ApplicationScoped
+public class BeanA
+{
+    @Inject
+    private EntityManager entityManager;
+
+    public void doA()
+    {
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d860a471/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanB.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanB.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanB.java
new file mode 100644
index 0000000..eb1ea55
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/BeanB.java
@@ -0,0 +1,34 @@
+/*
+ * 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.jpa.api.transactional.aggregation;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+
+@ApplicationScoped
+public class BeanB
+{
+    @Inject
+    private EntityManager entityManager;
+
+    public void doB()
+    {
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d860a471/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TestEntityManagerProducer.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TestEntityManagerProducer.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TestEntityManagerProducer.java
new file mode 100644
index 0000000..ae28269
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TestEntityManagerProducer.java
@@ -0,0 +1,42 @@
+/*
+ * 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.jpa.api.transactional.aggregation;
+
+import org.apache.deltaspike.test.jpa.api.shared.TestEntityManager;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.Produces;
+import javax.persistence.EntityManager;
+
+@RequestScoped
+public class TestEntityManagerProducer
+{
+    private TestEntityManager defaultEntityManager = new TestEntityManager();
+
+    @Produces
+    protected EntityManager defaultEntityManager()
+    {
+        return defaultEntityManager;
+    }
+
+    public TestEntityManager getDefaultEntityManager()
+    {
+        return defaultEntityManager;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/d860a471/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TransactionalBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TransactionalBean.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TransactionalBean.java
new file mode 100644
index 0000000..fadf08a
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/aggregation/TransactionalBean.java
@@ -0,0 +1,41 @@
+/*
+ * 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.jpa.api.transactional.aggregation;
+
+import org.apache.deltaspike.jpa.api.transaction.Transactional;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+
+@ApplicationScoped
+public class TransactionalBean
+{
+    @Inject
+    private BeanA beanA;
+
+    @Inject
+    private BeanB beanB;
+
+    @Transactional
+    public void executeInTransaction()
+    {
+        beanA.doA();
+        beanB.doB();
+    }
+}