1 19 package org.netbeans.modules.j2ee.jboss4.test; 20 21 import java.io.File ; 22 import java.net.URLClassLoader ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import javax.enterprise.deploy.shared.ModuleType ; 27 import javax.management.ObjectInstance ; 28 import javax.management.ObjectName ; 29 import javax.management.QueryExp ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.junit.NbTestCase; 34 import org.netbeans.junit.NbTestSuite; 35 import org.netbeans.junit.ide.ProjectSupport; 36 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants; 37 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 38 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance; 39 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 40 import org.netbeans.modules.j2ee.deployment.impl.ui.ProgressUI; 41 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 42 import org.netbeans.modules.j2ee.jboss4.JBDeploymentFactory; 43 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBInstantiatingIterator; 44 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties; 45 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginUtils; 46 import org.netbeans.spi.project.ActionProvider; 47 import org.openide.WizardDescriptor; 48 import org.openide.WizardDescriptor.Panel; 49 import org.openide.util.RequestProcessor; 50 import org.openide.util.RequestProcessor.Task; 51 52 56 public class JBoss4TestSuite extends NbTestCase { 57 58 private static final String DISPLAY_NAME = "JBoss Application Server"; 59 private static final String EJB_PROJECT_NAME = "JBoss4EjbTest"; 60 private static final String WEB_PROJECT_NAME = "JBoss4WebTest"; 61 private static final String EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME; 62 private static final String WEB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + WEB_PROJECT_NAME; 63 64 private static String URL = null; 65 66 public JBoss4TestSuite(String testName) { 67 super(testName); 68 } 69 70 73 public static NbTestSuite suite() { 74 NbTestSuite suite = new NbTestSuite("JBoss4TestSuite"); 75 suite.addTest(new JBoss4TestSuite("addJBossDefaultInstance")); 76 suite.addTest(new JBoss4TestSuite("startServer")); 77 suite.addTest(new JBoss4TestSuite("restartServer")); 78 suite.addTest(new JBoss4TestSuite("stopServer")); 79 suite.addTest(new JBoss4TestSuite("startDebugServer")); 80 suite.addTest(new JBoss4TestSuite("restartServer")); 81 suite.addTest(new JBoss4TestSuite("stopServer"));; 82 suite.addTest(new JBoss4TestSuite("deployWebModule"));; 83 suite.addTest(new JBoss4TestSuite("stopServer"));; 84 suite.addTest(new JBoss4TestSuite("deployEjbModule"));; 85 suite.addTest(new JBoss4TestSuite("stopServer"));; 86 suite.addTest(new JBoss4TestSuite("removeJBossInstance")); 87 return suite; 88 } 89 90 public void addJBossInstance(String domain) { 91 try { 92 final String SEP = System.getProperty("file.separator"); 93 String installLocation = System.getProperty("jboss.server.path"); 94 String host = "localhost"; 95 String port = "8080"; 96 String server = domain; 97 String serverPath = installLocation+SEP+"server"+SEP+domain; 98 String displayName = DISPLAY_NAME; 99 100 URL = "jboss-deployer:"+host+":"+port+"#"+domain+"&"+installLocation; 101 102 JBInstantiatingIterator inst = new JBInstantiatingIterator(); 103 WizardDescriptor wizard = new WizardDescriptor(new Panel[] {inst.current()}); 104 wizard.putProperty(org.netbeans.modules.j2ee.deployment.impl.ui.wizard.AddServerInstanceWizard.PROP_DISPLAY_NAME, displayName); 105 JBPluginProperties.getInstance().setInstallLocation(installLocation); 106 107 inst.setInstallLocation(installLocation); 108 inst.setHost(host); 109 inst.setPort(port); 110 inst.setServer(server); 111 inst.setServerPath(serverPath); 112 inst.setDeployDir(JBPluginUtils.getDeployDir(serverPath)); 113 inst.initialize(wizard); 114 inst.instantiate(); 115 116 ServerRegistry.getInstance().checkInstanceExists(URL); 117 118 sleep(); 119 } catch(Exception e) { 120 fail(e.getMessage()); 121 } 122 } 123 124 public void addJBossDefaultInstance() { 125 addJBossInstance("default"); 126 } 127 128 public void removeJBossInstance() { 129 try { 130 sleep(); 131 132 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(URL); 133 inst.remove(); 134 135 try { 136 ServerRegistry.getInstance().checkInstanceExists(URL); 137 } catch(Exception e) { 138 return; 139 } 140 141 fail("JBoss instance still exists !"); 142 } catch(Exception e) { 143 fail(e.getMessage()); 144 } 145 } 146 147 public void startServer() { 148 try { 149 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(URL); 150 151 if(inst.isRunning()) 152 return; 153 154 ProgressUI pui = new ProgressUI("Start JBoss", true); 155 inst.start(pui); 156 157 sleep(); 158 159 if(!inst.isRunning()) 160 throw new Exception ("JBoss server start failed"); 161 162 sleep(); 163 } catch(Exception e) { 164 fail(e.getMessage()); 165 } 166 } 167 168 public void startDebugServer() { 169 try { 170 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(URL); 171 172 if(inst.isRunning()) 173 return; 174 175 ProgressUI ui = new ProgressUI(DISPLAY_NAME, true); 176 inst.startDebug(ui); 177 178 sleep(); 179 180 if(!inst.isRunning()) 181 throw new Exception ("JBoss4 server start debug failed"); 182 183 sleep(); 184 } catch(Exception e) { 185 fail(e.getMessage()); 186 } 187 } 188 189 public void stopServer() { 190 try { 191 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(URL); 192 193 if(!inst.isRunning()) 194 return; 195 196 ProgressUI pui = new ProgressUI("Stop JBoss", true); 197 inst.stop(pui); 198 199 sleep(); 200 201 if(inst.isRunning()) 202 throw new Exception ("JBoss server stop failed"); 203 204 sleep(); 205 } catch(Exception e) { 206 fail(e.getMessage()); 207 } 208 } 209 210 public void restartServer() { 211 try { 212 ServerInstance inst = ServerRegistry.getInstance().getServerInstance(URL); 213 214 if(!inst.isRunning()) 215 return; 216 217 ProgressUI ui = new ProgressUI(DISPLAY_NAME, true); 218 inst.restart(ui); 219 220 sleep(); 221 222 if(!inst.isRunning()) 223 throw new Exception ("JBoss4 server stop failed"); 224 225 sleep(); 226 } catch(Exception e) { 227 fail(e.getMessage()); 228 } 229 } 230 231 public void deployWebModule() { 232 try { 233 Project project = (Project)openProject(new File (WEB_PROJECT_PATH)); 234 ActionProvider ap = (ActionProvider)project.getLookup().lookup(ActionProvider.class); 235 J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 236 final ServerInstance si = ServerRegistry.getInstance().getServerInstance(jmp.getServerInstanceID()); 237 238 Runnable startCondition = new Runnable () { 239 public void run() { 240 while(!si.isConnected()) { 241 try { 242 Thread.sleep(5000); 243 } catch(Exception e) {} 244 } 245 } 246 }; 247 248 Runnable deployCondition = new Runnable () { 249 public void run() { 250 while(!isProjectDeployed(ModuleType.WAR, WEB_PROJECT_NAME, si.getInstanceProperties())) { 251 try { 252 Thread.sleep(5000); 253 } catch(Exception e) {} 254 } 255 } 256 }; 257 258 Task t = RequestProcessor.getDefault().create(startCondition); 259 ap.invokeAction(EjbProjectConstants.COMMAND_REDEPLOY, project.getLookup()); 260 t.run(); 261 if(!t.waitFinished(300000)) 262 throw new Exception ("Server start timeout"); 263 264 t = RequestProcessor.getDefault().create(deployCondition); 265 t.run(); 266 if(!t.waitFinished(300000)) 267 throw new Exception ("WEB Application deploy timeout"); 268 269 closeProject(WEB_PROJECT_NAME); 270 271 sleep(); 272 } catch(Exception e) { 273 fail(e.getMessage()); 274 } 275 } 276 277 public void deployEjbModule() { 278 try { 279 Project project = (Project)openProject(new File (EJB_PROJECT_PATH)); 280 ActionProvider ap = (ActionProvider)project.getLookup().lookup(ActionProvider.class); 281 J2eeModuleProvider jmp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 282 final ServerInstance si = ServerRegistry.getInstance().getServerInstance(jmp.getServerInstanceID()); 283 284 Runnable startCondition = new Runnable () { 285 public void run() { 286 while(!si.isConnected()) { 287 try { 288 Thread.sleep(5000); 289 } catch(Exception e) {} 290 } 291 } 292 }; 293 294 Runnable deployCondition = new Runnable () { 295 public void run() { 296 while(!isProjectDeployed(ModuleType.WAR, WEB_PROJECT_NAME, si.getInstanceProperties())) { 297 try { 298 Thread.sleep(5000); 299 } catch(Exception e) {} 300 } 301 } 302 }; 303 304 Task t = RequestProcessor.getDefault().create(startCondition); 305 ap.invokeAction(EjbProjectConstants.COMMAND_REDEPLOY, project.getLookup()); 306 t.run(); 307 if(!t.waitFinished(300000)) 308 throw new Exception ("Server start timeout"); 309 310 t = RequestProcessor.getDefault().create(deployCondition); 311 t.run(); 312 if(!t.waitFinished(300000)) 313 throw new Exception ("EJB Application deploy timeout"); 314 315 closeProject(EJB_PROJECT_NAME); 316 317 sleep(); 318 } catch(Exception e) { 319 fail(e.getMessage()); 320 } 321 } 322 323 public void sleep() { 324 try { 325 Thread.sleep(10000); 326 } catch(Exception e) { 327 fail(e.getMessage()); 328 } 329 } 330 331 private Object openProject(File projectDir) { 332 return ProjectSupport.openProject(projectDir); 333 } 334 335 private void closeProject(String projectName) { 336 ProjectSupport.closeProject(projectName); 337 } 338 339 private boolean isProjectDeployed(ModuleType mt, String name, InstanceProperties ip) { 340 try { 341 URLClassLoader loader = JBDeploymentFactory.getJBClassLoader(ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR)); 342 Thread.currentThread().setContextClassLoader(loader); 343 344 Hashtable env = new Hashtable (); 345 346 env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 347 env.put(Context.PROVIDER_URL, "jnp://localhost:"+JBPluginUtils.getJnpPort(ip.getProperty(JBPluginProperties.PROPERTY_SERVER_DIR))); 348 env.put(Context.OBJECT_FACTORIES, "org.jboss.naming"); 349 env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" ); 350 env.put("jnp.disableDiscovery", Boolean.TRUE); 351 352 InitialContext ctx = new InitialContext (env); 353 Object server = ctx.lookup("/jmx/invoker/RMIAdaptor"); 354 355 ObjectName searchPattern = null; 356 357 if(mt.equals(ModuleType.WAR)) { 358 name += ".war"; 359 searchPattern = new ObjectName ("jboss.management.local:j2eeType=WebModule,*"); 360 } else if (mt.equals(ModuleType.EJB)) { 361 name += ".jar"; 362 searchPattern = new ObjectName ("jboss.management.local:j2eeType=EJBModule,*"); 363 } 364 365 Set managedObj = (Set )server.getClass().getMethod("queryMBeans", new Class [] {ObjectName .class, QueryExp .class}).invoke(server, new Object [] {searchPattern, null}); 366 367 for (Iterator it = managedObj.iterator(); it.hasNext();) { 368 ObjectName elem = ((ObjectInstance ) it.next()).getObjectName(); 369 if(elem.getKeyProperty("name").equals(name)) 370 return true; 371 } 372 } catch(Exception e) { 373 return false; 374 } 375 376 return false; 377 } 378 } | Popular Tags |