Updated Branches: refs/heads/master 364b84a83 -> 2bfcd3130
DELTASPIKE-513 Transactional interceptor throws NPE for missing EntityManager Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/2bfcd313 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/2bfcd313 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/2bfcd313 Branch: refs/heads/master Commit: 2bfcd31301eb5619cb9bd6e3a40ddbc5359800b1 Parents: 364b84a Author: Christian Kaltepoth <[email protected]> Authored: Mon Jan 27 07:41:21 2014 +0100 Committer: Christian Kaltepoth <[email protected]> Committed: Mon Jan 27 07:41:21 2014 +0100 ---------------------------------------------------------------------- .../ResourceLocalTransactionStrategy.java | 4 +- .../NoEntityManagerProducerTest.java | 92 ++++++++++++++++++++ .../TestEntityManagerProducer.java | 44 ++++++++++ .../noentitymanager/TransactionalBean.java | 41 +++++++++ 4 files changed, 180 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2bfcd313/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java index b7bd1fa..7f0f118 100644 --- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java +++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/ResourceLocalTransactionStrategy.java @@ -32,6 +32,7 @@ import javax.inject.Inject; import javax.interceptor.InvocationContext; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; + import java.lang.annotation.Annotation; import java.util.HashSet; import java.util.Set; @@ -307,7 +308,8 @@ public class ResourceLocalTransactionStrategy implements TransactionStrategy if (entityManagerBean == null) { - return null; + throw new IllegalStateException("Cannot find an EntityManager qualified with [" + emQualifier.getName() + + "]. Did you add a corresponding producer?"); } return (EntityManager) beanManager.getReference(entityManagerBean, EntityManager.class, http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2bfcd313/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java new file mode 100644 index 0000000..1540d06 --- /dev/null +++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/NoEntityManagerProducerTest.java @@ -0,0 +1,92 @@ +/* + * 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.noentitymanager; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import javax.inject.Inject; + +import org.apache.deltaspike.core.api.projectstage.ProjectStage; +import org.apache.deltaspike.core.util.ProjectStageProducer; +import org.apache.deltaspike.test.category.SeCategory; +import org.apache.deltaspike.test.jpa.api.shared.Second; +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.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +@RunWith(Arquillian.class) +@Category(SeCategory.class) +public class NoEntityManagerProducerTest +{ + + @Inject + private TransactionalBean transactionalBean; + + @Deployment + public static WebArchive deploy() + { + JavaArchive testJar = ShrinkWrap.create(JavaArchive.class, "noEntityManagerProducer.jar") + .addPackage(ArchiveUtils.SHARED_PACKAGE) + .addPackage(NoEntityManagerProducerTest.class.getPackage().getName()) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + + return ShrinkWrap.create(WebArchive.class) + .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreAndJpaArchive()) + .addAsLibraries(testJar) + .addAsWebInfResource(ArchiveUtils.getBeansXml(), "beans.xml"); + } + + @Before + public void init() + { + ProjectStageProducer.setProjectStage(ProjectStage.UnitTest); + } + + @Test + public void shouldFailIfNoEntityManager() + { + try + { + transactionalBean.executeInTransaction(); + fail("This should fail as there is no EntityManager"); + } + catch (Exception e) + { + /* + * The exception should tell the user that there is no EntityManager with the requested qualifier. The + * following asserts just check for the important keywords in the message of the exception. Not a very nice + * way to test this behavior but it will be sufficient to ensure the user gets all the information required + * to fix the problem. + */ + assertTrue(e.getMessage().contains("EntityManager")); + assertTrue(e.getMessage().contains(Second.class.getName())); + } + + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2bfcd313/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/TestEntityManagerProducer.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/TestEntityManagerProducer.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/TestEntityManagerProducer.java new file mode 100644 index 0000000..74ffb7e --- /dev/null +++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/TestEntityManagerProducer.java @@ -0,0 +1,44 @@ +/* + * 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.noentitymanager; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Produces; +import javax.persistence.EntityManager; + +import org.apache.deltaspike.test.jpa.api.shared.First; +import org.apache.deltaspike.test.jpa.api.shared.TestEntityManager; + +@RequestScoped +public class TestEntityManagerProducer +{ + + private TestEntityManager entityManager = new TestEntityManager(); + + /** + * Producer method for an {@link EntityManager} qualified with {@link First}. + */ + @Produces + @First + protected EntityManager entityManager() + { + return entityManager; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2bfcd313/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/TransactionalBean.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/TransactionalBean.java b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/TransactionalBean.java new file mode 100644 index 0000000..78961aa --- /dev/null +++ b/deltaspike/modules/jpa/impl/src/test/java/org/apache/deltaspike/test/jpa/api/transactional/noentitymanager/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.noentitymanager; + +import javax.enterprise.context.ApplicationScoped; +import javax.persistence.EntityManager; + +import org.apache.deltaspike.jpa.api.transaction.Transactional; +import org.apache.deltaspike.test.jpa.api.shared.Second; + +@ApplicationScoped +public class TransactionalBean +{ + + /** + * This methods requests a transaction for the EntityManager qualified with {@link Second} although there is no + * producer for such an {@link EntityManager}. + */ + @Transactional(qualifier = Second.class) + public void executeInTransaction() + { + // no need to do anything + } + +}
