KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > recover > test > CrashOfFirstRemoteResourceBeforeItPreparesJBRemIIOPTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.recover.test;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.rmi.PortableRemoteObject JavaDoc;
28 import javax.transaction.TransactionRolledbackException JavaDoc;
29
30 import junit.framework.Test;
31
32 import org.jboss.logging.Logger;
33 import org.jboss.test.recover.interfaces.DummyXAResource;
34 import org.jboss.test.recover.interfaces.XAResourceEnlisterCaller;
35 import org.jboss.test.recover.interfaces.XAResourceEnlisterCallerHome;
36
37 /**
38  * A CrashOfFirstRemoteResourceBeforeItPreparesJBRemIIOPTestCase.
39  *
40  * @author <a HREF="reverbel@ime.usp.br">Francisco Reverbel</a>
41  * @version $Revision: 37406 $
42  */

43 public class CrashOfFirstRemoteResourceBeforeItPreparesJBRemIIOPTestCase
44       extends JBossCrashRecoveryTestCase
45 {
46    private Logger log = Logger.getLogger(this.getClass());
47
48    public CrashOfFirstRemoteResourceBeforeItPreparesJBRemIIOPTestCase(String JavaDoc name)
49       throws Exception JavaDoc
50    {
51       super(name);
52    }
53    
54    public void test() throws Exception JavaDoc
55    {
56       log.info("*** starting " + getUnqualifiedClassName() + " ***");
57       
58       DummyXAResource xaRes1 = getXAResource("DummyRecoverableProxy1");
59       DummyXAResource xaRes2 = getXAResource("DummyRecoverableProxy2");
60       DummyXAResource xaRes3 = getXAResource("DummyRecoverableProxy3");
61
62       xaRes1.clear();
63       xaRes2.clear();
64       xaRes3.clear();
65
66       Object JavaDoc obj;
67       XAResourceEnlisterCallerHome home;
68       
69       obj = getInitialContext().lookup(
70                                     "dtmrectest/XAResourceEnlisterCallerEJB");
71       home = (XAResourceEnlisterCallerHome)
72          PortableRemoteObject.narrow(obj, XAResourceEnlisterCallerHome.class);
73       XAResourceEnlisterCaller xaResEnlisterCaller = home.create();
74
75       for (int i = 0; i < N; i++)
76          xaResEnlisterCaller.callXAResourceEnlistersOverJBRemAndIIOP();
77          
78       int xa1CommitsBefore = xaRes1.getCommittedCount();
79       int xa2CommitsBefore = xaRes2.getCommittedCount();
80       int xa3CommitsBefore = xaRes3.getCommittedCount();
81
82       log.info("Commits before crash of remote resource: " + xa1CommitsBefore +
83                ", " + xa2CommitsBefore + ", " + xa3CommitsBefore);
84
85       ObjectName JavaDoc logger = new ObjectName JavaDoc("jboss:service=RecoveryLogger");
86       MBeanServerConnection JavaDoc firstResource = getResource1Server();
87       try
88       {
89          // Make first remote resource crash before saving its prepare decision.
90
// The first remote resource will crash before answering the prepare
91
// call from the root coordinator, which will rollback the transaction
92
// before calling prepare on the second remote resource.
93
firstResource.setAttribute(logger, new Attribute JavaDoc("CrashOnFailure",
94                                                           Boolean.TRUE));
95          firstResource.setAttribute(logger, new Attribute JavaDoc("FailBeforePreparing",
96                                                           Boolean.TRUE));
97          firstResource.setAttribute(logger, new Attribute JavaDoc("FailBefore",
98                                                           new Integer JavaDoc(0)));
99          xaResEnlisterCaller.callXAResourceEnlistersOverJBRemAndIIOP();
100          fail("TransactionRolledbackException expected.");
101       }
102       catch (TransactionRolledbackException JavaDoc e)
103       {
104          log.info("Expected exception: ", e);
105       }
106       catch (Throwable JavaDoc e)
107       {
108          log.info("Unexpected throwable:", e);
109          fail("Unexpected throwable: " + e);
110       }
111
112       int xa1CommitsAfter = xaRes1.getCommittedCount();
113       int xa2CommitsAfter = xaRes2.getCommittedCount();
114       int xa3CommitsAfter = xaRes3.getCommittedCount();
115
116       int xa1Rollbacks = xaRes1.getRollbackCount();
117       int xa2Rollbacks = xaRes2.getRollbackCount();
118       int xa3Rollbacks = xaRes3.getRollbackCount();
119
120       log.info("Commits after crash of remote resource: " + xa1CommitsAfter +
121                ", " + xa2CommitsAfter + ", " + xa3CommitsAfter);
122       log.info("Rollbacks after crash of remote resource: " + xa1CommitsAfter +
123             ", " + xa2CommitsAfter + ", " + xa3CommitsAfter);
124
125       assertEquals(3 * N, xa1CommitsBefore);
126       assertEquals(3 * N, xa2CommitsBefore);
127       assertEquals(2 * N, xa3CommitsBefore);
128       assertEquals(3 * N, xa1CommitsAfter);
129       assertEquals(3 * N, xa2CommitsAfter);
130       assertEquals(2 * N, xa3CommitsAfter);
131       // expected XA rollbacks are (2, 2, 1) rather than (3, 3, 2), because
132
// the crashed remote resource did not drive XA rollbacks:
133
// (2, 2, 1) = (1, 1, 0) /* XA rollbacks driven by the root coordinator */
134
// + (0, 0, 0) /* XA rollbacks driven by the first resource */
135
// + (1, 1, 1) /* XA rollbacks driven by the second resouce */
136
assertEquals(2, xa1Rollbacks);
137       assertEquals(2, xa2Rollbacks);
138       assertEquals(1, xa3Rollbacks);
139    }
140
141    public static Test suite() throws Exception JavaDoc
142    {
143       return suite(CrashOfFirstRemoteResourceBeforeItPreparesJBRemIIOPTestCase.class);
144    }
145
146 }
147
Popular Tags