KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > txpropiiop > test > TxPropIIOPUnitTestCase


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.txpropiiop.test;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.util.Properties JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.rmi.PortableRemoteObject JavaDoc;
29
30 import junit.framework.Test;
31 import org.jboss.iiop.CorbaORB;
32 import org.jboss.test.JBossIIOPTestCase;
33 import org.jboss.test.txpropiiop.interfaces.a.SessionA;
34 import org.jboss.test.txpropiiop.interfaces.a.SessionAHome;
35 import org.jboss.test.txpropiiop.interfaces.b.SessionB;
36 import org.jboss.test.txpropiiop.interfaces.b.SessionBHome;
37 import org.jboss.tm.iiop.TxClientInterceptor;
38 import org.omg.CORBA.ORB JavaDoc;
39 import org.omg.CosTransactions.PropagationContext;
40 import org.omg.CosTransactions.TransIdentity;
41 import org.omg.CosTransactions.otid_t;
42
43 /**
44  * A Tx Propagation UnitTestCase.
45  *
46  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
47  * @version $Revision: 37406 $
48  */

49 public class TxPropIIOPUnitTestCase extends JBossIIOPTestCase
50 {
51    private static SessionA sessionA;
52    private static SessionB sessionB;
53    
54    public TxPropIIOPUnitTestCase(String JavaDoc name) throws Exception JavaDoc
55    {
56       super(name);
57    }
58
59    public static Test suite() throws Exception JavaDoc
60    {
61       return getDeploySetup(TxPropIIOPUnitTestCase.class, "txpropiiopB.jar");
62    }
63    
64    protected void setUp() throws Exception JavaDoc
65    {
66       log.debug("====== Starting test " + getName());
67       /*deploy("txpropiiopA.jar");
68       try
69       {
70          deploy("txpropiiopB.jar");
71       }
72       catch (Throwable t)
73       {
74          log.error("Error deploying txpropiiopB", t);
75          undeploy("txpropiiopA.jar");
76       }*/

77    }
78    
79    protected void tearDown() throws Exception JavaDoc
80    {
81       log.debug("====== Ended test " + getName());
82       /*try
83       {
84          undeploy("txpropiiopB.jar");
85       }
86       finally
87       {
88          undeploy("txpropiiopA.jar");
89       }*/

90    }
91    
92    /* public void testRedeployment() throws Exception
93    {
94       getSessionA().invokeSessionB();
95       undeploy("txpropiiopB.jar");
96       deploy("txpropiiopB.jar");
97       getSessionA().invokeSessionB();
98    }*/

99    
100    public void testTxToNotSupported() throws Exception JavaDoc
101    {
102       setTransaction();
103       try
104       {
105          String JavaDoc hello = "Hello";
106          String JavaDoc result = getSessionB().testNotSupported(hello);
107          validate(hello, result);
108       }
109       finally
110       {
111          unsetTransaction();
112       }
113    }
114    
115    public void testTxToRequired() throws Exception JavaDoc
116    {
117       setTransaction();
118       try
119       {
120          String JavaDoc hello = "Hello";
121          String JavaDoc result = getSessionB().testRequired(hello);
122          fail("Expected remote exception");
123       }
124       catch (RemoteException JavaDoc expected)
125       {
126       }
127       finally
128       {
129          unsetTransaction();
130       }
131    }
132    
133    public void testTxToRequiresNew() throws Exception JavaDoc
134    {
135       setTransaction();
136       try
137       {
138          String JavaDoc hello = "Hello";
139          String JavaDoc result = getSessionB().testRequiresNew(hello);
140          validate(hello, result);
141       }
142       finally
143       {
144          unsetTransaction();
145       }
146    }
147    
148    public void testTxToSupports() throws Exception JavaDoc
149    {
150       setTransaction();
151       try
152       {
153          String JavaDoc hello = "Hello";
154          String JavaDoc result = getSessionB().testSupports(hello);
155          fail("Expected remote exception");
156       }
157       catch (RemoteException JavaDoc expected)
158       {
159       }
160       finally
161       {
162          unsetTransaction();
163       }
164    }
165    
166    public void testTxToMandatory() throws Exception JavaDoc
167    {
168       setTransaction();
169       try
170       {
171          String JavaDoc hello = "Hello";
172          String JavaDoc result = getSessionB().testMandatory(hello);
173          fail("Expected remote exception");
174       }
175       catch (RemoteException JavaDoc expected)
176       {
177       }
178       finally
179       {
180          unsetTransaction();
181       }
182    }
183    
184    public void testTxToNever() throws Exception JavaDoc
185    {
186       setTransaction();
187       try
188       {
189          String JavaDoc hello = "Hello";
190          String JavaDoc result = getSessionB().testNever(hello);
191          fail("Expected remote exception");
192       }
193       catch (RemoteException JavaDoc expected)
194       {
195       }
196       finally
197       {
198          unsetTransaction();
199       }
200    }
201    
202    public void testNoTxToNotSupported() throws Exception JavaDoc
203    {
204       String JavaDoc hello = "Hello";
205       String JavaDoc result = getSessionB().testNotSupported(hello);
206       validate(hello, result);
207    }
208    
209    public void testNoTxToRequired() throws Exception JavaDoc
210    {
211       String JavaDoc hello = "Hello";
212       String JavaDoc result = getSessionB().testNotSupported(hello);
213       validate(hello, result);
214    }
215    
216    public void testNoTxToRequiresNew() throws Exception JavaDoc
217    {
218       String JavaDoc hello = "Hello";
219       String JavaDoc result = getSessionB().testNotSupported(hello);
220       validate(hello, result);
221    }
222    
223    public void testNoTxToSupports() throws Exception JavaDoc
224    {
225       String JavaDoc hello = "Hello";
226       String JavaDoc result = getSessionB().testNotSupported(hello);
227       validate(hello, result);
228    }
229    
230    public void testNoTxToMandatory() throws Exception JavaDoc
231    {
232       try
233       {
234          String JavaDoc hello = "Hello";
235          String JavaDoc result = getSessionB().testMandatory(hello);
236          fail("Expected remote exception");
237       }
238       catch (RemoteException JavaDoc expected)
239       {
240       }
241    }
242    
243    public void testNoTxToNever() throws Exception JavaDoc
244    {
245       String JavaDoc hello = "Hello";
246       String JavaDoc result = getSessionB().testNotSupported(hello);
247       validate(hello, result);
248    }
249    
250    private static void validate(String JavaDoc hello, String JavaDoc result) throws Exception JavaDoc
251    {
252       if (hello == result)
253          fail("Should be pass by value");
254       assertEquals(hello, result);
255    }
256
257    private SessionA getSessionA() throws Exception JavaDoc
258    {
259       if (sessionA == null)
260       {
261          SessionAHome home = (SessionAHome) lookup("SessionA", SessionAHome.class);
262          sessionA = home.create();
263       }
264       return sessionA;
265    }
266    
267    private SessionB getSessionB() throws Exception JavaDoc
268    {
269       if (sessionB == null)
270       {
271          SessionBHome home = (SessionBHome) lookup("SessionB", SessionBHome.class);
272          sessionB = home.create();
273       }
274       return sessionB;
275    }
276
277    protected void setTransaction() throws Exception JavaDoc
278    {
279       PropagationContext pc = new PropagationContext();
280       pc.parents = new TransIdentity[0];
281       pc.current = new TransIdentity();
282       pc.current.otid = new otid_t();
283       pc.current.otid.formatID = 666;
284       pc.current.otid.bqual_length = 1;
285       pc.current.otid.tid = new byte[] { (byte) 1 };
286       pc.implementation_specific_data = getORB().create_any();
287       pc.implementation_specific_data.insert_long(1);
288       TxClientInterceptor.setOutgoingPropagationContext(pc);
289    }
290    
291    protected void unsetTransaction()
292    {
293       TxClientInterceptor.unsetOutgoingPropagationContext();
294    }
295    
296    private static ORB getORB() throws Exception JavaDoc
297    {
298       return CorbaORB.getInstance();
299    }
300
301    private Object JavaDoc lookup(String JavaDoc name, Class JavaDoc clazz) throws Exception JavaDoc
302    {
303       Context JavaDoc ctx = getInitialContext();
304       Object JavaDoc obj = ctx.lookup(name);
305       Object JavaDoc result = PortableRemoteObject.narrow(obj, clazz);
306       return result;
307    }
308 }
309
Popular Tags