001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with 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,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.annotations;
020:
021: import java.util.List;
022:
023: import javax.persistence.*;
024: import junit.framework.*;
025:
026: import org.apache.openjpa.persistence.common.utils.*;
027:
028: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
029: import org.apache.openjpa.persistence.OpenJPAEntityManager;
030:
031: public class TestEntityListenerAnnot extends AnnotationTestCase {
032:
033: public TestEntityListenerAnnot(String name) {
034: super (name, "annotationcactusapp");
035: }
036:
037: public void setUp() {
038: deleteAll(Employee.class);
039: deleteAll(ContractEmployee.class);
040: CallbackStorage.clearStore();
041: }
042:
043: public void testPrePersist() {
044: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
045:
046: ContractEmployee cemp = new ContractEmployee(21, "afam", 25, 5);
047:
048: em.persist(cemp);
049: CallbackStorage store = CallbackStorage.getInstance();
050:
051: assertNotNull(store.getClist());
052: assertEquals("@pre/post persist callback is over/under-firing",
053: 2, store.getClist().size());
054: assertEquals("longnamevalidatorprr", store.getClist().get(0));
055: assertEquals("contractemployee", store.getClist().get(1));
056:
057: endEm(em);
058: }
059:
060: public void testPostPersist() {
061: OpenJPAEntityManager em = null;
062: try {
063: em = (OpenJPAEntityManager) currentEntityManager();
064: startTx(em);
065:
066: Employee emp = new Employee(1, "john");
067:
068: em.persist(emp);
069: CallbackStorage store = CallbackStorage.getInstance();
070:
071: assertNotNull(store.getClist());
072: assertEquals(
073: "@pre/post persist callback is over/under-firing",
074: 4, store.getClist().size());
075: assertEquals("namevalidator", store.getClist().get(0));
076: assertEquals("longnamevalidatorprr", store.getClist()
077: .get(1));
078: assertEquals("employeepop", store.getClist().get(2));
079: assertEquals("We expected 'gen#" + emp.getCheck() + " : "
080: + emp.getCheck() + "'. However, we got '"
081: + store.getClist().get(3) + "'", "gen#"
082: + emp.getCheck(), store.getClist().get(3));
083: } finally {
084: endTx(em);
085: endEm(em);
086: }
087: }
088:
089: public void testPre_PostRemove() {
090: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
091: startTx(em);
092:
093: Employee emp = new Employee(8, "Jonathan");
094: em.persist(emp);
095:
096: endTx(em);
097: endEm(em);
098: //--------------------------------------------------------------
099: em = (OpenJPAEntityManager) currentEntityManager();
100: startTx(em);
101:
102: emp = em.find(Employee.class, 8);
103:
104: CallbackStorage.clearStore(); //clear the store
105: assertTrue(CallbackStorage.isEmpty());
106:
107: em.remove(emp);
108:
109: assertTrue(!CallbackStorage.isEmpty());
110: assertEquals("callback is under/over-firing...", 2,
111: CallbackStorage.size());
112: assertEquals("namevalidatorprr", CallbackStorage.getInstance()
113: .getClist().get(0));
114: assertEquals("namevalidatorpor", CallbackStorage.getInstance()
115: .getClist().get(1));
116:
117: endTx(em);
118: endEm(em);
119: }
120:
121: public void testPreUpdate() {
122: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
123: startTx(em);
124:
125: Employee emp = new Employee(5, "Abraham");
126: em.persist(emp);
127:
128: CallbackStorage.clearStore();
129:
130: String query = "Update Employee e SET e.name = 'Joseph' WHERE e.id = :id";
131:
132: int result = em.createQuery(query).setParameter("id", 5)
133: .executeUpdate();
134:
135: List store = CallbackStorage.getInstance().getClist();
136:
137: assertNotNull(result);
138: assertEquals(1, result);
139: assertNotNull(store);
140: assertEquals(3, store.size());
141: assertEquals("namevalidatorpou", store.get(0));
142: assertEquals("longnamevalidatorpou", store.get(1));
143: assertEquals("employeepou", store.get(2));
144:
145: endTx(em);
146: endEm(em);
147: }
148:
149: public void testPreUpdate2() {
150: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
151: startTx(em);
152:
153: Employee emp = new Employee(5, "Abraham");
154: em.persist(emp);
155:
156: CallbackStorage.clearStore();
157: endTx(em);
158:
159: startTx(em);
160: emp = em.find(Employee.class, 5);
161:
162: CallbackStorage.clearStore();
163: assertEquals("Abraham", emp.getName());
164:
165: emp.setName("Abrahamovich");
166: em.flush();
167:
168: List store = CallbackStorage.getInstance().getClist();
169:
170: assertNotNull(store);
171: assertEquals(
172: "update callback is either underfiring or overfiring...",
173: 3, store.size());
174: assertEquals("namevalidatorpou", store.get(0));
175: assertEquals("longnamevalidatorpou", store.get(1));
176: assertEquals("employeepou", store.get(2));
177:
178: endTx(em);
179: endEm(em);
180: }
181:
182: public void testPostLoad() {
183: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
184: startTx(em);
185:
186: Employee emp = new Employee(6, "Jefferson");
187:
188: em.persist(emp);
189: CallbackStorage.clearStore();
190:
191: endTx(em);
192:
193: startTx(em);
194: CallbackStorage.clearStore();
195:
196: assertTrue(CallbackStorage.getInstance().getClist().isEmpty());
197:
198: emp = em.find(Employee.class, 6);
199: em.refresh(emp);
200:
201: assertNotNull(emp);
202: assertNotNull(CallbackStorage.getInstance().getClist());
203: assertEquals("PostLoad is overfiring...not accurate", 2,
204: CallbackStorage.getInstance().getClist().size());
205: assertEquals("employeepol", CallbackStorage.getInstance()
206: .getClist().get(0));
207: assertEquals("employeepol", CallbackStorage.getInstance()
208: .getClist().get(1));
209:
210: endTx(em);
211: endEm(em);
212: }
213:
214: public void testGenPriKeyAvailInPostPersist() {
215: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
216: startTx(em);
217:
218: assertNotNull(em);
219:
220: Employee emp = new Employee(7, "Maxwell");
221:
222: assertEquals(0, emp.getCheck());
223:
224: em.persist(emp);
225: int check = emp.getCheck();
226:
227: assertNotNull(check);
228: assertTrue(CallbackStorage.getInstance().getClist().contains(
229: "gen#" + check));
230:
231: endTx(em);
232: endEm(em);
233: }
234: /*Fix Me: aokeke - should fail when persisting with invalid id*/
235: // public void testExceptionCauseTxRollback2()
236: // {
237: // OpenJPAEntityManager em =(OpenJPAEntityManager) currentEntityManager();
238: // startTx(em);
239: //
240: // Employee emp = new Employee(-1, "failure");
241: //
242: // try
243: // {
244: // //persisting an entity with an invalid id throws an exception
245: // em.persist(emp);
246: // endTx(em);
247: // fail("Should have failed..persisting an entity with invalid id");
248: // }
249: // catch(RuntimeException e)
250: // {
251: // assertFalse(em.isPersistent(emp));
252: // assertTrue("transaction was not marked for rollback", em.getRollbackOnly());
253: // e.printStackTrace();
254: // if(em.getRollbackOnly() == true)
255: // endEm(em);
256: // }
257: // catch(Exception e)
258: // {
259: // assertFalse(em.isPersistent(emp));
260: // assertTrue("transaction was not marked for rollback", em.getRollbackOnly());
261: // e.printStackTrace();
262: // if(em.getRollbackOnly() == true)
263: // endEm(em);
264: // }
265: //
266: // if(em.isActive())
267: // endEm(em);
268: // }
269: }
|