KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import junit.framework.Test;
29
30 import org.jboss.logging.Logger;
31 import org.jboss.test.recover.interfaces.DummyXAResource;
32 import org.jboss.test.recover.interfaces.StatelessFoo;
33
34 /**
35  * A CrashBeforeTxCommittedTestCase.
36  *
37  * @author <a HREF="reverbel@ime.usp.br">Francisco Reverbel</a>
38  * @version $Revision: 37929 $
39  */

40 public class CrashBeforeTxCommittedTestCase
41       extends JBossCrashRecoveryTestCase
42 {
43    private Logger log = Logger.getLogger(this.getClass());
44    private MBeanServerConnection JavaDoc server = delegate.getServer();
45
46    public CrashBeforeTxCommittedTestCase(String JavaDoc name)
47       throws Exception JavaDoc
48    {
49       super(name);
50    }
51    
52    public void test() throws Exception JavaDoc
53    {
54       log.info("*** starting " + getUnqualifiedClassName() + " ***");
55       
56       DummyXAResource xaRes1 = getXAResource("DummyRecoverableProxy1");
57       DummyXAResource xaRes2 = getXAResource("DummyRecoverableProxy2");
58       DummyXAResource xaRes3 = getXAResource("DummyRecoverableProxy3");
59       
60       xaRes1.clear();
61       xaRes2.clear();
62       xaRes3.clear();
63
64       StatelessFoo foo =
65          (StatelessFoo) getInitialContext().lookup("StatelessFoo");
66
67       for (int i = 0; i < N; i++)
68          foo.method();
69          
70       int xa1CommitsBefore = xaRes1.getCommittedCount();
71       int xa2CommitsBefore = xaRes2.getCommittedCount();
72       int xa3CommitsBefore = xaRes3.getCommittedCount();
73
74       log.info("Commits before crash: " + xa1CommitsBefore + ", "
75                + xa2CommitsBefore + ", " + xa3CommitsBefore);
76
77       ObjectName JavaDoc logger = new ObjectName JavaDoc("jboss:service=RecoveryLogger");
78       try
79       {
80          server.setAttribute(logger, new Attribute JavaDoc("CrashOnFailure",
81                                                     Boolean.TRUE));
82          server.setAttribute(logger, new Attribute JavaDoc("FailBeforeCommitting",
83                                                     Boolean.TRUE));
84          server.setAttribute(logger, new Attribute JavaDoc("FailBefore",
85                                                     new Integer JavaDoc(0)));
86          foo.method();
87          fail("Server crash expected.");
88       }
89       catch (Throwable JavaDoc e)
90       {
91          if (e instanceof java.lang.reflect.UndeclaredThrowableException JavaDoc
92                && e.getCause() != null
93                && e.getCause() instanceof java.rmi.MarshalException JavaDoc)
94          {
95             log.info("Expected throwable:", e);
96          }
97          else
98          {
99             log.info("Unexpected throwable:", e);
100             fail("Unexpected throwable: " + e);
101          }
102       }
103
104       int xa1CommitsAfter = xaRes1.getCommittedCount();
105       int xa2CommitsAfter = xaRes2.getCommittedCount();
106       int xa3CommitsAfter = xaRes3.getCommittedCount();
107
108       int xa1Rollbacks = xaRes1.getRollbackCount();
109       int xa2Rollbacks = xaRes2.getRollbackCount();
110       int xa3Rollbacks = xaRes3.getRollbackCount();
111
112       log.info("Commits after crash: " + xa1CommitsAfter + ", "
113             + xa2CommitsAfter + ", " + xa3CommitsAfter);
114       log.info("Rollbacks after crash: " + xa1Rollbacks + ", "
115             + xa2Rollbacks + ", " + xa3Rollbacks);
116
117       assertEquals(N, xa1CommitsBefore);
118       assertEquals(N, xa2CommitsBefore);
119       assertEquals(N, xa3CommitsBefore);
120       assertEquals(N, xa1CommitsAfter);
121       assertEquals(N, xa2CommitsAfter);
122       assertEquals(N, xa3CommitsAfter);
123       assertEquals(0, xa1Rollbacks);
124       assertEquals(0, xa2Rollbacks);
125       assertEquals(0, xa3Rollbacks);
126    }
127
128    public static Test suite() throws Exception JavaDoc
129    {
130       return suite(CrashBeforeTxCommittedTestCase.class);
131    }
132
133 }
134
Popular Tags