001: /*
002: * TestSavepointOrdering.java
003: *
004: * Created on October 16, 2006, 10:45 AM
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 java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
034:
035: import org.apache.openjpa.kernel.Broker;
036: import org.apache.openjpa.kernel.OpenJPASavepoint;
037: import org.apache.openjpa.kernel.SavepointManager;
038: import org.apache.openjpa.persistence.OpenJPAEntityManager;
039: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
040:
041: public class TestSavepointOrdering extends BaseKernelTest {
042:
043: private static final int USER = 1;
044: private static final int RELEASED = 2;
045: private static final int ROLLBACK = 4;
046:
047: static Map _assigned = new HashMap();
048:
049: /**
050: * Creates a new instance of TestSavepointOrdering
051: */
052: public TestSavepointOrdering(String name) {
053: super (name);
054: }
055:
056: public void setUp() {
057: deleteAll(RuntimeTest1.class);
058: _assigned.clear();
059: }
060:
061: public void testCleanUpCommit() {
062: doCleanUpTest(true);
063: }
064:
065: public void testCleanUpRollback() {
066: doCleanUpTest(false);
067: }
068:
069: public void doCleanUpTest(boolean commit) {
070: Map props = new HashMap();
071: props.put("openjpa.SavepointManager",
072: TrackingSavepointManager.class.getName());
073: OpenJPAEntityManagerFactory pmf = getEmf(props);
074: OpenJPAEntityManager pm = pmf.createEntityManager();
075: startTx(pm);
076: pm.setSavepoint("test");
077: pm.setSavepoint("test2");
078: if (commit)
079: endTx(pm);
080: else
081: rollbackTx(pm);
082: assertFlags("test", RELEASED, USER | ROLLBACK);
083: assertFlags("test2", RELEASED, USER | ROLLBACK);
084: endEm(pm);
085: }
086:
087: public void testOrderingWithRollback() {
088: doOrderingTest(true);
089: }
090:
091: public void testOrderingWithRelease() {
092: doOrderingTest(false);
093: }
094:
095: private void doOrderingTest(boolean rollback) {
096: Map props = new HashMap();
097: props.put("openjpa.SavepointManager",
098: TrackingSavepointManager.class.getName());
099: OpenJPAEntityManagerFactory pmf = getEmf(props);
100: OpenJPAEntityManager pm = pmf.createEntityManager();
101: startTx(pm);
102: pm.setSavepoint("before");
103: pm.setSavepoint("before2");
104: pm.setSavepoint("test");
105: pm.setSavepoint("test2");
106: pm.setSavepoint("after");
107: pm.setSavepoint("after2");
108: if (rollback)
109: pm.rollbackToSavepoint("test2");
110: else
111: pm.releaseSavepoint("test2");
112:
113: assertFlags("before", 0, RELEASED | ROLLBACK);
114: assertFlags("before2", 0, RELEASED | ROLLBACK);
115: assertFlags("test", 0, RELEASED | ROLLBACK);
116: assertFlags("after", RELEASED, USER);
117: assertFlags("after2", RELEASED, USER);
118:
119: if (rollback)
120: assertFlags("test2", ROLLBACK, 0);
121: else
122: assertFlags("test2", RELEASED | USER, 0);
123:
124: pm.setSavepoint("after3");
125: if (rollback)
126: pm.rollbackToSavepoint("test");
127: else
128: pm.releaseSavepoint("test");
129:
130: assertFlags("before", 0, RELEASED | ROLLBACK);
131: assertFlags("before2", 0, RELEASED | ROLLBACK);
132: if (rollback)
133: assertFlags("test", ROLLBACK, 0);
134: else
135: assertFlags("test", RELEASED | USER, 0);
136: assertFlags("after3", RELEASED, USER);
137: rollbackTx(pm);
138: endEm(pm);
139: }
140:
141: public void testDisallowFlush() {
142: Map props = new HashMap();
143: props.put("openjpa.SavepointManager",
144: TrackingSavepointManager.class.getName()
145: + "(AllowFlush=false)");
146: OpenJPAEntityManagerFactory pmf = getEmf(props);
147: OpenJPAEntityManager pm = pmf.createEntityManager();
148: startTx(pm);
149: pm.persist(new RuntimeTest1());
150: pm.setSavepoint("a");
151: try {
152: pm.flush();
153: fail("should have failed.");
154: } catch (Exception e) {
155: }
156: rollbackTx(pm);
157: endEm(pm);
158: }
159:
160: public void testDisallowFlush2() {
161: Map props = new HashMap();
162: props.put("openjpa.SavepointManager",
163: TrackingSavepointManager.class.getName()
164: + "(AllowFlush=false)");
165: OpenJPAEntityManagerFactory pmf = getEmf(props);
166: OpenJPAEntityManager pm = pmf.createEntityManager();
167:
168: startTx(pm);
169: pm.persist(new RuntimeTest1());
170: pm.flush();
171: try {
172: pm.setSavepoint("a");
173: fail("should have failed.");
174: } catch (Exception e) {
175: }
176: rollbackTx(pm);
177: endEm(pm);
178: }
179:
180: public void testAllowFlush() {
181: Map props = new HashMap();
182: props.put("openjpa.SavepointManager",
183: TrackingSavepointManager.class.getName()
184: + "(AllowFlush=true)");
185: OpenJPAEntityManagerFactory pmf = getEmf(props);
186: OpenJPAEntityManager pm = pmf.createEntityManager();
187:
188: startTx(pm);
189: pm.persist(new RuntimeTest1());
190: pm.setSavepoint("a");
191: try {
192: pm.flush();
193: } catch (Exception e) {
194: fail("allows flush.");
195: }
196: rollbackTx(pm);
197: endEm(pm);
198: }
199:
200: public void testAllowFlush2() {
201: Map props = new HashMap();
202: props.put("openjpa.SavepointManager",
203: TrackingSavepointManager.class.getName()
204: + "(AllowFlush=true)");
205: OpenJPAEntityManagerFactory pmf = getEmf(props);
206: OpenJPAEntityManager pm = pmf.createEntityManager();
207:
208: startTx(pm);
209: pm.persist(new RuntimeTest1());
210: pm.flush();
211: try {
212: pm.setSavepoint("a");
213: } catch (Exception e) {
214: fail("allows flush.");
215: }
216: rollbackTx(pm);
217: endEm(pm);
218: }
219:
220: private void assertFlags(String name, int flag, int noflag) {
221: TrackingSavepoint sp = (TrackingSavepoint) _assigned.get(name);
222: assertNotNull(sp);
223: assertEquals(sp.flags & flag, flag);
224: assertTrue((sp.flags & noflag) == 0);
225: }
226:
227: public static class TrackingSavepointManager implements
228: SavepointManager {
229:
230: public boolean allowFlush = false;
231:
232: public boolean supportsIncrementalFlush() {
233: return allowFlush;
234: }
235:
236: public OpenJPASavepoint newSavepoint(String name, Broker broker) {
237: TrackingSavepoint sp = new TrackingSavepoint(broker, name);
238: _assigned.put(sp.getName(), sp);
239: return sp;
240: }
241: }
242:
243: private static class TrackingSavepoint extends OpenJPASavepoint {
244:
245: int flags = 0;
246:
247: public TrackingSavepoint(Broker broker, String name) {
248: super (broker, name, false);
249: }
250:
251: public Collection rollback(Collection previous) {
252: if ((flags & (ROLLBACK | RELEASED)) != 0)
253: fail("already used");
254: flags |= ROLLBACK;
255: return super .rollback(previous);
256: }
257:
258: public void release(boolean user) {
259: if ((flags & (ROLLBACK | RELEASED)) != 0)
260: fail("already used");
261: flags |= RELEASED;
262: if (user) {
263: if ((flags & USER) != 0)
264: fail("already released");
265: flags |= USER;
266: }
267:
268: super.release(user);
269: }
270: }
271: }
|