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 2015/11/19 17:07:04 UTC
tomee git commit: OPENEJB-2122 ensuring @Tx behavior
Repository: tomee
Updated Branches:
refs/heads/master 186404f67 -> 1b6536fec
OPENEJB-2122 ensuring @Tx behavior
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/1b6536fe
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/1b6536fe
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/1b6536fe
Branch: refs/heads/master
Commit: 1b6536fecacbf8a16a9ed876820eb0cf816e99c7
Parents: 186404f
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Thu Nov 19 08:06:57 2015 -0800
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Thu Nov 19 08:06:57 2015 -0800
----------------------------------------------------------------------
.../TransactionalOPENEJB2122Test.java | 102 +++++++++++++++++++
1 file changed, 102 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/1b6536fe/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalOPENEJB2122Test.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalOPENEJB2122Test.java b/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalOPENEJB2122Test.java
new file mode 100644
index 0000000..70a9fd5
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalOPENEJB2122Test.java
@@ -0,0 +1,102 @@
+/*
+ * 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.cdi.transactional;
+
+import org.apache.openejb.api.configuration.PersistenceUnitDefinition;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.SimpleLog;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.SessionContext;
+import javax.ejb.Stateless;
+import javax.inject.Inject;
+import javax.persistence.Entity;
+import javax.persistence.EntityManager;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.PersistenceContext;
+import javax.transaction.Transactional;
+
+import static org.junit.Assert.assertEquals;
+
+@SimpleLog
+@Classes(cdi = true, innerClassesAsBean = true)
+@RunWith(ApplicationComposer.class)
+@PersistenceUnitDefinition
+public class TransactionalOPENEJB2122Test {
+ @Inject
+ private MyTestRessource bean;
+
+ @Test
+ public void run() {
+ assertEquals("execution finished", bean.simpleTestMethod());
+ }
+
+ @Stateless
+ public static class MyTestRessource {
+ @Inject
+ private MyTestBean myTestBean;
+
+ @PersistenceContext
+ private EntityManager em;
+
+ @Transactional(Transactional.TxType.NOT_SUPPORTED)
+ public String simpleTestMethod() {
+ myTestBean.persistAnEntity();
+ assertEquals(0, em.createQuery("select count(e) from TransactionalOPENEJB2122Test$MyEntity e", Number.class).getSingleResult().intValue());
+ myTestBean.doOnFail();
+ assertEquals(1, em.createQuery("select count(e) from TransactionalOPENEJB2122Test$MyEntity e", Number.class).getSingleResult().intValue());
+ return "execution finished";
+ }
+ }
+
+ @Stateless
+ public static class MyTestBean {
+ @PersistenceContext
+ private EntityManager em;
+
+ @Resource
+ private SessionContext sessionContext;
+
+ @Transactional(Transactional.TxType.REQUIRED)
+ public void persistAnEntity() {
+ final MyEntity e = new MyEntity();
+ em.persist(e);
+ sessionContext.setRollbackOnly();
+ }
+
+ @Transactional(Transactional.TxType.REQUIRES_NEW)
+ public void doOnFail() {
+ final MyEntity e = new MyEntity();
+ em.persist(e);
+ }
+ }
+
+ @Entity
+ public static class MyEntity {
+ @Id
+ @GeneratedValue
+ private long id;
+
+ public long getId() {
+ return id;
+ }
+ }
+}