KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > manager > AbstractRecoveryTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.manager;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.transaction.xa.XAException JavaDoc;
25 import javax.transaction.xa.XAResource JavaDoc;
26 import javax.transaction.xa.Xid JavaDoc;
27
28 import junit.framework.TestCase;
29
30 /**
31  * This is just a unit test for recovery, depending on proper behavior of the log(s) it uses.
32  *
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  *
35  * */

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 JavaDoc RM1 = "rm1";
42     private static final String JavaDoc RM2 = "rm2";
43     private static final String JavaDoc RM3 = "rm3";
44     private static final int XID_COUNT = 3;
45     private int branchCounter;
46
47     public void testDummy() throws Exception JavaDoc {}
48
49     public void test2ResOnlineAfterRecoveryStart() throws Exception JavaDoc {
50         Xid JavaDoc[] 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 JavaDoc {
72         Xid JavaDoc[] xids12 = getXidArray(XID_COUNT);
73         List JavaDoc xids12List = Arrays.asList(xids12);
74         Xid JavaDoc[] xids13 = getXidArray(XID_COUNT);
75         List JavaDoc xids13List = Arrays.asList(xids13);
76         Xid JavaDoc[] xids23 = getXidArray(XID_COUNT);
77         List JavaDoc xids23List = Arrays.asList(xids23);
78         ArrayList JavaDoc tmp = new ArrayList JavaDoc();
79         tmp.addAll(xids12List);
80         tmp.addAll(xids13List);
81         Xid JavaDoc[] xids1 = (Xid JavaDoc[]) tmp.toArray(new Xid JavaDoc[6]);
82         tmp.clear();
83         tmp.addAll(xids12List);
84         tmp.addAll(xids23List);
85         Xid JavaDoc[] xids2 = (Xid JavaDoc[]) tmp.toArray(new Xid JavaDoc[6]);
86         tmp.clear();
87         tmp.addAll(xids13List);
88         tmp.addAll(xids23List);
89         Xid JavaDoc[] xids3 = (Xid JavaDoc[]) tmp.toArray(new Xid JavaDoc[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 JavaDoc;
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 JavaDoc[] getXidArray(int i) {
135         Xid JavaDoc[] xids = new Xid JavaDoc[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 JavaDoc globalXid = txInfo.globalXid;
146             Xid JavaDoc branchXid = xidFactory.createBranch(globalXid, branchCounter++);
147             txInfo.branches.add(new MockTransactionBranchInfo(xaRes.getName(), branchXid));
148         }
149     }
150
151     private MockTransactionInfo[] makeTxInfos(Xid JavaDoc[] xids) {
152         MockTransactionInfo[] txInfos = new MockTransactionInfo[xids.length];
153         for (int i = 0; i < xids.length; i++) {
154             Xid JavaDoc xid = xids[i];
155             txInfos[i] = new MockTransactionInfo(xid, new ArrayList JavaDoc());
156         }
157         return txInfos;
158     }
159
160     private static class MockXAResource implements NamedXAResource {
161
162         private final String JavaDoc name;
163         private final Xid JavaDoc[] xids;
164         private final List JavaDoc committed = new ArrayList JavaDoc();
165         private final List JavaDoc rolledBack = new ArrayList JavaDoc();
166
167         public MockXAResource(String JavaDoc name, Xid JavaDoc[] xids) {
168             this.name = name;
169             this.xids = xids;
170         }
171
172         public String JavaDoc getName() {
173             return name;
174         }
175
176         public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
177             committed.add(xid);
178         }
179
180         public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
181         }
182
183         public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
184         }
185
186         public int getTransactionTimeout() throws XAException JavaDoc {
187             return 0;
188         }
189
190         public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
191             return false;
192         }
193
194         public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
195             return 0;
196         }
197
198         public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
199             return xids;
200         }
201
202         public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
203             rolledBack.add(xid);
204         }
205
206         public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
207             return false;
208         }
209
210         public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
211         }
212
213         public List JavaDoc getCommitted() {
214             return committed;
215         }
216
217         public List JavaDoc getRolledBack() {
218             return rolledBack;
219         }
220
221
222     }
223
224     private static class MockTransactionInfo {
225         private Xid JavaDoc globalXid;
226         private List JavaDoc branches;
227
228         public MockTransactionInfo(Xid JavaDoc globalXid, List JavaDoc branches) {
229             this.globalXid = globalXid;
230             this.branches = branches;
231         }
232     }
233
234     private static class MockTransactionBranchInfo implements TransactionBranchInfo {
235         private final String JavaDoc name;
236         private final Xid JavaDoc branchXid;
237
238         public MockTransactionBranchInfo(String JavaDoc name, Xid JavaDoc branchXid) {
239             this.name = name;
240             this.branchXid = branchXid;
241         }
242
243         public String JavaDoc getResourceName() {
244             return name;
245         }
246
247         public Xid JavaDoc getBranchXid() {
248             return branchXid;
249         }
250     }
251 }
252
Popular Tags