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 1any 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: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JWebServicesTestCase.java 7516 2005-10-18 07:09:43Z sauthieg $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.jtests.util;
026:
027: import java.io.File;
028:
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.rmi.PortableRemoteObject;
033:
034: import junit.framework.TestCase;
035:
036: import org.objectweb.jonas.adm.AdmInterface;
037:
038: import com.meterware.httpunit.WebConversation;
039:
040: /**
041: * Define a class to add useful methods for test the webservices
042: * - Deploy ear, war and beans
043: * - Retrieve initial context
044: * @author Florent Benoit
045: */
046: public class JWebServicesTestCase extends TestCase {
047:
048: /**
049: * Name of the JOnAS server used for tests
050: */
051: private static String jonasName = "jonas";
052:
053: /**
054: * Initial context used for lookup
055: */
056: protected static Context ictx = null;
057:
058: /**
059: * JOnAS admin used for communicate via JMX to JOnAS
060: */
061: private static AdmInterface admI = null;
062:
063: /**
064: * Conversation used for HttpUnit
065: */
066: protected WebConversation wc = null;
067:
068: /**
069: * URL used for the constructor
070: */
071: protected String url = null;
072:
073: /**
074: * Prefix for build URLs
075: */
076: private String prefixUrl = null;
077:
078: /**
079: * Add to the specified url the prefix
080: * @param url relative URL
081: * @return absolute path of URL
082: */
083: protected String getAbsoluteUrl(String url) {
084: return (this .prefixUrl + url);
085: }
086:
087: /**
088: * Initialize the port used by tests and the prefix
089: */
090: private void init() {
091: String port = System.getProperty("http.port");
092: if (port == null) {
093: port = "9000";
094: }
095:
096: prefixUrl = "http://localhost:" + port;
097: }
098:
099: /**
100: * Constructor with a specified name
101: * @param s the name
102: */
103: public JWebServicesTestCase(String s) {
104: super (s);
105: init();
106: }
107:
108: /**
109: * Constructor with a specified name and url
110: * @param s the name
111: * @param url the url which can be used
112: */
113: public JWebServicesTestCase(String s, String url) {
114: super (s);
115: wc = new WebConversation();
116: init();
117: this .url = getAbsoluteUrl(url);
118: }
119:
120: /**
121: * Get initialContext
122: * @return the initialContext
123: * @throws NamingException if the initial context can't be retrieved
124: */
125: private Context getInitialContext() throws NamingException {
126: return new InitialContext();
127: }
128:
129: /**
130: * Common setUp routine, used for every test.
131: * @throws Exception if an error occurs
132: */
133: protected void setUp() throws Exception {
134: try {
135: // get InitialContext
136: if (ictx == null) {
137: ictx = getInitialContext();
138: }
139: if (admI == null) {
140: admI = (AdmInterface) PortableRemoteObject
141: .narrow(ictx.lookup(jonasName + "_Adm"),
142: AdmInterface.class);
143: }
144:
145: } catch (NamingException e) {
146: System.err.println("Cannot setup test: " + e);
147: e.printStackTrace();
148: }
149: }
150:
151: /**
152: * Load an ear file in the jonas server
153: * @param filename ear file, without ".ear" extension
154: * @throws Exception if an error occurs
155: */
156: public void useEar(String filename) throws Exception {
157:
158: try {
159: // Load ear in JOnAS if not already loaded.
160: if (ictx == null) {
161: ictx = getInitialContext();
162: }
163:
164: if (admI == null) {
165: admI = (AdmInterface) PortableRemoteObject
166: .narrow(ictx.lookup(jonasName + "_Adm"),
167: AdmInterface.class);
168: }
169:
170: //Test in both directories (apps/ and apps/autoload)
171: String appsFileName = filename + ".ear";
172: String autoloadAppsFileName = "autoload" + File.separator
173: + filename + ".ear";
174: if (!admI.isEarLoaded(appsFileName)
175: && !admI.isEarLoaded(autoloadAppsFileName)) {
176: //if the file was in autoload, it was loaded
177: admI.addEar(appsFileName);
178: }
179:
180: } catch (Exception e) {
181: throw new Exception("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 filename) throws Exception {
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) PortableRemoteObject
200: .narrow(ictx.lookup(jonasName + "_Adm"),
201: AdmInterface.class);
202: }
203:
204: //Test in both directories (apps/ and apps/autoload)
205: String webappsFileName = filename + ".war";
206: String autoloadWebappsFileName = "autoload"
207: + File.separator + filename + ".war";
208: if (!admI.isWarLoaded(webappsFileName)
209: && !admI.isWarLoaded(autoloadWebappsFileName)) {
210: //if the file was in autoload, it was loaded
211: admI.addWar(webappsFileName);
212: }
213:
214: } catch (Exception e) {
215: throw new Exception("Cannot load War : " + e.getMessage());
216: }
217: }
218:
219: /**
220: * Load a bean jar file in the jonas server
221: * @param filename jar file, without ".jar" extension
222: * @throws Exception if an error occurs
223: */
224: public void useBeans(String filename) throws Exception {
225: try {
226: // Load bean in EJBServer if not already loaded.
227: if (ictx == null) {
228: ictx = getInitialContext();
229: }
230: if (admI == null) {
231: admI = (AdmInterface) PortableRemoteObject
232: .narrow(ictx.lookup(jonasName + "_Adm"),
233: AdmInterface.class);
234: }
235: if (!admI.isLoaded(filename + ".jar")) {
236: admI.addBeans(filename + ".jar");
237: }
238: } catch (Exception e) {
239: throw new Exception("Cannot load Bean : " + e.getMessage());
240: }
241: }
242:
243: /**
244: * Unload a bean jar file in the jonas server
245: * @param filename jar file, without ".jar" extension
246: * @throws Exception if an error occurs
247: */
248: public void unUseBeans(String filename) throws Exception {
249: try {
250: // Load bean in EJBServer if not already loaded.
251: if (ictx == null) {
252: ictx = getInitialContext();
253: }
254: if (admI == null) {
255: admI = (AdmInterface) PortableRemoteObject
256: .narrow(ictx.lookup(jonasName + "_Adm"),
257: AdmInterface.class);
258: }
259: if (admI.isLoaded(filename + ".jar")) {
260: admI.removeBeans(filename + ".jar");
261: }
262: } catch (Exception e) {
263: throw new Exception("Cannot unload Bean : ", e);
264: }
265: }
266:
267: }
|