KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > 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.9 2004/12/17 11:05:12 camillej Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.stests.util;
27
28 import java.util.Properties JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34 import junit.framework.TestCase;
35 import org.objectweb.jonas.adm.AdmInterface;
36
37 /**
38  * JTestCase extends TestCase to provide a set of standard routines
39  * used in jonas tests.
40  */

41 public abstract class JTestCase extends TestCase {
42     protected static String JavaDoc jonasName = "jonas"; // change this XXX
43
protected static String JavaDoc testtorun = null;
44     protected static Context JavaDoc ictx = null;
45     public static UserTransaction JavaDoc utx = null; // public besause threads accesses this
46
private static AdmInterface admI = null;
47
48     public JTestCase(String JavaDoc name) {
49         super(name);
50     }
51
52     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
53
54         String JavaDoc registryPort = System.getProperty("jonas.registryport");
55         Context JavaDoc ctx = null;
56
57         if (registryPort != null) {
58             // it means we are using David
59
Properties JavaDoc prop = new Properties JavaDoc();
60             prop.put("java.naming.factory.initial", "com.sun.jndi.rmi.registry.RegistryContextFactory");
61             prop.put("java.naming.provider.url", "rmi://localhost:" + registryPort);
62             ctx = new InitialContext JavaDoc(prop);
63         } else {
64             ctx = new InitialContext JavaDoc();
65         }
66         return ctx;
67     }
68
69     /**
70      * common setUp routine, used for every test.
71      */

72     protected void setUp() throws Exception JavaDoc {
73         // get InitialContext
74
if (ictx == null) {
75             ictx = getInitialContext();
76         }
77
78         // get UserTransaction
79
if (utx == null) {
80             utx = (UserTransaction JavaDoc) PortableRemoteObject.narrow(ictx.lookup("javax.transaction.UserTransaction"), UserTransaction JavaDoc.class);
81         }
82     }
83
84     /**
85      * load a bean jar file in the jonas server
86      */

87     public void useBeans(String JavaDoc filename) {
88
89         try {
90             // Load bean in EJBServer if not already loaded.
91
if (ictx == null) {
92                 ictx = getInitialContext();
93             }
94             if (admI == null) {
95                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
96             }
97             if (! admI.isLoaded(filename + ".jar")) {
98                 admI.addBeans(filename + ".jar");
99             }
100         } catch (Exception JavaDoc e) {
101             System.err.println("Cannot load bean: " + e);
102         }
103     }
104
105     /**
106      * Set the Default Transaction Timeout value on the server.
107      * @param tt timeout in seconds
108      */

109     public static void stopTxAt(int tt) {
110         try {
111             if (admI == null) {
112                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
113             }
114             admI.setTransactionTimeout(tt);
115         } catch (Exception JavaDoc e) {
116             System.err.println("Cannot set transaction timeout: " + e);
117         }
118     }
119
120     /**
121      * for debugging : Uncomment the line.
122      */

123     public static void debug(String JavaDoc msg) {
124         //System.out.println(msg);
125
}
126
127     /**
128      * sleep n millisec.
129      */

130     public void sleep(int msec) {
131         try {
132             Thread.sleep(msec);
133         } catch (InterruptedException JavaDoc e) {
134             fail(e.toString());
135         }
136     }
137
138     public void testEmpty() throws Exception JavaDoc {
139     }
140 }
141
Popular Tags