KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > dtmusertx > test > DTMUserTransactionUnitTestCase


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.dtmusertx.test;
23
24 import javax.naming.Context JavaDoc;
25 import javax.rmi.PortableRemoteObject JavaDoc;
26 import javax.transaction.Status JavaDoc;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import junit.framework.Test;
30
31 import org.jboss.test.JBossTestCase;
32 import org.jboss.test.dtmusertx.interfaces.StatefulSession;
33 import org.jboss.test.dtmusertx.interfaces.StatefulSessionHome;
34
35 /** Tests of DTM UserTransaction
36  *
37  * @author kimptoc
38  * @author Scott.Stark@jboss.org
39  * @author d_jencks converted to JBossTestCase, added logging.
40  * @author reverbel@ime.usp.br
41  * @version $Revision: 37406 $
42  */

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

54    public void testUserTx()
55       throws Exception JavaDoc
56    {
57       getLog().debug("+++ testUsrTx");
58
59       getLog().debug("Obtain home interface");
60       // Create a new session object
61
Context JavaDoc ctx = getInitialContext();
62       Object JavaDoc ref = ctx.lookup("dtmusertx/StatefulSessionBean");
63       StatefulSessionHome home =
64          (StatefulSessionHome) PortableRemoteObject.narrow(
65                                                ref, StatefulSessionHome.class);
66       StatefulSession bean = home.create("testUserTx");
67
68       bean.setCounter(100);
69       getLog().debug("Try to instantiate a UserTransaction");
70       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc)ctx.lookup("UserTransaction");
71       userTx.begin();
72          bean.incCounter();
73          bean.incCounter();
74       userTx.commit();
75       int counter = bean.getCounter();
76       assertTrue("counter == 102", counter == 102);
77
78       bean.setCounter(100);
79       userTx.begin();
80          bean.incCounter();
81          bean.incCounter();
82       userTx.rollback();
83       counter = bean.getCounter();
84       assertTrue("counter == 100", counter == 100);
85
86       bean.remove();
87    }
88
89    public void testSetRollbackOnly()
90       throws Exception JavaDoc
91    {
92       getLog().debug("+++ testSetRollbackOnly");
93
94       getLog().debug("Obtain home interface");
95       // Create a new session object
96
Context JavaDoc ctx = getInitialContext();
97       Object JavaDoc ref = ctx.lookup("dtmusertx/StatefulSessionBean");
98       StatefulSessionHome home =
99          (StatefulSessionHome) PortableRemoteObject.narrow(
100                                                ref, StatefulSessionHome.class);
101       StatefulSession bean = home.create("testUserTx");
102
103       bean.setCounter(100);
104       getLog().debug("Try to instantiate a UserTransaction");
105       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc)ctx.lookup("UserTransaction");
106       bean.setCounter(100);
107       try
108       {
109          userTx.begin();
110             bean.incCounter();
111             bean.incCounter();
112             userTx.setRollbackOnly();
113          userTx.commit();
114          getLog().debug("Should not get here!");
115          fail("RollbackException should have been thrown");
116       }
117       catch(javax.transaction.RollbackException JavaDoc e)
118       {
119          getLog().debug("Expected exception: " + e);
120       }
121       assertTrue("userTx.getStatus() == Status.STATUS_NO_TRANSACTION",
122                  userTx.getStatus() == Status.STATUS_NO_TRANSACTION);
123       assertTrue("counter == 100", bean.getCounter() == 100);
124
125       try
126       {
127          userTx.begin();
128             bean.incCounter();
129             userTx.setRollbackOnly();
130             bean.incCounter(); // Could throw TransactionRolledbackException,
131
// as the transaction is marked to roll back.
132
userTx.commit();
133          getLog().debug("Should not get here!");
134          fail("Either TransactionRolledbackException or " +
135                "RollbackException should have been thrown");
136       }
137       catch (javax.transaction.TransactionRolledbackException JavaDoc e)
138       {
139          // We never get here because JBoss does not throw this exception
140
// from a business method called within a transaction marked to
141
// roll back. Instead of getting a TransactionRolledbackException from
142
// the second call to the business method incCounter(), we get a
143
// RollbackException from userTx.commit(). This behavior is correct
144
// and is better than throwing TransactionRolledbackException from the
145
// business method, which would still require the client side to take
146
// some action to dissociate the client thread from the transaction.
147
getLog().debug("This does not happen in JBoss: " + e);
148          userTx.rollback(); // just to dissociate this thread from the
149
// transaction that has already been rolled back
150
}
151       catch(javax.transaction.RollbackException JavaDoc e)
152       {
153          getLog().debug("Expected exception: " + e);
154       }
155       assertTrue("userTx.getStatus() == Status.STATUS_NO_TRANSACTION",
156                  userTx.getStatus() == Status.STATUS_NO_TRANSACTION);
157       assertTrue("counter == 100", bean.getCounter() == 100);
158       
159       bean.remove();
160 }
161
162    public void testTxMandatory()
163       throws Exception JavaDoc
164    {
165       getLog().debug("+++ testTxMandatory");
166
167       getLog().debug("Obtain home interface");
168       // Create a new session object
169
Context JavaDoc ctx = getInitialContext();
170       Object JavaDoc ref = ctx.lookup("dtmusertx/StatefulSessionBean");
171       StatefulSessionHome home =
172          (StatefulSessionHome) PortableRemoteObject.narrow(
173                                                ref, StatefulSessionHome.class);
174       StatefulSession bean = home.create("testTxMandatory");
175       getLog().debug("Call txMandatoryMethod without a UserTransaction");
176       try
177       {
178          bean.txMandatoryMethod("without a UserTransaction");
179          getLog().debug("Should not get here!");
180          fail("TransactionRequiredException should have been thrown");
181       }
182       catch (javax.transaction.TransactionRequiredException JavaDoc e)
183       {
184          getLog().debug("Expected exception: " + e);
185       }
186       getLog().debug("Begin UserTransaction");
187       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc)ctx.lookup("UserTransaction");
188       userTx.begin();
189          bean.txMandatoryMethod("within a UserTransaction");
190       getLog().debug("Commit UserTransaction");
191       userTx.commit();
192       bean.remove();
193    }
194
195    public static Test suite() throws Exception JavaDoc
196    {
197       return getDeploySetup(DTMUserTransactionUnitTestCase.class,
198                             "dtmusertx.jar");
199    }
200
201 }
202
Popular Tags