KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > jonasserver > F_JonasAdminInfoServer


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: F_JonasAdminInfoServer.java,v 1.5 2005/07/12 13:20:01 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jonasadmin.test.jonasserver;
27
28 import java.io.IOException JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30
31 import junit.framework.TestSuite;
32
33 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
34 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
35 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminUtils;
36 import org.objectweb.jonas_lib.version.Version;
37 import org.xml.sax.SAXException JavaDoc;
38
39 import com.meterware.httpunit.HttpUnitOptions;
40 import com.meterware.httpunit.WebConversation;
41 import com.meterware.httpunit.WebLink;
42 import com.meterware.httpunit.WebResponse;
43 import com.meterware.httpunit.WebTable;
44
45 /**
46  * Class for testing server infos in jonasAdmin
47  * @author kemlerp
48  *
49  */

50 public class F_JonasAdminInfoServer extends JonasAdminTestCase {
51
52     /**
53      * URL of the Server Management
54      */

55     private static final String JavaDoc URL_JONASADMIN_SERVER = "EditJonasServer.do";
56
57     /**
58      * URL of the JMX SERVER
59      */

60     private static final String JavaDoc URL_JONASADMIN_JMX_SERVER = "EditJmxServer.do";
61
62     /**
63      * URL of the server registry
64      */

65     private static final String JavaDoc URL_JONASADMIN_SERVER_REGISTRY = "ListRegistry.do";
66
67     /**
68      * URL of the SERVLET SERVER
69      */

70     private static final String JavaDoc URL_JONASADMIN_SERVLET_SERVER = "EditServletServer.do";
71
72     /**
73      * URL of the JVM
74      */

75     private static final String JavaDoc URL_JONASADMIN_JVM = "EditJvm.do";
76
77     /**
78      * URL of owner mbeans
79      */

80     private static final String JavaDoc URL_JONASADMIN_OWNER = "/jonasAdmin/ListOwnerMBeans.do";
81
82     /**
83      * RMI PROTOCOL
84      */

85     private static final String JavaDoc RMI_PROTOCOL = "rmi/";
86
87     /**
88      * Value of select parameter for JVM
89      */

90     private static final String JavaDoc SELECT_JVM_MBEAN = "jonas%3Aj2eeType%3DJVM%2Cname%3Djonas%2CJ2EEServer%3Djonas";
91
92     /**
93      * Value of select parameter for MBEAN Server
94      */

95     private static final String JavaDoc SELECT_MBEAN_SERVER_MBEAN = "JMImplementation%3Atype%3DMBeanServerDelegate";
96
97     /**
98      * Value of select parameter for Servlet Server
99      */

100     private static final String JavaDoc SELECT_SERVLET_SERVER_MBEAN = "jonas%3Atype%3Dservice%2Cname%3DwebContainers";
101
102     /**
103      * Value of select parameter for Catalina Server
104      */

105     private static final String JavaDoc SELECT_CATALINA_SERVER = "Catalina%3Atype%3DServer";
106
107     /**
108      * URL of the MBean attributs
109      */

110     private static final String JavaDoc URL_MBEAN_ATTRIBUTS = "/jonasAdmin/ListMBeanAttributes.do?select=";
111
112     /**
113      * Balise HTML
114      */

115     private static final String JavaDoc END_OF_COLUMN = "</td>";
116
117     /**
118      * A 2nd connection to jonasAdmin
119      */

120     private static WebConversation wc2 = new WebConversation();
121
122     /**
123      * Constructor with a specified name
124      * @param s name
125      */

126     public F_JonasAdminInfoServer(String JavaDoc s) {
127         super(s, URL_JONASADMIN);
128     }
129
130     /**
131      * Constructor with a specified name
132      * @param wc the WebConversation of the suite test
133      * @param s name
134      */

135     public F_JonasAdminInfoServer(WebConversation wc, String JavaDoc s) {
136         super(wc, s, URL_JONASADMIN);
137     }
138
139     /**
140      * Main method
141      * @param args the arguments
142      */

143     public static void main(String JavaDoc[] args) {
144
145         String JavaDoc testtorun = null;
146         // Get args
147
for (int argn = 0; argn < args.length; argn++) {
148             String JavaDoc sArg = args[argn];
149             if (sArg.equals("-n")) {
150                 testtorun = args[++argn];
151             }
152         }
153         if (testtorun == null) {
154             junit.textui.TestRunner.run(suite());
155         } else {
156             junit.textui.TestRunner.run(new F_JonasAdminInfoServer(testtorun));
157         }
158     }
159
160     /**
161      * Get a new TestSuite for this class
162      * @return a new TestSuite for this class
163      */

164     public static TestSuite suite() {
165         return new TestSuite(F_JonasAdminInfoServer.class);
166     }
167
168     /**
169      * Get a new TestSuite for this class
170      * @param wc the WebConversation
171      * @return a new TestSuite for this class with the WebConversation instance
172      */

173     public static TestSuite suite(WebConversation wc) {
174         TestSuite suite = new TestSuite();
175         suite.addTest(new F_JonasAdminInfoServer(wc, "testInfos"));
176         return suite;
177     }
178
179     /**
180      * Setup need for these tests
181      * jonasAdmin is required
182      * @throws Exception if it fails
183      */

184     protected void setUp() throws Exception JavaDoc {
185         super.setUp();
186
187         unUseEar("earsample");
188
189         if (wc.getCurrentPage().getURL() == null) {
190             useWar("jonasAdmin");
191             // login to jonas admin
192
try {
193                 JonasAdminAuth.doValidAuth(wc, url);
194             } catch (Exception JavaDoc e) {
195                 fail("authentification failed : " + e);
196             }
197         } else {
198             // if there was an error, the connection must be restablished
199
try {
200                 wc.getFrameContents(FRAME_TREE);
201             } catch (Exception JavaDoc e) {
202                 wc.getResponse(urlLogOut);
203                 // login to jonas admin
204
try {
205                     JonasAdminAuth.doValidAuth(wc, url);
206                 } catch (Exception JavaDoc auth) {
207                     fail("authentification failed : " + auth);
208                 }
209             }
210         }
211     }
212
213     /**
214      * Test JOnAS server infos
215      * @throws Exception if error occurs
216      *
217      */

218     public void testInfos() throws Exception JavaDoc {
219
220         WebResponse wr;
221         WebLink link;
222         WebTable table;
223         JonasAdminUtils utils = new JonasAdminUtils();
224
225         // Disable errors of javascript
226
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
227         // Disable exception thrown on error status
228
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
229
230         String JavaDoc jonasName = null;
231         String JavaDoc jonasVersion = Version.NUMBER;
232         String JavaDoc jonasProtocol = RMI_PROTOCOL + jProp.getRegistryProtocol();
233
234         String JavaDoc mbeanId = null;
235         String JavaDoc mbeanSpecification = null;
236         String JavaDoc mbeanSpecVersion = null;
237         String JavaDoc mbeanSpecVendor = null;
238         String JavaDoc mbeanImplementation = null;
239         String JavaDoc mbeanImplVersion = null;
240         String JavaDoc mbeanImplVendor = null;
241
242         String JavaDoc registryProtocol = jProp.getRegistryProtocol();
243         String JavaDoc registryUrl = jProp.getRegistryUrl();
244         int jndiNb = 0;
245
246         String JavaDoc servletName = null;
247         String JavaDoc servletVersion = null;
248         String JavaDoc servletService = null;
249         String JavaDoc servletEngine = null;
250         String JavaDoc servletHost = null;
251
252         String JavaDoc jvmVersion = jProp.getJvmVersion();
253         String JavaDoc jvmVendor = jProp.getJvmVendor();
254         String JavaDoc jvmNode = null;
255
256
257         // OPEN A SECOND CONNECTION
258
// login to jonas admin
259
try {
260             JonasAdminAuth.doValidAuth(wc2, url);
261         } catch (Exception JavaDoc e) {
262             fail("authentification failed : " + e);
263         }
264
265         // TEST JONAS SERVER
266
// Go to jonas server management
267
wr = wc.getFrameContents(FRAME_TREE);
268         link = wr.getFirstMatchingLink( WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVER);
269         link.click();
270         wr = wc.getFrameContents(FRAME_CONTENT);
271
272         // Get jonas server name
273
String JavaDoc text;
274         text = wr.getText();
275         int beginIndex = text.indexOf("Server JOnAS ( ");
276         int endIndex = text.indexOf(" )", beginIndex);
277         jonasName = text.substring(beginIndex + "Server JOnAS ( ".length(), endIndex);
278
279         try {
280             // Get table
281
table = utils.getTable(wr, 0);
282
283             // Verify
284
assertEquals("It is not the jonas name. ", jonasName, table.getTableCell(0, 2).getText());
285             assertEquals("It is not the jonas version. ", jonasVersion, table.getTableCell(1, 2).getText());
286             assertEquals("It is not the jonas protocol. ", jonasProtocol, table.getTableCell(2, 2).getText());
287         } catch (SAXException JavaDoc e) {
288             fail("The jonas table is incorrect.");
289         }
290
291
292         // TEST JMX SERVER
293
link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_JMX_SERVER);
294         link.click();
295         wr = wc.getFrameContents(FRAME_CONTENT);
296
297         mbeanId = getMBeanAttribut(SELECT_MBEAN_SERVER_MBEAN, "MBeanServerId");
298         mbeanSpecification = getMBeanAttribut(SELECT_MBEAN_SERVER_MBEAN, "SpecificationName");
299         mbeanSpecVersion = getMBeanAttribut(SELECT_MBEAN_SERVER_MBEAN, "SpecificationVersion");
300         mbeanSpecVendor = getMBeanAttribut(SELECT_MBEAN_SERVER_MBEAN, "SpecificationVendor");
301         mbeanImplementation = getMBeanAttribut(SELECT_MBEAN_SERVER_MBEAN, "ImplementationName");
302         mbeanImplVersion = getMBeanAttribut(SELECT_MBEAN_SERVER_MBEAN, "ImplementationVersion");
303         mbeanImplVendor = getMBeanAttribut(SELECT_MBEAN_SERVER_MBEAN, "ImplementationVendor");
304
305         try {
306             // Get table
307
table = utils.getTable(wr, 0);
308
309             // Verify
310
assertEquals("It is not the mbean server id. ", mbeanId, table.getTableCell(0, 2).getText());
311             assertEquals("It is not the mbean server specification name. ", mbeanSpecification, table.getTableCell(1, 2).getText());
312             assertEquals("It is not the mbean server specification version. ", mbeanSpecVersion, table.getTableCell(2, 2).getText());
313             assertEquals("It is not the mbean server specification vendor. ", mbeanSpecVendor, table.getTableCell(3, 2).getText());
314             assertEquals("It is not the mbean server implementation name. ", mbeanImplementation, table.getTableCell(4, 2).getText());
315             assertEquals("It is not the mbean server implementation version. ", mbeanImplVersion, table.getTableCell(5, 2).getText());
316             assertEquals("It is not the mbean server implementation vendor. ", mbeanImplVendor, table.getTableCell(6, 2).getText());
317         } catch (SAXException JavaDoc e) {
318             fail("The jmx server table is incorrect.");
319         }
320
321
322         // TEST REGISTRY INFOS
323
link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVER_REGISTRY);
324         link.click();
325         wr = wc.getFrameContents(FRAME_CONTENT);
326
327         try {
328             // Get table
329
table = utils.getTable(wr, 0);
330
331             // Verify
332
assertEquals("It is not the registry protocol. ", registryProtocol, table.getTableCell(1, 0).getText());
333             assertEquals("It is not the registry url. ", registryUrl, table.getTableCell(1, 1).getText());
334         } catch (SAXException JavaDoc e) {
335             fail("The registry table is incorrect.");
336         }
337
338         try {
339             //Get table
340
table = utils.getTable(wr, 3);
341             int rowNb = table.getRowCount();
342             jndiNb = getJndiNb(wr);
343             text = table.getText();
344
345             // Verify
346
if (jndiNb > 0) {
347                 assertEquals("The number of jndi names is not " + jndiNb, rowNb, jndiNb);
348             }
349             assertFalse("'EarOpHome' is found. ", text.indexOf("EarOpHome") > -1);
350             assertFalse("'EarOpHome_L' is found. ", text.indexOf("EarOpHome_L") > -1);
351             assertFalse("'eisName' is found. ", text.indexOf("eisName") > -1);
352         } catch (SAXException JavaDoc e) {
353             fail("The jndi name table is incorrect.");
354         }
355
356         // Deploy earsample.ear
357
useEar("earsample");
358
359         // Refresh
360
link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_JVM);
361         link.click();
362         wr = wc.getFrameContents(FRAME_CONTENT);
363         link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVER_REGISTRY);
364         link.click();
365         wr = wc.getFrameContents(FRAME_CONTENT);
366
367         try {
368             //Get table
369
table = utils.getTable(wr, 3);
370             int jndiNb2 = getJndiNb(wr);
371             text = table.getText();
372
373             // Verify
374
assertEquals("The number of JNDI names has not been updated: ", jndiNb + 3, jndiNb2);
375             assertTrue("'EarOpHome' is not found. ", text.indexOf("EarOpHome") > -1);
376             assertTrue("'EarOpHome_L' is not found. ", text.indexOf("EarOpHome_L") > -1);
377             assertTrue("'eisName' is not found. ", text.indexOf("eisName") > -1);
378         } catch (SAXException JavaDoc e) {
379             fail("The jndi name table is incorrect.");
380         }
381
382
383         // TEST SERVLET SERVER
384
link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_SERVLET_SERVER);
385         link.click();
386         wr = wc.getFrameContents(FRAME_CONTENT);
387
388         servletName = getMBeanAttribut(SELECT_SERVLET_SERVER_MBEAN, "ServerName");
389         servletVersion = getMBeanAttribut(SELECT_SERVLET_SERVER_MBEAN, "ServerVersion");
390         // if CATALINA
391
if (jProp.isCatalina()) {
392             String JavaDoc selectService = getMBeanAttribut(SELECT_CATALINA_SERVER, "serviceNames");
393             if (selectService.indexOf("[ ") == 0) {
394                 if (selectService.indexOf(" ]") == selectService.length()-2) {
395                     selectService = selectService.substring(2, selectService.length()-2);
396                 } else {
397                     selectService = selectService.substring(2);
398                 }
399             }
400             servletService = getMBeanAttribut(selectService, "name");
401             String JavaDoc selectContainer = getMBeanAttribut(selectService, "containerName");
402             servletEngine = getMBeanAttribut(selectContainer, "name");
403             servletHost = getMBeanAttribut(selectContainer, "defaultHost");
404         }
405
406         try {
407             // Get table
408
table = utils.getTable(wr, 0);
409
410             // Verify
411
assertEquals("It is not the servlet server name. ", servletName, table.getTableCell(0, 2).getText());
412             assertEquals("It is not the servlet server version. ", servletVersion, table.getTableCell(1, 2).getText());
413             if (jProp.isCatalina()) {
414                 assertEquals("It is not the servlet server service. ", servletService, table.getTableCell(2, 2).getText());
415                 assertEquals("It is not the servlet server engine. ", servletEngine, table.getTableCell(3, 2).getText());
416                 assertEquals("It is not the servlet server host. ", servletHost, table.getTableCell(4, 2).getText());
417             }
418         } catch (SAXException JavaDoc e) {
419             fail("The jmx server table is incorrect.");
420         }
421
422
423         // TEST JVM INFOS
424
link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_JVM);
425         link.click();
426         wr = wc.getFrameContents(FRAME_CONTENT);
427
428         jvmNode = getMBeanAttribut(SELECT_JVM_MBEAN, "node");
429         try {
430             // Get table
431
table = utils.getTable(wr, 0);
432
433             // Verify
434
assertEquals("It is not the jvm version. ", jvmVersion, table.getTableCell(0, 2).getText());
435             assertEquals("It is not the jvm vendor. ", jvmVendor, table.getTableCell(1, 2).getText());
436             assertEquals("It is not the jvm node. ", jvmNode, table.getTableCell(2, 2).getText());
437         } catch (SAXException JavaDoc e) {
438             fail("The jvm table is incorrect.");
439         }
440
441     }
442
443     /**
444      * Get the number of JNDI object
445      * @param contentFrame the content frame
446      * @return number of JNDI objects
447      * @throws SAXException if a table or cell doesn't match.
448      */

449     private int getJndiNb(WebResponse contentFrame) throws SAXException JavaDoc {
450         JonasAdminUtils utils = new JonasAdminUtils();
451         WebTable table = utils.getTable(contentFrame, 2);
452
453         String JavaDoc text = table.getTableCell(0, 0).getText();
454         int beginIndex = text.indexOf("(");
455         int endIndex = text.indexOf(")", beginIndex);
456         String JavaDoc nb = text.substring(beginIndex + "(".length(), endIndex);
457
458         return Integer.parseInt(nb);
459     }
460
461     /**
462      * Get MBean attribut
463      * @param select value of select param to indicate the chosen MBean
464      * @param attribut name of attribut
465      * @return value of the attribut
466      * @throws MalformedURLException if url is invalid
467      * @throws IOException if url is not correct
468      * @throws SAXException if an error is found
469      */

470     private String JavaDoc getMBeanAttribut(String JavaDoc select, String JavaDoc attribut) {
471         WebResponse wr;
472         String JavaDoc urlAttributs = prefixUrl + URL_MBEAN_ATTRIBUTS + select;
473
474         try {
475             wr = wc2.getResponse(urlAttributs);
476             String JavaDoc attributs = wr.getText();
477             int position = attributs.indexOf("<b>" + attribut + "</b>");
478             int beginIndex = attributs.indexOf("== ", position);
479             int endIndex = attributs.indexOf(END_OF_COLUMN, beginIndex);
480             return attributs.substring(beginIndex + "== ".length(), endIndex);
481         } catch (MalformedURLException JavaDoc e) {
482             throw new IllegalStateException JavaDoc("Invalid URL: " + url + ". " + e);
483         } catch (IOException JavaDoc e) {
484             throw new IllegalStateException JavaDoc("No response from: " + url + ". " + e);
485         } catch (SAXException JavaDoc e) {
486             throw new IllegalStateException JavaDoc("No text found. " + e);
487         }
488     }
489
490     /**
491      * Tear Down
492      * cleanUp action
493      */

494     public void tearDown() throws Exception JavaDoc {
495     }
496 }
497
Popular Tags