KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > util > JonasAdminTestCase


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 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  * --------------------------------------------------------------------------
22  * $Id: JonasAdminTestCase.java,v 1.9 2005/07/25 12:29:58 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

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

57 public class JonasAdminTestCase extends TestCase {
58
59     /**
60      * URL of the login page
61      */

62     protected static final String JavaDoc URL_JONASADMIN = "/jonasAdmin/";
63
64     /**
65      * NAME of FRAME content
66      */

67     protected static final String JavaDoc FRAME_CONTENT = "content";
68
69     /**
70      * NAME of FRAME tree
71      */

72     protected static final String JavaDoc FRAME_TREE = "tree";
73
74     /**
75      * NAME of FRAME top
76      */

77     protected static final String JavaDoc FRAME_TOP = "top";
78
79     /**
80      * URL of the deployment of EARs
81      */

82     protected static final String JavaDoc URL_JONASADMIN_DEPLOYEAR = "/jonasAdmin/EditDeploy.do?type=ear";
83
84     /**
85      * URL of the deployment of JARs
86      */

87     protected static final String JavaDoc URL_JONASADMIN_DEPLOYJAR = "/jonasAdmin/EditDeploy.do?type=jar";
88
89     /**
90      * Name of the property file of jonasAdmin tests
91      */

92     private static final String JavaDoc JONASADMINTEST_PROPERTY_NAME = "jonasAdminTests.properties";
93
94     /**
95      * Name of the JOnAS server used for tests
96      */

97     private static String JavaDoc jonasName = "jonas";
98
99     /**
100      * Initial context used for lookup
101      */

102     private static Context JavaDoc ictx = null;
103
104     /**
105      * JOnAS admin used for communicate via JMX to JOnAS
106      */

107     private static AdmInterface admI = null;
108
109     /**
110      * Conversation used for HttpUnit
111      */

112     protected WebConversation wc = null;
113
114     /**
115      * URL used for the constructor
116      */

117     protected String JavaDoc url = null;
118
119     /**
120      * URL used to return to Welcome.jsp
121      */

122     protected String JavaDoc urlWelcome = null;
123
124     /**
125      * URL used to log out
126      */

127     protected String JavaDoc urlLogOut = null;
128
129
130     /**
131      * Prefix for build URLs
132      */

133     protected String JavaDoc prefixUrl = null;
134
135     /**
136      * JOnAS and jonasAdmin properties
137      */

138     protected JProperties jProp = null;
139
140     /**
141      * jonasAdmin test properties
142      */

143     protected Properties JavaDoc configFile;
144
145     /**
146      * jonasAdmin port
147      */

148     protected String JavaDoc port;
149
150     /**
151      * Add to the specified url the prefix
152      * @param url relative URL
153      * @return absolute path of URL
154      */

155     protected String JavaDoc getAbsoluteUrl (String JavaDoc url) {
156         return (this.prefixUrl + url);
157     }
158
159     /**
160      * Initialize the port used by tests and the prefix
161      */

162     private void init() {
163         port = System.getProperty("http.port");
164         if (port == null) {
165             port = "9000";
166         }
167
168         prefixUrl = "http://localhost:" + port;
169
170         urlWelcome = prefixUrl + "/jonasAdmin/Welcome.do";
171         urlLogOut = prefixUrl + "/jonasAdmin/logOut.do";
172
173         jProp = new JProperties();
174
175         try {
176             configFile = jProp.getProperties(JONASADMINTEST_PROPERTY_NAME);
177         } catch (Exception JavaDoc e) {
178             System.err.println("Cannot find file : " + JONASADMINTEST_PROPERTY_NAME + ". " + e);
179             e.printStackTrace();
180         }
181     }
182
183     /**
184      * Constructor with a specified name
185      * @param s the name
186      */

187     public JonasAdminTestCase(String JavaDoc s) {
188         super(s);
189         init();
190     }
191
192     /**
193      * Constructor with a specified name and url
194      * @param s the name
195      * @param url the url which can be used
196      */

197     public JonasAdminTestCase(String JavaDoc s, String JavaDoc url) {
198         super(s);
199         wc = new WebConversation();
200         init();
201         this.url = getAbsoluteUrl(url);
202     }
203     /**
204      * Constructor with a specified name and url
205      * @param wc the WebConversation
206      * @param s the name
207      * @param url the url which can be used
208      */

209     public JonasAdminTestCase(WebConversation wc, String JavaDoc s,String JavaDoc url) {
210         super(s);
211         this.wc = wc;
212         init();
213         this.url = getAbsoluteUrl(url);
214     }
215
216     /**
217      * Get initialContext
218      * @return the initialContext
219      * @throws NamingException if the initial context can't be retrieved
220      */

221     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
222         return new InitialContext JavaDoc();
223     }
224
225     /**
226      * Common setUp routine, used for every test.
227      * @throws Exception if an error occurs
228      */

229     protected void setUp() throws Exception JavaDoc {
230         try {
231             // get InitialContext
232
if (ictx == null) {
233                 ictx = getInitialContext();
234             }
235             if (admI == null) {
236                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
237             }
238
239         } catch (NamingException JavaDoc e) {
240             System.err.println("Cannot setup test: " + e);
241             e.printStackTrace();
242         }
243     }
244
245
246     /**
247      * Load an ear file in the jonas server
248      * @param filename ear file, without ".ear" extension
249      * @throws Exception if an error occurs
250      */

251     public void useEar(String JavaDoc filename) throws Exception JavaDoc {
252
253         try {
254             // Load ear in JOnAS if not already loaded.
255
if (ictx == null) {
256                 ictx = getInitialContext();
257             }
258
259             if (admI == null) {
260                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
261             }
262
263             //Test in both directories (apps/ and apps/autoload)
264
String JavaDoc appsFileName = filename + ".ear";
265             String JavaDoc autoloadAppsFileName = "autoload" + File.separator + filename + ".ear";
266             if (!admI.isEarLoaded(appsFileName) && !admI.isEarLoaded(autoloadAppsFileName)) {
267                 //if the file was in autoload, it was loaded
268
admI.addEar(appsFileName);
269             }
270
271         } catch (Exception JavaDoc e) {
272             throw new Exception JavaDoc("Cannot load Ear : " + e.getMessage());
273         }
274     }
275
276
277     /**
278      * Unload a ear file in the jonas server
279      * @param filename ear file, without ".ear" extension
280      * @throws Exception if an error occurs
281      */

282     public void unUseEar(String JavaDoc filename) throws Exception JavaDoc {
283         try {
284             // Unload ear in EARServer if not already loaded.
285
if (ictx == null) {
286                 ictx = getInitialContext();
287             }
288             if (admI == null) {
289                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
290             }
291             if (admI.isEarLoaded(filename + ".ear")) {
292                 admI.removeEar(filename + ".ear");
293             }
294         } catch (Exception JavaDoc e) {
295             throw new Exception JavaDoc("Cannot unload Bean : " + e.getMessage());
296         }
297     }
298
299     /**
300      * Load a war file in the jonas server
301      * @param filename war file, without ".war" extension
302      * @throws Exception if an error occurs
303      */

304     public void useWar(String JavaDoc filename) throws Exception JavaDoc {
305
306         try {
307             // Load war in JOnAS if not already loaded.
308
if (ictx == null) {
309                 ictx = getInitialContext();
310             }
311
312             if (admI == null) {
313                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
314             }
315
316             //Test in both directories (apps/ and apps/autoload)
317
String JavaDoc webappsFileName = filename + ".war";
318             String JavaDoc autoloadWebappsFileName = "autoload" + File.separator + filename + ".war";
319             if (!admI.isWarLoaded(webappsFileName) && !admI.isWarLoaded(autoloadWebappsFileName)) {
320                 //if the file was in autoload, it was loaded
321
admI.addWar(webappsFileName);
322             }
323
324         } catch (Exception JavaDoc e) {
325             throw new Exception JavaDoc("Cannot load War : " + e.getMessage());
326         }
327     }
328
329     /**
330      * Load a bean jar file in the jonas server
331      * @param filename jar file, without ".jar" extension
332      * @throws Exception if an error occurs
333      */

334     public void useBeans(String JavaDoc filename) throws Exception JavaDoc {
335         try {
336             // Load bean in EJBServer if not already loaded.
337
if (ictx == null) {
338                 ictx = getInitialContext();
339             }
340             if (admI == null) {
341                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
342             }
343             if (!admI.isLoaded(filename + ".jar")) {
344                 admI.addBeans(filename + ".jar");
345             }
346         } catch (Exception JavaDoc e) {
347             throw new Exception JavaDoc("Cannot load Bean : " + e.getMessage());
348         }
349     }
350
351
352     /**
353      * Unload a bean jar file in the jonas server
354      * @param filename jar file, without ".jar" extension
355      * @throws Exception if an error occurs
356      */

357     public void unUseBeans(String JavaDoc filename) throws Exception JavaDoc {
358         try {
359             // Load bean in EJBServer if not already loaded.
360
if (ictx == null) {
361                 ictx = getInitialContext();
362             }
363             if (admI == null) {
364                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
365             }
366             if (admI.isLoaded(filename + ".jar")) {
367                 admI.removeBeans(filename + ".jar");
368             }
369         } catch (Exception JavaDoc e) {
370             throw new Exception JavaDoc("Cannot unload Bean : " + e.getMessage());
371         }
372     }
373
374
375     /**
376      * Load a rar file in the jonas server
377      * @param filename rar file, without ".rar" extension
378      * @throws Exception if an error occurs
379      */

380     public void useRar(String JavaDoc filename) throws Exception JavaDoc {
381
382         try {
383             // Load rar in JOnAS if not already loaded.
384
if (ictx == null) {
385                 ictx = getInitialContext();
386             }
387
388             if (admI == null) {
389                 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
390             }
391
392             //Test in both directories (rar/ and rar/autoload)
393
String JavaDoc raFileName = filename + ".rar";
394             String JavaDoc autoloadRaFileName = "autoload" + File.separator + filename + ".rar";
395             if (!admI.isEarLoaded(raFileName) && !admI.isEarLoaded(autoloadRaFileName)) {
396                 //if the file was in autoload, it was loaded
397
admI.addRar(raFileName);
398             }
399
400         } catch (Exception JavaDoc e) {
401             throw new Exception JavaDoc("Cannot load Rar : " + e.getMessage());
402         }
403     }
404
405
406     /**
407      * Call the main method of a specific class with empty args
408      * @param classToLoad name of class which contains the main method
409      * @throws Exception if it fails
410      */

411     protected void callMainMethod(String JavaDoc classToLoad) throws Exception JavaDoc {
412         callMainMethod(classToLoad, new String JavaDoc[]{});
413     }
414
415
416     /**
417      * Call the main method of a specific class and the specific args
418      * @param classToLoad name of class which contains the main method
419      * @param args args to give to the main method
420      * @throws Exception if it fails
421      */

422     protected void callMainMethod(String JavaDoc classToLoad, String JavaDoc[] args) throws Exception JavaDoc {
423         //Build classloader
424
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
425         URL JavaDoc[] urls = new URL JavaDoc[1];
426         urls[0] = new File JavaDoc(System.getProperty("jonas.root") + File.separator + "examples" + File.separator + "classes").toURL();
427         URLClassLoader JavaDoc loader = new URLClassLoader JavaDoc(urls);
428         Thread.currentThread().setContextClassLoader(loader);
429         Class JavaDoc clazz = loader.loadClass(classToLoad);
430         Class JavaDoc[] argList = new Class JavaDoc[] {args.getClass()};
431         Method JavaDoc meth = clazz.getMethod("main", argList);
432         Object JavaDoc appli = meth.invoke(null, new Object JavaDoc[]{args});
433     }
434
435     /**
436      * Get current time
437      * @return A String Time with the format yyyy-MM-dd.HH-mm-ss
438      */

439     protected String JavaDoc getTime() {
440         Calendar JavaDoc cal = Calendar.getInstance();
441         Date JavaDoc date = cal.getTime();
442         SimpleDateFormat JavaDoc formatter;
443         formatter = new SimpleDateFormat JavaDoc("yyyy-MM-dd.HH-mm-ss");
444         return formatter.format(date);
445     }
446
447     /**
448      * Undeploy an EAR
449      * @param name String of EAR Name
450      * @throws Exception if error occurs
451      */

452     protected void undeployEar(String JavaDoc name) throws Exception JavaDoc {
453         String JavaDoc fileName = name + ".ear";
454
455         // Disable errors of javascript
456
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
457         // Disable exception thrown on error status
458
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
459
460         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYEAR));
461         WebForm[] webForms = wr.getForms();
462         WebForm webForm = webForms[0];
463
464         String JavaDoc params = webForm.getParameterValue("undeploy");
465         WebForm.Scriptable script = webForm.getScriptableObject();
466
467         // Disable errors of javascript
468
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
469
470         if (params.length() == 0) {
471             params += fileName;
472         } else {
473             params += "," + fileName;
474         }
475
476         script.setParameterValue("undeploy", params);
477
478         WebResponse submitUndeploy = webForm.submit();
479
480         webForms = submitUndeploy.getForms();
481         webForm = webForms[0];
482
483         WebResponse endResp = webForm.submit();
484     }
485
486     /**
487      * Undeploy all EAR
488      * @throws Exception if error occurs
489      */

490     protected void undeployAllEar() throws Exception JavaDoc {
491         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYEAR));
492         undeployAll(wr);
493     }
494
495     /**
496      * Undeploy all Jars
497      * @throws Exception if error occurs
498      */

499     protected void undeployAllJar() throws Exception JavaDoc {
500         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYJAR));
501         undeployAll(wr);
502     }
503
504     /**
505      * Undeploy all
506      * @throws Exception if error occurs
507      */

508     protected void undeployAll(WebResponse wr) throws Exception JavaDoc {
509
510         WebForm[] webForms = wr.getForms();
511         WebForm webForm = webForms[0];
512
513         // Disable errors of javascript
514
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
515         // Disable exception thrown on error status
516
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
517
518         String JavaDoc params = webForm.getParameterValue("deploy");
519         WebForm.Scriptable script = webForm.getScriptableObject();
520
521         if (params.length() == 0) {
522             params += webForm.getParameterValue("undeploy");
523         } else {
524             params += "," + webForm.getParameterValue("undeploy");
525         }
526
527         script.setParameterValue("undeploy", params);
528
529         WebResponse submitUndeploy = webForm.submit();
530
531         webForms = submitUndeploy.getForms();
532         webForm = webForms[0];
533
534         WebResponse endResp = webForm.submit();
535
536         wc.getResponse(getAbsoluteUrl(URL_JONASADMIN));
537     }
538
539     /**
540      * Print a debug message
541      * @param msg The message
542      */

543     public void debug(String JavaDoc msg) {
544         System.out.println("DEBUG: " + msg);
545     }
546 }
547
Popular Tags