1 17 18 package org.apache.geronimo.transaction.jta11; 19 20 import javax.transaction.Synchronization ; 21 import javax.transaction.HeuristicMixedException ; 22 import javax.transaction.HeuristicRollbackException ; 23 import javax.transaction.RollbackException ; 24 import javax.transaction.SystemException ; 25 import javax.transaction.NotSupportedException ; 26 27 import junit.framework.TestCase; 28 29 32 public class TransactionSynchronizationRegistryTest extends TestCase { 33 34 private static int beforeCounter = 0; 35 private static int afterCounter = 0; 36 37 38 39 private GeronimoTransactionManagerJTA11 tm; 40 41 private CountingSync interposedSync; 42 private CountingSync normalSync; 43 44 protected void setUp() throws Exception { 45 tm = new GeronimoTransactionManagerJTA11(); 46 } 47 48 private void setUpInterposedSync() throws NotSupportedException , SystemException { 49 interposedSync = new CountingSync(); 50 tm.begin(); 51 tm.registerInterposedSynchronization(interposedSync); 52 } 53 54 private void setUpSyncs() throws Exception { 55 normalSync = new CountingSync(); 56 setUpInterposedSync(); 57 tm.getTransaction().registerSynchronization(normalSync); 58 } 59 60 61 public void testInterposedSynchIsCalledOnCommit() throws Exception { 62 setUpInterposedSync(); 63 tm.commit(); 64 checkInterposedSyncCalled(); 65 } 66 67 private void checkInterposedSyncCalled() { 68 assertTrue("interposedSync beforeCompletion was not called", interposedSync.getBeforeCount() != -1); 69 assertTrue("interposedSync afterCompletion was not called", interposedSync.getAfterCount() != -1); 70 } 71 72 public void testInterposedSynchIsCalledOnRollback() throws Exception { 73 setUpInterposedSync(); 74 tm.rollback(); 75 checkInterposedSyncCalled(); 76 } 77 78 public void testInterposedSynchIsCalledOnMarkRollback() throws Exception { 79 setUpInterposedSync(); 80 tm.setRollbackOnly(); 81 try { 82 tm.commit(); 83 fail("expected a RollbackException"); 84 } catch (HeuristicMixedException e) { 85 fail("expected a RollbackException not " + e.getClass()); 86 } catch (HeuristicRollbackException e) { 87 fail("expected a RollbackException not " + e.getClass()); 88 } catch (IllegalStateException e) { 89 fail("expected a RollbackException not " + e.getClass()); 90 } catch (RollbackException e) { 91 92 } catch (SecurityException e) { 93 fail("expected a RollbackException not " + e.getClass()); 94 } catch (SystemException e) { 95 fail("expected a RollbackException not " + e.getClass()); 96 } 97 checkInterposedSyncCalled(); 98 } 99 100 public void testSynchCallOrderOnCommit() throws Exception { 101 setUpSyncs(); 102 tm.commit(); 103 checkSyncCallOrder(); 104 } 105 106 private void checkSyncCallOrder() { 107 checkInterposedSyncCalled(); 108 assertTrue("interposedSync beforeCompletion was not called after normalSync beforeCompletion", interposedSync.getBeforeCount() > normalSync.getBeforeCount()); 109 assertTrue("interposedSync afterCompletion was not called before normalSync beforeCompletion", interposedSync.getAfterCount() < normalSync.getAfterCount()); 110 } 111 112 public void testSynchCallOrderOnRollback() throws Exception { 113 setUpSyncs(); 114 tm.rollback(); 115 checkSyncCallOrder(); 116 } 117 118 public void testSynchCallOrderOnMarkRollback() throws Exception { 119 setUpSyncs(); 120 tm.setRollbackOnly(); 121 try { 122 tm.commit(); 123 fail("expected a RollbackException"); 124 } catch (HeuristicMixedException e) { 125 fail("expected a RollbackException not " + e.getClass()); 126 } catch (HeuristicRollbackException e) { 127 fail("expected a RollbackException not " + e.getClass()); 128 } catch (IllegalStateException e) { 129 fail("expected a RollbackException not " + e.getClass()); 130 } catch (RollbackException e) { 131 132 } catch (SecurityException e) { 133 fail("expected a RollbackException not " + e.getClass()); 134 } catch (SystemException e) { 135 fail("expected a RollbackException not " + e.getClass()); 136 } 137 checkSyncCallOrder(); 138 } 139 140 private class CountingSync implements Synchronization { 141 142 private int beforeCount = -1; 143 private int afterCount = -1; 144 145 public void beforeCompletion() { 146 beforeCount = beforeCounter++; 147 } 148 149 public void afterCompletion(int i) { 150 afterCount = afterCounter++; 151 } 152 153 public int getBeforeCount() { 154 return beforeCount; 155 } 156 157 public int getAfterCount() { 158 return afterCount; 159 } 160 } 161 162 } 163 | Popular Tags |