KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > service > Tester


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;
23
24
25 import org.jboss.system.ServiceMBeanSupport;
26
27 import javax.naming.InitialContext JavaDoc;
28
29 import java.io.File JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 39547 $
37  */

38 public class Tester extends ServiceMBeanSupport implements TesterMBean
39 {
40    public static ArrayList JavaDoc creates = new ArrayList JavaDoc();
41    public static ArrayList JavaDoc starts = new ArrayList JavaDoc();
42    
43    public void testLocalServiceWithInterfaceAnnotation() throws Exception JavaDoc
44    {
45       final int count = 15;
46       final InitialContext JavaDoc ctx = new InitialContext JavaDoc();
47       ServiceSevenLocal test = (ServiceSevenLocal) ctx.lookup("ServiceSeven/local");
48       test.setLocalMethodCalls(0);
49
50       Thread JavaDoc[] threads = new Thread JavaDoc[count];
51       for (int i = 0 ; i < count ; i++)
52       {
53          final int outer = i;
54          threads[i] = new Thread JavaDoc(
55                new Runnable JavaDoc()
56                {
57                   public void run()
58                   {
59                      try
60                      {
61                         ServiceSevenLocal test = (ServiceSevenLocal) ctx.lookup("ServiceSeven/local");
62                         for (int j = 0 ; j < count ; j++)
63                         {
64                            String JavaDoc s = outer + "_" + j;
65                            //System.out.println(s);
66
test.localMethod(s);
67                         }
68                      }
69                      catch(Exception JavaDoc e)
70                      {
71                         throw new RuntimeException JavaDoc(e);
72                      }
73                   }
74                }
75             );
76          threads[i].start();
77       }
78
79       Thread.sleep(5000);
80       for (int i = 0 ; i < count ; i++)
81       {
82          threads[i].join();
83       }
84
85       if (test.getInstances() != 1)
86       {
87          throw new RuntimeException JavaDoc("There should only ever be one instance of the service. We have " + test.getInstances());
88       }
89
90       int localCalls = test.getLocalMethodCalls();
91       if (localCalls != (count * count))
92       {
93          throw new RuntimeException JavaDoc("There should be " + count * count + " local method calls, not " + localCalls);
94       }
95    }
96
97    public void testServiceWithDefaultLocalJNDIName() throws Exception JavaDoc
98    {
99       final int count = 15;
100       final InitialContext JavaDoc ctx = new InitialContext JavaDoc();
101       ServiceOneLocal test = (ServiceOneLocal) ctx.lookup("ServiceOne/local");
102       test.setLocalMethodCalls(0);
103
104       Thread JavaDoc[] threads = new Thread JavaDoc[count];
105       for (int i = 0 ; i < count ; i++)
106       {
107          final int outer = i;
108          threads[i] = new Thread JavaDoc(
109                new Runnable JavaDoc()
110                {
111                   public void run()
112                   {
113                      try
114                      {
115                         ServiceOneLocal test = (ServiceOneLocal) ctx.lookup("ServiceOne/local");
116                         for (int j = 0 ; j < count ; j++)
117                         {
118                            String JavaDoc s = outer + "_" + j;
119                            //System.out.println(s);
120
test.localMethod(s);
121                         }
122                      }
123                      catch(Exception JavaDoc e)
124                      {
125                         throw new RuntimeException JavaDoc(e);
126                      }
127                   }
128                }
129             );
130          threads[i].start();
131       }
132
133       Thread.sleep(5000);
134       for (int i = 0 ; i < count ; i++)
135       {
136          threads[i].join();
137       }
138
139       if (test.getInstances() != 1)
140       {
141          throw new RuntimeException JavaDoc("There should only ever be one instance of the service. We have " + test.getInstances());
142       }
143
144       int localCalls = test.getLocalMethodCalls();
145       if (localCalls != (count * count))
146       {
147          throw new RuntimeException JavaDoc("There should be " + count * count + " local method calls, not " + localCalls);
148       }
149    }
150
151    public void testServiceWithLocalBinding() throws Exception JavaDoc
152    {
153       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
154       ServiceTwoLocal test = (ServiceTwoLocal) ctx.lookup("serviceTwo/local");
155       test.setCalled(false);
156       if (test.getCalled()) throw new RuntimeException JavaDoc("Called should be false, not " + test.getCalled());
157       test.localMethod();
158       if (!test.getCalled()) throw new RuntimeException JavaDoc("Called should be true, not " + test.getCalled());
159    }
160    
161    public void testDeploymentDescriptorServiceWithLocalBinding() throws Exception JavaDoc
162    {
163       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
164       ServiceSixLocal test = (ServiceSixLocal) ctx.lookup("serviceSix/local");
165       test.setCalled(false);
166       if (test.getCalled()) throw new RuntimeException JavaDoc("Called should be false, not " + test.getCalled());
167       test.localMethod();
168       if (!test.getCalled()) throw new RuntimeException JavaDoc("Called should be true, not " + test.getCalled());
169    }
170
171    public ArrayList JavaDoc getCreates()
172    {
173       return creates;
174    }
175
176    public ArrayList JavaDoc getStarts()
177    {
178       return starts;
179    }
180
181 }
182
Popular Tags