001: /*
002: * TestOptimisticLockGroups.java
003: *
004: * Created on October 12, 2006, 2:57 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009: /*
010: * Licensed to the Apache Software Foundation (ASF) under one
011: * or more contributor license agreements. See the NOTICE file
012: * distributed with this work for additional information
013: * regarding copyright ownership. The ASF licenses this file
014: * to you under the Apache License, Version 2.0 (the
015: * "License"); you may not use this file except in compliance
016: * with the License. You may obtain a copy of the License at
017: *
018: * http://www.apache.org/licenses/LICENSE-2.0
019: *
020: * Unless required by applicable law or agreed to in writing,
021: * software distributed under the License is distributed on an
022: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
023: * KIND, either express or implied. See the License for the
024: * specific language governing permissions and limitations
025: * under the License.
026: */
027: package org.apache.openjpa.persistence.kernel;
028:
029: import org.apache.openjpa.persistence.kernel.common.apps.LockGroupPC;
030: import org.apache.openjpa.persistence.kernel.common.apps.LockGroupPC2;
031: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
032:
033: import org.apache.openjpa.persistence.OpenJPAEntityManager;
034:
035: public class TestOptimisticLockGroups extends BaseKernelTest {
036:
037: private Object oid;
038:
039: /**
040: * Creates a new instance of TestOptimisticLockGroups
041: */
042: public TestOptimisticLockGroups() {
043: }
044:
045: public TestOptimisticLockGroups(String name) {
046: super (name);
047: }
048:
049: public void setUp() {
050: deleteAll(LockGroupPC.class);
051:
052: OpenJPAEntityManager pm = getPM(false, false);
053: startTx(pm);
054: Object o = new LockGroupPC();
055: pm.persist(o);
056: endTx(pm);
057: oid = pm.getObjectId(o);
058: endEm(pm);
059: }
060:
061: public void testDefaultLockGroupFailure1() {
062: OpenJPAEntityManager pm1 = getPM(false, false);
063: startTx(pm1);
064: LockGroupPC pc1 = (LockGroupPC) pm1
065: .find(LockGroupPC.class, oid);
066: pc1.setDefaultLockGroupStringField("pm1 value");
067:
068: OpenJPAEntityManager pm2 = getPM(false, false);
069: startTx(pm2);
070: LockGroupPC pc2 = (LockGroupPC) pm2
071: .find(LockGroupPC.class, oid);
072: pc2.setDefaultLockGroupStringField("pm2 value");
073:
074: endTx(pm1);
075: endEm(pm1);
076:
077: try {
078: endTx(pm2);
079: fail("should not be able to commit change to same value");
080: } catch (Exception e) {
081: assertEquals(pc2, getFailedObject(e));
082: }
083: endEm(pm2);
084: }
085:
086: public void testDefaultLockGroupFailure2() {
087: OpenJPAEntityManager pm1 = getPM(false, false);
088: startTx(pm1);
089: LockGroupPC pc1 = (LockGroupPC) pm1
090: .find(LockGroupPC.class, oid);
091: pc1.setDefaultLockGroupStringField("pm1 value");
092:
093: OpenJPAEntityManager pm2 = getPM(false, false);
094: startTx(pm2);
095: LockGroupPC pc2 = (LockGroupPC) pm2
096: .find(LockGroupPC.class, oid);
097: pc2.setExplicitDefaultLockGroupIntField(2);
098:
099: endTx(pm1);
100: endEm(pm1);
101:
102: try {
103: endTx(pm2);
104: fail("should not be able to commit change to same value");
105: } catch (Exception e) {
106: assertEquals(pc2, getFailedObject(e));
107: }
108: endEm(pm2);
109: }
110:
111: public void testNonDefaultLockGroupFailure1() {
112: OpenJPAEntityManager pm1 = getPM(false, false);
113: startTx(pm1);
114: LockGroupPC pc1 = (LockGroupPC) pm1
115: .find(LockGroupPC.class, oid);
116: pc1.setLockGroup0IntField(1);
117:
118: OpenJPAEntityManager pm2 = getPM(false, false);
119: startTx(pm2);
120: LockGroupPC pc2 = (LockGroupPC) pm2
121: .find(LockGroupPC.class, oid);
122: pc2.setLockGroup0IntField(2);
123:
124: endTx(pm1);
125: endEm(pm1);
126:
127: try {
128: endTx(pm2);
129: fail("should not be able to commit change to same value");
130: } catch (Exception e) {
131: assertEquals(pc2, getFailedObject(e));
132: }
133: endEm(pm2);
134: }
135:
136: public void testNonDefaultLockGroupFailure2() {
137: OpenJPAEntityManager pm1 = getPM(false, false);
138: startTx(pm1);
139: LockGroupPC pc1 = (LockGroupPC) pm1
140: .find(LockGroupPC.class, oid);
141: pc1.setLockGroup0IntField(1);
142:
143: OpenJPAEntityManager pm2 = getPM(false, false);
144: startTx(pm2);
145: LockGroupPC pc2 = (LockGroupPC) pm2
146: .find(LockGroupPC.class, oid);
147: pc2.setLockGroup0StringField("pm2");
148:
149: endTx(pm1);
150: endEm(pm1);
151:
152: try {
153: endTx(pm2);
154: fail("should not be able to commit change to same value");
155: } catch (Exception e) {
156: assertEquals(pc2, getFailedObject(e));
157: }
158: endEm(pm2);
159: }
160:
161: public void testMultipleLockGroupSuccess1() {
162: OpenJPAEntityManager pm1 = getPM(false, false);
163: startTx(pm1);
164: LockGroupPC pc1 = (LockGroupPC) pm1
165: .find(LockGroupPC.class, oid);
166: pc1.setDefaultLockGroupStringField("pm1 value");
167: pc1.setExplicitDefaultLockGroupIntField(1);
168:
169: OpenJPAEntityManager pm2 = getPM(false, false);
170: startTx(pm2);
171: LockGroupPC pc2 = (LockGroupPC) pm2
172: .find(LockGroupPC.class, oid);
173: pc2.setLockGroup0IntField(2);
174:
175: OpenJPAEntityManager pm3 = getPM(false, false);
176: startTx(pm3);
177: LockGroupPC pc3 = (LockGroupPC) pm3
178: .find(LockGroupPC.class, oid);
179: pc3.setLockGroup1RelationField(new RuntimeTest1());
180:
181: endTx(pm1);
182: endEm(pm1);
183:
184: endTx(pm2);
185: endEm(pm2);
186:
187: endTx(pm3);
188: endEm(pm3);
189: }
190:
191: public void testMultipleLockGroupSuccess2() {
192: OpenJPAEntityManager pm1 = getPM(false, false);
193: startTx(pm1);
194: LockGroupPC pc1 = (LockGroupPC) pm1
195: .find(LockGroupPC.class, oid);
196: pc1.setDefaultLockGroupStringField("pm1 value");
197: pc1.setLockGroup0IntField(1);
198:
199: OpenJPAEntityManager pm2 = getPM(false, false);
200: startTx(pm2);
201: LockGroupPC pc2 = (LockGroupPC) pm2
202: .find(LockGroupPC.class, oid);
203: pc2.setLockGroup1RelationField(new RuntimeTest1());
204:
205: endTx(pm2);
206: endEm(pm2);
207:
208: endTx(pm1);
209: endEm(pm1);
210: }
211:
212: // FIX ME: aokeke - test is taking so much resource and causing subsequent test to fail
213: // public void testNoLockGroupSuccess() {
214: // OpenJPAEntityManager pm1 = getPM(false, false);
215: // startTx(pm1,());
216: // LockGroupPC pc1 = (LockGroupPC) pm1.find(LockGroupPC.class,oid);
217: // pc1.setDefaultLockGroupStringField("pm1 value");
218: // pc1.setLockGroup0IntField(1);
219: // pc1.setUnlockedStringField("pm1 value");
220: //
221: // OpenJPAEntityManager pm2 = getPM(false, false);
222: // startTx(pm2,());
223: // LockGroupPC pc2 = (LockGroupPC) pm2.find(LockGroupPC.class,oid);
224: // pc2.setLockGroup1RelationField(new RuntimeTest1());
225: // pc2.setUnlockedStringField("pm2 value");
226: //
227: // endTx(pm1,());
228: // endEm(pm1);
229: //
230: // endTx(pm2,());
231: // endEm(pm2);
232: // }
233:
234: public void testAttachDetachSuccess() throws Exception {
235: OpenJPAEntityManager pm1 = getPM(false, false);
236: LockGroupPC pc1 = (LockGroupPC) pm1
237: .find(LockGroupPC.class, oid);
238: Object detached = pm1.detach(pc1);
239: startTx(pm1);
240: pc1.setLockGroup0IntField(1);
241: pc1.setUnlockedStringField("pm1 changed value");
242: endTx(pm1);
243: endEm(pm1);
244:
245: /*
246: // won't work without non-transient detached state
247: ByteArrayOutputStream baos = new ByteArrayOutputStream ();
248: ObjectOutputStream oos = new ObjectOutputStream (baos);
249: oos.writeObject (detached);
250: oos.close ();
251: baos.close ();
252:
253: ByteArrayInputStream bais =
254: new ByteArrayInputStream (baos.toByteArray ());
255: ObjectInputStream ois = new ObjectInputStream (bais);
256: LockGroupPC clone = (LockGroupPC) ois.readObject ();
257: */
258: LockGroupPC clone = (LockGroupPC) detached;
259: clone.setLockGroup1IntField(2);
260: clone.setUnlockedStringField("pm2 value");
261:
262: OpenJPAEntityManager pm2 = getPM(false, false);
263: startTx(pm2);
264: pm2.merge(clone);
265: endTx(pm2);
266: endEm(pm2);
267: }
268:
269: public void testAttachDetachFailure() throws Exception {
270: OpenJPAEntityManager pm1 = getPM(false, false);
271: LockGroupPC pc1 = (LockGroupPC) pm1
272: .find(LockGroupPC.class, oid);
273: Object detached = pm1.detach(pc1);
274: startTx(pm1);
275: pc1.setLockGroup0IntField(1);
276: endTx(pm1);
277: endEm(pm1);
278:
279: /*
280: // won't work without non-transient detached state
281: ByteArrayOutputStream baos = new ByteArrayOutputStream ();
282: ObjectOutputStream oos = new ObjectOutputStream (baos);
283: oos.writeObject (detached);
284: oos.close ();
285: baos.close ();
286:
287: ByteArrayInputStream bais =
288: new ByteArrayInputStream (baos.toByteArray ());
289: ObjectInputStream ois = new ObjectInputStream (bais);
290: LockGroupPC clone = (LockGroupPC) ois.readObject ();
291: */
292: LockGroupPC clone = (LockGroupPC) detached;
293: clone.setLockGroup0IntField(2);
294:
295: OpenJPAEntityManager pm2 = getPM(false, false);
296: startTx(pm2);
297: boolean failed = false;
298: try {
299: pm2.merge(clone);
300: } catch (Exception e) {
301: failed = true;
302: }
303:
304: if (failed)
305: rollbackTx(pm2);
306: else {
307: try {
308: endTx(pm2);
309: fail("Allowed conflicting changes");
310: } catch (Exception jve) {
311: }
312: }
313: }
314:
315: public void testLockGroupNone() {
316: OpenJPAEntityManager pm = getPM(false, false);
317: // pm.begin();
318: deleteAll(LockGroupPC2.class, pm);
319: startTx(pm);
320: LockGroupPC2 pc = new LockGroupPC2();
321: pc.setName("pc");
322: pm.persist(pc);
323: endTx(pm);
324: Object oid = pm.getObjectId(pc);
325: endEm(pm);
326:
327: pm = getPM(false, false);
328: pc = (LockGroupPC2) pm.find(LockGroupPC2.class, oid);
329: startTx(pm);
330: pc.getList().add("foo");
331:
332: OpenJPAEntityManager pm2 = getPM(false, false);
333: LockGroupPC2 pc2 = (LockGroupPC2) pm2.find(LockGroupPC2.class,
334: oid);
335: startTx(pm2);
336: pc2.getList().add("bar");
337: endTx(pm2);
338: endEm(pm2);
339:
340: endTx(pm);
341: endEm(pm);
342:
343: pm = getPM(false, false);
344: pc = (LockGroupPC2) pm.find(LockGroupPC2.class, oid);
345: assertEquals(2, pc.getList().size());
346: endEm(pm);
347: }
348:
349: // public void testKnownSubclass() {
350: // OpenJPAEntityManager pm = getPM(false, false);
351: // LockGroupPCKnownSubclass pc = new LockGroupPCKnownSubclass();
352: // pc.setDefaultLockGroupStringField("pc");
353: // startTx(pm,());
354: // pm.persist(pc);
355: // endTx(pm,());
356: //
357: // startTx(pm,());
358: // pc.setKnownSubclassStringField("foo");
359: // endTx(pm,());
360: // }
361:
362: //FIXME jthomas - what do we need to substitute for JDOException ?
363: // private Object getFailedObject(JDOException e) {
364: // return ((JDOException) e.getNestedExceptions()[0]).getFailedObject();
365: // }
366:
367: private Object getFailedObject(Exception e) {
368: return null;
369: }
370: }
|