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: JTestCase.java 5987 2004-12-17 11:05:12Z camillej $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.stests.util;
027:
028: import java.util.Properties;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.rmi.PortableRemoteObject;
033: import javax.transaction.UserTransaction;
034: import junit.framework.TestCase;
035: import org.objectweb.jonas.adm.AdmInterface;
036:
037: /**
038: * JTestCase extends TestCase to provide a set of standard routines
039: * used in jonas tests.
040: */
041: public abstract class JTestCase extends TestCase {
042: protected static String jonasName = "jonas"; // change this XXX
043: protected static String testtorun = null;
044: protected static Context ictx = null;
045: public static UserTransaction utx = null; // public besause threads accesses this
046: private static AdmInterface admI = null;
047:
048: public JTestCase(String name) {
049: super (name);
050: }
051:
052: private Context getInitialContext() throws NamingException {
053:
054: String registryPort = System.getProperty("jonas.registryport");
055: Context ctx = null;
056:
057: if (registryPort != null) {
058: // it means we are using David
059: Properties prop = new Properties();
060: prop.put("java.naming.factory.initial",
061: "com.sun.jndi.rmi.registry.RegistryContextFactory");
062: prop.put("java.naming.provider.url", "rmi://localhost:"
063: + registryPort);
064: ctx = new InitialContext(prop);
065: } else {
066: ctx = new InitialContext();
067: }
068: return ctx;
069: }
070:
071: /**
072: * common setUp routine, used for every test.
073: */
074: protected void setUp() throws Exception {
075: // get InitialContext
076: if (ictx == null) {
077: ictx = getInitialContext();
078: }
079:
080: // get UserTransaction
081: if (utx == null) {
082: utx = (UserTransaction) PortableRemoteObject.narrow(ictx
083: .lookup("javax.transaction.UserTransaction"),
084: UserTransaction.class);
085: }
086: }
087:
088: /**
089: * load a bean jar file in the jonas server
090: */
091: public void useBeans(String filename) {
092:
093: try {
094: // Load bean in EJBServer if not already loaded.
095: if (ictx == null) {
096: ictx = getInitialContext();
097: }
098: if (admI == null) {
099: admI = (AdmInterface) PortableRemoteObject
100: .narrow(ictx.lookup(jonasName + "_Adm"),
101: AdmInterface.class);
102: }
103: if (!admI.isLoaded(filename + ".jar")) {
104: admI.addBeans(filename + ".jar");
105: }
106: } catch (Exception e) {
107: System.err.println("Cannot load bean: " + e);
108: }
109: }
110:
111: /**
112: * Set the Default Transaction Timeout value on the server.
113: * @param tt timeout in seconds
114: */
115: public static void stopTxAt(int tt) {
116: try {
117: if (admI == null) {
118: admI = (AdmInterface) PortableRemoteObject
119: .narrow(ictx.lookup(jonasName + "_Adm"),
120: AdmInterface.class);
121: }
122: admI.setTransactionTimeout(tt);
123: } catch (Exception e) {
124: System.err.println("Cannot set transaction timeout: " + e);
125: }
126: }
127:
128: /**
129: * for debugging : Uncomment the line.
130: */
131: public static void debug(String msg) {
132: //System.out.println(msg);
133: }
134:
135: /**
136: * sleep n millisec.
137: */
138: public void sleep(int msec) {
139: try {
140: Thread.sleep(msec);
141: } catch (InterruptedException e) {
142: fail(e.toString());
143: }
144: }
145:
146: public void testEmpty() throws Exception {
147: }
148: }
|