1 25 26 package org.objectweb.jonas.jonasadmin.test.util; 27 28 import java.io.File ; 29 import java.lang.reflect.Method ; 30 import java.net.URL ; 31 import java.net.URLClassLoader ; 32 import java.text.SimpleDateFormat ; 33 import java.util.Calendar ; 34 import java.util.Date ; 35 import java.util.Properties ; 36 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import javax.rmi.PortableRemoteObject ; 41 42 import junit.framework.TestCase; 43 44 import org.objectweb.jonas.adm.AdmInterface; 45 46 import com.meterware.httpunit.HttpUnitOptions; 47 import com.meterware.httpunit.WebConversation; 48 import com.meterware.httpunit.WebForm; 49 import com.meterware.httpunit.WebResponse; 50 51 57 public class JonasAdminTestCase extends TestCase { 58 59 62 protected static final String URL_JONASADMIN = "/jonasAdmin/"; 63 64 67 protected static final String FRAME_CONTENT = "content"; 68 69 72 protected static final String FRAME_TREE = "tree"; 73 74 77 protected static final String FRAME_TOP = "top"; 78 79 82 protected static final String URL_JONASADMIN_DEPLOYEAR = "/jonasAdmin/EditDeploy.do?type=ear"; 83 84 87 protected static final String URL_JONASADMIN_DEPLOYJAR = "/jonasAdmin/EditDeploy.do?type=jar"; 88 89 92 private static final String JONASADMINTEST_PROPERTY_NAME = "jonasAdminTests.properties"; 93 94 97 private static String jonasName = "jonas"; 98 99 102 private static Context ictx = null; 103 104 107 private static AdmInterface admI = null; 108 109 112 protected WebConversation wc = null; 113 114 117 protected String url = null; 118 119 122 protected String urlWelcome = null; 123 124 127 protected String urlLogOut = null; 128 129 130 133 protected String prefixUrl = null; 134 135 138 protected JProperties jProp = null; 139 140 143 protected Properties configFile; 144 145 148 protected String port; 149 150 155 protected String getAbsoluteUrl (String url) { 156 return (this.prefixUrl + url); 157 } 158 159 162 private void init() { 163 port = System.getProperty("http.port"); 164 if (port == null) { 165 port = "9000"; 166 } 167 168 prefixUrl = "http://localhost:" + port; 169 170 urlWelcome = prefixUrl + "/jonasAdmin/Welcome.do"; 171 urlLogOut = prefixUrl + "/jonasAdmin/logOut.do"; 172 173 jProp = new JProperties(); 174 175 try { 176 configFile = jProp.getProperties(JONASADMINTEST_PROPERTY_NAME); 177 } catch (Exception e) { 178 System.err.println("Cannot find file : " + JONASADMINTEST_PROPERTY_NAME + ". " + e); 179 e.printStackTrace(); 180 } 181 } 182 183 187 public JonasAdminTestCase(String s) { 188 super(s); 189 init(); 190 } 191 192 197 public JonasAdminTestCase(String s, String url) { 198 super(s); 199 wc = new WebConversation(); 200 init(); 201 this.url = getAbsoluteUrl(url); 202 } 203 209 public JonasAdminTestCase(WebConversation wc, String s,String url) { 210 super(s); 211 this.wc = wc; 212 init(); 213 this.url = getAbsoluteUrl(url); 214 } 215 216 221 private Context getInitialContext() throws NamingException { 222 return new InitialContext (); 223 } 224 225 229 protected void setUp() throws Exception { 230 try { 231 if (ictx == null) { 233 ictx = getInitialContext(); 234 } 235 if (admI == null) { 236 admI = (AdmInterface) PortableRemoteObject.narrow(ictx.lookup(jonasName + "_Adm"), AdmInterface.class); 237 } 238 239 } catch (NamingException e) { 240 System.err.println("Cannot setup test: " + e); 241 e.printStackTrace(); 242 } 243 } 244 245 246 251 public void useEar(String filename) throws Exception { 252 253 try { 254 if (ictx == null) { 256 ictx = getInitialContext(); 257 } 258 259 if (admI == null) { 260 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 261 } 262 263 String appsFileName = filename + ".ear"; 265 String autoloadAppsFileName = "autoload" + File.separator + filename + ".ear"; 266 if (!admI.isEarLoaded(appsFileName) && !admI.isEarLoaded(autoloadAppsFileName)) { 267 admI.addEar(appsFileName); 269 } 270 271 } catch (Exception e) { 272 throw new Exception ("Cannot load Ear : " + e.getMessage()); 273 } 274 } 275 276 277 282 public void unUseEar(String filename) throws Exception { 283 try { 284 if (ictx == null) { 286 ictx = getInitialContext(); 287 } 288 if (admI == null) { 289 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 290 } 291 if (admI.isEarLoaded(filename + ".ear")) { 292 admI.removeEar(filename + ".ear"); 293 } 294 } catch (Exception e) { 295 throw new Exception ("Cannot unload Bean : " + e.getMessage()); 296 } 297 } 298 299 304 public void useWar(String filename) throws Exception { 305 306 try { 307 if (ictx == null) { 309 ictx = getInitialContext(); 310 } 311 312 if (admI == null) { 313 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 314 } 315 316 String webappsFileName = filename + ".war"; 318 String autoloadWebappsFileName = "autoload" + File.separator + filename + ".war"; 319 if (!admI.isWarLoaded(webappsFileName) && !admI.isWarLoaded(autoloadWebappsFileName)) { 320 admI.addWar(webappsFileName); 322 } 323 324 } catch (Exception e) { 325 throw new Exception ("Cannot load War : " + e.getMessage()); 326 } 327 } 328 329 334 public void useBeans(String filename) throws Exception { 335 try { 336 if (ictx == null) { 338 ictx = getInitialContext(); 339 } 340 if (admI == null) { 341 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 342 } 343 if (!admI.isLoaded(filename + ".jar")) { 344 admI.addBeans(filename + ".jar"); 345 } 346 } catch (Exception e) { 347 throw new Exception ("Cannot load Bean : " + e.getMessage()); 348 } 349 } 350 351 352 357 public void unUseBeans(String filename) throws Exception { 358 try { 359 if (ictx == null) { 361 ictx = getInitialContext(); 362 } 363 if (admI == null) { 364 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 365 } 366 if (admI.isLoaded(filename + ".jar")) { 367 admI.removeBeans(filename + ".jar"); 368 } 369 } catch (Exception e) { 370 throw new Exception ("Cannot unload Bean : " + e.getMessage()); 371 } 372 } 373 374 375 380 public void useRar(String filename) throws Exception { 381 382 try { 383 if (ictx == null) { 385 ictx = getInitialContext(); 386 } 387 388 if (admI == null) { 389 admI = (AdmInterface) ictx.lookup(jonasName + "_Adm"); 390 } 391 392 String raFileName = filename + ".rar"; 394 String autoloadRaFileName = "autoload" + File.separator + filename + ".rar"; 395 if (!admI.isEarLoaded(raFileName) && !admI.isEarLoaded(autoloadRaFileName)) { 396 admI.addRar(raFileName); 398 } 399 400 } catch (Exception e) { 401 throw new Exception ("Cannot load Rar : " + e.getMessage()); 402 } 403 } 404 405 406 411 protected void callMainMethod(String classToLoad) throws Exception { 412 callMainMethod(classToLoad, new String []{}); 413 } 414 415 416 422 protected void callMainMethod(String classToLoad, String [] args) throws Exception { 423 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 425 URL [] urls = new URL [1]; 426 urls[0] = new File (System.getProperty("jonas.root") + File.separator + "examples" + File.separator + "classes").toURL(); 427 URLClassLoader loader = new URLClassLoader (urls); 428 Thread.currentThread().setContextClassLoader(loader); 429 Class clazz = loader.loadClass(classToLoad); 430 Class [] argList = new Class [] {args.getClass()}; 431 Method meth = clazz.getMethod("main", argList); 432 Object appli = meth.invoke(null, new Object []{args}); 433 } 434 435 439 protected String getTime() { 440 Calendar cal = Calendar.getInstance(); 441 Date date = cal.getTime(); 442 SimpleDateFormat formatter; 443 formatter = new SimpleDateFormat ("yyyy-MM-dd.HH-mm-ss"); 444 return formatter.format(date); 445 } 446 447 452 protected void undeployEar(String name) throws Exception { 453 String fileName = name + ".ear"; 454 455 HttpUnitOptions.setExceptionsThrownOnScriptError(false); 457 HttpUnitOptions.setExceptionsThrownOnErrorStatus(false); 459 460 WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYEAR)); 461 WebForm[] webForms = wr.getForms(); 462 WebForm webForm = webForms[0]; 463 464 String params = webForm.getParameterValue("undeploy"); 465 WebForm.Scriptable script = webForm.getScriptableObject(); 466 467 HttpUnitOptions.setExceptionsThrownOnScriptError(false); 469 470 if (params.length() == 0) { 471 params += fileName; 472 } else { 473 params += "," + fileName; 474 } 475 476 script.setParameterValue("undeploy", params); 477 478 WebResponse submitUndeploy = webForm.submit(); 479 480 webForms = submitUndeploy.getForms(); 481 webForm = webForms[0]; 482 483 WebResponse endResp = webForm.submit(); 484 } 485 486 490 protected void undeployAllEar() throws Exception { 491 WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYEAR)); 492 undeployAll(wr); 493 } 494 495 499 protected void undeployAllJar() throws Exception { 500 WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYJAR)); 501 undeployAll(wr); 502 } 503 504 508 protected void undeployAll(WebResponse wr) throws Exception { 509 510 WebForm[] webForms = wr.getForms(); 511 WebForm webForm = webForms[0]; 512 513 HttpUnitOptions.setExceptionsThrownOnScriptError(false); 515 HttpUnitOptions.setExceptionsThrownOnErrorStatus(false); 517 518 String params = webForm.getParameterValue("deploy"); 519 WebForm.Scriptable script = webForm.getScriptableObject(); 520 521 if (params.length() == 0) { 522 params += webForm.getParameterValue("undeploy"); 523 } else { 524 params += "," + webForm.getParameterValue("undeploy"); 525 } 526 527 script.setParameterValue("undeploy", params); 528 529 WebResponse submitUndeploy = webForm.submit(); 530 531 webForms = submitUndeploy.getForms(); 532 webForm = webForms[0]; 533 534 WebResponse endResp = webForm.submit(); 535 536 wc.getResponse(getAbsoluteUrl(URL_JONASADMIN)); 537 } 538 539 543 public void debug(String msg) { 544 System.out.println("DEBUG: " + msg); 545 } 546 } 547 | Popular Tags |