KickJava   Java API By Example, From Geeks To Geeks.

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


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 class RecoveryTest extends TestCase {
37
38     XidFactory xidFactory = new XidFactoryImpl();
39     private final String JavaDoc RM1 = "rm1";
40     private final String JavaDoc RM2 = "rm2";
41     private final String JavaDoc RM3 = "rm3";
42
43     public void testCommittedRMToBeRecovered() throws Exception JavaDoc {
44         MockLog mockLog = new MockLog();
45         Xid JavaDoc[] xids = getXidArray(1);
46         // specifies an empty Xid array such that this XAResource has nothing
47
// to recover. This means that the presumed abort protocol has failed
48
// right after the commit of the RM and before the force-write of the
49
// committed log record.
50
MockXAResource xares1 = new MockXAResource(RM1, new Xid JavaDoc[0]);
51         MockTransactionInfo[] txInfos = makeTxInfos(xids);
52         addBranch(txInfos, xares1);
53         prepareLog(mockLog, txInfos);
54         Recovery recovery = new RecoveryImpl(mockLog, xidFactory);
55         recovery.recoverLog();
56         assertTrue(!recovery.hasRecoveryErrors());
57         assertTrue(recovery.getExternalXids().isEmpty());
58         assertTrue(!recovery.localRecoveryComplete());
59         recovery.recoverResourceManager(xares1);
60         assertEquals(0, xares1.committed.size());
61         assertTrue(recovery.localRecoveryComplete());
62         
63     }
64     
65     public void test2ResOnlineAfterRecoveryStart() throws Exception JavaDoc {
66         MockLog mockLog = new MockLog();
67         Xid JavaDoc[] xids = getXidArray(3);
68         MockXAResource xares1 = new MockXAResource(RM1, xids);
69         MockXAResource xares2 = new MockXAResource(RM2, xids);
70         MockTransactionInfo[] txInfos = makeTxInfos(xids);
71         addBranch(txInfos, xares1);
72         addBranch(txInfos, xares2);
73         prepareLog(mockLog, txInfos);
74         Recovery recovery = new RecoveryImpl(mockLog, xidFactory);
75         recovery.recoverLog();
76         assertTrue(!recovery.hasRecoveryErrors());
77         assertTrue(recovery.getExternalXids().isEmpty());
78         assertTrue(!recovery.localRecoveryComplete());
79         recovery.recoverResourceManager(xares1);
80         assertTrue(!recovery.localRecoveryComplete());
81         assertEquals(3, xares1.committed.size());
82         recovery.recoverResourceManager(xares2);
83         assertEquals(3, xares2.committed.size());
84         assertTrue(recovery.localRecoveryComplete());
85
86     }
87
88     private void addBranch(MockTransactionInfo[] txInfos, MockXAResource xaRes) {
89         for (int i = 0; i < txInfos.length; i++) {
90             MockTransactionInfo txInfo = txInfos[i];
91             txInfo.branches.add(new MockTransactionBranchInfo(xaRes.getName()));
92         }
93     }
94
95     private MockTransactionInfo[] makeTxInfos(Xid JavaDoc[] xids) {
96         MockTransactionInfo[] txInfos = new MockTransactionInfo[xids.length];
97         for (int i = 0; i < xids.length; i++) {
98             Xid JavaDoc xid = xids[i];
99             txInfos[i] = new MockTransactionInfo(xid, new ArrayList JavaDoc());
100         }
101         return txInfos;
102     }
103
104     public void test3ResOnlineAfterRecoveryStart() throws Exception JavaDoc {
105         MockLog mockLog = new MockLog();
106         Xid JavaDoc[] xids12 = getXidArray(3);
107         List JavaDoc xids12List = Arrays.asList(xids12);
108         Xid JavaDoc[] xids13 = getXidArray(3);
109         List JavaDoc xids13List = Arrays.asList(xids13);
110         Xid JavaDoc[] xids23 = getXidArray(3);
111         List JavaDoc xids23List = Arrays.asList(xids23);
112         ArrayList JavaDoc tmp = new ArrayList JavaDoc();
113         tmp.addAll(xids12List);
114         tmp.addAll(xids13List);
115         Xid JavaDoc[] xids1 = (Xid JavaDoc[]) tmp.toArray(new Xid JavaDoc[6]);
116         tmp.clear();
117         tmp.addAll(xids12List);
118         tmp.addAll(xids23List);
119         Xid JavaDoc[] xids2 = (Xid JavaDoc[]) tmp.toArray(new Xid JavaDoc[6]);
120         tmp.clear();
121         tmp.addAll(xids13List);
122         tmp.addAll(xids23List);
123         Xid JavaDoc[] xids3 = (Xid JavaDoc[]) tmp.toArray(new Xid JavaDoc[6]);
124
125         MockXAResource xares1 = new MockXAResource(RM1, xids1);
126         MockXAResource xares2 = new MockXAResource(RM2, xids2);
127         MockXAResource xares3 = new MockXAResource(RM3, xids3);
128         MockTransactionInfo[] txInfos12 = makeTxInfos(xids12);
129         addBranch(txInfos12, xares1);
130         addBranch(txInfos12, xares2);
131         prepareLog(mockLog, txInfos12);
132         MockTransactionInfo[] txInfos13 = makeTxInfos(xids13);
133         addBranch(txInfos13, xares1);
134         addBranch(txInfos13, xares3);
135         prepareLog(mockLog, txInfos13);
136         MockTransactionInfo[] txInfos23 = makeTxInfos(xids23);
137         addBranch(txInfos23, xares2);
138         addBranch(txInfos23, xares3);
139         prepareLog(mockLog, txInfos23);
140         Recovery recovery = new RecoveryImpl(mockLog, xidFactory);
141         recovery.recoverLog();
142         assertTrue(!recovery.hasRecoveryErrors());
143         assertTrue(recovery.getExternalXids().isEmpty());
144         assertEquals(9, recovery.localUnrecoveredCount());
145         recovery.recoverResourceManager(xares1);
146         assertEquals(9, recovery.localUnrecoveredCount());
147         assertEquals(6, xares1.committed.size());
148         recovery.recoverResourceManager(xares2);
149         assertEquals(6, recovery.localUnrecoveredCount());
150         assertEquals(6, xares2.committed.size());
151         recovery.recoverResourceManager(xares3);
152         assertEquals(0, recovery.localUnrecoveredCount());
153         assertEquals(6, xares3.committed.size());
154
155     }
156
157     private void prepareLog(TransactionLog txLog, MockTransactionInfo[] txInfos) throws LogException {
158         for (int i = 0; i < txInfos.length; i++) {
159             MockTransactionInfo txInfo = txInfos[i];
160             txLog.prepare(txInfo.globalXid, txInfo.branches);
161         }
162     }
163
164
165     private Xid JavaDoc[] getXidArray(int i) {
166         Xid JavaDoc[] xids = new Xid JavaDoc[i];
167         for (int j = 0; j < xids.length; j++) {
168             xids[j] = xidFactory.createXid();
169         }
170         return xids;
171     }
172
173     private static class MockXAResource implements NamedXAResource {
174
175         private final String JavaDoc name;
176         private final Xid JavaDoc[] xids;
177         private final List JavaDoc committed = new ArrayList JavaDoc();
178         private final List JavaDoc rolledBack = new ArrayList JavaDoc();
179
180         public MockXAResource(String JavaDoc name, Xid JavaDoc[] xids) {
181             this.name = name;
182             this.xids = xids;
183         }
184
185         public String JavaDoc getName() {
186             return name;
187         }
188
189         public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
190             committed.add(xid);
191         }
192
193         public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
194         }
195
196         public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
197         }
198
199         public int getTransactionTimeout() throws XAException JavaDoc {
200             return 0;
201         }
202
203         public boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc {
204             return false;
205         }
206
207         public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
208             return 0;
209         }
210
211         public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
212             return xids;
213         }
214
215         public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
216             rolledBack.add(xid);
217         }
218
219         public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
220             return false;
221         }
222
223         public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
224         }
225
226         public List JavaDoc getCommitted() {
227             return committed;
228         }
229
230         public List JavaDoc getRolledBack() {
231             return rolledBack;
232         }
233
234
235     }
236
237     private static class MockTransactionInfo {
238         private Xid JavaDoc globalXid;
239         private List JavaDoc branches;
240
241         public MockTransactionInfo(Xid JavaDoc globalXid, List JavaDoc branches) {
242             this.globalXid = globalXid;
243             this.branches = branches;
244         }
245     }
246
247     private static class MockTransactionBranchInfo implements TransactionBranchInfo {
248         private String JavaDoc name;
249
250         public MockTransactionBranchInfo(String JavaDoc name) {
251             this.name = name;
252         }
253
254         public String JavaDoc getResourceName() {
255             return name;
256         }
257
258         public Xid JavaDoc getBranchXid() {
259             return null;
260         }
261     }
262 }
263
Popular Tags