KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > util > JWebServicesTestCase


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: JWebServicesTestCase.java,v 1.6 2004/12/17 11:05:11 camillej Exp $
24  * --------------------------------------------------------------------------
25  */

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

48 public class JWebServicesTestCase extends TestCase {
49
50     /**
51      * Name of the JOnAS server used for tests
52      */

53     private static String JavaDoc jonasName = "jonas";
54
55     /**
56      * Initial context used for lookup
57      */

58     protected static Context JavaDoc ictx = null;
59
60     /**
61      * JOnAS admin used for communicate via JMX to JOnAS
62      */

63     private static AdmInterface admI = null;
64
65     /**
66      * Conversation used for HttpUnit
67      */

68     protected WebConversation wc = null;
69
70     /**
71      * URL used for the constructor
72      */

73     protected String JavaDoc url = null;
74
75     /**
76      * Prefix for build URLs
77      */

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

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

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

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

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

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

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

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

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

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

240     public void unUseBeans(String JavaDoc filename) throws Exception JavaDoc {
241         try {
242             // Load bean in EJBServer if not already loaded.
243
if (ictx == null) {
244                 ictx = getInitialContext();
245             }
246             if (admI == null) {
247                 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class);
248             }
249             if (admI.isLoaded(filename + ".jar")) {
250                 admI.removeBeans(filename + ".jar");
251             }
252         } catch (Exception JavaDoc e) {
253             throw new Exception JavaDoc("Cannot unload Bean : " + e.getMessage());
254         }
255     }
256
257
258
259
260 }
261
Popular Tags