001: /*
002: * TestInstanceCallbacks.java
003: *
004: * Created on October 12, 2006, 1:19 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: /*
011: * Licensed to the Apache Software Foundation (ASF) under one
012: * or more contributor license agreements. See the NOTICE file
013: * distributed with this work for additional information
014: * regarding copyright ownership. The ASF licenses this file
015: * to you under the Apache License, Version 2.0 (the
016: * "License"); you may not use this file except in compliance
017: * with the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing,
022: * software distributed under the License is distributed on an
023: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024: * KIND, either express or implied. See the License for the
025: * specific language governing permissions and limitations
026: * under the License.
027: */
028: package org.apache.openjpa.persistence.kernel;
029:
030: import org.apache.openjpa.persistence.kernel.common.apps.InstanceCallbacksTest;
031: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
032: import junit.framework.AssertionFailedError;
033:
034: import org.apache.openjpa.persistence.OpenJPAEntityManager;
035:
036: public class TestInstanceCallbacks extends BaseKernelTest {
037:
038: private static final int COMMIT = 0;
039: private static final int FLUSH = 1;
040: private static final int PRESTORE = 2;
041:
042: private OpenJPAEntityManager _pm = null;
043: private InstanceCallbacksTest _callbacks = null;
044:
045: public TestInstanceCallbacks(String name) {
046: super (name);
047: }
048:
049: /**
050: * Creates a new instance of TestInstanceCallbacks
051: */
052: public TestInstanceCallbacks() {
053: }
054:
055: public void setUp() throws Exception {
056: deleteAll(InstanceCallbacksTest.class);
057: deleteAll(RuntimeTest1.class);
058: _pm = getPM(true, true);
059: startTx(_pm);
060: _callbacks = new InstanceCallbacksTest();
061: _callbacks.setStringField("foo");
062: _pm.persist(_callbacks);
063: Object id = _pm.getObjectId(_callbacks);
064: endTx(_pm);
065: endEm(_pm);
066:
067: // re-find with different PM
068: _pm = getPM();
069: _callbacks = (InstanceCallbacksTest) _pm.find(
070: InstanceCallbacksTest.class, id);
071: }
072:
073: public void tearDown() throws Exception {
074: rollbackTx(_pm);
075: endEm(_pm);
076: super .tearDown();
077: }
078:
079: public void testPostLoad() {
080: _callbacks.getStringField();
081: assertTrue(_callbacks.postLoadCalled);
082: }
083:
084: public void testPreStore() {
085: preStoreTest(COMMIT);
086: }
087:
088: public void testPreStoreWithFlush() {
089: preStoreTest(FLUSH);
090: }
091:
092: public void testPreStoreWithPreStore() {
093: preStoreTest(PRESTORE);
094: }
095:
096: private void preStoreTest(int action) {
097: assertNoCallbacksInvoked(_callbacks);
098: // _pm.begin();
099: startTx(_pm);
100:
101: _callbacks.setStringField("bar");
102: Object oid = _pm.getObjectId(_callbacks);
103: if (action == COMMIT) {
104: _pm.flush();
105: endTx(_pm);
106: } else if (action == FLUSH)
107: _pm.flush();
108: else if (action == PRESTORE)
109: _pm.preFlush();
110: assertTrue("prestore wasnt called", _callbacks.preStoreCalled);
111: if (action != COMMIT) {
112: // _pm.commit();
113: if (action != FLUSH)
114: _pm.flush();
115: endTx(_pm);
116: }
117:
118: OpenJPAEntityManager pm = getPM();
119: InstanceCallbacksTest callbacks = (InstanceCallbacksTest) pm
120: .find(InstanceCallbacksTest.class, oid);
121: assertNoCallbacksInvoked(callbacks);
122: assertEquals("getonetoone strng is not jdoprestore",
123: "jdoPreStore", callbacks.getOneOne().getStringField());
124: endEm(pm);
125: }
126:
127: public void testPreDelete() {
128: assertNoCallbacksInvoked(_callbacks);
129: startTx(_pm);
130: _pm.remove(_callbacks);
131: assertTrue(_callbacks.preDeleteCalled);
132: endTx(_pm);
133: }
134:
135: public void testPreDeleteRecursion() {
136: assertNoCallbacksInvoked(_callbacks);
137: startTx(_pm);
138: _callbacks.preDeleteCycle = 0;
139: _pm.remove(_callbacks);
140: assertEquals(1, _callbacks.preDeleteCycle);
141: endTx(_pm);
142: }
143:
144: public void testSetRelatedReferenceInPreStore() {
145: assertNull(_callbacks.getRelId());
146: InstanceCallbacksTest callbacks2 = new InstanceCallbacksTest();
147: callbacks2.setRelId(_pm.getObjectId(_callbacks));
148: startTx(_pm);
149: _pm.persist(callbacks2);
150: _pm.flush();
151: endTx(_pm);
152: assertEquals(8888, _callbacks.getIntField());
153: try {
154: assertEquals(callbacks2, _callbacks.getRel());
155: } catch (AssertionFailedError afe) {
156: bug(1162, afe, "Setting a related object reference in "
157: + "preStore fails");
158: }
159: }
160:
161: public void testFlushCausesFlush() {
162: //### JDO2MIG : this is failing because we're consuming exceptions
163: // throws from callbacks; need to decide what to do with them
164: causeFlushTest(FLUSH);
165: }
166:
167: public void testPreStoreCausesFlush() {
168: //### JDO2MIG : this is failing because we're consuming exceptions
169: // throws from callbacks; need to decide what to do with them
170: causeFlushTest(PRESTORE);
171: }
172:
173: private void causeFlushTest(int action) {
174: startTx(_pm);
175: _callbacks.setStringField("sss");
176: _callbacks.flushInPreStore = true;
177: try {
178: if (action == FLUSH)
179: _pm.flush();
180: else
181: _pm.preFlush();
182:
183: bug(1139,
184: "Recursive flush allowed because exception swallowed");
185: } catch (Exception je) {
186: }
187: rollbackTx(_pm);
188: }
189:
190: private void assertNoCallbacksInvoked(InstanceCallbacksTest pc) {
191: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
192:
193: assertFalse(
194: "Expected preDelete to not have been called for object ID "
195: + pm.getObjectId(pc), pc.preDeleteCalled);
196: assertFalse(
197: "Expected preClear to not have been called for object ID "
198: + pm.getObjectId(pc), pc.preClearCalled);
199: assertFalse(
200: "Expected preStore to not have been called for object ID "
201: + pm.getObjectId(pc), pc.preStoreCalled);
202: }
203:
204: /*
205: // no JPA equivalent
206:
207: public void testDetachAttach()
208: throws Exception {
209: OpenJPAEntityManager pm = getPM();
210: DetachAttachEvent pc = (DetachAttachEvent) pm.find
211: (DetachAttachEvent.class,createDetachableId(4));
212: DetachAttachEvent.EVENTS.clear();
213: pc = (DetachAttachEvent) pm.detach(pc);
214: assertDetachEvents(new String[]{ "PRED4", "POSTD4" });
215: endEm(pm,());
216:
217: assertTrue(pm.isDetached(pc));
218:
219: pm = getPM();
220: startTx(pm,());
221: //FIXME jthomas
222:
223: // pm.addInstanceLifecycleListener(new CreateLifecycleListener() {
224: // public void postCreate(InstanceLifecycleEvent ev) {
225: // fail("No post create necessary");
226: // }
227: // }, null);
228: pm.persist(pc);
229: assertDetachEvents(new String[]{ "PREA4", "POSTA4" });
230: endTx(pm,());
231: endEm(pm,());
232: }
233:
234: public void testDetachAttachRelations() {
235: OpenJPAEntityManager pm = getPM();
236: startTx(pm,());
237: DetachAttachEvent pc = (DetachAttachEvent) pm.find
238: (DetachAttachEvent.class,createDetachableId(2));
239: pc.setOneOne((DetachAttachEvent) pm.find
240: (DetachAttachEvent.class,createDetachableId(4)));
241: endTx(pm,());
242: DetachAttachEvent.EVENTS.clear();
243: pc = (DetachAttachEvent) pm.detach(pc);
244: endEm(pm,());
245: assertDetachEvents(
246: new String[]{ "PRED2", "PRED4", "POSTD2", "POSTD4" });
247:
248: pm = getPM();
249: startTx(pm,());
250: pm.persist(pc);
251: assertDetachEvents(
252: new String[]{ "PREA2", "PREA4", "POSTA2", "POSTA4" });
253: rollbackTx(pm,());
254: endEm(pm,());
255: }
256:
257: private void assertDetachEvents(String[] expected) {
258: Collection events = DetachAttachEvent.EVENTS;
259: if (expected.length != events.size()) {
260: StringBuffer buf = new StringBuffer();
261: for (int i = 0; i < expected.length; i++)
262: buf.append(expected[i]).append(",");
263: buf.append("!=");
264: for (Iterator it = events.iterator(); it.hasNext();)
265: buf.append(it.next()).append(",");
266: fail("mismatch event count:" + buf);
267: }
268: String event;
269: for (int i = 0; i < expected.length; i++) {
270: if (!events.remove(expected[i]))
271: fail("Event not fired:" + expected[i]);
272: if (events.contains(expected[i]))
273: fail("Event fired twice:" + expected[i]);
274: }
275: if (!events.isEmpty())
276: fail("Excess events fired:" + events);
277: DetachAttachEvent.EVENTS.clear();
278: }
279:
280: private Object createDetachableId(int field) {
281: OpenJPAEntityManager pm = getPM();
282: startTx(pm,());
283: DetachAttachEvent pc = new DetachAttachEvent();
284: pc.setIntField(field);
285: pm.persist(pc);
286: endTx(pm,());
287: endEm(pm,());
288: // return pm.getObjectId(pc);
289: return pc.getId();
290: }
291: */
292: }
|