KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > util > JTestCase


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JTestCase.java,v 1.24 2004/12/17 11:05:11 camillej Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.util;
27
28 import java.lang.reflect.Constructor JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.rmi.PortableRemoteObject JavaDoc;
35 import javax.transaction.UserTransaction JavaDoc;
36
37 import junit.extensions.RepeatedTest;
38 import junit.framework.TestCase;
39
40 import org.objectweb.jonas.adm.AdmInterface;
41 import org.objectweb.jonas.jtests.tables.DBEnv;
42 import org.objectweb.jonas.jtests.tables.DBEnvHome;
43
44 /**
45  * JTestCase extends TestCase to provide a set of standard routines
46  * used in jonas tests.
47  */

48 public abstract class JTestCase extends TestCase {
49     protected static String JavaDoc jonasName = "jonas"; // change this XXX
50
protected static String JavaDoc testtorun = null;
51     protected static Context JavaDoc ictx = null;
52     public static UserTransaction JavaDoc utx = null;
53     private static AdmInterface admI = null;
54     private static DBEnv dbEnv = null;
55     private static boolean tableSessionLoaded = false;
56
57     protected StringBuffer JavaDoc msgerror = null;
58
59     public static final String JavaDoc PACKAGE = "org.objectweb.jonas.jtests.clients.";
60
61     public JTestCase(String JavaDoc name) {
62         super(name);
63     }
64
65     protected static void callTest(String JavaDoc classname, String JavaDoc testname) throws Exception JavaDoc {
66         Class JavaDoc clazz = Class.forName(PACKAGE + classname);
67         Class JavaDoc[] paramTypes = {String JavaDoc.class};
68         Object JavaDoc[] arguments = {testname};
69         Constructor JavaDoc constructor = clazz.getDeclaredConstructor(paramTypes);
70         JTestCase mytestcase = (JTestCase) constructor.newInstance(arguments);
71         System.out.print("Running " + classname + " (" + testname + ")\t");
72         junit.textui.TestRunner.run(mytestcase);
73     }
74
75      protected static void callrepeatedTest(String JavaDoc classname, String JavaDoc testname, int n) throws Exception JavaDoc {
76         Class JavaDoc clazz = Class.forName(PACKAGE + classname);
77         Class JavaDoc[] paramTypes = {String JavaDoc.class};
78         Object JavaDoc[] arguments = {testname};
79         Constructor JavaDoc constructor = clazz.getDeclaredConstructor(paramTypes);
80         JTestCase mytestcase = (JTestCase) constructor.newInstance(arguments);
81         RepeatedTest myrepeatedtest = new RepeatedTest(mytestcase,n);
82         System.out.print("Running repeated " + n + "times " + classname + " (" + testname + ")\t");
83         junit.textui.TestRunner.run(myrepeatedtest);
84     }
85
86     /**
87      * random returns an integer between 0 and max - 1
88      */

89     public int random(int max) throws RemoteException JavaDoc {
90         double d = Math.random();
91         int ret = (int) (max * d);
92         return ret;
93     }
94
95     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
96
97         String JavaDoc registryPort = System.getProperty("jonas.registryport");
98         return new InitialContext JavaDoc();
99     }
100
101     /**
102      * common setUp routine, used for every test.
103      */

104     protected void setUp() {
105         try {
106             // get InitialContext
107
if (ictx == null) {
108                 ictx = getInitialContext();
109             }
110
111             // get UserTransaction
112
if (utx == null) {
113                 utx = (UserTransaction JavaDoc) PortableRemoteObject.narrow(ictx.lookup("java:comp/UserTransaction"), UserTransaction JavaDoc.class);
114             }
115
116             // Load Session bean tables.jar
117
if (!tableSessionLoaded) {
118                 if (admI == null) {
119                     admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
120                 }
121                 String JavaDoc filename = "tables";
122                 if (!admI.isLoaded(filename + ".jar")) {
123                     admI.addBeans(filename + ".jar");
124                 }
125                 tableSessionLoaded = true;
126             }
127         } catch (NamingException JavaDoc e) {
128             System.err.println("Cannot setup test: " + e);
129             e.printStackTrace();
130         } catch (RemoteException JavaDoc e) {
131             System.err.println("Cannot load session bean: " + e);
132         }
133         debug("Junit Running " + getName());
134     }
135
136     protected void tearDown() throws Exception JavaDoc {
137     }
138
139     /**
140      * unload a bean
141      */

142     public void unloadBeans(String JavaDoc filename) {
143         debug("unloadBeans " + filename);
144         try {
145             // Load bean in EJBServer if not already loaded.
146
if (ictx == null) {
147                 ictx = getInitialContext();
148             }
149             if (admI == null) {
150                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
151             }
152             admI.removeBeans(filename + ".jar");
153         } catch (Exception JavaDoc e) {
154             System.err.println("Cannot unload bean: " + e);
155         }
156     }
157
158     /**
159      * synchronize all entity beans
160      * @param passivate passivate all instances after synchronization.
161      */

162     public void sync(boolean passivate) {
163         debug("sync");
164         try {
165             if (ictx == null) {
166                 ictx = getInitialContext();
167             }
168             if (admI == null) {
169                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
170             }
171             admI.syncAllEntities(passivate);
172         } catch (Exception JavaDoc e) {
173             error("Cannot sync entities" + e);
174         }
175     }
176
177     /**
178      * load a bean jar file in the jonas server
179      * @param filename jar file, without ".jar" extension
180      * @param create creates tables at loading (CMP1 only)
181      * Note that in CMP2, the decision to create the tables is in
182      * the jonas specific deployment descriptor.
183      */

184     public void useBeans(String JavaDoc filename, boolean create) {
185         debug("useBeans " + filename);
186         try {
187             // Load bean in EJBServer if not already loaded.
188
if (ictx == null) {
189                 ictx = getInitialContext();
190             }
191             if (admI == null) {
192                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
193             }
194             if (!admI.isLoaded(filename + ".jar")) {
195                 admI.addBeans(filename + ".jar");
196
197                 // Create table if the bean was not loaded
198
if (create) {
199                     getDBEnv().initTable(filename);
200                 }
201             }
202         } catch (Exception JavaDoc e) {
203             error("Cannot load bean: " + e);
204         }
205     }
206
207     /**
208      * Set the Default Transaction Timeout value on the server.
209      * @param tt timeout in seconds
210      */

211     public static void stopTxAt(int tt) {
212         try {
213             if (admI == null) {
214                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
215             }
216             admI.setTransactionTimeout(tt);
217         } catch (Exception JavaDoc e) {
218             System.err.println("Cannot set transaction timeout: " + e);
219         }
220     }
221
222     /**
223      * Get the session bean to manage database tables.
224      */

225     public DBEnv getDBEnv() {
226         if (dbEnv == null) {
227
228             useBeans("tables", false);
229
230             // Connect to the DBEnvHome object
231
DBEnvHome dbhome = null;
232             try {
233                 dbhome = (DBEnvHome) PortableRemoteObject.narrow(ictx.lookup("tablesDBEnvHome"), DBEnvHome.class);
234             } catch (NamingException JavaDoc e) {
235                 System.err.println(">>> " + e);
236                 fail("Cannot bind to DBEnvHome");
237             }
238
239             // Create the table using a session bean
240
try {
241                 dbEnv = dbhome.create();
242             } catch (Exception JavaDoc e) {
243                 fail(e.toString());
244             }
245         }
246         return dbEnv;
247     }
248
249     /**
250      * for debugging : Uncomment the line.
251      */

252     public static void debug(String JavaDoc msg) {
253         //System.out.println("DEBUG: " + msg);
254
}
255
256     /**
257      * Print error message.
258      */

259     public static void error(String JavaDoc msg) {
260         System.err.println("ERROR: " + msg);
261     }
262
263     /**
264      * sleep n millisec.
265      */

266     public void sleep(int msec) {
267         try {
268             Thread.sleep(msec);
269         } catch (InterruptedException JavaDoc e) {
270             fail(e.toString());
271         }
272     }
273
274     public void testEmpty() throws Exception JavaDoc {
275     }
276
277 }
278
Popular Tags