1 17 18 package org.apache.geronimo.transaction.manager; 19 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.List ; 23 24 import javax.transaction.xa.XAException ; 25 import javax.transaction.xa.XAResource ; 26 import javax.transaction.xa.Xid ; 27 28 import junit.framework.TestCase; 29 30 36 public abstract class AbstractRecoveryTest extends TestCase { 37 38 protected TransactionLog txLog; 39 40 protected final XidFactory xidFactory = new XidFactoryImpl(); 41 private static final String RM1 = "rm1"; 42 private static final String RM2 = "rm2"; 43 private static final String RM3 = "rm3"; 44 private static final int XID_COUNT = 3; 45 private int branchCounter; 46 47 public void testDummy() throws Exception {} 48 49 public void test2ResOnlineAfterRecoveryStart() throws Exception { 50 Xid [] xids = getXidArray(XID_COUNT); 51 MockXAResource xares1 = new MockXAResource(RM1, xids); 52 MockXAResource xares2 = new MockXAResource(RM2, xids); 53 MockTransactionInfo[] txInfos = makeTxInfos(xids); 54 addBranch(txInfos, xares1); 55 addBranch(txInfos, xares2); 56 prepareLog(txLog, txInfos); 57 prepareForReplay(); 58 Recovery recovery = new RecoveryImpl(txLog, xidFactory); 59 recovery.recoverLog(); 60 assertTrue(!recovery.hasRecoveryErrors()); 61 assertTrue(recovery.getExternalXids().isEmpty()); 62 assertTrue(!recovery.localRecoveryComplete()); 63 recovery.recoverResourceManager(xares1); 64 assertTrue(!recovery.localRecoveryComplete()); 65 assertEquals(XID_COUNT, xares1.committed.size()); 66 recovery.recoverResourceManager(xares2); 67 assertEquals(XID_COUNT, xares2.committed.size()); 68 assertTrue(recovery.localRecoveryComplete()); 69 } 70 71 public void test3ResOnlineAfterRecoveryStart() throws Exception { 72 Xid [] xids12 = getXidArray(XID_COUNT); 73 List xids12List = Arrays.asList(xids12); 74 Xid [] xids13 = getXidArray(XID_COUNT); 75 List xids13List = Arrays.asList(xids13); 76 Xid [] xids23 = getXidArray(XID_COUNT); 77 List xids23List = Arrays.asList(xids23); 78 ArrayList tmp = new ArrayList (); 79 tmp.addAll(xids12List); 80 tmp.addAll(xids13List); 81 Xid [] xids1 = (Xid []) tmp.toArray(new Xid [6]); 82 tmp.clear(); 83 tmp.addAll(xids12List); 84 tmp.addAll(xids23List); 85 Xid [] xids2 = (Xid []) tmp.toArray(new Xid [6]); 86 tmp.clear(); 87 tmp.addAll(xids13List); 88 tmp.addAll(xids23List); 89 Xid [] xids3 = (Xid []) tmp.toArray(new Xid [6]); 90 91 MockXAResource xares1 = new MockXAResource(RM1, xids1); 92 MockXAResource xares2 = new MockXAResource(RM2, xids2); 93 MockXAResource xares3 = new MockXAResource(RM3, xids3); 94 MockTransactionInfo[] txInfos12 = makeTxInfos(xids12); 95 addBranch(txInfos12, xares1); 96 addBranch(txInfos12, xares2); 97 prepareLog(txLog, txInfos12); 98 MockTransactionInfo[] txInfos13 = makeTxInfos(xids13); 99 addBranch(txInfos13, xares1); 100 addBranch(txInfos13, xares3); 101 prepareLog(txLog, txInfos13); 102 MockTransactionInfo[] txInfos23 = makeTxInfos(xids23); 103 addBranch(txInfos23, xares2); 104 addBranch(txInfos23, xares3); 105 prepareLog(txLog, txInfos23); 106 prepareForReplay(); 107 Recovery recovery = new RecoveryImpl(txLog, xidFactory); 108 recovery.recoverLog(); 109 assertTrue(!recovery.hasRecoveryErrors()); 110 assertTrue(recovery.getExternalXids().isEmpty()); 111 assertEquals(XID_COUNT * 3, recovery.localUnrecoveredCount()); 112 recovery.recoverResourceManager(xares1); 113 assertEquals(XID_COUNT * 3, recovery.localUnrecoveredCount()); 114 assertEquals(XID_COUNT * 2, xares1.committed.size()); 115 recovery.recoverResourceManager(xares2); 116 assertEquals(XID_COUNT * 2, recovery.localUnrecoveredCount()); 117 assertEquals(XID_COUNT * 2, xares2.committed.size()); 118 recovery.recoverResourceManager(xares3); 119 assertEquals(0, recovery.localUnrecoveredCount()); 120 assertEquals(XID_COUNT * 2, xares3.committed.size()); 121 122 } 123 124 protected abstract void prepareForReplay() throws Exception ; 125 126 private void prepareLog(TransactionLog txLog, MockTransactionInfo[] txInfos) throws LogException { 127 for (int i = 0; i < txInfos.length; i++) { 128 MockTransactionInfo txInfo = txInfos[i]; 129 txLog.prepare(txInfo.globalXid, txInfo.branches); 130 } 131 } 132 133 134 private Xid [] getXidArray(int i) { 135 Xid [] xids = new Xid [i]; 136 for (int j = 0; j < xids.length; j++) { 137 xids[j] = xidFactory.createXid(); 138 } 139 return xids; 140 } 141 142 private void addBranch(MockTransactionInfo[] txInfos, MockXAResource xaRes) { 143 for (int i = 0; i < txInfos.length; i++) { 144 MockTransactionInfo txInfo = txInfos[i]; 145 Xid globalXid = txInfo.globalXid; 146 Xid branchXid = xidFactory.createBranch(globalXid, branchCounter++); 147 txInfo.branches.add(new MockTransactionBranchInfo(xaRes.getName(), branchXid)); 148 } 149 } 150 151 private MockTransactionInfo[] makeTxInfos(Xid [] xids) { 152 MockTransactionInfo[] txInfos = new MockTransactionInfo[xids.length]; 153 for (int i = 0; i < xids.length; i++) { 154 Xid xid = xids[i]; 155 txInfos[i] = new MockTransactionInfo(xid, new ArrayList ()); 156 } 157 return txInfos; 158 } 159 160 private static class MockXAResource implements NamedXAResource { 161 162 private final String name; 163 private final Xid [] xids; 164 private final List committed = new ArrayList (); 165 private final List rolledBack = new ArrayList (); 166 167 public MockXAResource(String name, Xid [] xids) { 168 this.name = name; 169 this.xids = xids; 170 } 171 172 public String getName() { 173 return name; 174 } 175 176 public void commit(Xid xid, boolean onePhase) throws XAException { 177 committed.add(xid); 178 } 179 180 public void end(Xid xid, int flags) throws XAException { 181 } 182 183 public void forget(Xid xid) throws XAException { 184 } 185 186 public int getTransactionTimeout() throws XAException { 187 return 0; 188 } 189 190 public boolean isSameRM(XAResource xaResource) throws XAException { 191 return false; 192 } 193 194 public int prepare(Xid xid) throws XAException { 195 return 0; 196 } 197 198 public Xid [] recover(int flag) throws XAException { 199 return xids; 200 } 201 202 public void rollback(Xid xid) throws XAException { 203 rolledBack.add(xid); 204 } 205 206 public boolean setTransactionTimeout(int seconds) throws XAException { 207 return false; 208 } 209 210 public void start(Xid xid, int flags) throws XAException { 211 } 212 213 public List getCommitted() { 214 return committed; 215 } 216 217 public List getRolledBack() { 218 return rolledBack; 219 } 220 221 222 } 223 224 private static class MockTransactionInfo { 225 private Xid globalXid; 226 private List branches; 227 228 public MockTransactionInfo(Xid globalXid, List branches) { 229 this.globalXid = globalXid; 230 this.branches = branches; 231 } 232 } 233 234 private static class MockTransactionBranchInfo implements TransactionBranchInfo { 235 private final String name; 236 private final Xid branchXid; 237 238 public MockTransactionBranchInfo(String name, Xid branchXid) { 239 this.name = name; 240 this.branchXid = branchXid; 241 } 242 243 public String getResourceName() { 244 return name; 245 } 246 247 public Xid getBranchXid() { 248 return branchXid; 249 } 250 } 251 } 252 | Popular Tags |