001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.core.cmp.jpa;
018:
019: import junit.framework.Assert;
020:
021: import javax.persistence.EntityManager;
022: import javax.persistence.EntityManagerFactory;
023: import javax.persistence.EntityTransaction;
024: import javax.transaction.Status;
025: import javax.transaction.TransactionManager;
026: import java.util.Collection;
027:
028: public class UnenhancedUnits extends Assert {
029: private EntityManagerFactory entityManagerFactory;
030: private TransactionManager transactionManager;
031:
032: private EntityManager entityManager;
033: private EntityTransaction transaction;
034:
035: public void setTransactionManager(
036: TransactionManager transactionManager) {
037: this .transactionManager = transactionManager;
038: }
039:
040: public void setEntityManagerFactory(
041: EntityManagerFactory entityManagerFactory) {
042: this .entityManagerFactory = entityManagerFactory;
043: }
044:
045: public void setUp() throws Exception {
046: }
047:
048: public void tearDown() throws Exception {
049: if (entityManager != null && entityManager.isOpen()) {
050: if (transaction != null) {
051: try {
052: if (transaction.getRollbackOnly()) {
053: transaction.rollback();
054: } else {
055: transaction.commit();
056: }
057: } catch (Exception e) {
058: e.printStackTrace();
059: }
060: } else {
061: try {
062: if (transactionManager.getStatus() != Status.STATUS_NO_TRANSACTION) {
063: transactionManager.rollback();
064: }
065: } catch (Exception e) {
066: e.printStackTrace();
067: }
068: }
069: entityManager.close();
070: }
071:
072: entityManager = null;
073: entityManagerFactory = null;
074: transactionManager = null;
075: }
076:
077: public void complexId() throws Exception {
078: beginTx();
079:
080: // constructor
081: ComplexStandalone complex = new ComplexStandalone("first",
082: "second");
083:
084: // em should not know about our entity
085: assertFalse(entityManager.contains(complex));
086:
087: // persist the entity
088: entityManager.persist(complex);
089:
090: // em should now be aware of our entity
091: assertTrue(entityManager.contains(complex));
092:
093: commitTx();
094: }
095:
096: public void complexIdSubclass() throws Exception {
097: beginTx();
098:
099: // create entity
100: ComplexSuperclass complex = new ComplexSubclass();
101: complex.firstId = "first";
102: complex.secondId = "second";
103:
104: // em should not know about our entity
105: assertFalse(entityManager.contains(complex));
106:
107: // persist the entity
108: entityManager.persist(complex);
109:
110: // em should now be aware of our entity
111: assertTrue(entityManager.contains(complex));
112:
113: commitTx();
114: }
115:
116: public void generatedId() throws Exception {
117: beginTx();
118:
119: // constructor
120: GeneratedStandalone generated = new GeneratedStandalone();
121:
122: // entity should not have an id yet
123: assertNull("generated.getId() is not null", generated.getId());
124:
125: // em should not know about our entity
126: assertFalse(entityManager.contains(generated));
127:
128: // persist the entity
129: entityManager.persist(generated);
130: entityManager.flush();
131:
132: // entity should now have an id
133: assertNotNull("generated.getId() is null", generated.getId());
134:
135: // em should now be aware of our entity
136: assertTrue(entityManager.contains(generated));
137:
138: commitTx();
139: }
140:
141: public void generatedIdSubclass() throws Exception {
142: beginTx();
143:
144: // constructor
145: GeneratedSuperclass generated = new GeneratedSubclass();
146:
147: // entity should not have an id yet
148: assertNull("generated.getId() is not null", generated.getId());
149:
150: // em should not know about our entity
151: assertFalse(entityManager.contains(generated));
152:
153: // persist the entity
154: entityManager.persist(generated);
155: entityManager.flush();
156:
157: // entity should now have an id
158: assertNotNull("generated.getId() is null", generated.getId());
159:
160: // em should now be aware of our entity
161: assertTrue(entityManager.contains(generated));
162:
163: commitTx();
164: }
165:
166: public void collection() throws Exception {
167: beginTx();
168:
169: OneStandalone one = new OneStandalone(1000);
170:
171: ManyStandalone manyA = new ManyStandalone(1);
172: one.getMany().add(manyA);
173: manyA.setOne(one);
174:
175: ManyStandalone manyB = new ManyStandalone(2);
176: one.getMany().add(manyB);
177: manyB.setOne(one);
178:
179: ManyStandalone manyC = new ManyStandalone(3);
180: one.getMany().add(manyC);
181: manyC.setOne(one);
182:
183: // em should not know about our entities
184: assertFalse(entityManager.contains(one));
185: assertFalse(entityManager.contains(manyA));
186: assertFalse(entityManager.contains(manyB));
187: assertFalse(entityManager.contains(manyC));
188:
189: // persist the entity
190: entityManager.persist(one);
191: entityManager.persist(manyA);
192: entityManager.persist(manyB);
193: entityManager.persist(manyC);
194: entityManager.flush();
195:
196: // em should now be aware of our entity
197: assertTrue(entityManager.contains(one));
198: assertTrue(entityManager.contains(manyA));
199: assertTrue(entityManager.contains(manyB));
200: assertTrue(entityManager.contains(manyC));
201:
202: commitTx();
203:
204: one = null;
205: manyA = null;
206: manyB = null;
207: manyC = null;
208:
209: beginTx();
210:
211: // reload one
212: one = entityManager.find(OneStandalone.class, 1000);
213: assertNotNull("one is null", one);
214:
215: // verify one.getMany()
216: assertNotNull("one.getMany() is null", one.getMany());
217: Collection<ManyStandalone> many = one.getMany();
218: assertEquals(3, many.size());
219:
220: // reload the many
221: manyA = entityManager.find(ManyStandalone.class, 1);
222: assertNotNull("manyA is null", manyA);
223: manyB = entityManager.find(ManyStandalone.class, 2);
224: assertNotNull("manyB is null", manyA);
225: manyC = entityManager.find(ManyStandalone.class, 3);
226: assertNotNull("manyc is null", manyA);
227:
228: // verify many.getOne()
229: assertNotNull("manyA.getOne() is null", manyA.getOne());
230: assertEquals(one, manyA.getOne());
231: assertNotNull("manyB.getOne() is null", manyB.getOne());
232: assertEquals(one, manyB.getOne());
233: assertNotNull("manyC.getOne() is null", manyC.getOne());
234: assertEquals(one, manyC.getOne());
235:
236: // verify collection contains each many
237: assertTrue(many.contains(manyA));
238: assertTrue(many.contains(manyB));
239: assertTrue(many.contains(manyC));
240:
241: commitTx();
242: }
243:
244: private void beginTx() throws Exception {
245: entityManager = entityManagerFactory.createEntityManager();
246:
247: try {
248: transaction = entityManager.getTransaction();
249: } catch (Exception e) {
250: // must be JTA
251: }
252:
253: log("BEGIN_TX");
254: if (transaction == null) {
255: transactionManager.begin();
256: entityManager.joinTransaction();
257: } else {
258: transaction.begin();
259: }
260: }
261:
262: private void commitTx() throws Exception {
263: log(" BEFORE_COMMIT_TX");
264: try {
265: if (transaction == null) {
266: transactionManager.commit();
267: } else {
268: transaction.commit();
269: }
270: } finally {
271: if (entityManager != null && entityManager.isOpen()) {
272: entityManager.close();
273: }
274: entityManager = null;
275: log("AFTER_COMMIT_TX");
276: }
277: }
278:
279: public void log(String msg) {
280: // System.out.println(msg);
281: }
282: }
|