001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.transaction;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.test.AbstractTestCase;
022:
023: public class LocalTransactionTests extends AbstractTestCase {
024:
025: protected String[] getMappings() {
026: return new String[] { "transaction/A.cpm.xml" };
027: }
028:
029: public void testNestedLocalTransactionsWithCommits()
030: throws Exception {
031: CompassSession session = openSession();
032: CompassTransaction tr = session.beginTransaction();
033:
034: Long id = new Long(1);
035: A a = new A();
036: a.setId(id);
037: session.save(a);
038:
039: CompassSession nestedSession = openSession();
040: CompassTransaction nestedTr = nestedSession.beginTransaction();
041: assertTrue(session == nestedSession);
042: a = (A) nestedSession.get(A.class, id);
043: assertNotNull(a);
044: nestedTr.commit();
045: nestedSession.close();
046:
047: a = (A) session.get(A.class, id);
048: assertNotNull(a);
049:
050: tr.commit();
051:
052: // verify that the instance was saved
053: tr = session.beginTransaction();
054: a = (A) session.get(A.class, id);
055: assertNotNull(a);
056:
057: tr.commit();
058: session.close();
059: }
060:
061: public void testNestedLocalTransactionsWithNestedRollback()
062: throws Exception {
063: CompassSession session = openSession();
064: CompassTransaction tr = session.beginTransaction();
065:
066: Long id = new Long(1);
067: A a = new A();
068: a.setId(id);
069: session.save(a);
070:
071: CompassSession nestedSession = openSession();
072: CompassTransaction nestedTr = nestedSession.beginTransaction();
073: assertTrue(session == nestedSession);
074: a = (A) nestedSession.get(A.class, id);
075: assertNotNull(a);
076: nestedTr.rollback();
077: nestedSession.close();
078:
079: try {
080: a = (A) session.get(A.class, id);
081: tr.commit();
082: fail();
083: } catch (Exception e) {
084: try {
085: tr.rollback();
086: } catch (Exception e1) {
087:
088: }
089: }
090: session.close();
091: }
092:
093: public void testNestedLocalTransacitonWithCommitsAndNoNestedBegin()
094: throws Exception {
095: CompassSession session = openSession();
096: CompassTransaction tr = session.beginTransaction();
097:
098: Long id = new Long(1);
099: A a = new A();
100: a.setId(id);
101: session.save(a);
102:
103: CompassSession nestedSession = openSession();
104: assertTrue(session == nestedSession);
105: a = (A) nestedSession.get(A.class, id);
106: assertNotNull(a);
107: // this actually might not be called as well
108: // nestedSession.close();
109:
110: a = (A) session.get(A.class, id);
111: assertNotNull(a);
112:
113: tr.commit();
114:
115: // verify that the instance was saved
116: tr = session.beginTransaction();
117: a = (A) session.get(A.class, id);
118: assertNotNull(a);
119:
120: tr.commit();
121: session.close();
122: }
123: }
|