KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > iiop > unit > IiopRemoteUnitTestCase


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.ejb3.test.iiop.unit;
23
24 import java.rmi.MarshalledObject JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import javax.ejb.Handle JavaDoc;
29 import javax.ejb.HomeHandle JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34 import javax.transaction.UserTransaction JavaDoc;
35
36 import junit.framework.Test;
37
38 import org.jboss.ejb3.test.CustomJNDIJBossTestCase;
39 import org.jboss.ejb3.test.iiop.HomedStatelessHome;
40 import org.jboss.ejb3.test.iiop.MySession;
41 import org.jboss.ejb3.test.iiop.MyStateful;
42 import org.jboss.ejb3.test.iiop.MyStatefulHome;
43 import org.jboss.ejb3.test.iiop.TxTester;
44 import org.jboss.security.SecurityAssociation;
45 import org.jboss.security.SimplePrincipal;
46
47 /**
48  * TODO: use JBossIIOPTestCase
49  *
50  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
51  * @version $Revision: 56434 $
52  */

53 public class IiopRemoteUnitTestCase extends CustomJNDIJBossTestCase
54 {
55
56    public IiopRemoteUnitTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60
61    @Override JavaDoc
62    public InitialContext JavaDoc getInitialContext() throws NamingException JavaDoc
63    {
64       return new InitialContext JavaDoc(getJndiProperties());
65    }
66    
67    private Properties JavaDoc getJndiProperties()
68    {
69       Properties JavaDoc props = new Properties JavaDoc();
70       props.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
71       props.put("java.naming.provider.url", "corbaloc::localhost:3528/NameService");
72       props.put("java.naming.factory.object", "org.jboss.tm.iiop.client.IIOPClientUserTransactionObjectFactory");
73       props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming.client:org.jnp.interfaces");
74       props.put("j2ee.clientName", "iiop-unit-test");
75       
76 // props.put(InitialContext.SECURITY_PRINCIPAL, "somebody");
77
// props.put(InitialContext.SECURITY_CREDENTIALS, "password");
78

79       return props;
80 // return null;
81
}
82    
83    public void test0() throws Exception JavaDoc
84    {
85       System.out.println("java: " + getInitialContext().lookup("java:comp/HandleDelegate"));
86    }
87    
88    public void test1() throws Exception JavaDoc
89    {
90       SimplePrincipal principal = new SimplePrincipal("somebody");
91       SecurityAssociation.setPrincipal(principal);
92       SecurityAssociation.setCredential("password".toCharArray());
93       
94       InitialContext JavaDoc ctx = getInitialContext();
95       Object JavaDoc obj = ctx.lookup("MySessionBean/remote");
96       System.err.println(obj.getClass());
97       MySession session = (MySession) PortableRemoteObject.narrow(obj, MySession.class);
98       assertNotNull(session);
99       String JavaDoc me = new Date JavaDoc().toString();
100       String JavaDoc response = session.sayHelloTo(me);
101       assertEquals("Hi " + me, response);
102    }
103    
104    public void test2() throws Exception JavaDoc
105    {
106       InitialContext JavaDoc ctx = getInitialContext();
107       Object JavaDoc obj = ctx.lookup("MyStatefulBean/remoteHome");
108       MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class);
109       //MyStateful bean1 = (MyStateful) PortableRemoteObject.narrow(obj, MyStateful.class);
110
MyStateful bean1 = home.create();
111       bean1.setName("bean1");
112       String JavaDoc response = bean1.sayHello();
113       assertEquals("Hello bean1", response);
114       bean1.remove();
115    }
116    
117    public void testGetEJBHome() throws Exception JavaDoc
118    {
119       InitialContext JavaDoc ctx = getInitialContext();
120       Object JavaDoc obj = ctx.lookup("HomedStatelessBean/remoteHome");
121       HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class);
122       MySession session = home.create();
123       Object JavaDoc o = session.getEJBHome();
124       HomedStatelessHome home2 = (HomedStatelessHome) PortableRemoteObject.narrow(o, HomedStatelessHome.class);
125       // TODO: check home2
126
}
127    
128    public void testGetHandle() throws Exception JavaDoc
129    {
130       InitialContext JavaDoc ctx = getInitialContext();
131       Object JavaDoc obj = ctx.lookup("HomedStatelessBean/remoteHome");
132       HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class);
133       MySession session = home.create();
134       Handle JavaDoc h = session.getHandle();
135       MarshalledObject JavaDoc mo = new MarshalledObject JavaDoc(h);
136       Handle JavaDoc h2 = (Handle JavaDoc) mo.get();
137       Object JavaDoc o = h2.getEJBObject();
138       MySession session2 = (MySession) PortableRemoteObject.narrow(o, MySession.class);
139       String JavaDoc me = new Date JavaDoc().toString();
140       String JavaDoc response = session2.sayHelloTo(me);
141       assertEquals("Hi " + me, response);
142    }
143    
144    public void testGetHomeHandle() throws Exception JavaDoc
145    {
146       InitialContext JavaDoc ctx = getInitialContext();
147       Object JavaDoc obj = ctx.lookup("HomedStatelessBean/remoteHome");
148       HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class);
149       HomeHandle JavaDoc h = home.getHomeHandle();
150       MarshalledObject JavaDoc mo = new MarshalledObject JavaDoc(h);
151       HomeHandle JavaDoc h2 = (HomeHandle JavaDoc) mo.get();
152       Object JavaDoc o = h2.getEJBHome();
153       HomedStatelessHome home2 = (HomedStatelessHome) PortableRemoteObject.narrow(o, HomedStatelessHome.class);
154       // TODO: check home2
155
}
156    
157    public void testGetPrimaryKeyAndRemove() throws Exception JavaDoc
158    {
159       InitialContext JavaDoc ctx = getInitialContext();
160       Object JavaDoc obj = ctx.lookup("MyStatefulBean/remoteHome");
161       MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class);
162       MyStateful session = home.create();
163       Object JavaDoc primaryKey = session.getPrimaryKey();
164       assertNotNull(primaryKey);
165       
166       home.remove(primaryKey);
167       try
168       {
169          session.sayHello();
170          fail("should throw an exception");
171       }
172       catch(Exception JavaDoc e)
173       {
174          // TODO: check exception (NoSuchEJBException)
175
}
176    }
177    
178    public void testHomedStateless() throws Exception JavaDoc
179    {
180       InitialContext JavaDoc ctx = getInitialContext();
181       Object JavaDoc obj = ctx.lookup("HomedStatelessBean/remoteHome");
182       HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class);
183       MySession session = home.create();
184       String JavaDoc me = new Date JavaDoc().toString();
185       String JavaDoc response = session.sayHelloTo(me);
186       assertEquals("Hi " + me, response);
187    }
188    
189    public void testIsIdentical() throws Exception JavaDoc
190    {
191       InitialContext JavaDoc ctx = getInitialContext();
192       Object JavaDoc obj = ctx.lookup("MyStatefulBean/remoteHome");
193       MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class);
194       MyStateful session = home.create();
195       Handle JavaDoc h = session.getHandle();
196       MarshalledObject JavaDoc mo = new MarshalledObject JavaDoc(h);
197       Handle JavaDoc h2 = (Handle JavaDoc) mo.get();
198       Object JavaDoc o = h2.getEJBObject();
199       MyStateful session2 = (MyStateful) PortableRemoteObject.narrow(o, MyStateful.class);
200       assertTrue(session.isIdentical(session2));
201    }
202    
203    public void testRemoveByHandle() throws Exception JavaDoc
204    {
205       InitialContext JavaDoc ctx = getInitialContext();
206       Object JavaDoc obj = ctx.lookup("MyStatefulBean/remoteHome");
207       MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class);
208       MyStateful session = home.create();
209       session.setName("Me");
210       home.remove(session.getHandle());
211       try
212       {
213          session.sayHello();
214          fail("should throw an exception");
215       }
216       catch(Exception JavaDoc e)
217       {
218          // TODO: check exception (NoSuchEJBException)
219
}
220    }
221    
222    public void testSecurity() throws Exception JavaDoc
223    {
224       SimplePrincipal principal = new SimplePrincipal("somebody");
225       SecurityAssociation.setPrincipal(principal);
226       
227       InitialContext JavaDoc ctx = getInitialContext();
228       Object JavaDoc obj = ctx.lookup("MySessionBean/remote");
229       System.err.println(obj.getClass());
230       MySession session = (MySession) PortableRemoteObject.narrow(obj, MySession.class);
231       assertNotNull(session);
232       String JavaDoc actual = session.getWhoAmI();
233       System.err.println("whoAmI = " + actual);
234    }
235    
236    public void testService() throws Exception JavaDoc
237    {
238       InitialContext JavaDoc ctx = getInitialContext();
239       Object JavaDoc obj = ctx.lookup("MyServiceBean/remote");
240       MyStateful bean1 = (MyStateful) PortableRemoteObject.narrow(obj, MyStateful.class);
241       bean1.setName("bean1");
242       String JavaDoc response = bean1.sayHello();
243       assertEquals("Hello bean1", response);
244    }
245    
246    public void testTxPropegation() throws Exception JavaDoc
247    {
248       InitialContext JavaDoc ctx = getInitialContext();
249       Object JavaDoc obj = ctx.lookup("TxTesterBean/remote");
250       TxTester session = (TxTester) PortableRemoteObject.narrow(obj, TxTester.class);
251       assertNotNull(session);
252       UserTransaction JavaDoc tx = (UserTransaction JavaDoc) PortableRemoteObject.narrow(ctx.lookup("UserTransaction"), UserTransaction JavaDoc.class);
253       tx.begin();
254       try
255       {
256          session.txMandatoryMethod();
257       }
258       finally
259       {
260          tx.rollback();
261       }
262       // If it doesn't throw an exception everything is fine.
263
}
264
265    public void testTxRequired() throws Exception JavaDoc
266    {
267       InitialContext JavaDoc ctx = getInitialContext();
268       Object JavaDoc obj = ctx.lookup("TxTesterBean/remote");
269       TxTester session = (TxTester) PortableRemoteObject.narrow(obj, TxTester.class);
270       assertNotNull(session);
271       try
272       {
273          session.txMandatoryMethod();
274          fail("Expected an exception");
275       }
276       catch(Exception JavaDoc e)
277       {
278          //fail("TODO: check exception");
279
}
280       // TODO: throws an ugly exception, needs assertions to check
281
}
282    
283    public static Test suite() throws Exception JavaDoc
284    {
285       try
286       {
287          System.err.println(IiopRemoteUnitTestCase.class.getClassLoader().getResource("jacorb.properties"));
288       }
289       catch(Throwable JavaDoc t)
290       {
291          t.printStackTrace();
292       }
293       return getDeploySetup(IiopRemoteUnitTestCase.class, "iiop.jar");
294    }
295
296 }
297
Popular Tags