KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > stateful > unit > RemoteUnitTestCase


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.stateful.unit;
23
24 import java.util.Map JavaDoc;
25
26 import javax.ejb.NoSuchEJBException JavaDoc;
27 import javax.management.MBeanServerConnection JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30
31 import org.jboss.ejb3.ClientKernelAbstraction;
32 import org.jboss.ejb3.KernelAbstractionFactory;
33 import org.jboss.ejb3.test.stateful.ClusteredStateful;
34 import org.jboss.ejb3.test.stateful.ConcurrentStateful;
35 import org.jboss.ejb3.test.stateful.Entity;
36 import org.jboss.ejb3.test.stateful.EntityFacade;
37 import org.jboss.ejb3.test.stateful.ServiceRemote;
38 import org.jboss.ejb3.test.stateful.SmallCacheStateful;
39 import org.jboss.ejb3.test.stateful.Stateful;
40 import org.jboss.ejb3.test.stateful.StatefulInvoker;
41 import org.jboss.ejb3.test.stateful.StatefulLocal;
42 import org.jboss.ejb3.test.stateful.StatefulTx;
43 import org.jboss.ejb3.test.stateful.ProxyFactoryInterface;
44 import org.jboss.ejb3.test.stateful.RemoteBindingInterceptor;
45 import org.jboss.ejb3.test.stateful.State;
46 import org.jboss.ejb3.test.stateful.StatefulHome;
47 import org.jboss.ejb3.test.stateful.Stateless;
48 import org.jboss.ejb3.test.stateful.ExtendedState;
49 import org.jboss.ejb3.test.stateful.Tester;
50 import org.jboss.logging.Logger;
51 import org.jboss.test.JBossTestCase;
52 import junit.framework.Test;
53
54 import org.jboss.security.SimplePrincipal;
55 import org.jboss.security.SecurityAssociation;
56
57 /**
58  * Sample client for the jboss container.
59  *
60  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
61  * @version $Id: RemoteUnitTestCase.java 58197 2006-11-08 01:13:31Z bdecoste $
62  */

63
64 public class RemoteUnitTestCase
65 extends JBossTestCase
66 {
67    private static final Logger log = Logger.getLogger(RemoteUnitTestCase.class);
68
69    static boolean deployed = false;
70    static int test = 0;
71
72    public RemoteUnitTestCase(String JavaDoc name)
73    {
74
75       super(name);
76
77    }
78    
79    private class ConcurrentInvocation extends Thread JavaDoc
80    {
81       SmallCacheStateful small = null;
82       public Exception JavaDoc ex;
83       private int id;
84       
85       public ConcurrentInvocation(int id)
86       {
87          this.id = id;
88          try
89          {
90             small = (SmallCacheStateful)getInitialContext().lookup("SmallCacheStatefulBean/remote");
91             small.setId(id);
92          }
93          catch (Exception JavaDoc e)
94          {
95          }
96       }
97       
98       public void run()
99       {
100          for (int i = 0; i < 5; i++)
101          {
102             try
103             {
104                assertEquals(small.doit(id),i);
105             }
106             catch (Exception JavaDoc e)
107             {
108                ex = e;
109             }
110             catch (Error JavaDoc er)
111             {
112                ex = new RuntimeException JavaDoc("Failed assert: " + id, er);
113             }
114          }
115       }
116    }
117    
118    public void testSmallCache() throws Exception JavaDoc
119    {
120       ConcurrentInvocation[] threads = new ConcurrentInvocation[5];
121       for (int i = 0; i < 5; i++) threads[i] = new ConcurrentInvocation(i);
122       for (int i = 0; i < 5; i++) threads[i].start();
123       for (int i = 0; i < 5; i++) threads[i].join();
124       for (int i = 0; i < 5; i++)
125       {
126          if (threads[i].ex != null)
127          {
128             throw new Exception JavaDoc(threads[i].ex);
129          }
130       }
131    }
132    
133    
134
135    public void testStatefulSynchronization() throws Exception JavaDoc
136    {
137       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
138       SecurityAssociation.setCredential("password".toCharArray());
139       
140       Tester test = (Tester) getInitialContext().lookup("TesterBean/remote");
141       test.testSessionSynchronization();
142
143    }
144    
145    public void testEJBObject() throws Exception JavaDoc
146    {
147       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
148       SecurityAssociation.setCredential("password".toCharArray());
149       
150       StatefulHome home = (StatefulHome)getInitialContext().lookup("StatefulHome");
151       assertNotNull(home);
152       javax.ejb.EJBObject JavaDoc stateful = (javax.ejb.EJBObject JavaDoc)home.create();
153       assertNotNull(stateful);
154       stateful = (javax.ejb.EJBObject JavaDoc)getInitialContext().lookup("Stateful");
155       assertNotNull(stateful);
156    }
157    
158    public void testStatefulTx() throws Exception JavaDoc
159    {
160       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
161       SecurityAssociation.setCredential("password".toCharArray());
162       
163       StatefulTx stateful = (StatefulTx)getInitialContext().lookup("StatefulTx");
164       assertNotNull(stateful);
165       
166       boolean transacted = stateful.isLocalTransacted();
167       assertTrue(transacted);
168       transacted = stateful.isGlobalTransacted();
169       assertFalse(transacted);
170       transacted = stateful.testNewTx();
171       assertTrue(transacted);
172       
173       try
174       {
175          stateful.testTxRollback();
176          fail("should have caught exception");
177       }
178       catch (javax.ejb.EJBException JavaDoc e)
179       {
180       }
181    }
182    
183    public void testTemplateInterfaceTx() throws Exception JavaDoc
184    {
185       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
186       SecurityAssociation.setCredential("password".toCharArray());
187       
188       StatefulTx stateful = (StatefulTx)getInitialContext().lookup("StatefulTx");
189       assertNotNull(stateful);
190       
191       try
192       {
193          stateful.testMandatoryTx(new State("test"));
194          fail("should have caught exception");
195       }
196       catch (javax.ejb.EJBTransactionRequiredException JavaDoc e)
197       {
198       }
199    }
200    
201    public void testLocalSFSB() throws Exception JavaDoc
202    {
203       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
204       SecurityAssociation.setCredential("password".toCharArray());
205       
206       try
207       {
208          StatefulLocal stateful = (StatefulLocal)getInitialContext().lookup("StatefulBean/local");
209          assertNotNull(stateful);
210          
211          stateful.getState();
212          fail("EJBException should be thrown");
213       }
214       catch (Exception JavaDoc e)
215       {
216          if (e.getCause() == null || !(e.getCause() instanceof javax.ejb.EJBException JavaDoc))
217             fail("EJBException should be thrown as cause");
218       }
219    }
220    
221    public void testNotSerialableSFSB() throws Exception JavaDoc
222    {
223       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
224       SecurityAssociation.setCredential("password".toCharArray());
225       
226       Stateful stateful = (Stateful)getInitialContext().lookup("Stateful");
227       assertNotNull(stateful);
228       stateful.setState("state");
229       String JavaDoc state = stateful.getState();
230       assertEquals("state", state);
231    }
232    
233    public void testSFSBInit() throws Exception JavaDoc
234    {
235       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
236       SecurityAssociation.setCredential("password".toCharArray());
237       
238       StatefulHome home = (StatefulHome)getInitialContext().lookup("StatefulHome");
239       assertNotNull(home);
240       ExtendedState state = new ExtendedState("init");
241       Stateful stateful = home.create(state);
242       assertNotNull(stateful);
243       String JavaDoc s = stateful.getState();
244       assertEquals("Extended_init", s);
245    }
246    
247    public void testStackTrace() throws Exception JavaDoc
248    {
249       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
250       SecurityAssociation.setCredential("password".toCharArray());
251       
252       Stateful stateful = (Stateful)getInitialContext().lookup("Stateful");
253       assertNotNull(stateful);
254       
255       try
256       {
257          stateful.testThrownException();
258          this.fail("no exception caught");
259       }
260       catch (Exception JavaDoc e)
261       {
262          StackTraceElement JavaDoc[] stackTrace = e.getStackTrace();
263          assertTrue(stackTrace[stackTrace.length - 1].getClassName().startsWith("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"));
264       }
265    }
266    
267    public void testExceptionCase() throws Exception JavaDoc
268    {
269       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
270       SecurityAssociation.setCredential("password".toCharArray());
271       
272       Stateful stateful = (Stateful)getInitialContext().lookup("Stateful");
273       assertNotNull(stateful);
274       
275       try
276       {
277          stateful.testExceptionCause();
278          this.fail("no exception caught");
279       }
280       catch (Exception JavaDoc e)
281       {
282          Throwable JavaDoc cause = e.getCause();
283          assertNotNull(cause);
284          assertEquals(NullPointerException JavaDoc.class.getName(), cause.getClass().getName());
285       }
286    }
287    
288    public void testRemoteBindingProxyFactory() throws Exception JavaDoc
289    {
290       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
291       SecurityAssociation.setCredential("password".toCharArray());
292       
293       ProxyFactoryInterface stateful = (ProxyFactoryInterface)getInitialContext().lookup("Stateful");
294       assertNotNull(stateful);
295    }
296    
297    public void testRemoteBindingInterceptorStack() throws Exception JavaDoc
298    {
299       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
300       SecurityAssociation.setCredential("password".toCharArray());
301       
302       Stateful stateful = (Stateful)getInitialContext().lookup("Stateful");
303       assertNotNull(stateful);
304       assertFalse(stateful.interceptorAccessed());
305       assertTrue(RemoteBindingInterceptor.accessed);
306    }
307
308    public void testUninstantiatedPassivation() throws Exception JavaDoc
309    {
310       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
311       SecurityAssociation.setCredential("password".toCharArray());
312       
313       Stateful stateful = (Stateful)getInitialContext().lookup("Stateful");
314       assertNotNull(stateful);
315       stateful.lookupStateful();
316       Thread.sleep(10 * 1000);
317       stateful.testStateful();
318       
319       stateful.lookupStateful();
320       stateful.testStateful();
321       Thread.sleep(10 * 1000);
322       stateful.testStateful();
323    }
324    
325    public void testExtendedPersistencePassivation() throws Exception JavaDoc
326    {
327       EntityFacade stateful = (EntityFacade)getInitialContext().lookup("EntityFacadeBean/remote");
328       Entity entity = null;
329       entity = stateful.createEntity("Kalin");
330       
331       //passivate
332
Thread.sleep(10 * 1000);
333       
334       MBeanServerConnection JavaDoc server = getServer();
335       ObjectName JavaDoc objectName = new ObjectName JavaDoc("jboss.jca:service=CachedConnectionManager");
336       int inUseConnections = (Integer JavaDoc)server.getAttribute(objectName, "InUseConnections");
337       System.out.println("inUseConnections \n" + inUseConnections);
338       assertEquals(0, inUseConnections);
339       
340       //activate
341
entity = stateful.loadEntity(entity.getId());
342       
343       inUseConnections = (Integer JavaDoc)server.getAttribute(objectName, "InUseConnections");
344       System.out.println("inUseConnections \n" + inUseConnections);
345       if (inUseConnections != 0)
346          printStats(server);
347       assertEquals(0, inUseConnections);
348       
349       entity = stateful.createEntity("Kalin" + entity.getId());
350       
351       inUseConnections = (Integer JavaDoc)server.getAttribute(objectName, "InUseConnections");
352       System.out.println("inUseConnections \n" + inUseConnections);
353       
354       if (inUseConnections != 0)
355          printStats(server);
356       assertEquals(0, inUseConnections);
357    }
358    
359    protected void printStats(MBeanServerConnection JavaDoc server) throws Exception JavaDoc
360    {
361       ObjectName JavaDoc objectName = new ObjectName JavaDoc("jboss.jca:service=CachedConnectionManager");
362       Object JavaDoc[] params = {};
363       String JavaDoc[] sig = {};
364       Map JavaDoc result = (Map JavaDoc)server.invoke(objectName, "listInUseConnections", params, sig);
365       System.out.println(result);
366       
367       objectName = new ObjectName JavaDoc("jboss:service=TransactionManager");
368       long activeTransactions = (Long JavaDoc)server.getAttribute(objectName, "TransactionCount");
369       System.out.println("activeTransactions \n" + activeTransactions);
370       
371       objectName = new ObjectName JavaDoc("jboss.jca:service=ManagedConnectionPool,name=DefaultDS");
372       long inUseConnectionCount = (Long JavaDoc)server.getAttribute(objectName, "InUseConnectionCount");
373       System.out.println("inUseConnectionCount \n" + inUseConnectionCount);
374       
375       long maxConnectionsInUseCount = (Long JavaDoc)server.getAttribute(objectName, "MaxConnectionsInUseCount");
376       System.out.println("maxConnectionsInUseCount \n" + maxConnectionsInUseCount);
377    }
378
379    public void testPassivation() throws Exception JavaDoc
380    {
381       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
382       SecurityAssociation.setCredential("password".toCharArray());
383       
384       System.out.println("testPassivation");
385       Stateless stateless = (Stateless)getInitialContext().lookup("Stateless");
386       stateless.testInjection();
387       
388       ServiceRemote service = (ServiceRemote) getInitialContext().lookup("ServiceBean/remote");
389       service.testInjection();
390       
391       Stateful stateful = (Stateful)getInitialContext().lookup("Stateful");
392       assertNotNull(stateful);
393       stateful.setState("state");
394       assertEquals("state", stateful.getState());
395       stateful.testSerializedState("state");
396       assertEquals(null, stateful.getInterceptorState());
397       stateful.setInterceptorState("hello world");
398       assertFalse(stateful.testSessionContext());
399       Thread.sleep(10 * 1000);
400       assertTrue(stateful.wasPassivated());
401       
402       assertEquals("state", stateful.getState());
403       assertEquals("hello world", stateful.getInterceptorState());
404
405       Stateful another = (Stateful)getInitialContext().lookup("Stateful");
406       assertEquals(null, another.getInterceptorState());
407       another.setInterceptorState("foo");
408       assertEquals("foo", another.getInterceptorState());
409       assertEquals("hello world", stateful.getInterceptorState());
410       
411       assertFalse(stateful.testSessionContext());
412       
413       stateful.testResources();
414       
415       stateless.testInjection();
416       
417       service.testInjection();
418    }
419    
420    public void testClusteredPassivation() throws Exception JavaDoc
421    {
422       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
423       SecurityAssociation.setCredential("password".toCharArray());
424       
425       System.out.println("testPassivation");
426       Stateless stateless = (Stateless)getInitialContext().lookup("Stateless");
427       stateless.testInjection();
428       
429       ServiceRemote service = (ServiceRemote) getInitialContext().lookup("ServiceBean/remote");
430       service.testInjection();
431       
432       ClusteredStateful stateful = (ClusteredStateful)getInitialContext().lookup("ClusteredStateful");
433       assertNotNull(stateful);
434       System.out.println("!!!! clusteredStateful " + stateful);
435       stateful.setState("state");
436       assertEquals("state", stateful.getState());
437       stateful.testSerializedState("state");
438       assertEquals(null, stateful.getInterceptorState());
439       stateful.setInterceptorState("hello world");
440       assertFalse(stateful.testSessionContext());
441       Thread.sleep(10 * 1000);
442       assertTrue(stateful.wasPassivated());
443       
444       assertEquals("state", stateful.getState());
445       assertEquals("hello world", stateful.getInterceptorState());
446
447       Stateful another = (Stateful)getInitialContext().lookup("Stateful");
448       assertEquals(null, another.getInterceptorState());
449       another.setInterceptorState("foo");
450       assertEquals("foo", another.getInterceptorState());
451       assertEquals("hello world", stateful.getInterceptorState());
452       
453       assertFalse(stateful.testSessionContext());
454       
455       stateful.testResources();
456       
457       stateless.testInjection();
458       
459       service.testInjection();
460    }
461
462    
463    public void testRemove() throws Exception JavaDoc
464    {
465       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
466       SecurityAssociation.setCredential("password".toCharArray());
467       
468       System.out.println("testPassivation");
469       Stateful stateful = (Stateful)getInitialContext().lookup("Stateful");
470       assertNotNull(stateful);
471  // stateful.setState("state");
472

473       stateful.removeBean();
474       
475       try
476       {
477          stateful.getState();
478          fail("Bean should have been removed");
479       } catch (NoSuchEJBException JavaDoc e)
480       {
481          
482       }
483    }
484
485    public void testRemoveWithRollback() throws Exception JavaDoc
486    {
487       Tester test = (Tester) getInitialContext().lookup("TesterBean/remote");
488       test.testRollback1();
489       test.testRollback2();
490    }
491    
492    public void testConcurrentAccess() throws Exception JavaDoc
493    {
494       ConcurrentStateful stateful = (ConcurrentStateful) new InitialContext JavaDoc().lookup("ConcurrentStateful");
495       stateful.getState();
496       
497       StatefulInvoker[] invokers = new StatefulInvoker[2];
498       for (int i = 0; i < 2 ; ++i)
499       {
500          invokers[i] = new StatefulInvoker(stateful);
501       }
502       
503       for (StatefulInvoker invoker: invokers)
504       {
505          invoker.start();
506       }
507       
508       Thread.sleep(10000);
509       
510       for (StatefulInvoker invoker: invokers)
511       {
512          if (invoker.getException() != null)
513             throw invoker.getException();
514       }
515       
516       stateful = (ConcurrentStateful) new InitialContext JavaDoc().lookup("Stateful");
517       
518       invokers = new StatefulInvoker[2];
519       for (int i = 0; i < 2 ; ++i)
520       {
521          invokers[i] = new StatefulInvoker(stateful);
522       }
523       
524       for (StatefulInvoker invoker: invokers)
525       {
526          invoker.start();
527       }
528       
529       Thread.sleep(10000);
530       
531       boolean wasConcurrentException = false;
532       for (StatefulInvoker invoker: invokers)
533       {
534          if (invoker.getException() != null)
535             wasConcurrentException = true;
536       }
537       
538       assertTrue(wasConcurrentException);
539    }
540    
541    public void testOverrideConcurrentAccess() throws Exception JavaDoc
542    {
543       ConcurrentStateful stateful = (ConcurrentStateful) new InitialContext JavaDoc().lookup("OverrideConcurrentStateful");
544       stateful.getState();
545       
546       StatefulInvoker[] invokers = new StatefulInvoker[2];
547       for (int i = 0; i < 2 ; ++i)
548       {
549          invokers[i] = new StatefulInvoker(stateful);
550       }
551       
552       for (StatefulInvoker invoker: invokers)
553       {
554          invoker.start();
555       }
556       
557       Thread.sleep(5000);
558       
559       boolean wasConcurrentException = false;
560       for (StatefulInvoker invoker: invokers)
561       {
562          if (invoker.getException() != null)
563             wasConcurrentException = true;
564       }
565
566       assertTrue(wasConcurrentException);
567    }
568    
569    public void testJmxName() throws Exception JavaDoc
570    {
571       ObjectName JavaDoc deployment = new ObjectName JavaDoc("test.ejb3:name=Bill,service=EJB3");
572
573       ClientKernelAbstraction kernel = KernelAbstractionFactory.getClientInstance();
574       kernel.invoke(deployment, "stop", new Object JavaDoc[0], new String JavaDoc[0]);
575       kernel.invoke(deployment, "start", new Object JavaDoc[0], new String JavaDoc[0]);
576    }
577    
578    public void testDestroyException() throws Exception JavaDoc
579    {
580       EntityFacade stateful = (EntityFacade)getInitialContext().lookup("EntityFacadeBean/remote");
581       assertNotNull(stateful);
582       stateful.createEntity("Kalin");
583       
584       try
585       {
586          stateful.remove();
587          fail("should catch RuntimeException");
588       }
589       catch (RuntimeException JavaDoc e)
590       {
591       }
592       
593       stateful.createEntity("Cabernet");
594       
595       try
596       {
597          stateful.removeWithTx();
598          fail("should catch RuntimeException");
599       }
600       catch (RuntimeException JavaDoc e)
601       {
602       }
603       
604       stateful.createEntity("Bailey");
605       
606    }
607
608    public static Test suite() throws Exception JavaDoc
609    {
610       return getDeploySetup(RemoteUnitTestCase.class, "stateful-test.jar");
611    }
612
613 }
614
Popular Tags