KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > tm > test > MTTransactionManagerUnitTestCase


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.tm.test;
23
24 import javax.management.ObjectName JavaDoc;
25 import javax.transaction.RollbackException JavaDoc;
26
27 import junit.framework.Test;
28
29 import org.jboss.test.JBossTestCase;
30 import org.jboss.test.JBossTestSetup;
31 import org.jboss.test.tm.resource.MTOperation;
32
33 /**
34  * Multithreaded tests for the transaction manager
35  *
36  * Based on TransactionManagerUnitTestCase
37  *
38  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
39  * @version $Revision: 37406 $
40  */

41 public class MTTransactionManagerUnitTestCase extends JBossTestCase
42 {
43    static String JavaDoc[] SIG = new String JavaDoc[] { String JavaDoc.class.getName(), new MTOperation[0][0].getClass().getName() };
44
45    ObjectName JavaDoc mtMBean;
46
47    public MTTransactionManagerUnitTestCase(String JavaDoc name)
48    {
49       super(name);
50
51       try
52       {
53          mtMBean = new ObjectName JavaDoc("jboss.test:test=MTTransactionManagerUnitTestCase");
54       }
55       catch (Exception JavaDoc e)
56       {
57          throw new RuntimeException JavaDoc(e.toString());
58       }
59    }
60
61    public void runTest(MTOperation[][] ops) throws Exception JavaDoc
62    {
63       getServer().invoke(mtMBean, "testMTOperations", new Object JavaDoc[] { getName(), ops }, SIG);
64    }
65
66    public static Test suite() throws Exception JavaDoc
67    {
68       return new JBossTestSetup(getDeploySetup(MTTransactionManagerUnitTestCase.class, "mttest.sar"));
69    }
70
71    /**
72     * Start a tx on one thread and commit it on another
73     * withouth the 2nd thread actually associating itself
74     * with the transaction.
75     *
76     * This is an error if the TransactionIntegrity plugin is active
77     */

78    public void testCommitTxStartedOnADifferentThread() throws Exception JavaDoc
79    {
80       if (isTransactionIntegrityActive())
81       {
82          runTest(new MTOperation[][]
83            {
84               {
85                  // thread 0
86
new MTOperation(MTOperation.TM_BEGIN, 10),
87                  new MTOperation(MTOperation.TM_GET_STATUS),
88                  new MTOperation(MTOperation.XX_SLEEP_200), // other thread must commit
89
new MTOperation(MTOperation.TM_GET_STATUS)
90               }
91               ,
92               {
93                  // thread 1
94
new MTOperation(MTOperation.XX_WAIT_FOR, 10),
95                  new MTOperation(MTOperation.TX_COMMIT, 10, new RollbackException JavaDoc()),
96                  new MTOperation(MTOperation.TM_GET_STATUS),
97               }
98            });
99       }
100       else
101       {
102          runTest(new MTOperation[][]
103          {
104             {
105                // thread 0
106
new MTOperation(MTOperation.TM_BEGIN, 10),
107                new MTOperation(MTOperation.TM_GET_STATUS),
108                new MTOperation(MTOperation.XX_SLEEP_200), // other thread must commit
109
new MTOperation(MTOperation.TM_GET_STATUS)
110             }
111             ,
112             {
113                // thread 1
114
new MTOperation(MTOperation.XX_WAIT_FOR, 10),
115                new MTOperation(MTOperation.TX_COMMIT, 10),
116                new MTOperation(MTOperation.TM_GET_STATUS),
117             }
118          });
119       }
120    }
121
122    /**
123     * Start a tx on one thread, then resume this tx and commit it from
124     * another thread. Normally this is allowed, but if the
125     * TransactionIntegrity policy is active, then the 2 threads associated
126     * with the tx will be detected at commit time and an exception
127     * will be thrown.
128     */

129    public void testResumeAndCommitTxStartedOnADifferentThread() throws Exception JavaDoc
130    {
131       if (isTransactionIntegrityActive())
132       {
133          runTest(new MTOperation[][]
134          {
135             {
136                // thread 0
137
new MTOperation(MTOperation.TM_BEGIN, 10),
138                new MTOperation(MTOperation.TM_GET_STATUS),
139             }
140             ,
141             {
142                // thread 1
143
new MTOperation(MTOperation.TM_GET_STATUS),
144                new MTOperation(MTOperation.XX_WAIT_FOR, 10),
145                new MTOperation(MTOperation.TM_RESUME, 10),
146                new MTOperation(MTOperation.TM_GET_STATUS),
147                new MTOperation(MTOperation.TX_COMMIT, 10, new RollbackException JavaDoc()),
148                new MTOperation(MTOperation.TM_GET_STATUS),
149             }
150          });
151       }
152       else
153       {
154          runTest(new MTOperation[][]
155            {
156               {
157                  // thread 0
158
new MTOperation(MTOperation.TM_BEGIN, 10),
159                  new MTOperation(MTOperation.TM_GET_STATUS),
160               }
161               ,
162               {
163                  // thread 1
164
new MTOperation(MTOperation.TM_GET_STATUS),
165                  new MTOperation(MTOperation.XX_WAIT_FOR, 10),
166                  new MTOperation(MTOperation.TM_RESUME, 10),
167                  new MTOperation(MTOperation.TM_GET_STATUS),
168                  new MTOperation(MTOperation.TX_COMMIT, 10),
169                  new MTOperation(MTOperation.TM_GET_STATUS),
170               }
171         });
172       }
173    }
174    
175    /**
176     * Start a tx on one thread and commit it on another thread
177     * without the 2nd thread actually associating itself with
178     * the transaction. The try to commit the tx on the 1st
179     * thread as well, thus producing an exception.
180     *
181     * This only works when the TransactionIntegrity policy is innactive
182     */

183    public void testCommitSameTxInTwoThreads() throws Exception JavaDoc
184    {
185       if (!isTransactionIntegrityActive())
186       {
187          runTest(new MTOperation[][]
188          {
189             {
190                // thread 0
191
new MTOperation(MTOperation.TM_BEGIN, 10),
192                new MTOperation(MTOperation.TM_GET_STATUS),
193                new MTOperation(MTOperation.XX_SLEEP_200),
194                new MTOperation(MTOperation.TM_GET_STATUS),
195                new MTOperation(MTOperation.TM_COMMIT, -1, new IllegalStateException JavaDoc())
196    
197             }
198             ,
199             {
200                // thread 1
201
new MTOperation(MTOperation.XX_WAIT_FOR, 10),
202                new MTOperation(MTOperation.TX_COMMIT, 10),
203                new MTOperation(MTOperation.TM_GET_STATUS),
204             }
205          });
206       }
207    }
208    
209    /**
210     * Find out if the plugin is installed.
211     *
212     * This is specific to this particular plugin!
213     */

214    private boolean isTransactionIntegrityActive()
215    {
216       boolean isActive = false;
217       try
218       {
219          ObjectName JavaDoc target = new ObjectName JavaDoc("jboss:service=TransactionManager,plugin=TransactionIntegrity");
220          isActive = getServer().isRegistered(target);
221       }
222       catch (Exception JavaDoc ignore)
223       {
224          // empty
225
}
226       return isActive;
227    }
228    
229    
230 }
231
Popular Tags