001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.transaction.jta11;
017:
018: import javax.transaction.Synchronization;
019: import javax.transaction.HeuristicMixedException;
020: import javax.transaction.HeuristicRollbackException;
021: import javax.transaction.RollbackException;
022: import javax.transaction.SystemException;
023: import javax.transaction.NotSupportedException;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
029: */
030: public class TransactionSynchronizationRegistryTest extends TestCase {
031:
032: private static int beforeCounter = 0;
033: private static int afterCounter = 0;
034:
035: private GeronimoTransactionManagerJTA11 tm;
036:
037: private CountingSync interposedSync;
038: private CountingSync normalSync;
039:
040: protected void setUp() throws Exception {
041: tm = new GeronimoTransactionManagerJTA11();
042: }
043:
044: private void setUpInterposedSync() throws NotSupportedException,
045: SystemException {
046: interposedSync = new CountingSync();
047: tm.begin();
048: tm.registerInterposedSynchronization(interposedSync);
049: }
050:
051: private void setUpSyncs() throws Exception {
052: normalSync = new CountingSync();
053: setUpInterposedSync();
054: tm.getTransaction().registerSynchronization(normalSync);
055: }
056:
057: public void testInterposedSynchIsCalledOnCommit() throws Exception {
058: setUpInterposedSync();
059: tm.commit();
060: checkInterposedSyncCalled();
061: }
062:
063: private void checkInterposedSyncCalled() {
064: assertTrue("interposedSync beforeCompletion was not called",
065: interposedSync.getBeforeCount() != -1);
066: assertTrue("interposedSync afterCompletion was not called",
067: interposedSync.getAfterCount() != -1);
068: }
069:
070: public void testInterposedSynchIsCalledOnRollback()
071: throws Exception {
072: setUpInterposedSync();
073: tm.rollback();
074: checkInterposedSyncCalled();
075: }
076:
077: public void testInterposedSynchIsCalledOnMarkRollback()
078: throws Exception {
079: setUpInterposedSync();
080: tm.setRollbackOnly();
081: try {
082: tm.commit();
083: fail("expected a RollbackException");
084: } catch (HeuristicMixedException e) {
085: fail("expected a RollbackException not " + e.getClass());
086: } catch (HeuristicRollbackException e) {
087: fail("expected a RollbackException not " + e.getClass());
088: } catch (IllegalStateException e) {
089: fail("expected a RollbackException not " + e.getClass());
090: } catch (RollbackException e) {
091:
092: } catch (SecurityException e) {
093: fail("expected a RollbackException not " + e.getClass());
094: } catch (SystemException e) {
095: fail("expected a RollbackException not " + e.getClass());
096: }
097: checkInterposedSyncCalled();
098: }
099:
100: public void testSynchCallOrderOnCommit() throws Exception {
101: setUpSyncs();
102: tm.commit();
103: checkSyncCallOrder();
104: }
105:
106: private void checkSyncCallOrder() {
107: checkInterposedSyncCalled();
108: assertTrue(
109: "interposedSync beforeCompletion was not called after normalSync beforeCompletion",
110: interposedSync.getBeforeCount() > normalSync
111: .getBeforeCount());
112: assertTrue(
113: "interposedSync afterCompletion was not called before normalSync beforeCompletion",
114: interposedSync.getAfterCount() < normalSync
115: .getAfterCount());
116: }
117:
118: public void testSynchCallOrderOnRollback() throws Exception {
119: setUpSyncs();
120: tm.rollback();
121: checkSyncCallOrder();
122: }
123:
124: public void testSynchCallOrderOnMarkRollback() throws Exception {
125: setUpSyncs();
126: tm.setRollbackOnly();
127: try {
128: tm.commit();
129: fail("expected a RollbackException");
130: } catch (HeuristicMixedException e) {
131: fail("expected a RollbackException not " + e.getClass());
132: } catch (HeuristicRollbackException e) {
133: fail("expected a RollbackException not " + e.getClass());
134: } catch (IllegalStateException e) {
135: fail("expected a RollbackException not " + e.getClass());
136: } catch (RollbackException e) {
137:
138: } catch (SecurityException e) {
139: fail("expected a RollbackException not " + e.getClass());
140: } catch (SystemException e) {
141: fail("expected a RollbackException not " + e.getClass());
142: }
143: checkSyncCallOrder();
144: }
145:
146: private class CountingSync implements Synchronization {
147:
148: private int beforeCount = -1;
149: private int afterCount = -1;
150:
151: public void beforeCompletion() {
152: beforeCount = beforeCounter++;
153: }
154:
155: public void afterCompletion(int i) {
156: afterCount = afterCounter++;
157: }
158:
159: public int getBeforeCount() {
160: return beforeCount;
161: }
162:
163: public int getAfterCount() {
164: return afterCount;
165: }
166: }
167:
168: }
|