1 26 27 package org.objectweb.jonas.examples.util; 28 29 import java.io.File ; 30 import java.lang.reflect.Method ; 31 import java.net.URL ; 32 import java.net.URLClassLoader ; 33 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 import javax.rmi.PortableRemoteObject ; 38 39 import junit.framework.TestCase; 40 41 import org.objectweb.jonas.adm.AdmInterface; 42 43 import com.meterware.httpunit.WebConversation; 44 45 51 public class JExampleTestCase extends TestCase { 52 53 56 private static String jonasName = "jonas"; 57 58 61 private static Context ictx = null; 62 63 66 private static AdmInterface admI = null; 67 68 71 protected WebConversation wc = null; 72 73 76 protected String url = null; 77 78 81 private String prefixUrl = null; 82 83 88 protected String getAbsoluteUrl (String url) { 89 return (this.prefixUrl + url); 90 } 91 92 95 private void init() { 96 String port = System.getProperty("http.port"); 97 if (port == null) { 98 port = "9000"; 99 } 100 101 prefixUrl = "http://localhost:" + port; 102 } 103 104 108 public JExampleTestCase(String s) { 109 super(s); 110 init(); 111 } 112 117 public JExampleTestCase(String s, String url) { 118 super(s); 119 wc = new WebConversation(); 120 init(); 121 this.url = getAbsoluteUrl(url); 122 } 123 124 129 private Context getInitialContext() throws NamingException { 130 return new InitialContext (); 131 } 132 133 137 protected void setUp() throws Exception { 138 try { 139 if (ictx == null) { 141 ictx = getInitialContext(); 142 } 143 if (admI == null) { 144 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class); 145 } 146 147 148 } catch (NamingException e) { 149 System.err.println("Cannot setup test: " + e); 150 e.printStackTrace(); 151 } 152 } 153 154 155 160 public void useEar(String filename) throws Exception { 161 162 try { 163 if (ictx == null) { 165 ictx = getInitialContext(); 166 } 167 168 if (admI == null) { 169 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 170 } 171 172 String appsFileName = filename + ".ear"; 174 String autoloadAppsFileName = "autoload" + File.separator + filename + ".ear"; 175 if (!admI.isEarLoaded(appsFileName) && !admI.isEarLoaded(autoloadAppsFileName)) { 176 admI.addEar(appsFileName); 178 } 179 180 } catch (Exception e) { 181 throw new Exception ("Cannot load Ear : " + e.getMessage()); 182 } 183 } 184 185 190 public void useWar(String filename) throws Exception { 191 192 try { 193 if (ictx == null) { 195 ictx = getInitialContext(); 196 } 197 198 if (admI == null) { 199 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 200 } 201 202 String webappsFileName = filename + ".war"; 204 String autoloadWebappsFileName = "autoload" + File.separator + filename + ".war"; 205 if (!admI.isWarLoaded(webappsFileName) && !admI.isWarLoaded(autoloadWebappsFileName)) { 206 admI.addWar(webappsFileName); 208 } 209 210 } catch (Exception e) { 211 throw new Exception ("Cannot load War : " + e.getMessage()); 212 } 213 } 214 215 220 public void useBeans(String filename) throws Exception { 221 try { 222 if (ictx == null) { 224 ictx = getInitialContext(); 225 } 226 if (admI == null) { 227 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 228 } 229 if (!admI.isLoaded(filename + ".jar")) { 230 admI.addBeans(filename + ".jar"); 231 } 232 } catch (Exception e) { 233 throw new Exception ("Cannot load Bean : " + e.getMessage()); 234 } 235 } 236 237 238 243 public void unUseBeans(String filename) throws Exception { 244 try { 245 if (ictx == null) { 247 ictx = getInitialContext(); 248 } 249 if (admI == null) { 250 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 251 } 252 if (admI.isLoaded(filename + ".jar")) { 253 admI.removeBeans(filename + ".jar"); 254 } 255 } catch (Exception e) { 256 throw new Exception ("Cannot unload Bean : " + e.getMessage()); 257 } 258 } 259 260 261 266 protected void callMainMethod(String classToLoad) throws Exception { 267 callMainMethod(classToLoad, new String []{}); 268 } 269 270 271 277 protected void callMainMethod(String classToLoad, String [] args) throws Exception { 278 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 280 URL [] urls = new URL [1]; 281 urls[0] = new File (System.getProperty("jonas.root") + File.separator + "examples" + File.separator + "classes").toURL(); 282 URLClassLoader loader = new URLClassLoader (urls); 283 Thread.currentThread().setContextClassLoader(loader); 284 Class clazz = loader.loadClass(classToLoad); 285 Class [] argList = new Class [] {args.getClass()}; 286 Method meth = clazz.getMethod("main", argList); 287 Object appli = meth.invoke(null, new Object []{args}); 288 } 289 290 291 292 293 294 } 295 | Popular Tags |