KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > txiiop > test > IIOPUserTransactionStressTestCase


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.txiiop.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 import org.jboss.test.JBossIIOPTestCase;
30 import org.jboss.test.txiiop.interfaces.StatefulSession;
31 import org.jboss.test.txiiop.interfaces.StatefulSessionHome;
32
33 /** Tests of IIOP UserTransaction
34  *
35  * @author kimptoc
36  * @author Scott.Stark@jboss.org
37  * @author d_jencks converted to JBossTestCase, added logging.
38  * @author reverbel@ime.usp.br
39  * @version $Revision: 37406 $
40  */

41 public class IIOPUserTransactionStressTestCase
42    extends JBossIIOPTestCase
43 {
44     // Constructors --------------------------------------------------
45
public IIOPUserTransactionStressTestCase (String JavaDoc name)
46    {
47       super(name);
48    }
49
50    // Public --------------------------------------------------------
51

52    public void testUserTx()
53       throws Exception JavaDoc
54    {
55       getLog().debug("+++ testUsrTx");
56
57       getLog().debug("Obtain home interface");
58       // Create a new session object
59
Context JavaDoc ctx = getInitialContext();
60       Object JavaDoc ref = ctx.lookup("txiiop/StatefulSessionBean");
61       StatefulSessionHome home =
62          (StatefulSessionHome) PortableRemoteObject.narrow(
63                                                ref, StatefulSessionHome.class);
64       StatefulSession bean = home.create("testUserTx");
65
66       bean.setCounter(100);
67       getLog().debug("Try to instantiate a UserTransaction");
68       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc)ctx.lookup("UserTransaction");
69       userTx.begin();
70          bean.incCounter();
71          bean.incCounter();
72       userTx.commit();
73       int counter = bean.getCounter();
74       assertTrue("counter == 102", counter == 102);
75
76       bean.setCounter(100);
77       userTx.begin();
78          bean.incCounter();
79          bean.incCounter();
80       userTx.rollback();
81       counter = bean.getCounter();
82       assertTrue("counter == 100", counter == 100);
83
84       bean.remove();
85    }
86
87    public void testTxMandatory()
88       throws Exception JavaDoc
89    {
90       getLog().debug("+++ testTxMandatory");
91
92       getLog().debug("Obtain home interface");
93       // Create a new session object
94
Context JavaDoc ctx = getInitialContext();
95       Object JavaDoc ref = ctx.lookup("txiiop/StatefulSessionBean");
96       StatefulSessionHome home =
97          (StatefulSessionHome) PortableRemoteObject.narrow(
98                                                ref, StatefulSessionHome.class);
99       StatefulSession bean = home.create("testTxMandatory");
100       getLog().debug("Call txMandatoryMethod without a UserTransaction");
101       try
102       {
103          bean.txMandatoryMethod("without a UserTransaction");
104          getLog().debug("Should not get here!");
105          fail("TransactionRequiredException should have been thrown");
106       }
107       catch (javax.transaction.TransactionRequiredException JavaDoc e)
108       {
109          getLog().debug("Expected exception: " + e);
110       }
111       getLog().debug("Begin UserTransaction");
112       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc)ctx.lookup("UserTransaction");
113       userTx.begin();
114          bean.txMandatoryMethod("within a UserTransaction");
115       getLog().debug("Commit UserTransaction");
116       userTx.commit();
117       bean.remove();
118    }
119
120    public static Test suite() throws Exception JavaDoc
121    {
122       return getDeploySetup(IIOPUserTransactionStressTestCase.class,
123                             "txiiop.jar");
124    }
125
126 }
127
Popular Tags