001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.tm.test;
023:
024: import javax.management.ObjectName;
025: import javax.transaction.RollbackException;
026:
027: import junit.framework.Test;
028:
029: import org.jboss.test.JBossTestCase;
030: import org.jboss.test.JBossTestSetup;
031: import org.jboss.test.tm.resource.MTOperation;
032:
033: /**
034: * Multithreaded tests for the transaction manager
035: *
036: * Based on TransactionManagerUnitTestCase
037: *
038: * @author <a href="dimitris@jboss.org">Dimitris Andreadis</a>
039: * @version $Revision: 57211 $
040: */
041: public class MTTransactionManagerUnitTestCase extends JBossTestCase {
042: static String[] SIG = new String[] { String.class.getName(),
043: new MTOperation[0][0].getClass().getName() };
044:
045: ObjectName mtMBean;
046:
047: public MTTransactionManagerUnitTestCase(String name) {
048: super (name);
049:
050: try {
051: mtMBean = new ObjectName(
052: "jboss.test:test=MTTransactionManagerUnitTestCase");
053: } catch (Exception e) {
054: throw new RuntimeException(e.toString());
055: }
056: }
057:
058: public void runTest(MTOperation[][] ops) throws Exception {
059: getServer().invoke(mtMBean, "testMTOperations",
060: new Object[] { getName(), ops }, SIG);
061: }
062:
063: public static Test suite() throws Exception {
064: return new JBossTestSetup(getDeploySetup(
065: MTTransactionManagerUnitTestCase.class, "mttest.sar"));
066: }
067:
068: /**
069: * Start a tx on one thread and commit it on another
070: * withouth the 2nd thread actually associating itself
071: * with the transaction.
072: *
073: * This is an error if the TransactionIntegrity plugin is active
074: */
075: public void testCommitTxStartedOnADifferentThread()
076: throws Exception {
077: if (isTransactionIntegrityActive()) {
078: runTest(new MTOperation[][] {
079: {
080: // thread 0
081: new MTOperation(MTOperation.TM_BEGIN, 10),
082: new MTOperation(MTOperation.TM_GET_STATUS),
083: new MTOperation(MTOperation.XX_SLEEP_200), // other thread must commit
084: new MTOperation(MTOperation.TM_GET_STATUS) },
085: {
086: // thread 1
087: new MTOperation(MTOperation.XX_WAIT_FOR, 10),
088: new MTOperation(MTOperation.TX_COMMIT, 10,
089: new RollbackException()),
090: new MTOperation(MTOperation.TM_GET_STATUS), } });
091: } else {
092: runTest(new MTOperation[][] {
093: {
094: // thread 0
095: new MTOperation(MTOperation.TM_BEGIN, 10),
096: new MTOperation(MTOperation.TM_GET_STATUS),
097: new MTOperation(MTOperation.XX_SLEEP_200), // other thread must commit
098: new MTOperation(MTOperation.TM_GET_STATUS) },
099: {
100: // thread 1
101: new MTOperation(MTOperation.XX_WAIT_FOR, 10),
102: new MTOperation(MTOperation.TX_COMMIT, 10),
103: new MTOperation(MTOperation.TM_GET_STATUS), } });
104: }
105: }
106:
107: /**
108: * Start a tx on one thread, then resume this tx and commit it from
109: * another thread. Normally this is allowed, but if the
110: * TransactionIntegrity policy is active, then the 2 threads associated
111: * with the tx will be detected at commit time and an exception
112: * will be thrown.
113: */
114: public void testResumeAndCommitTxStartedOnADifferentThread()
115: throws Exception {
116: if (isTransactionIntegrityActive()) {
117: runTest(new MTOperation[][] {
118: {
119: // thread 0
120: new MTOperation(MTOperation.TM_BEGIN, 10),
121: new MTOperation(MTOperation.TM_GET_STATUS), },
122: {
123: // thread 1
124: new MTOperation(MTOperation.TM_GET_STATUS),
125: new MTOperation(MTOperation.XX_WAIT_FOR, 10),
126: new MTOperation(MTOperation.TM_RESUME, 10),
127: new MTOperation(MTOperation.TM_GET_STATUS),
128: new MTOperation(MTOperation.TX_COMMIT, 10,
129: new RollbackException()),
130: new MTOperation(MTOperation.TM_GET_STATUS), } });
131: } else {
132: runTest(new MTOperation[][] {
133: {
134: // thread 0
135: new MTOperation(MTOperation.TM_BEGIN, 10),
136: new MTOperation(MTOperation.TM_GET_STATUS), },
137: {
138: // thread 1
139: new MTOperation(MTOperation.TM_GET_STATUS),
140: new MTOperation(MTOperation.XX_WAIT_FOR, 10),
141: new MTOperation(MTOperation.TM_RESUME, 10),
142: new MTOperation(MTOperation.TM_GET_STATUS),
143: new MTOperation(MTOperation.TX_COMMIT, 10),
144: new MTOperation(MTOperation.TM_GET_STATUS), } });
145: }
146: }
147:
148: /**
149: * Start a tx on one thread and commit it on another thread
150: * without the 2nd thread actually associating itself with
151: * the transaction. The try to commit the tx on the 1st
152: * thread as well, thus producing an exception.
153: *
154: * This only works when the TransactionIntegrity policy is innactive
155: */
156: public void testCommitSameTxInTwoThreads() throws Exception {
157: if (!isTransactionIntegrityActive()) {
158: runTest(new MTOperation[][] {
159: {
160: // thread 0
161: new MTOperation(MTOperation.TM_BEGIN, 10),
162: new MTOperation(MTOperation.TM_GET_STATUS),
163: new MTOperation(MTOperation.XX_SLEEP_200),
164: new MTOperation(MTOperation.TM_GET_STATUS),
165: new MTOperation(MTOperation.TM_COMMIT, -1,
166: new IllegalStateException())
167:
168: },
169: {
170: // thread 1
171: new MTOperation(MTOperation.XX_WAIT_FOR, 10),
172: new MTOperation(MTOperation.TX_COMMIT, 10),
173: new MTOperation(MTOperation.TM_GET_STATUS), } });
174: }
175: }
176:
177: /**
178: * Find out if the plugin is installed.
179: *
180: * This is specific to this particular plugin!
181: */
182: private boolean isTransactionIntegrityActive() {
183: boolean isActive = false;
184: try {
185: ObjectName target = new ObjectName(
186: "jboss:service=TransactionManager,plugin=TransactionIntegrity");
187: isActive = getServer().isRegistered(target);
188: } catch (Exception ignore) {
189: // empty
190: }
191: return isActive;
192: }
193:
194: }
|