KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > util > JExampleTestCase


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 1any 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  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JExampleTestCase.java,v 1.6 2004/12/17 11:05:12 camillej Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.examples.util;
28
29 import java.io.File JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLClassLoader JavaDoc;
33
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.rmi.PortableRemoteObject JavaDoc;
38
39 import junit.framework.TestCase;
40
41 import org.objectweb.jonas.adm.AdmInterface;
42
43 import com.meterware.httpunit.WebConversation;
44
45 /**
46  * Define a class to add useful methods for test the examples
47  * - Deploy ear, war and beans
48  * - Retrieve initial context
49  * @author Florent Benoit
50  */

51 public class JExampleTestCase extends TestCase {
52
53     /**
54      * Name of the JOnAS server used for tests
55      */

56     private static String JavaDoc jonasName = "jonas";
57
58     /**
59      * Initial context used for lookup
60      */

61     private static Context JavaDoc ictx = null;
62
63     /**
64      * JOnAS admin used for communicate via JMX to JOnAS
65      */

66     private static AdmInterface admI = null;
67
68     /**
69      * Conversation used for HttpUnit
70      */

71     protected WebConversation wc = null;
72
73     /**
74      * URL used for the constructor
75      */

76     protected String JavaDoc url = null;
77
78     /**
79      * Prefix for build URLs
80      */

81     private String JavaDoc prefixUrl = null;
82
83     /**
84      * Add to the specified url the prefix
85      * @param url relative URL
86      * @return absolute path of URL
87      */

88     protected String JavaDoc getAbsoluteUrl (String JavaDoc url) {
89         return (this.prefixUrl + url);
90     }
91
92     /**
93      * Initialize the port used by tests and the prefix
94      */

95     private void init() {
96         String JavaDoc port = System.getProperty("http.port");
97         if (port == null) {
98             port = "9000";
99         }
100
101         prefixUrl = "http://localhost:" + port;
102     }
103
104     /**
105      * Constructor with a specified name
106      * @param s the name
107      */

108     public JExampleTestCase(String JavaDoc s) {
109         super(s);
110         init();
111     }
112     /**
113      * Constructor with a specified name and url
114      * @param s the name
115      * @param url the url which can be used
116      */

117     public JExampleTestCase(String JavaDoc s, String JavaDoc url) {
118         super(s);
119         wc = new WebConversation();
120         init();
121         this.url = getAbsoluteUrl(url);
122     }
123
124     /**
125      * Get initialContext
126      * @return the initialContext
127      * @throws NamingException if the initial context can't be retrieved
128      */

129     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
130         return new InitialContext JavaDoc();
131     }
132
133     /**
134      * Common setUp routine, used for every test.
135      * @throws Exception if an error occurs
136      */

137     protected void setUp() throws Exception JavaDoc {
138         try {
139             // get InitialContext
140
if (ictx == null) {
141                 ictx = getInitialContext();
142             }
143             if (admI == null) {
144                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
145             }
146
147             
148         } catch (NamingException JavaDoc e) {
149             System.err.println("Cannot setup test: " + e);
150             e.printStackTrace();
151         }
152     }
153
154
155     /**
156      * Load an ear file in the jonas server
157      * @param filename ear file, without ".ear" extension
158      * @throws Exception if an error occurs
159      */

160     public void useEar(String JavaDoc filename) throws Exception JavaDoc {
161         
162         try {
163             // Load ear in JOnAS if not already loaded.
164
if (ictx == null) {
165                 ictx = getInitialContext();
166             }
167
168             if (admI == null) {
169                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
170             }
171
172             //Test in both directories (apps/ and apps/autoload)
173
String JavaDoc appsFileName = filename + ".ear";
174             String JavaDoc autoloadAppsFileName = "autoload" + File.separator + filename + ".ear";
175             if (!admI.isEarLoaded(appsFileName) && !admI.isEarLoaded(autoloadAppsFileName)) {
176                 //if the file was in autoload, it was loaded
177
admI.addEar(appsFileName);
178             }
179
180         } catch (Exception JavaDoc e) {
181             throw new Exception JavaDoc("Cannot load Ear : " + e.getMessage());
182         }
183     }
184
185     /**
186      * Load a war file in the jonas server
187      * @param filename war file, without ".war" extension
188      * @throws Exception if an error occurs
189      */

190     public void useWar(String JavaDoc filename) throws Exception JavaDoc {
191         
192         try {
193             // Load war in JOnAS if not already loaded.
194
if (ictx == null) {
195                 ictx = getInitialContext();
196             }
197
198             if (admI == null) {
199                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
200             }
201
202             //Test in both directories (apps/ and apps/autoload)
203
String JavaDoc webappsFileName = filename + ".war";
204             String JavaDoc autoloadWebappsFileName = "autoload" + File.separator + filename + ".war";
205             if (!admI.isWarLoaded(webappsFileName) && !admI.isWarLoaded(autoloadWebappsFileName)) {
206                 //if the file was in autoload, it was loaded
207
admI.addWar(webappsFileName);
208             }
209
210         } catch (Exception JavaDoc e) {
211             throw new Exception JavaDoc("Cannot load War : " + e.getMessage());
212         }
213     }
214
215     /**
216      * Load a bean jar file in the jonas server
217      * @param filename jar file, without ".jar" extension
218      * @throws Exception if an error occurs
219      */

220     public void useBeans(String JavaDoc filename) throws Exception JavaDoc {
221         try {
222             // Load bean in EJBServer if not already loaded.
223
if (ictx == null) {
224                 ictx = getInitialContext();
225             }
226             if (admI == null) {
227                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
228             }
229             if (!admI.isLoaded(filename + ".jar")) {
230                 admI.addBeans(filename + ".jar");
231             }
232         } catch (Exception JavaDoc e) {
233             throw new Exception JavaDoc("Cannot load Bean : " + e.getMessage());
234         }
235     }
236
237
238     /**
239      * Unload a bean jar file in the jonas server
240      * @param filename jar file, without ".jar" extension
241      * @throws Exception if an error occurs
242      */

243     public void unUseBeans(String JavaDoc filename) throws Exception JavaDoc {
244         try {
245             // Load bean in EJBServer if not already loaded.
246
if (ictx == null) {
247                 ictx = getInitialContext();
248             }
249             if (admI == null) {
250                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
251             }
252             if (admI.isLoaded(filename + ".jar")) {
253                 admI.removeBeans(filename + ".jar");
254             }
255         } catch (Exception JavaDoc e) {
256             throw new Exception JavaDoc("Cannot unload Bean : " + e.getMessage());
257         }
258     }
259
260
261     /**
262      * Call the main method of a specific class with empty args
263      * @param classToLoad name of class which contains the main method
264      * @throws Exception if it fails
265      */

266     protected void callMainMethod(String JavaDoc classToLoad) throws Exception JavaDoc {
267         callMainMethod(classToLoad, new String JavaDoc[]{});
268     }
269
270
271     /**
272      * Call the main method of a specific class and the specific args
273      * @param classToLoad name of class which contains the main method
274      * @param args args to give to the main method
275      * @throws Exception if it fails
276      */

277     protected void callMainMethod(String JavaDoc classToLoad, String JavaDoc[] args) throws Exception JavaDoc {
278         //Build classloader
279
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
280         URL JavaDoc[] urls = new URL JavaDoc[1];
281         urls[0] = new File JavaDoc(System.getProperty("jonas.root") + File.separator + "examples" + File.separator + "classes").toURL();
282         URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls);
283         Thread.currentThread().setContextClassLoader(loader);
284         Class JavaDoc clazz = loader.loadClass(classToLoad);
285         Class JavaDoc[] argList = new Class JavaDoc[] {args.getClass()};
286         Method JavaDoc meth = clazz.getMethod("main", argList);
287         Object JavaDoc appli = meth.invoke(null, new Object JavaDoc[]{args});
288     }
289
290
291
292
293
294 }
295
Popular Tags