KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > test > JBoss4TestSuite


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.jboss4.test;
20
21 import java.io.File JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
27 import javax.management.ObjectInstance JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.QueryExp JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
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 /**
53  *
54  * @author Michal Mocnak
55  */

56 public class JBoss4TestSuite extends NbTestCase {
57     
58     private static final String JavaDoc DISPLAY_NAME = "JBoss Application Server";
59     private static final String JavaDoc EJB_PROJECT_NAME = "JBoss4EjbTest";
60     private static final String JavaDoc WEB_PROJECT_NAME = "JBoss4WebTest";
61     private static final String JavaDoc EJB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + EJB_PROJECT_NAME;
62     private static final String JavaDoc WEB_PROJECT_PATH = System.getProperty("xtest.tmpdir") + File.separator + WEB_PROJECT_NAME;
63     
64     private static String JavaDoc URL = null;
65     
66     public JBoss4TestSuite(String JavaDoc testName) {
67         super(testName);
68     }
69     
70     /**
71      * suite method automatically generated by JUnit module
72      */

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 JavaDoc domain) {
91         try {
92             final String JavaDoc SEP = System.getProperty("file.separator");
93             String JavaDoc installLocation = System.getProperty("jboss.server.path");
94             String JavaDoc host = "localhost";
95             String JavaDoc port = "8080";
96             String JavaDoc server = domain;
97             String JavaDoc serverPath = installLocation+SEP+"server"+SEP+domain;
98             String JavaDoc 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 JavaDoc 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 JavaDoc e) {
138                 return;
139             }
140             
141             fail("JBoss instance still exists !");
142         } catch(Exception JavaDoc 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 JavaDoc("JBoss server start failed");
161             
162             sleep();
163         } catch(Exception JavaDoc 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 JavaDoc("JBoss4 server start debug failed");
182             
183             sleep();
184         } catch(Exception JavaDoc 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 JavaDoc("JBoss server stop failed");
203             
204             sleep();
205         } catch(Exception JavaDoc 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 JavaDoc("JBoss4 server stop failed");
224             
225             sleep();
226         } catch(Exception JavaDoc e) {
227             fail(e.getMessage());
228         }
229     }
230     
231     public void deployWebModule() {
232         try {
233             Project project = (Project)openProject(new File JavaDoc(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 JavaDoc startCondition = new Runnable JavaDoc() {
239                 public void run() {
240                     while(!si.isConnected()) {
241                         try {
242                             Thread.sleep(5000);
243                         } catch(Exception JavaDoc e) {}
244                     }
245                 }
246             };
247             
248             Runnable JavaDoc deployCondition = new Runnable JavaDoc() {
249                 public void run() {
250                     while(!isProjectDeployed(ModuleType.WAR, WEB_PROJECT_NAME, si.getInstanceProperties())) {
251                         try {
252                             Thread.sleep(5000);
253                         } catch(Exception JavaDoc 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 JavaDoc("Server start timeout");
263             
264             t = RequestProcessor.getDefault().create(deployCondition);
265             t.run();
266             if(!t.waitFinished(300000))
267                 throw new Exception JavaDoc("WEB Application deploy timeout");
268             
269             closeProject(WEB_PROJECT_NAME);
270             
271             sleep();
272         } catch(Exception JavaDoc e) {
273             fail(e.getMessage());
274         }
275     }
276     
277     public void deployEjbModule() {
278         try {
279             Project project = (Project)openProject(new File JavaDoc(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 JavaDoc startCondition = new Runnable JavaDoc() {
285                 public void run() {
286                     while(!si.isConnected()) {
287                         try {
288                             Thread.sleep(5000);
289                         } catch(Exception JavaDoc e) {}
290                     }
291                 }
292             };
293             
294             Runnable JavaDoc deployCondition = new Runnable JavaDoc() {
295                 public void run() {
296                     while(!isProjectDeployed(ModuleType.WAR, WEB_PROJECT_NAME, si.getInstanceProperties())) {
297                         try {
298                             Thread.sleep(5000);
299                         } catch(Exception JavaDoc 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 JavaDoc("Server start timeout");
309             
310             t = RequestProcessor.getDefault().create(deployCondition);
311             t.run();
312             if(!t.waitFinished(300000))
313                 throw new Exception JavaDoc("EJB Application deploy timeout");
314             
315             closeProject(EJB_PROJECT_NAME);
316             
317             sleep();
318         } catch(Exception JavaDoc e) {
319             fail(e.getMessage());
320         }
321     }
322     
323     public void sleep() {
324         try {
325             Thread.sleep(10000);
326         } catch(Exception JavaDoc e) {
327             fail(e.getMessage());
328         }
329     }
330     
331     private Object JavaDoc openProject(File JavaDoc projectDir) {
332         return ProjectSupport.openProject(projectDir);
333     }
334     
335     private void closeProject(String JavaDoc projectName) {
336         ProjectSupport.closeProject(projectName);
337     }
338     
339     private boolean isProjectDeployed(ModuleType JavaDoc mt, String JavaDoc name, InstanceProperties ip) {
340         try {
341             URLClassLoader JavaDoc loader = JBDeploymentFactory.getJBClassLoader(ip.getProperty(JBPluginProperties.PROPERTY_ROOT_DIR));
342             Thread.currentThread().setContextClassLoader(loader);
343             
344             Hashtable JavaDoc env = new Hashtable JavaDoc();
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 JavaDoc ctx = new InitialContext JavaDoc(env);
353             Object JavaDoc server = ctx.lookup("/jmx/invoker/RMIAdaptor");
354             
355             ObjectName JavaDoc searchPattern = null;
356             
357             if(mt.equals(ModuleType.WAR)) {
358                 name += ".war";
359                 searchPattern = new ObjectName JavaDoc("jboss.management.local:j2eeType=WebModule,*");
360             } else if (mt.equals(ModuleType.EJB)) {
361                 name += ".jar";
362                 searchPattern = new ObjectName JavaDoc("jboss.management.local:j2eeType=EJBModule,*");
363             }
364             
365             Set JavaDoc managedObj = (Set JavaDoc)server.getClass().getMethod("queryMBeans", new Class JavaDoc[] {ObjectName JavaDoc.class, QueryExp JavaDoc.class}).invoke(server, new Object JavaDoc[] {searchPattern, null});
366             
367             for (Iterator JavaDoc it = managedObj.iterator(); it.hasNext();) {
368                 ObjectName JavaDoc elem = ((ObjectInstance JavaDoc) it.next()).getObjectName();
369                 if(elem.getKeyProperty("name").equals(name))
370                     return true;
371             }
372         } catch(Exception JavaDoc e) {
373             return false;
374         }
375         
376         return false;
377     }
378 }
Popular Tags