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 junit.framework.*;
024: import javax.persistence.*;
025:
026: import org.apache.openjpa.persistence.common.utils.*;
027: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.ddtype.*;
028: import org.apache.openjpa.persistence.OpenJPAEntityManager;
029:
030: public class TestDDCallbackMethods extends AnnotationTestCase {
031:
032: public TestDDCallbackMethods(String name) {
033: super (name, "ddcactusapp");
034: }
035:
036: public void setUp() {
037: deleteAll(LifeCycleDDEntity.class);
038: deleteAll(LifeCycleDDEntity2.class);
039: CallbackStorage.clearStore();
040: }
041:
042: public void testDDPrpPop() {
043: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
044: startTx(em);
045:
046: LifeCycleDDEntity lcd = new LifeCycleDDEntity("afam", "okeke");
047:
048: em.persist(lcd);
049: CallbackStorage store = CallbackStorage.getInstance();
050:
051: assertNotNull(store.getClist());
052: assertEquals(2, store.getClist().size());
053: assertEquals("def-prepersist", store.getClist().get(0));
054: assertEquals("def-postpersist", store.getClist().get(1));
055:
056: endTx(em);
057: endEm(em);
058: }
059:
060: public void testDDPrrPor() {
061: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
062: startTx(em);
063:
064: LifeCycleDDEntity lcd = new LifeCycleDDEntity("john", "rash");
065:
066: em.persist(lcd);
067: int id = lcd.getId();
068:
069: lcd = em.find(LifeCycleDDEntity.class, id);
070:
071: assertNotNull(lcd);
072: CallbackStorage store = CallbackStorage.getInstance();
073: store.clearStore();
074:
075: em.remove(lcd);
076:
077: assertNotNull(store.getClist());
078: assertEquals(2, store.getClist().size());
079: assertEquals("def-preremove", store.getClist().get(0));
080: assertEquals("def-postremove", store.getClist().get(1));
081:
082: endTx(em);
083: endEm(em);
084: }
085:
086: public void testDDPouPru() {
087: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
088: startTx(em);
089:
090: LifeCycleDDEntity lcd = new LifeCycleDDEntity("Don", "Shiddle");
091:
092: em.persist(lcd);
093: int id = lcd.getId();
094:
095: CallbackStorage.clearStore();
096:
097: String query = "Update LifeCycleDDEntity e SET e.name = 'Joseph' WHERE e.id = :id";
098:
099: int result = em.createQuery(query).setParameter("id", id)
100: .executeUpdate();
101:
102: List store = CallbackStorage.getInstance().getClist();
103:
104: assertNotNull(result);
105: assertEquals(1, result);
106: assertNotNull(store);
107: assertEquals(2, store.size());
108: assertEquals("def-preupdate", store.get(0));
109: assertEquals("def-postupdate", store.get(1));
110:
111: endTx(em);
112: endEm(em);
113: }
114:
115: public void testDDPol() {
116: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
117: startTx(em);
118:
119: LifeCycleDDEntity lcd = new LifeCycleDDEntity("Julie", "Jolie");
120:
121: em.persist(lcd);
122: int id = lcd.getId();
123:
124: CallbackStorage.clearStore();
125:
126: endTx(em);
127:
128: startTx(em);
129: CallbackStorage.clearStore();
130:
131: assertTrue(CallbackStorage.getInstance().getClist().isEmpty());
132:
133: lcd = em.find(LifeCycleDDEntity.class, id);
134: em.refresh(lcd);
135:
136: assertNotNull(lcd);
137: assertNotNull(CallbackStorage.getInstance().getClist());
138: assertEquals(2, CallbackStorage.getInstance().getClist().size());
139: assertEquals("def-postload", CallbackStorage.getInstance()
140: .getClist().get(0));
141: assertEquals("def-postload", CallbackStorage.getInstance()
142: .getClist().get(1));
143:
144: endTx(em);
145: endEm(em);
146:
147: }
148:
149: /** DEFAULT LISTENER DD TESTING **/
150:
151: public void testDefaultPrePostPersistListener() {
152: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
153: startTx(em);
154:
155: LifeCycleDDEntity2 lc = new LifeCycleDDEntity2("Bill",
156: "Clinton");
157:
158: CallbackStorage store = CallbackStorage.getInstance();
159: store.clearStore();
160:
161: em.persist(lc);
162:
163: assertEquals(4, store.getClist().size());
164: assertEquals("def-prepersist", store.getClist().get(0));
165: assertEquals("verifyprp", store.getClist().get(1));
166: assertEquals("def-postpersist", store.getClist().get(2));
167: assertEquals("verifypop", store.getClist().get(3));
168:
169: endTx(em);
170: endEm(em);
171: }
172:
173: //FIX-ME Default-Entity-listener Impl. is over firing
174: public void testDefaultPrePostUpdateListener() {
175: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
176: startTx(em);
177:
178: LifeCycleDDEntity2 emp = new LifeCycleDDEntity2("lincoln",
179: "Abraham");
180:
181: em.persist(emp);
182:
183: int id = emp.getId();
184:
185: CallbackStorage.clearStore();
186:
187: String query = "Update LifeCycleDDEntity2 e SET e.name = 'Joseph' WHERE e.id = :id";
188:
189: int result = em.createQuery(query).setParameter("id", id)
190: .executeUpdate();
191:
192: List store = CallbackStorage.getInstance().getClist();
193:
194: assertNotNull(result);
195: assertEquals(1, result);
196: assertNotNull(store);
197: assertEquals(2, store.size());
198: assertEquals("def-preupdate", store.get(0));
199: assertEquals("def-postupdate", store.get(1));
200: // assertEquals("def-postupdate", store.get(2));
201: // assertEquals("def-postupdate", store.get(3));
202:
203: endTx(em);
204: endEm(em);
205: }
206:
207: //FIX-ME Default-Entity-listener Impl. is over firing
208: public void testDefaultPostLoadListener() {
209: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
210: startTx(em);
211:
212: LifeCycleDDEntity2 emp = new LifeCycleDDEntity2("Thomas",
213: "Jefferson");
214:
215: em.persist(emp);
216: int id = emp.getId();
217: CallbackStorage.clearStore();
218:
219: endTx(em);
220:
221: startTx(em);
222: CallbackStorage.clearStore();
223:
224: assertTrue("not empty...", CallbackStorage.isEmpty());
225:
226: emp = em.find(LifeCycleDDEntity2.class, id);
227: em.refresh(emp);
228:
229: assertNotNull(emp);
230: assertNotNull(CallbackStorage.getInstance().getClist());
231: assertEquals(2, CallbackStorage.getInstance().getClist().size());
232: assertEquals("def-postload", CallbackStorage.getInstance()
233: .getClist().get(0));
234: assertEquals("def-postload", CallbackStorage.getInstance()
235: .getClist().get(1));
236:
237: endTx(em);
238: endEm(em);
239: }
240:
241: public void testSubClassOverrideSuperCallbacksInh() {
242: OpenJPAEntityManager em = (OpenJPAEntityManager) currentEntityManager();
243: startTx(em);
244:
245: LifeCycleDDEntity2 emp = new LifeCycleDDEntity2("Thomas",
246: "Jefferson");
247:
248: em.persist(emp);
249:
250: assertNotNull(emp);
251: assertNotNull(CallbackStorage.getInstance().getClist());
252: assertEquals(4, CallbackStorage.getInstance().getClist().size());
253: assertEquals("def-prepersist", CallbackStorage.getInstance()
254: .getClist().get(0));
255: assertEquals("verifyprp", CallbackStorage.getInstance()
256: .getClist().get(1));
257: assertEquals("def-postpersist", CallbackStorage.getInstance()
258: .getClist().get(2));
259: assertEquals("verifypop", CallbackStorage.getInstance()
260: .getClist().get(3));
261:
262: endTx(em);
263: endEm(em);
264: }
265: }
|