KickJava   Java API By Example, From Geeks To Geeks.

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


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.naming.Context JavaDoc;
25 import javax.rmi.PortableRemoteObject JavaDoc;
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import junit.framework.Test;
29
30 import org.jboss.test.recover.interfaces.DummyXAResource;
31 import org.jboss.test.recover.interfaces.FrontEnd;
32 import org.jboss.test.recover.interfaces.FrontEndHome;
33 import org.jboss.test.recover.interfaces.XAResourceEnlisterCaller;
34 import org.jboss.test.recover.interfaces.XAResourceEnlisterCallerHome;
35 import org.jboss.tm.recovery.RecoveryTestingException;
36
37 /**
38  * A CrashOfFirstRemoteResourceWithXADatabaseAfterPrepareIIOPTestCase.
39  *
40  * @author <a HREF="reverbel@ime.usp.br">Francisco Reverbel</a>
41  * @version $Revision: 37406 $
42  */

43 public class CrashOfFirstRemoteResourceWithXADatabaseAfterPrepareIIOPTestCase
44       extends JBossCrashRecoveryIIOPTestCase
45 {
46    public CrashOfFirstRemoteResourceWithXADatabaseAfterPrepareIIOPTestCase(String JavaDoc name)
47       throws java.io.IOException JavaDoc
48    {
49       super(name);
50    }
51
52    // Public --------------------------------------------------------
53

54    public void test() throws Exception JavaDoc
55    {
56       getLog().info("*** starting " + getUnqualifiedClassName() + " ***");
57
58       Context JavaDoc ctx = getInitialContext();
59
60       DummyXAResource xaRes1 = getXAResource("DummyRecoverableProxy1");
61       DummyXAResource xaRes2 = getXAResource("DummyRecoverableProxy2");
62       DummyXAResource xaRes3 = getXAResource("DummyRecoverableProxy3");
63
64       xaRes1.clear();
65       xaRes2.clear();
66       xaRes3.clear();
67
68       Object JavaDoc obj;
69       
70       getLog().debug("Obtain FrontEnd home interface");
71       obj = ctx.lookup("dtmrecoverytest/FrontEndEJB");
72       FrontEndHome frontEndHome =
73          (FrontEndHome) PortableRemoteObject.narrow(obj, FrontEndHome.class);
74       
75       getLog().debug("Create FrontEnd bean");
76       FrontEnd frontEnd = frontEndHome.create("testCommittedTx");
77
78       getLog().debug("Obtain XAResourceEnlisterCaller home interface");
79       obj = ctx.lookup("dtmrectest/XAResourceEnlisterCallerEJB");
80       XAResourceEnlisterCallerHome xaResEnlisterCallerHome =
81          (XAResourceEnlisterCallerHome) PortableRemoteObject.narrow(
82                                     obj, XAResourceEnlisterCallerHome.class);
83
84       getLog().debug("Create XAResourceEnlisterCaller bean");
85       XAResourceEnlisterCaller xaResEnlisterCaller =
86          xaResEnlisterCallerHome.create();
87
88       getLog().debug("Obtain UserTransaction instance");
89       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc) ctx.lookup("UserTransaction");
90    
91       getLog().debug("Set balances");
92       userTx.begin();
93       frontEnd.setBalancesOverIIOP(101, 102);
94       userTx.commit();
95    
96       int[] balances;
97
98       getLog().debug("Get balances");
99       userTx.begin();
100       balances = frontEnd.getBalancesOverIIOP();
101       userTx.commit();
102    
103       assertTrue("first balance == 101", balances[0] == 101);
104       assertTrue("second balance == 102", balances[1] == 102);
105    
106       getLog().debug("Update balances");
107       // XAResource usage is as follows:
108
// - xaResEnlisterCaller (the root coordinator) enlists xaRes1
109
// - the first XAResourceEnlister (remote resource 1) enlists xaRes2
110
// - the second XAResourceEnlister (remote resource 2) enlists xaRes3
111
//
112
// This will make remote resource 1 crash after prepare,
113
// when it issues commit to xaRes2:
114
xaRes2.setCommitException(new RecoveryTestingException(
115                                        "FAILURE WHEN XA2 WAS ABOUT TO COMMIT"));
116       // Run the transaction. The commit calls to the XA resources in the
117
// appserver that contains the remote resource 1 will not happen now,
118
// due to crash of that appserver. They will be performed by that
119
// appserver's recovery procedure.
120
try
121       {
122          userTx.begin();
123          xaResEnlisterCaller.enlistOneXAResourcePerAppServerOverIIOP();
124          frontEnd.addToBalancesOverIIOP(100);
125          userTx.commit();
126       }
127       finally
128       {
129          // Clear crash-causing exception so that the commit on xaRes2 succeeds
130
// at recovery time.
131
xaRes2.clearCommitException();
132       }
133    }
134
135    public static Test suite() throws Exception JavaDoc
136    {
137       return suite(CrashOfFirstRemoteResourceWithXADatabaseAfterPrepareIIOPTestCase.class);
138    }
139
140 }
141
Popular Tags