KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > lock > test > EnterpriseEntityTest


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.lock.test;
23
24 import java.rmi.*;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.ejb.FinderException JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import junit.framework.Assert;
34
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38
39 import org.jboss.logging.Logger;
40
41 import org.jboss.test.JBossTestCase;
42 import org.jboss.test.lock.interfaces.EnterpriseEntity;
43
44 import org.jboss.test.lock.interfaces.EnterpriseEntityHome;
45
46 /**
47  * #Description of the Class
48  */

49 public abstract class EnterpriseEntityTest
50        extends JBossTestCase
51 {
52    /**
53     * Description of the Field
54     */

55    public final static int DEFAULT_THREAD_COUNT = 20;
56    /**
57     * Description of the Field
58     */

59    public final static int DEFAULT_ITERATIONS = 10;
60
61    private String JavaDoc jndiname;
62
63    /**
64     * The number of threads to test with.
65     */

66    private int nbThreads;
67    private int completedThreads;
68
69    /**
70     * The number of iterations each thread will go through
71     */

72    private int iterations;
73
74    private Worker[] threads;
75
76    private HashMap JavaDoc param = new HashMap JavaDoc();
77
78    private EnterpriseEntity entity;
79
80    private boolean failed;
81
82    /**
83     * Constructor for the EnterpriseEntityTest object
84     *
85     * @param name Description of Parameter
86     * @param jndiname Description of Parameter
87     */

88    public EnterpriseEntityTest(final String JavaDoc name,
89          final String JavaDoc jndiname)
90    {
91       super(name);
92       this.jndiname = jndiname;
93    }
94
95    /**
96     * A unit test for JUnit
97     *
98     * @exception Exception Description of Exception
99     */

100    public void testSingleBean() throws Exception JavaDoc
101    {
102       getLog().debug("Spawning " + nbThreads + " threads for " +
103             iterations + " iterations with single bean call");
104
105       Task prototype =
106          new Task()
107          {
108             /**
109              * Main processing method for the EnterpriseEntityTest object
110              *
111              * @param name Description of Parameter
112              * @param i Description of Parameter
113              * @exception Exception Description of Exception
114              */

115             public void run(String JavaDoc name, int i) throws Exception JavaDoc
116             {
117                entity.setField(name + " i=" + i);
118             }
119          };
120
121       run(prototype);
122    }
123
124    /**
125     * A unit test for JUnit
126     *
127     * @exception Exception Description of Exception
128     */

129    public void testB2B() throws Exception JavaDoc
130    {
131       getLog().debug("Spawning " + nbThreads + " threads for " +
132             iterations + " iterations with bean to bean call");
133
134       entity.setNextEntity("daniel");
135
136       Task prototype =
137          new Task()
138          {
139             /**
140              * Main processing method for the EnterpriseEntityTest object
141              *
142              * @param name Description of Parameter
143              * @param i Description of Parameter
144              * @exception Exception Description of Exception
145              */

146             public void run(String JavaDoc name, int i) throws Exception JavaDoc
147             {
148                entity.setAndCopyField(name + " i=" + i);
149             }
150          };
151
152       run(prototype);
153    }
154
155    /**
156     * The JUnit setup method
157     *
158     * @exception Exception Description of Exception
159     */

160    protected void setUp() throws Exception JavaDoc
161    {
162       nbThreads = getThreadCount();//DEFAULT_THREAD_COUNT;
163
iterations = getIterationCount();//DEFAULT_ITERATIONS;
164
getLog().debug("+++ Setting up: " + getClass().getName() + " test: " + getName());
165       EnterpriseEntityHome home =
166           (EnterpriseEntityHome)getInitialContext().lookup(jndiname);
167
168       try
169       {
170          entity = home.findByPrimaryKey("seb");
171       }
172       catch (FinderException JavaDoc e)
173       {
174          entity = home.create("seb");
175       }
176
177       // setup the threads
178
threads = new Worker[nbThreads];
179    }
180
181    /**
182     * Sets the Failed attribute of the EnterpriseEntityTest object
183     */

184    protected synchronized void setFailed()
185    {
186       failed = true;
187    }
188
189
190    /**
191     * #Description of the Method
192     *
193     * @param prototype Description of Parameter
194     * @exception Exception Description of Exception
195     */

196    protected void startAll(Task prototype) throws Exception JavaDoc
197    {
198       completedThreads = 0;
199       for (int i = 0; i < nbThreads; i++)
200       {
201          Task task = (Task)prototype.clone();
202          threads[i] = new Worker("Thread #" + (i + 1), task, getLog());
203          threads[i].start();
204       }
205    }
206
207    /**
208     * #Description of the Method
209     *
210     * @exception Exception Description of Exception
211     */

212    protected void joinAll() throws Exception JavaDoc
213    {
214       // wait for all the threads to finish
215
for (int i = 0; i < nbThreads; i++)
216       {
217          threads[i].join();
218       }
219    }
220
221    /**
222     * Main processing method for the EnterpriseEntityTest object
223     *
224     * @param prototype Description of Parameter
225     * @exception Exception Description of Exception
226     */

227    protected void run(Task prototype) throws Exception JavaDoc
228    {
229       startAll(prototype);
230       joinAll();
231       assertTrue(!failed);
232    }
233
234    /**
235     * #Description of the Method
236     *
237     * @return Description of the Returned Value
238     */

239    protected boolean hasFailed()
240    {
241       return failed;
242    }
243
244
245    /////////////////////////////////////////////////////////////////////////
246
// Iteration Worker & Task //
247
/////////////////////////////////////////////////////////////////////////
248

249    /**
250     * #Description of the Class
251     */

252    public abstract class Task
253           implements Cloneable JavaDoc
254    {
255       /**
256        * Main processing method for the Task object
257        *
258        * @param name Description of Parameter
259        * @param i Description of Parameter
260        * @exception Exception Description of Exception
261        */

262       public abstract void run(String JavaDoc name, int i) throws Exception JavaDoc;
263
264       /**
265        * #Description of the Method
266        *
267        * @return Description of the Returned Value
268        */

269       public Object JavaDoc clone()
270       {
271          try
272          {
273             return super.clone();
274          }
275          catch (CloneNotSupportedException JavaDoc e)
276          {
277             throw new InternalError JavaDoc();
278          }
279       }
280    }
281
282    /**
283     * #Description of the Class
284     */

285    public class Worker
286           extends Thread JavaDoc
287    {
288       /**
289        * Description of the Field
290        */

291       public String JavaDoc name;
292       /**
293        * Description of the Field
294        */

295       public boolean running;
296       /**
297        * Description of the Field
298        */

299       public Task task;
300
301       private Logger log;
302
303       /**
304        * Constructor for the Worker object
305        *
306        * @param name Description of Parameter
307        * @param task Description of Parameter
308        * @param log Description of Parameter
309        */

310       public Worker(final String JavaDoc name, final Task task, Logger log)
311       {
312          this.name = name;
313          this.task = task;
314          this.log = log;
315          running = true;
316       }
317
318       /**
319        * Main processing method for the Worker object
320        */

321       public void run()
322       {
323          long start = System.currentTimeMillis();
324          int i;
325
326          for (i = 0; i < iterations; i++)
327          {
328             if (!running || hasFailed())
329             {
330                break;
331             }
332
333             try
334             {
335                task.run(name, i);
336                //log.debug(name + " " + (i+1) + " iterations done");
337
}
338             catch (Throwable JavaDoc t)
339             {
340                log.error(name + " caught an exception, dying", t);
341                t.printStackTrace();
342                running = false;
343                setFailed();
344             }
345          }
346
347          synchronized (this)
348          {
349             completedThreads++;
350          }
351          long end = System.currentTimeMillis();
352          log.debug(name + ": did " + i +
353                " iterations in " + (end - start) + "ms, complete=" + completedThreads);
354       }
355    }
356 }
357
358
Popular Tags