001: package org.apache.ojb.broker;
002:
003: import org.apache.ojb.broker.query.Criteria;
004: import org.apache.ojb.broker.query.Query;
005: import org.apache.ojb.broker.query.QueryByCriteria;
006: import org.apache.ojb.broker.util.collections.ManageableVector;
007: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
008: import org.apache.ojb.junit.PBTestCase;
009:
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.Iterator;
013:
014: /**
015: * Tests rollback and (simple) commit behaviour.
016: *
017: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
018: */
019: public class PBRollbackTest extends PBTestCase {
020: public PBRollbackTest(String s) {
021: super (s);
022: }
023:
024: public void testEmptyTxDemarcation_1() {
025: try {
026: broker.beginTransaction();
027:
028: broker.commitTransaction();
029: } catch (Exception e) {
030: e.printStackTrace();
031: fail("'Empty' transaction demarcation sequence fails");
032: }
033: }
034:
035: public void testEmptyTxDemarcation_2() {
036: try {
037: broker.beginTransaction();
038:
039: broker.abortTransaction();
040: } catch (Exception e) {
041: e.printStackTrace();
042: fail("'Empty' transaction demarcation sequence fails");
043: }
044: }
045:
046: public void testUserCommitClearCache() throws Exception {
047: Collection projects = getNewProjects(10);
048: storeObjects(broker, projects);
049: Criteria c = new Criteria();
050: Query q = new QueryByCriteria(Project.class, c);
051: int beforeCommit = broker.getCount(q);
052:
053: broker.beginTransaction();
054: broker.clearCache();
055: storeObjects(broker, getNewProjects(10));
056: //while transaction we should see all stored objects
057: int whileTransaction = broker.getCount(q);
058: ManageableCollection result = broker.getCollectionByQuery(
059: ManageableVector.class, q);
060: int whileTransactionMC = 0;
061: Iterator it = result.ojbIterator();
062: while (it.hasNext()) {
063: it.next();
064: ++whileTransactionMC;
065: }
066: broker.commitTransaction();
067: //explicit clear cache
068: broker.clearCache();
069:
070: c = new Criteria();
071: q = new QueryByCriteria(Project.class, c);
072: int afterCommit = broker.getCount(q);
073:
074: assertEquals(beforeCommit + 10, afterCommit);
075: assertEquals(beforeCommit + 10, whileTransaction);
076: assertEquals(beforeCommit + 10, whileTransactionMC);
077: }
078:
079: public void testUserCommit() throws Exception {
080: Collection projects = getNewProjects(10);
081: storeObjects(broker, projects);
082: Criteria c = new Criteria();
083: Query q = new QueryByCriteria(Project.class, c);
084: int beforeCommit = broker.getCount(q);
085:
086: broker.beginTransaction();
087: storeObjects(broker, getNewProjects(10));
088: int whileTransaction = broker.getCount(q);
089: ManageableCollection result = broker.getCollectionByQuery(
090: ManageableVector.class, q);
091: int whileTransactionMC = 0;
092: Iterator it = result.ojbIterator();
093: while (it.hasNext()) {
094: it.next();
095: ++whileTransactionMC;
096: }
097: broker.commitTransaction();
098:
099: c = new Criteria();
100: q = new QueryByCriteria(Project.class, c);
101: broker.beginTransaction();
102: int afterCommit = broker.getCount(q);
103: broker.commitTransaction();
104:
105: assertEquals(beforeCommit + 10, afterCommit);
106: assertEquals(beforeCommit + 10, whileTransaction);
107: assertEquals(beforeCommit + 10, whileTransactionMC);
108: }
109:
110: public void testUserRollbackClearCache() throws Exception {
111: Collection projects = getNewProjects(10);
112: storeObjects(broker, projects);
113: Criteria c = new Criteria();
114: Query q = new QueryByCriteria(Project.class, c);
115: broker.beginTransaction();
116: int beforeRollback = broker.getCount(q);
117: broker.commitTransaction();
118:
119: broker.beginTransaction();
120: storeObjects(broker, getNewProjects(10));
121: broker.clearCache();
122:
123: int whileTransaction = broker.getCount(q);
124: ManageableCollection result = broker.getCollectionByQuery(
125: ManageableVector.class, q);
126: int whileTransactionMC = 0;
127: Iterator it = result.ojbIterator();
128: while (it.hasNext()) {
129: it.next();
130: ++whileTransactionMC;
131: }
132:
133: broker.abortTransaction();
134: //explicit clear cache
135: broker.clearCache();
136:
137: c = new Criteria();
138: q = new QueryByCriteria(Project.class, c);
139: int afterRollback = broker.getCount(q);
140:
141: assertEquals(beforeRollback, afterRollback);
142: assertEquals(beforeRollback + 10, whileTransaction);
143: assertEquals(beforeRollback + 10, whileTransactionMC);
144: }
145:
146: public void testUserRollback() throws Exception {
147: Collection projects = getNewProjects(10);
148: storeObjects(broker, projects);
149: Criteria c = new Criteria();
150: Query q = new QueryByCriteria(Project.class, c);
151: broker.beginTransaction();
152: int beforeRollback = broker.getCount(q);
153: broker.commitTransaction();
154:
155: broker.beginTransaction();
156: storeObjects(broker, getNewProjects(10));
157:
158: int whileTransaction = broker.getCount(q);
159: ManageableCollection result = broker.getCollectionByQuery(
160: ManageableVector.class, q);
161: int whileTransactionMC = 0;
162: Iterator it = result.ojbIterator();
163: while (it.hasNext()) {
164: it.next();
165: ++whileTransactionMC;
166: }
167:
168: broker.abortTransaction();
169:
170: c = new Criteria();
171: q = new QueryByCriteria(Project.class, c);
172: int afterRollback = broker.getCount(q);
173:
174: assertEquals(beforeRollback, afterRollback);
175: assertEquals(beforeRollback + 10, whileTransaction);
176: assertEquals(beforeRollback + 10, whileTransactionMC);
177: }
178:
179: public void testRollbackCausedByNotExistingObject()
180: throws Exception {
181: Collection projects = getNewProjects(10);
182: Query q;
183: int beforeRollback;
184: try {
185: broker.beginTransaction();
186: storeObjects(broker, projects);
187: broker.commitTransaction();
188:
189: Criteria c = new Criteria();
190: q = new QueryByCriteria(Project.class, c);
191: beforeRollback = broker.getCount(q);
192: } finally {
193: if (broker != null && !broker.isClosed())
194: broker.close();
195: }
196:
197: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
198: try {
199: broker.beginTransaction();
200: storeObjects(broker, getNewProjects(10));
201: //should fail
202: broker.store(new Dummy());
203:
204: fail("Test should throw a exception in place");
205: broker.commitTransaction();
206: } catch (PersistenceBrokerException e) {
207: assertTrue(true);
208: // e.printStackTrace();
209: broker.abortTransaction();
210: }
211:
212: int afterRollback = broker.getCount(q);
213:
214: assertEquals("Object count does not match after rollback",
215: beforeRollback, afterRollback);
216: }
217:
218: public void testRollbackCausedBySQLException() throws Exception {
219: // first we change metadata settings
220: ojbChangeReferenceSetting(Project.class, "persons", true,
221: ObjectReferenceDescriptor.CASCADE_OBJECT,
222: ObjectReferenceDescriptor.CASCADE_OBJECT, false);
223: ArrayList projects = getNewProjects(5);
224: Query q;
225: int beforeRollback;
226:
227: broker.beginTransaction();
228: storeObjects(broker, projects);
229: broker.commitTransaction();
230:
231: Criteria c = new Criteria();
232: q = new QueryByCriteria(Project.class, c);
233: beforeRollback = broker.getCount(q);
234:
235: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
236: try {
237: broker.beginTransaction();
238: projects = getNewProjects(5);
239: Project badProject = (Project) projects.get(0);
240: badProject.setTitle("Bad project!");
241: // set wrong kind of object to force exception
242: badProject.setPersons(projects);
243:
244: System.err
245: .println("!! The following SQLException is part of the Test !!");
246: storeObjects(broker, projects);
247:
248: fail("Test should throw a exception in place");
249: broker.commitTransaction();
250: } catch (PersistenceBrokerException e) {
251: assertTrue(true);
252: // e.printStackTrace();
253: broker.abortTransaction();
254: }
255:
256: int afterRollback = broker.getCount(q);
257:
258: assertEquals("Object count does not match after rollback",
259: beforeRollback, afterRollback);
260: }
261:
262: protected void storeObjects(PersistenceBroker broker,
263: Collection objects) {
264: boolean needsCommit = false;
265: if (!broker.isInTransaction()) {
266: broker.beginTransaction();
267: needsCommit = true;
268: }
269: for (Iterator it = objects.iterator(); it.hasNext();) {
270: broker.store(it.next());
271: }
272: if (needsCommit) {
273: broker.commitTransaction();
274: }
275: }
276:
277: private static int counter;
278:
279: protected ArrayList getNewProjects(int count) {
280: ArrayList list = new ArrayList();
281: for (int i = 0; i < count; i++) {
282: list.add(newProject());
283: }
284: return list;
285: }
286:
287: protected Project newProject() {
288: Project p = new Project();
289: ++counter;
290: p.setDescription("Test project " + counter);
291: p.setTitle("Test " + counter);
292: return p;
293: }
294:
295: public static void main(String[] args) {
296: String[] arr = { PBRollbackTest.class.getName() };
297: junit.textui.TestRunner.main(arr);
298: }
299:
300: class Dummy {
301:
302: }
303: }
|