KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > service > unit > ServiceUnitTestCase


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.service.unit;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.management.Attribute JavaDoc;
27 import javax.management.AttributeList JavaDoc;
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.jboss.ejb3.test.service.ServiceOneRemote;
32 import org.jboss.ejb3.test.service.ServiceSevenRemote;
33 import org.jboss.ejb3.test.service.ServiceSixRemote;
34 import org.jboss.ejb3.test.service.ServiceTwoRemote;
35 import org.jboss.ejb3.test.service.SessionRemote;
36 import org.jboss.logging.Logger;
37 import org.jboss.security.SecurityAssociation;
38 import org.jboss.security.SimplePrincipal;
39 import org.jboss.test.JBossTestCase;
40 import junit.framework.Test;
41
42 /**
43  * Sample client for the jboss container.
44  *
45  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
46  * @version $Id: ServiceUnitTestCase.java 57005 2006-09-20 13:10:12Z wolfc $
47  */

48
49 public class ServiceUnitTestCase
50 extends JBossTestCase
51 {
52    private static final Logger log = Logger.getLogger(ServiceUnitTestCase.class);
53
54    static boolean deployed = false;
55    static int test = 0;
56
57    public ServiceUnitTestCase(String JavaDoc name)
58    {
59
60       super(name);
61
62    }
63    
64    public void testEjbJar() throws Exception JavaDoc
65    {
66       SessionRemote session = (SessionRemote)getInitialContext().lookup("SessionBean/remote");
67       assertNotNull(session);
68    }
69    
70    public void testEjbInjection() throws Exception JavaDoc
71    {
72       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
73       SecurityAssociation.setCredential("password".toCharArray());
74       
75       ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote");
76       test.testEjbInjection();
77    }
78    
79    public void testSecurityDomain() throws Exception JavaDoc
80    {
81       try
82       {
83          SecurityAssociation.clear();
84          
85          ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote");
86          test.testEjbInjection();
87          fail("Should have thrown EJBAccessException");
88       }
89       catch (javax.ejb.EJBAccessException JavaDoc e)
90       {
91          
92       }
93    }
94
95    public void testServiceWithDefaultRemoteJNDIName() throws Exception JavaDoc
96    {
97       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
98       SecurityAssociation.setCredential("password".toCharArray());
99       
100       ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote");
101       test.setRemoteMethodCalls(0);
102       final int count = 15;
103       Thread JavaDoc[] threads = new Thread JavaDoc[count];
104       for (int i = 0 ; i < count ; i++)
105       {
106          final int outer = i;
107          threads[i] = new Thread JavaDoc(
108                new Runnable JavaDoc()
109                {
110                   public void run()
111                   {
112                      try
113                      {
114                         ServiceOneRemote test = (ServiceOneRemote) getInitialContext().lookup("ServiceOne/remote");
115                         for (int j = 0 ; j < count ; j++)
116                         {
117                            String JavaDoc s = outer + "_" + j;
118                            //System.out.println(s);
119
test.remoteMethod(s);
120                         }
121                      }
122                      catch(Exception JavaDoc e)
123                      {
124                         throw new RuntimeException JavaDoc(e);
125                      }
126                   }
127                }
128             );
129          threads[i].start();
130       }
131
132       Thread.sleep(5000);
133       for (int i = 0 ; i < count ; i++)
134       {
135          threads[i].join();
136       }
137       assertEquals("There should only ever be one instance of the service", 1, test.getInstances());
138       assertEquals("Wrong number of remote method calls", count * count, test.getRemoteMethodCalls());
139    }
140    
141    /**
142     * Was injection successful after start?
143     */

144    public void testEJB3_587() throws Exception JavaDoc
145    {
146       MBeanServerConnection JavaDoc server = getServer();
147       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3.bugs:service=TestResourceInjectionService");
148       boolean success = (Boolean JavaDoc)server.getAttribute(testerName, "TestedSuccessful");
149       assertTrue(success);
150    }
151
152    /**
153     * Is injection successful when getting a management attribute?
154     */

155    public void testEJB3_587_2() throws Exception JavaDoc
156    {
157       MBeanServerConnection JavaDoc server = getServer();
158       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3.bugs:service=TestResourceInjectionService");
159       boolean success = (Boolean JavaDoc)server.getAttribute(testerName, "TestedSuccessfulNow");
160       assertTrue(success);
161    }
162
163    public void testServiceWithDefaultLocalJNDIName() throws Exception JavaDoc
164    {
165       MBeanServerConnection JavaDoc server = getServer();
166       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=service");
167       Object JavaDoc[] params = {};
168       String JavaDoc[] sig = {};
169       server.invoke(testerName, "testServiceWithDefaultLocalJNDIName", params, sig);
170    }
171
172    private class RemoteTest extends Thread JavaDoc
173    {
174       private int count;
175       private int outer;
176
177       public Exception JavaDoc exception;
178
179       public RemoteTest(int outer, int count)
180       {
181          this.outer = outer;
182          this.count = count;
183       }
184
185       public void run()
186       {
187          try
188          {
189             ServiceSevenRemote test = (ServiceSevenRemote) getInitialContext().lookup("ServiceSeven/remote");
190             for (int j = 0 ; j < count ; j++)
191             {
192                String JavaDoc s = outer + "_" + j;
193                //System.out.println(s);
194
test.remoteMethod(s);
195             }
196          }
197          catch(Exception JavaDoc e)
198          {
199             exception = e;
200          }
201       }
202    }
203    
204    public void testRemoteServiceWithInterfaceAnnotations() throws Exception JavaDoc
205    {
206       ServiceSevenRemote test = (ServiceSevenRemote) getInitialContext().lookup("ServiceSeven/remote");
207       test.setRemoteMethodCalls(0);
208       final int count = 15;
209       RemoteTest[] threads = new RemoteTest[count];
210       for (int i = 0 ; i < count ; i++)
211       {
212          final int outer = i;
213          threads[i] = new RemoteTest(outer, count);
214          threads[i].start();
215       }
216
217       Thread.sleep(5000);
218       for (int i = 0 ; i < count ; i++)
219       {
220          threads[i].join();
221          if (threads[i].exception != null) throw threads[i].exception;
222       }
223       assertEquals("There should only ever be one instance of the service", 1, test.getInstances());
224       assertEquals("Wrong number of remote method calls", count * count, test.getRemoteMethodCalls());
225    }
226
227    public void testLocalServiceWithInterfaceAnnotation() throws Exception JavaDoc
228    {
229       MBeanServerConnection JavaDoc server = getServer();
230       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=service");
231       Object JavaDoc[] params = {};
232       String JavaDoc[] sig = {};
233       server.invoke(testerName, "testLocalServiceWithInterfaceAnnotation", params, sig);
234    }
235    
236    public void testServiceWithRemoteBinding() throws Exception JavaDoc
237    {
238       ServiceTwoRemote test = (ServiceTwoRemote) getInitialContext().lookup("serviceTwo/remote");
239       test.setCalled(false);
240       assertFalse("Called should be false", test.getCalled());
241       test.remoteMethod();
242       assertTrue("Called should be true", test.getCalled());
243    }
244
245    public void testServiceWithLocalBinding() throws Exception JavaDoc
246    {
247       MBeanServerConnection JavaDoc server = getServer();
248       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=service");
249       Object JavaDoc[] params = {};
250       String JavaDoc[] sig = {};
251       server.invoke(testerName, "testServiceWithLocalBinding", params, sig);
252    }
253    
254    public void testDeploymentDescriptorServiceInjection() throws Exception JavaDoc
255    {
256       ServiceSixRemote test = (ServiceSixRemote) getInitialContext().lookup("serviceSix/remote");
257       assertNotNull(test);
258       test.testInjection();
259    }
260
261    public void testDeploymentDescriptorServiceWithRemoteBinding() throws Exception JavaDoc
262    {
263       ServiceSixRemote test = (ServiceSixRemote) getInitialContext().lookup("serviceSix/remote");
264       test.setCalled(false);
265       assertFalse("Called should be false", test.getCalled());
266       test.remoteMethod();
267       assertTrue("Called should be true", test.getCalled());
268    }
269
270    public void testDeploymentDescriptorServiceWithLocalBinding() throws Exception JavaDoc
271    {
272       MBeanServerConnection JavaDoc server = getServer();
273       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=service");
274       Object JavaDoc[] params = {};
275       String JavaDoc[] sig = {};
276       server.invoke(testerName, "testDeploymentDescriptorServiceWithLocalBinding", params, sig);
277    }
278    
279    public void testDeploymentDescriptorManagementServiceWithDefaultName() throws Exception JavaDoc
280    {
281       MBeanServerConnection JavaDoc server = getServer();
282       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceSix,type=ManagementInterface");
283       server.setAttribute(testerName, new Attribute JavaDoc("Attribute", new Integer JavaDoc(1234)));
284       int ret = ((Integer JavaDoc)server.getAttribute(testerName, "Attribute"));
285       assertEquals("wrong value for Attribute", 1234, ret);
286
287       String JavaDoc s = (String JavaDoc)server.invoke(testerName, "jmxOperation", new Object JavaDoc[]{"1"}, new String JavaDoc[]{String JavaDoc.class.getName()});
288       assertEquals("wrong return value", "x1x", s);
289
290       String JavaDoc[] sa = (String JavaDoc[])server.invoke(testerName, "jmxOperation", new Object JavaDoc[]{new String JavaDoc[]{"0", "1"}}, new String JavaDoc[]{String JavaDoc[].class.getName()});
291       assertEquals("wrong number of return values", 2, sa.length);
292       for (int i = 0 ; i < sa.length ; i++)
293       {
294          assertEquals("Wrong return value index " + i, "x" + i + "x", sa[i]);
295       }
296
297       AttributeList JavaDoc attributes = new AttributeList JavaDoc();
298       attributes.add(new Attribute JavaDoc("SomeAttr", new Integer JavaDoc(72)));
299       attributes.add(new Attribute JavaDoc("OtherAttr", new Integer JavaDoc(99)));
300       attributes = server.setAttributes(testerName, attributes);
301
302       String JavaDoc[] attrs = new String JavaDoc[]{"Attribute", "SomeAttr", "OtherAttr"};
303       AttributeList JavaDoc list = server.getAttributes(testerName, attrs);
304
305       assertEquals("Wrong number of attributes returned", 3, list.size());
306       for (Iterator JavaDoc it = list.iterator() ; it.hasNext() ; )
307       {
308          Attribute JavaDoc attr = (Attribute JavaDoc)it.next();
309          String JavaDoc name = attr.getName();
310          if (name.equals("Attribute"))
311          {
312             assertEquals("Wrong number for Attribute", 1234, attr.getValue());
313          }
314          else if (name.equals("SomeAttr"))
315          {
316             assertEquals("Wrong number for SomeAttr", 72, attr.getValue());
317          }
318          else if (name.equals("OtherAttr"))
319          {
320             assertEquals("Wrong number for OtherAttr", 99, attr.getValue());
321          }
322          else
323          {
324             throw new RuntimeException JavaDoc("Unknown attribute returned: " + name);
325          }
326       }
327
328       server.setAttribute(testerName, new Attribute JavaDoc("WriteOnly", new Integer JavaDoc(2525)));
329       assertEquals("Wrong read only value", 2525, ((Integer JavaDoc)server.getAttribute(testerName, "ReadOnly")).intValue());
330    }
331
332    public void testManagementServiceWithDefaultName() throws Exception JavaDoc
333    {
334       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
335       SecurityAssociation.setCredential("password".toCharArray());
336       
337       MBeanServerConnection JavaDoc server = getServer();
338       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceOne,type=ManagementInterface");
339       server.setAttribute(testerName, new Attribute JavaDoc("Attribute", new Integer JavaDoc(1234)));
340       int ret = ((Integer JavaDoc)server.getAttribute(testerName, "Attribute"));
341       assertEquals("wrong value for Attribute", 1234, ret);
342
343       String JavaDoc s = (String JavaDoc)server.invoke(testerName, "jmxOperation", new Object JavaDoc[]{"1"}, new String JavaDoc[]{String JavaDoc.class.getName()});
344       assertEquals("wrong return value", "x1x", s);
345
346       String JavaDoc[] sa = (String JavaDoc[])server.invoke(testerName, "jmxOperation", new Object JavaDoc[]{new String JavaDoc[]{"0", "1"}}, new String JavaDoc[]{String JavaDoc[].class.getName()});
347       assertEquals("wrong number of return values", 2, sa.length);
348       for (int i = 0 ; i < sa.length ; i++)
349       {
350          assertEquals("Wrong return value index " + i, "x" + i + "x", sa[i]);
351       }
352
353       AttributeList JavaDoc attributes = new AttributeList JavaDoc();
354       attributes.add(new Attribute JavaDoc("SomeAttr", new Integer JavaDoc(72)));
355       attributes.add(new Attribute JavaDoc("OtherAttr", new Integer JavaDoc(99)));
356       attributes = server.setAttributes(testerName, attributes);
357
358       String JavaDoc[] attrs = new String JavaDoc[]{"Attribute", "SomeAttr", "OtherAttr"};
359       AttributeList JavaDoc list = server.getAttributes(testerName, attrs);
360
361       assertEquals("Wrong number of attributes returned", 3, list.size());
362       for (Iterator JavaDoc it = list.iterator() ; it.hasNext() ; )
363       {
364          Attribute JavaDoc attr = (Attribute JavaDoc)it.next();
365          String JavaDoc name = attr.getName();
366          if (name.equals("Attribute"))
367          {
368             assertEquals("Wrong number for Attribute", 1234, attr.getValue());
369          }
370          else if (name.equals("SomeAttr"))
371          {
372             assertEquals("Wrong number for SomeAttr", 72, attr.getValue());
373          }
374          else if (name.equals("OtherAttr"))
375          {
376             assertEquals("Wrong number for OtherAttr", 99, attr.getValue());
377          }
378          else
379          {
380             throw new RuntimeException JavaDoc("Unknown attribute returned: " + name);
381          }
382       }
383
384       server.setAttribute(testerName, new Attribute JavaDoc("WriteOnly", new Integer JavaDoc(2525)));
385       assertEquals("Wrong read only value", 2525, ((Integer JavaDoc)server.getAttribute(testerName, "ReadOnly")).intValue());
386    }
387    
388    public void testManagementServiceWithInterfaceAnnotations() throws Exception JavaDoc
389    {
390        MBeanServerConnection JavaDoc server = getServer();
391       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceSeven,type=ManagementInterface");
392       server.setAttribute(testerName, new Attribute JavaDoc("Attribute", new Integer JavaDoc(1234)));
393       int ret = ((Integer JavaDoc)server.getAttribute(testerName, "Attribute"));
394       assertEquals("wrong value for Attribute", 1234, ret);
395
396       String JavaDoc s = (String JavaDoc)server.invoke(testerName, "jmxOperation", new Object JavaDoc[]{"1"}, new String JavaDoc[]{String JavaDoc.class.getName()});
397       assertEquals("wrong return value", "x1x", s);
398
399       String JavaDoc[] sa = (String JavaDoc[])server.invoke(testerName, "jmxOperation", new Object JavaDoc[]{new String JavaDoc[]{"0", "1"}}, new String JavaDoc[]{String JavaDoc[].class.getName()});
400       assertEquals("wrong number of return values", 2, sa.length);
401       for (int i = 0 ; i < sa.length ; i++)
402       {
403          assertEquals("Wrong return value index " + i, "x" + i + "x", sa[i]);
404       }
405
406       AttributeList JavaDoc attributes = new AttributeList JavaDoc();
407       attributes.add(new Attribute JavaDoc("SomeAttr", new Integer JavaDoc(72)));
408       attributes.add(new Attribute JavaDoc("OtherAttr", new Integer JavaDoc(99)));
409       attributes = server.setAttributes(testerName, attributes);
410
411       String JavaDoc[] attrs = new String JavaDoc[]{"Attribute", "SomeAttr", "OtherAttr"};
412       AttributeList JavaDoc list = server.getAttributes(testerName, attrs);
413
414       assertEquals("Wrong number of attributes returned", 3, list.size());
415       for (Iterator JavaDoc it = list.iterator() ; it.hasNext() ; )
416       {
417          Attribute JavaDoc attr = (Attribute JavaDoc)it.next();
418          String JavaDoc name = attr.getName();
419          if (name.equals("Attribute"))
420          {
421             assertEquals("Wrong number for Attribute", 1234, attr.getValue());
422          }
423          else if (name.equals("SomeAttr"))
424          {
425             assertEquals("Wrong number for SomeAttr", 72, attr.getValue());
426          }
427          else if (name.equals("OtherAttr"))
428          {
429             assertEquals("Wrong number for OtherAttr", 99, attr.getValue());
430          }
431          else
432          {
433             throw new RuntimeException JavaDoc("Unknown attribute returned: " + name);
434          }
435       }
436
437       server.setAttribute(testerName, new Attribute JavaDoc("WriteOnly", new Integer JavaDoc(2525)));
438       assertEquals("Wrong read only value", 2525, ((Integer JavaDoc)server.getAttribute(testerName, "ReadOnly")).intValue());
439    }
440
441    public void testServiceDependencyInjectionAndInterception() throws Exception JavaDoc
442    {
443        MBeanServerConnection JavaDoc server = getServer();
444       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:management=interface,with=customName,is=serviceThree");
445       Boolean JavaDoc injected = (Boolean JavaDoc)server.getAttribute(testerName, "Injected");
446       assertTrue("Injected should have been true", injected);
447
448       Integer JavaDoc intercepted = (Integer JavaDoc)server.getAttribute(testerName, "Intercepted");;
449       assertEquals("Wrong number of interceptions", 2, intercepted.intValue());
450    }
451
452    public void testCreationOrder() throws Exception JavaDoc
453    {
454        MBeanServerConnection JavaDoc server = getServer();
455       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=service");
456       ArrayList JavaDoc creates = (ArrayList JavaDoc)server.getAttribute(testerName, "Creates");
457       ArrayList JavaDoc starts = (ArrayList JavaDoc)server.getAttribute(testerName, "Starts");
458
459       //ServiceTwo does not implement any lifecycle so it won't appear
460
int expectedLength = 4;
461       assertEquals("Wrong length of created MBeans", expectedLength, creates.size());
462       assertEquals("Wrong length of started MBeans", expectedLength, starts.size());
463
464       String JavaDoc createAll = " ";
465       String JavaDoc startAll = " ";
466       for (int i = 0; i < expectedLength; i++)
467       {
468          createAll += creates.get(i) + " ";
469          startAll += starts.get(i) + " ";
470       }
471
472       for (int i = 0 ; i < expectedLength ; i++)
473       {
474          // EJBTHREE-655: it's no use checking the creation order
475
// int create = Integer.parseInt((String)creates.get(i));
476
int start = Integer.parseInt((String JavaDoc)starts.get(i));
477          int expected = (i == 0) ? i + 1 : i + 2;
478 // assertEquals("Creation of Service" + create + "appears at the wronmg place" + createAll, expected, create);
479
assertEquals("Start of Service" + start + "appears at the wronmg place" + startAll, expected, start);
480       }
481    }
482
483    public void testDependsInjection() throws Exception JavaDoc
484    {
485        MBeanServerConnection JavaDoc server = getServer();
486       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.j2ee:service=EJB3,jar=service-test.jar,name=ServiceFive,type=ManagementInterface");
487       assertTrue("Proxy field was not injected", (Boolean JavaDoc)server.getAttribute(testerName, "InjectedProxyField"));
488       assertTrue("Proxy method was not injected", (Boolean JavaDoc)server.getAttribute(testerName, "InjectedProxyMethod"));
489       assertTrue("ObjectName field was not injected", (Boolean JavaDoc)server.getAttribute(testerName, "InjectedObjectNameField"));
490       assertTrue("ObjectName method was not injected", (Boolean JavaDoc)server.getAttribute(testerName, "InjectedObjectNameMethod"));
491       SessionRemote test = (SessionRemote) getInitialContext().lookup("Session/remote");
492       assertTrue("StatelessBean @Depends was not injected", test.injectedDepends());
493    }
494
495
496    public static Test suite() throws Exception JavaDoc
497    {
498       return getDeploySetup(ServiceUnitTestCase.class, "service-test.sar, service-test.jar");
499    }
500
501 }
502
Popular Tags