001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id:
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.util;
027:
028: import java.lang.reflect.Constructor;
029: import java.rmi.RemoteException;
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.naming.NamingException;
033: import javax.rmi.PortableRemoteObject;
034: import javax.transaction.UserTransaction;
035: import junit.framework.TestCase;
036: import junit.extensions.RepeatedTest; //import org.objectweb.jonas.adm.AdmInterface;
037: import org.objectweb.jonas.jtests.ftables.DBEnv;
038: import org.objectweb.jonas.jtests.ftables.DBEnvHome;
039:
040: /**
041: * JTestCase extends TestCase to provide a set of standard routines
042: * used in jonas tests.
043: */
044: public abstract class JTestCase extends TestCase {
045: protected static String jonasName = "jonas"; // change this XXX
046: protected static String testtorun = null;
047: protected static Context ictx = null;
048: public static UserTransaction utx = null;
049: //private static AdmInterface admI = null;
050: private static DBEnv dbEnv = null;
051: private static boolean tableSessionLoaded = false;
052:
053: protected StringBuffer msgerror = null;
054:
055: public static final String PACKAGE = "org.objectweb.jonas.jtests.clients.";
056:
057: public JTestCase(String name) {
058: super (name);
059: }
060:
061: protected static void callTest(String classname, String testname)
062: throws Exception {
063: Class clazz = Class.forName(PACKAGE + classname);
064: Class[] paramTypes = { String.class };
065: Object[] arguments = { testname };
066: Constructor constructor = clazz
067: .getDeclaredConstructor(paramTypes);
068: JTestCase mytestcase = (JTestCase) constructor
069: .newInstance(arguments);
070: // System.out.print("Running " + classname + " (" + testname + ")\t");
071: junit.textui.TestRunner.run(mytestcase);
072: }
073:
074: protected static void callrepeatedTest(String classname,
075: String testname, int n) throws Exception {
076: Class clazz = Class.forName(PACKAGE + classname);
077: Class[] paramTypes = { String.class };
078: Object[] arguments = { testname };
079: Constructor constructor = clazz
080: .getDeclaredConstructor(paramTypes);
081: JTestCase mytestcase = (JTestCase) constructor
082: .newInstance(arguments);
083: RepeatedTest myrepeatedtest = new RepeatedTest(mytestcase, n);
084: //System.out.print("Running repeated " + n + "times " + classname + " (" + testname + ")\t");
085: junit.textui.TestRunner.run(myrepeatedtest);
086: }
087:
088: /**
089: * random returns an integer between 0 and max - 1
090: */
091: public int random(int max) throws RemoteException {
092: double d = Math.random();
093: int ret = (int) (max * d);
094: return ret;
095: }
096:
097: private Context getInitialContext() throws NamingException {
098:
099: String registryPort = System.getProperty("jonas.registryport");
100: return new InitialContext();
101: }
102:
103: /**
104: * common setUp routine, used for every test.
105: */
106: protected void setUp() {
107: try {
108: // get InitialContext
109: if (ictx == null) {
110: ictx = getInitialContext();
111: }
112:
113: // get UserTransaction
114: if (utx == null) {
115: utx = (UserTransaction) PortableRemoteObject
116: .narrow(
117: ictx
118: .lookup("javax.transaction.UserTransaction"),
119: UserTransaction.class);
120: }
121:
122: /**
123: Load Session bean tables.jar
124:
125: if (!tableSessionLoaded) {
126: if (admI == null) {
127: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
128: }
129: String filename = "ftables";
130: if (!admI.isLoaded(filename + ".jar")) {
131: admI.addBeans(filename + ".jar");
132: }
133: tableSessionLoaded = true;
134: }
135: */
136: } catch (NamingException e) {
137: System.err.println("Cannot setup test: " + e);
138: e.printStackTrace();
139: /**
140: } catch (RemoteException e) {
141: System.err.println("Cannot load session bean: " + e);
142: */
143: }
144:
145: debug("Junit Running " + getName());
146: }
147:
148: protected void tearDown() throws Exception {
149: }
150:
151: public void unloadBeans(String filename) {
152: debug("unloadBeans " + filename);
153:
154: /**
155: * unload a bean
156:
157: try {
158: // Load bean in EJBServer if not already loaded.
159: if (ictx == null) {
160: ictx = getInitialContext();
161: }
162: if (admI == null) {
163: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
164: }
165: admI.removeBeans(filename + ".jar");
166: } catch (Exception e) {
167: System.err.println("Cannot unload bean: " + e);
168: }
169:
170: */
171:
172: }
173:
174: public void sync(boolean passivate) {
175: debug("sync");
176:
177: /**
178: * synchronize all entity beans
179: * @param passivate passivate all instances after synchronization.
180:
181: try {
182: if (ictx == null) {
183: ictx = getInitialContext();
184: }
185: if (admI == null) {
186: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
187: }
188: admI.syncAllEntities(passivate);
189: } catch (Exception e) {
190: error("Cannot sync entities" + e);
191: }
192: */
193: }
194:
195: public void useBeans(String filename, boolean create) {
196: debug("useBeans " + filename);
197:
198: /**
199: * load a bean jar file in the jonas server
200: * @param filename jar file, without ".jar" extension
201: * @param create creates tables at loading (CMP1 only)
202: * Note that in CMP2, the decision to create the tables is in
203: * the jonas specific deployment descriptor.
204:
205:
206: try {
207: // Load bean in EJBServer if not already loaded.
208: if (ictx == null) {
209: ictx = getInitialContext();
210: }
211: if (admI == null) {
212: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
213: }
214: if (!admI.isLoaded(filename + ".jar")) {
215: admI.addBeans(filename + ".jar");
216:
217: // Create table if the bean was not loaded
218: if (create) {
219: getDBEnv().initTable(filename);
220: }
221: }
222: } catch (Exception e) {
223: error("Cannot load bean: " + e);
224: }
225:
226: */
227: }
228:
229: /**
230: * Get the session bean to manage database tables.
231: */
232: public DBEnv getDBEnv() {
233: if (dbEnv == null) {
234:
235: useBeans("ftables", false);
236:
237: // Connect to the DBEnvHome object
238: DBEnvHome dbhome = null;
239: try {
240: dbhome = (DBEnvHome) PortableRemoteObject.narrow(ictx
241: .lookup("ftablesDBEnvHome"), DBEnvHome.class);
242: } catch (NamingException e) {
243: System.err.println(">>> " + e);
244: fail("Cannot bind to DBEnvHome");
245: }
246:
247: // Create the table using a session bean
248: try {
249: dbEnv = dbhome.create();
250: } catch (Exception e) {
251: fail(e.toString());
252: }
253: }
254: return dbEnv;
255: }
256:
257: /**
258: * for debugging : Uncomment the line.
259: */
260: public static void debug(String msg) {
261: // System.out.println("DEBUG: " + msg);
262: }
263:
264: /**
265: * Print error message.
266: */
267: public static void error(String msg) {
268: System.err.println("ERROR: " + msg);
269: }
270:
271: /**
272: * sleep n millisec.
273: */
274: public void sleep(int msec) {
275: try {
276: Thread.currentThread().sleep(msec);
277: } catch (InterruptedException e) {
278: fail(e.toString());
279: }
280: }
281:
282: public void testEmpty() throws Exception {
283: }
284: }
|