001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 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: Matt Wringe
022: * --------------------------------------------------------------------------
023: * $Id:
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.applications.util;
028:
029: import java.io.File;
030: import java.lang.reflect.Method;
031: import java.net.URL;
032: import java.net.URLClassLoader;
033:
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037: import javax.rmi.PortableRemoteObject;
038:
039: import junit.framework.TestCase;
040:
041: import org.objectweb.jonas.adm.AdmInterface;
042:
043: import com.meterware.httpunit.WebConversation;
044:
045: /**
046: * Define a class to add useful methods for test the examples
047: * - Deploy ear, war and beans
048: * - Retrieve initial context
049: * @author Florent Benoit
050: */
051: public class JApplicationsTestCase extends TestCase {
052:
053: /**
054: * Name of the JOnAS server used for tests
055: */
056: private static String jonasName = "jonas";
057:
058: /**
059: * Initial context used for lookup
060: */
061: private static Context ictx = null;
062:
063: /**
064: * JOnAS admin used for communicate via JMX to JOnAS
065: */
066: private static AdmInterface admI = null;
067:
068: /**
069: * Conversation used for HttpUnit
070: */
071: protected WebConversation wc = null;
072:
073: /**
074: * URL used for the constructor
075: */
076: protected String url = null;
077:
078: /**
079: * Prefix for build URLs
080: */
081: private String prefixUrl = null;
082:
083: /**
084: * Add to the specified url the prefix
085: * @param url relative URL
086: * @return absolute path of URL
087: */
088: protected String getAbsoluteUrl(String url) {
089: return (this .prefixUrl + url);
090: }
091:
092: /**
093: * Initialize the port used by tests and the prefix
094: */
095: private void init() {
096: String port = System.getProperty("http.port");
097: if (port == null) {
098: port = "9000";
099: }
100:
101: prefixUrl = "http://localhost:" + port;
102: }
103:
104: /**
105: * Constructor with a specified name
106: * @param s the name
107: */
108: public JApplicationsTestCase(String s) {
109: super (s);
110: init();
111: }
112:
113: /**
114: * Constructor with a specified name and url
115: * @param s the name
116: * @param url the url which can be used
117: */
118: public JApplicationsTestCase(String s, String url) {
119: super (s);
120: wc = new WebConversation();
121: init();
122: this .url = getAbsoluteUrl(url);
123: }
124:
125: /**
126: * Constructor with a specific name and url
127: * This constructor is to be used when Basic Authentication
128: * is required.
129: *
130: * @param s the name
131: * @param url the url which can be used
132: * @param username the username
133: * @param password the password
134: */
135: public JApplicationsTestCase(String s, String url, String username,
136: String password) {
137: super (s);
138: wc = new WebConversation();
139: wc.setAuthorization(username, password);
140: init();
141: this .url = getAbsoluteUrl(url);
142: }
143:
144: /**
145: * Get initialContext
146: * @return the initialContext
147: * @throws NamingException if the initial context can't be retrieved
148: */
149: private Context getInitialContext() throws NamingException {
150: return new InitialContext();
151: }
152:
153: /**
154: * Common setUp routine, used for every test.
155: * @throws Exception if an error occurs
156: */
157: protected void setUp() throws Exception {
158: try {
159: // get InitialContext
160: if (ictx == null) {
161: ictx = getInitialContext();
162: }
163: if (admI == null) {
164: admI = (AdmInterface) PortableRemoteObject
165: .narrow(ictx.lookup(jonasName + "_Adm"),
166: AdmInterface.class);
167: }
168:
169: } catch (NamingException e) {
170: System.err.println("Cannot setup test: " + e);
171: e.printStackTrace();
172: }
173: }
174:
175: /**
176: * Load an ear file in the jonas server
177: * @param filename ear file, without ".ear" extension
178: * @throws Exception if an error occurs
179: */
180: public void useEar(String filename) throws Exception {
181:
182: try {
183: // Load ear in JOnAS if not already loaded.
184: if (ictx == null) {
185: ictx = getInitialContext();
186: }
187:
188: if (admI == null) {
189: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
190: }
191:
192: //Test in both directories (apps/ and apps/autoload)
193: String appsFileName = filename + ".ear";
194: String autoloadAppsFileName = "autoload" + File.separator
195: + filename + ".ear";
196: if (!admI.isEarLoaded(appsFileName)
197: && !admI.isEarLoaded(autoloadAppsFileName)) {
198: //if the file was in autoload, it was loaded
199: admI.addEar(appsFileName);
200: }
201:
202: } catch (Exception e) {
203: throw new Exception("Cannot load Ear : " + e.getMessage());
204: }
205: }
206:
207: /**
208: * Load a war file in the jonas server
209: * @param filename war file, without ".war" extension
210: * @throws Exception if an error occurs
211: */
212: public void useWar(String filename) throws Exception {
213:
214: try {
215: // Load war in JOnAS if not already loaded.
216: if (ictx == null) {
217: ictx = getInitialContext();
218: }
219:
220: if (admI == null) {
221: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
222: }
223:
224: //Test in both directories (apps/ and apps/autoload)
225: String webappsFileName = filename + ".war";
226: String autoloadWebappsFileName = "autoload"
227: + File.separator + filename + ".war";
228: if (!admI.isWarLoaded(webappsFileName)
229: && !admI.isWarLoaded(autoloadWebappsFileName)) {
230: //if the file was in autoload, it was loaded
231: admI.addWar(webappsFileName);
232: }
233:
234: } catch (Exception e) {
235: throw new Exception("Cannot load War : " + e.getMessage());
236: }
237: }
238:
239: /**
240: * Load a bean jar file in the jonas server
241: * @param filename jar file, without ".jar" extension
242: * @throws Exception if an error occurs
243: */
244: public void useBeans(String filename) throws Exception {
245: try {
246: // Load bean in EJBServer if not already loaded.
247: if (ictx == null) {
248: ictx = getInitialContext();
249: }
250: if (admI == null) {
251: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
252: }
253: if (!admI.isLoaded(filename + ".jar")) {
254: admI.addBeans(filename + ".jar");
255: }
256: } catch (Exception e) {
257: throw new Exception("Cannot load Bean : " + e.getMessage());
258: }
259: }
260:
261: /**
262: * Unload a bean jar file in the jonas server
263: * @param filename jar file, without ".jar" extension
264: * @throws Exception if an error occurs
265: */
266: public void unUseBeans(String filename) throws Exception {
267: try {
268: // Load bean in EJBServer if not already loaded.
269: if (ictx == null) {
270: ictx = getInitialContext();
271: }
272: if (admI == null) {
273: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
274: }
275: if (admI.isLoaded(filename + ".jar")) {
276: admI.removeBeans(filename + ".jar");
277: }
278: } catch (Exception e) {
279: throw new Exception("Cannot unload Bean : "
280: + e.getMessage());
281: }
282: }
283:
284: /**
285: * Call the main method of a specific class with empty args
286: * @param classToLoad name of class which contains the main method
287: * @throws Exception if it fails
288: */
289: protected void callMainMethod(String classToLoad) throws Exception {
290: callMainMethod(classToLoad, new String[] {});
291: }
292:
293: /**
294: * Call the main method of a specific class and the specific args
295: * @param classToLoad name of class which contains the main method
296: * @param args args to give to the main method
297: * @throws Exception if it fails
298: */
299: protected void callMainMethod(String classToLoad, String[] args)
300: throws Exception {
301: //Build classloader
302: ClassLoader cl = Thread.currentThread().getContextClassLoader();
303: URL[] urls = new URL[1];
304: urls[0] = new File(System.getProperty("jonas.root")
305: + File.separator + "examples" + File.separator
306: + "classes").toURL();
307: URLClassLoader loader = new URLClassLoader(urls);
308: Thread.currentThread().setContextClassLoader(loader);
309: Class clazz = loader.loadClass(classToLoad);
310: Class[] argList = new Class[] { args.getClass() };
311: Method meth = clazz.getMethod("main", argList);
312: Object appli = meth.invoke(null, new Object[] { args });
313: }
314:
315: }
|