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: JExampleTestCase.java 7129 2005-07-27 14:42:00Z mwringe $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.examples.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 JExampleTestCase 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 JExampleTestCase(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 JExampleTestCase(String s, String url) {
119: super (s);
120: wc = new WebConversation();
121: init();
122: this .url = getAbsoluteUrl(url);
123: }
124:
125: public JExampleTestCase(String s, String url, String username,
126: String password) {
127: super (s);
128: wc = new WebConversation();
129: wc.setAuthorization(username, password);
130: init();
131: this .url = getAbsoluteUrl(url);
132: }
133:
134: /**
135: * Get initialContext
136: * @return the initialContext
137: * @throws NamingException if the initial context can't be retrieved
138: */
139: private Context getInitialContext() throws NamingException {
140: return new InitialContext();
141: }
142:
143: /**
144: * Common setUp routine, used for every test.
145: * @throws Exception if an error occurs
146: */
147: protected void setUp() throws Exception {
148: try {
149: // get InitialContext
150: if (ictx == null) {
151: ictx = getInitialContext();
152: }
153: if (admI == null) {
154: admI = (AdmInterface) PortableRemoteObject
155: .narrow(ictx.lookup(jonasName + "_Adm"),
156: AdmInterface.class);
157: }
158:
159: } catch (NamingException e) {
160: System.err.println("Cannot setup test: " + e);
161: e.printStackTrace();
162: }
163: }
164:
165: /**
166: * Load an ear file in the jonas server
167: * @param filename ear file, without ".ear" extension
168: * @throws Exception if an error occurs
169: */
170: public void useEar(String filename) throws Exception {
171:
172: try {
173: // Load ear in JOnAS if not already loaded.
174: if (ictx == null) {
175: ictx = getInitialContext();
176: }
177:
178: if (admI == null) {
179: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
180: }
181:
182: //Test in both directories (apps/ and apps/autoload)
183: String appsFileName = filename + ".ear";
184: String autoloadAppsFileName = "autoload" + File.separator
185: + filename + ".ear";
186: if (!admI.isEarLoaded(appsFileName)
187: && !admI.isEarLoaded(autoloadAppsFileName)) {
188: //if the file was in autoload, it was loaded
189: admI.addEar(appsFileName);
190: }
191:
192: } catch (Exception e) {
193: throw new Exception("Cannot load Ear : " + e.getMessage());
194: }
195: }
196:
197: /**
198: * Load a war file in the jonas server
199: * @param filename war file, without ".war" extension
200: * @throws Exception if an error occurs
201: */
202: public void useWar(String filename) throws Exception {
203:
204: try {
205: // Load war in JOnAS if not already loaded.
206: if (ictx == null) {
207: ictx = getInitialContext();
208: }
209:
210: if (admI == null) {
211: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
212: }
213:
214: //Test in both directories (apps/ and apps/autoload)
215: String webappsFileName = filename + ".war";
216: String autoloadWebappsFileName = "autoload"
217: + File.separator + filename + ".war";
218: if (!admI.isWarLoaded(webappsFileName)
219: && !admI.isWarLoaded(autoloadWebappsFileName)) {
220: //if the file was in autoload, it was loaded
221: admI.addWar(webappsFileName);
222: }
223:
224: } catch (Exception e) {
225: throw new Exception("Cannot load War : " + e.getMessage());
226: }
227: }
228:
229: /**
230: * Load a bean jar file in the jonas server
231: * @param filename jar file, without ".jar" extension
232: * @throws Exception if an error occurs
233: */
234: public void useBeans(String filename) throws Exception {
235: try {
236: // Load bean in EJBServer if not already loaded.
237: if (ictx == null) {
238: ictx = getInitialContext();
239: }
240: if (admI == null) {
241: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
242: }
243: if (!admI.isLoaded(filename + ".jar")) {
244: admI.addBeans(filename + ".jar");
245: }
246: } catch (Exception e) {
247: throw new Exception("Cannot load Bean : " + e.getMessage());
248: }
249: }
250:
251: /**
252: * Unload a bean jar file in the jonas server
253: * @param filename jar file, without ".jar" extension
254: * @throws Exception if an error occurs
255: */
256: public void unUseBeans(String filename) throws Exception {
257: try {
258: // Load bean in EJBServer if not already loaded.
259: if (ictx == null) {
260: ictx = getInitialContext();
261: }
262: if (admI == null) {
263: admI = (AdmInterface) ictx.lookup(jonasName + "_Adm");
264: }
265: if (admI.isLoaded(filename + ".jar")) {
266: admI.removeBeans(filename + ".jar");
267: }
268: } catch (Exception e) {
269: throw new Exception("Cannot unload Bean : "
270: + e.getMessage());
271: }
272: }
273:
274: /**
275: * Call the main method of a specific class with empty args
276: * @param classToLoad name of class which contains the main method
277: * @throws Exception if it fails
278: */
279: protected void callMainMethod(String classToLoad) throws Exception {
280: callMainMethod(classToLoad, new String[] {});
281: }
282:
283: /**
284: * Call the main method of a specific class and the specific args
285: * @param classToLoad name of class which contains the main method
286: * @param args args to give to the main method
287: * @throws Exception if it fails
288: */
289: protected void callMainMethod(String classToLoad, String[] args)
290: throws Exception {
291: //Build classloader
292: ClassLoader cl = Thread.currentThread().getContextClassLoader();
293: URL[] urls = new URL[1];
294: urls[0] = new File(System.getProperty("jonas.root")
295: + File.separator + "examples" + File.separator
296: + "classes").toURL();
297: URLClassLoader loader = new URLClassLoader(urls);
298: Thread.currentThread().setContextClassLoader(loader);
299: Class clazz = loader.loadClass(classToLoad);
300: Class[] argList = new Class[] { args.getClass() };
301: Method meth = clazz.getMethod("main", argList);
302: Object appli = meth.invoke(null, new Object[] { args });
303: }
304:
305: }
|