001: /*
002: * TestCheckConsistency.java
003: *
004: * Created on October 9, 2006, 6:23 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.RuntimeTest1;
031:
032: import org.apache.openjpa.persistence.JPAFacadeHelper;
033: import org.apache.openjpa.persistence.OpenJPAEntityManager;
034:
035: public class TestCheckConsistency extends BaseKernelTest {
036:
037: private Object _oid = null;
038:
039: /**
040: * Creates a new instance of TestCheckConsistency
041: */
042: public TestCheckConsistency(String name) {
043: super (name);
044: }
045:
046: public void setUp() {
047: deleteAll(RuntimeTest1.class);
048:
049: RuntimeTest1 pc = new RuntimeTest1();
050: pc.setIntField(1);
051: pc.setIntField1(1);
052: _oid = persist(pc);
053: }
054:
055: public void testConsistentDatastoreTransaction() {
056:
057: OpenJPAEntityManager pm = getPM();
058: pm.setOptimistic(false);
059: pm.validateChanges(); // no-op outside trans
060: startTx(pm);
061:
062: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,
063: _oid);
064: pc.setIntField1(100);
065:
066: RuntimeTest1 npc = new RuntimeTest1();
067: npc.setIntField(2);
068: npc.setIntField1(2);
069: pm.persist(npc);
070: pm.validateChanges();
071:
072: assertEquals(100, pc.getIntField1());
073: assertTrue(pm.isPersistent(npc));
074:
075: pc.setIntField1(200);
076: npc.setIntField1(300);
077: endTx(pm);
078:
079: assertEquals(200, pc.getIntField1());
080: assertTrue(pm.isPersistent(npc));
081: assertEquals(300, npc.getIntField1());
082: endEm(pm);
083: }
084:
085: public void testConsistentDatastoreTransactionWithRollback() {
086: OpenJPAEntityManager pm = getPM();
087: pm.setOptimistic(false);
088: pm.validateChanges(); // no-op outside trans
089: startTx(pm);
090:
091: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,
092: _oid);
093: pc.setIntField1(100);
094:
095: RuntimeTest1 npc = new RuntimeTest1();
096: pm.persist(npc);
097: Object noid = pm.getObjectId(npc);
098:
099: pm.validateChanges();
100: assertEquals(100, pc.getIntField1());
101: assertTrue(pm.isPersistent(npc));
102:
103: pc.setIntField1(200);
104: npc.setIntField1(300);
105: rollbackTx(pm);
106:
107: assertEquals(1, pc.getIntField1());
108: assertFalse(pm.isPersistent(npc));
109: assertEquals(0, npc.getIntField1());
110: endEm(pm);
111:
112: pm = getPM();
113: try {
114: RuntimeTest1 temp = (RuntimeTest1) pm.find(
115: RuntimeTest1.class, noid);
116: fail("Object should not exist." + temp.getIntField() + "::"
117: + temp.getIntField1());
118: } catch (Exception jonfe) {
119: }
120: endEm(pm);
121: }
122:
123: //FIXME jthomas
124: /*
125: public void testInconsistentDatastoreTransaction() {
126: OpenJPAEntityManager pm = getPM();
127: FetchPlan fetch = (FetchPlan) pm.getFetchPlan();
128: pm.setOptimistic(false);
129: pm.setRetainState(false);
130: pm.validateChanges(); // no-op outside trans
131: pm.begin();
132: */
133: //FIXME jthomas
134: /*
135: fetch.setReadLockLevel(pm.LOCK_NONE);
136: fetch.setWriteLockLevel(pm.LOCK_NONE);
137: */
138: /*
139: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,_oid);
140: pc.setIntField(100);
141:
142: OpenJPAEntityManager pm2 = getPM();
143: pm2.begin();
144: RuntimeTest1 copy = (RuntimeTest1) pm2.find(RuntimeTest1.class,_oid);
145: copy.setIntField(-1);
146: pm2.commit();
147: pm2.close();
148:
149: RuntimeTest1 npc = new RuntimeTest1();
150: pm.persist(npc);
151:
152: try {
153: pm.validateChanges();
154: fail("Didn't find inconsistency.");
155: } catch (Exception jove) {
156: //FIXME
157: /*
158: Throwable[] t = jove.getNestedExceptions();
159: assertEquals(1, t.length);
160: assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
161: */
162: // }
163: /* assertTrue(pm.getRollbackOnly());
164: pm.rollback();
165:
166: assertEquals(-1, pc.getIntField());
167: assertFalse(pm.isPersistent(npc));
168: endEm(pm,());
169: }
170: */
171: public void testConsistentOptimisticTransaction() {
172: OpenJPAEntityManager pm = getPM();
173: pm.setOptimistic(true);
174: pm.validateChanges(); // no-op outside trans
175: startTx(pm);
176: boolean hasConn = hasConnection(pm);
177:
178: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,
179: _oid);
180: pc.setIntField1(100);
181:
182: RuntimeTest1 npc = new RuntimeTest1();
183: npc.setIntField(2);
184: npc.setIntField1(2);
185: pm.persist(npc);
186:
187: pm.validateChanges();
188: if (!hasConn)
189: assertFalse(hasConnection(pm));
190:
191: assertEquals(100, pc.getIntField1());
192: assertTrue(pm.isPersistent(npc));
193:
194: pc.setIntField1(200);
195: npc.setIntField1(300);
196: endTx(pm);
197:
198: assertEquals(200, pc.getIntField1());
199: assertTrue(pm.isPersistent(npc));
200: assertEquals(300, npc.getIntField1());
201: endEm(pm);
202: }
203:
204: private boolean hasConnection(OpenJPAEntityManager pm) {
205: return JPAFacadeHelper.toBroker(pm).hasConnection();
206: }
207:
208: public void testConsistentOptimisticTransactionWithRollback() {
209: OpenJPAEntityManager pm = getPM();
210: pm.setOptimistic(true);
211: pm.validateChanges(); // no-op outside trans
212: startTx(pm);
213: boolean hasConn = hasConnection(pm);
214:
215: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,
216: _oid);
217: pc.setIntField1(100);
218:
219: RuntimeTest1 npc = new RuntimeTest1();
220: pm.persist(npc);
221: Object noid = pm.getObjectId(npc);
222:
223: pm.validateChanges();
224: if (!hasConn)
225: assertFalse(hasConnection(pm));
226:
227: assertEquals(100, pc.getIntField1());
228: assertTrue(pm.isPersistent(npc));
229:
230: pc.setIntField1(200);
231: npc.setIntField1(300);
232: rollbackTx(pm);
233:
234: assertEquals(1, pc.getIntField1());
235: assertFalse(pm.isPersistent(npc));
236: assertEquals(0, npc.getIntField1());
237: endEm(pm);
238:
239: pm = getPM();
240: try {
241: RuntimeTest1 temp = (RuntimeTest1) pm.find(
242: RuntimeTest1.class, noid);
243:
244: fail("Object should not exist." + temp.getIntField() + "::"
245: + temp.getIntField1());
246: } catch (Exception jonfe) {
247: }
248: }
249: //FIXME
250: /*
251: public void testInconsistentOptimisticTransactionWithoutRefresh() {
252: OpenJPAEntityManager pm = getPM();
253: pm.setRetainState(false);
254: pm.setOptimistic(true);
255: pm.validateChanges(); // no-op outside trans
256: pm.begin();
257:
258: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,_oid);
259: pc.setIntField(100);
260:
261: OpenJPAEntityManager pm2 = getPM();
262: pm2.begin();
263: RuntimeTest1 copy = (RuntimeTest1) pm2.find(RuntimeTest1.class,_oid);
264: copy.setIntField(-1);
265: pm2.commit();
266: pm2.close();
267:
268: RuntimeTest1 npc = new RuntimeTest1();
269: pm.persist(npc);
270: Object noid = pm.getObjectId(npc);
271:
272: try {
273: pm.validateChanges();
274: fail("Didn't find inconsistency.");
275: } catch (Exception jove) {
276: //FIXME jthomas
277: /*
278: Throwable[] t = jove.getNestedExceptions();
279: assertEquals(1, t.length);
280: assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
281: */
282: // }
283: /* assertFalse(pm.getRollbackOnly());
284:
285: try {
286: pm.commit();
287: fail("Committed inconsistent transaction.");
288: } catch (Exception je) {
289: }
290:
291: assertEquals(-1, pc.getIntField());
292: assertFalse(pm.isPersistent(npc));
293: endEm(pm,());
294:
295: pm = getPM();
296: try {
297: pm.find(RuntimeTest1.class,noid);
298: fail("Object should not exist.");
299: } catch (Exception jonfe) {
300: }
301: endEm(pm,());
302: }
303: */
304:
305: //FIXME
306: /*
307: public void testInconsistentOptimisticTransactionWithRefresh() {
308: OpenJPAEntityManager pm = getPM();
309: pm.setOptimistic(true);
310: pm.validateChanges(); // no-op outside trans
311: pm.begin();
312:
313: RuntimeTest1 pc = pm.find(RuntimeTest1.class,_oid);
314: pc.setIntField(100);
315:
316: OpenJPAEntityManager pm2 = getPM();
317: pm2.begin();
318: RuntimeTest1 copy = pm2.find(RuntimeTest1.class,_oid);
319: copy.setIntField(-1);
320: pm2.commit();
321: pm2.close();
322:
323: RuntimeTest1 npc = new RuntimeTest1();
324: pm.persist(npc);
325: try {
326: pm.validateChanges();
327: fail("Didn't find inconsistency.");
328: } catch (Exception jove) {
329: //FIXME jthomas
330: /*
331: Throwable[] t = jove.getNestedExceptions();
332: assertEquals(1, t.length);
333: assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
334: */
335: // }
336: /* assertFalse(pm.getRollbackOnly());
337: pm.refresh(pc);
338:
339: assertEquals(-1, pc.getIntField());
340: assertTrue(pm.isPersistent(npc));
341:
342: pc.setIntField(200);
343: npc.setIntField(300);
344: pm.commit();
345:
346: assertEquals(200, pc.getIntField());
347: assertTrue(pm.isPersistent(npc));
348: assertEquals(300, npc.getIntField());
349: endEm(pm,());
350: }
351: */
352:
353: //FIXME
354: /*
355: public void testInconsistentOptimisticTransactionWithRollback() {
356: OpenJPAEntityManager pm = getPM();
357: pm.setRetainState(false);
358: pm.setOptimistic(true);
359: pm.validateChanges(); // no-op outside trans
360: pm.begin();
361:
362: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,_oid);
363: pc.setIntField(100);
364:
365: OpenJPAEntityManager pm2 = getPM();
366: pm2.begin();
367: RuntimeTest1 copy = (RuntimeTest1) pm2.find(RuntimeTest1.class,_oid);
368: copy.setIntField(-1);
369: pm2.commit();
370: pm2.close();
371:
372: RuntimeTest1 npc = new RuntimeTest1();
373: pm.persist(npc);
374: Object noid = pm.getObjectId(npc);
375:
376: try {
377: pm.validateChanges();
378: fail("Didn't find inconsistency.");
379: } catch (Exception jove) {
380: //FIXME jthomas
381: /*
382: Throwable[] t = jove.getNestedExceptions();
383: assertEquals(1, t.length);
384: assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
385: */
386: // }
387: /* assertFalse(pm.getRollbackOnly());
388: pm.rollback();
389:
390: assertEquals(-1, pc.getIntField());
391: assertFalse(pm.isPersistent(npc));
392: endEm(pm,());
393:
394: pm = getPM();
395: try {
396: pm.find(RuntimeTest1.class,_oid);
397: fail("Object should not exist.");
398: } catch (Exception jonfe) {
399: }
400: endEm(pm,());
401: }
402:
403: */
404: }
|