KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > logging > F_JonasAdminLogger


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_JonasAdminLogger.java,v 1.3 2005/07/12 13:20:01 kemlerp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jonasadmin.test.logging;
27
28 import junit.framework.TestSuite;
29
30 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
31 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
32 import org.objectweb.jonas.jonasadmin.test.util.JonasAdminUtils;
33 import org.xml.sax.SAXException JavaDoc;
34
35 import com.meterware.httpunit.HttpUnitOptions;
36 import com.meterware.httpunit.SubmitButton;
37 import com.meterware.httpunit.WebConversation;
38 import com.meterware.httpunit.WebForm;
39 import com.meterware.httpunit.WebLink;
40 import com.meterware.httpunit.WebResponse;
41 import com.meterware.httpunit.WebTable;
42
43
44 /**
45  * Define a class to test the JOnAS Admin console
46  * Test logging
47  * @author Paul Kemler
48  *
49  */

50 public class F_JonasAdminLogger extends JonasAdminTestCase {
51
52     /**
53      * URL of Jonas Logger
54      */

55     private static final String JavaDoc URL_JONASADMIN_JONAS_LOGGER = "/EditLoggingJonas.do";
56
57     /**
58      * INPUT Checkbox
59      */

60     private static final String JavaDoc INPUT_CHECKBOX = "input type=\"checkbox\"";
61
62     /**
63      * Title logger type
64      */

65     private static final String JavaDoc TITLE_LOGGER_TYPE = "Logger type";
66
67     /**
68      * Title container type
69      */

70     private static final String JavaDoc TITLE_CONTAINER_TYPE = "Container type";
71
72     /**
73      * Title container name
74      */

75     private static final String JavaDoc TITLE_CONTAINER_NAME = "Container name (or path)";
76
77     /**
78      * A 2nd connection to jonasAdmin
79      */

80     private static WebConversation wc2 = new WebConversation();
81
82     /**
83      * Constructor with a specified name
84      * @param s name
85      */

86     public F_JonasAdminLogger(String JavaDoc s) {
87         super(s, URL_JONASADMIN);
88     }
89
90     /**
91      * Constructor with a specified name
92      * @param wc the WebConversation of the suite test
93      * @param s name
94      */

95     public F_JonasAdminLogger(WebConversation wc, String JavaDoc s) {
96         super(wc, s, URL_JONASADMIN);
97     }
98
99     /**
100      * Main method
101      * @param args the arguments
102      */

103     public static void main(String JavaDoc[] args) {
104
105         String JavaDoc testtorun = null;
106         // Get args
107
for (int argn = 0; argn < args.length; argn++) {
108             String JavaDoc sArg = args[argn];
109             if (sArg.equals("-n")) {
110                 testtorun = args[++argn];
111             }
112         }
113         if (testtorun == null) {
114             junit.textui.TestRunner.run(suite());
115         } else {
116             junit.textui.TestRunner.run(new F_JonasAdminLogger(testtorun));
117         }
118     }
119
120     /**
121      * Get a new TestSuite for this class
122      * @return a new TestSuite for this class
123      */

124     public static TestSuite suite() {
125         return new TestSuite(F_JonasAdminLogger.class);
126     }
127
128     /**
129      * Get a new TestSuite for this class
130      * @param wc the WebConversation
131      * @return a new TestSuite for this class with the WebConversation instance
132      */

133     public static TestSuite suite(WebConversation wc) {
134         TestSuite suite = new TestSuite();
135         suite.addTest(new F_JonasAdminLogger(wc, "testLoggers"));
136         suite.addTest(new F_JonasAdminLogger(wc, "testJonasLogger"));
137         return suite;
138     }
139
140     /**
141      * Setup need for these tests
142      * jonasAdmin is required
143      * @throws Exception if it fails
144      */

145     protected void setUp() throws Exception JavaDoc {
146         super.setUp();
147
148         if (wc.getCurrentPage().getURL() == null) {
149             useWar("jonasAdmin");
150             // login to jonas admin
151
try {
152                 JonasAdminAuth.doValidAuth(wc, url);
153             } catch (Exception JavaDoc e) {
154                 fail("authentification failed : " + e);
155             }
156         } else {
157             // if there was an error, the connection must be restablished
158
try {
159                 wc.getFrameContents(FRAME_TREE);
160             } catch (Exception JavaDoc e) {
161                 wc.getResponse(urlLogOut);
162                 // login to jonas admin
163
try {
164                     JonasAdminAuth.doValidAuth(wc, url);
165                 } catch (Exception JavaDoc auth) {
166                     fail("authentification failed : " + auth);
167                 }
168             }
169         }
170     }
171
172     /**
173      * Test creation, modification et removal acces log valves
174      * @throws Exception if error occurs
175      *
176      */

177     public void testLoggers() throws Exception JavaDoc {
178
179         WebResponse wr;
180         WebLink link;
181         WebTable table;
182         WebForm form;
183         String JavaDoc host = "localhost";
184         int row;
185         int columnLoggerType;
186         JonasAdminUtils utils = new JonasAdminUtils();
187
188         // Disable errors of javascript
189
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
190         // Disable exception thrown on error status
191
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
192
193
194         // OPEN A SECOND CONNECTION
195
// login to jonas admin
196
try {
197             JonasAdminAuth.doValidAuth(wc2, url);
198         } catch (Exception JavaDoc e) {
199             fail("authentification failed : " + e);
200         }
201
202         if (jProp.isCatalina()) {
203             wr = LoggerUtils.getLoggerPage(wc);
204             table = utils.getTable(wr, 3);
205
206             if (table.getRowCount() == 2) {
207                 assertFalse("There is a remove button. ", wr.getText().indexOf("'remove'") != -1);
208             } else {
209                 assertTrue("There is no remove button but there is no one and only one logger. ", wr.getText().indexOf("'remove'") != -1);
210             }
211
212             // 1-Create new loggers
213
LoggerUtils.createNewEngineLogger(wc);
214             // Verify
215
wr = LoggerUtils.getLoggerPage(wc);
216             table = utils.getTable(wr, 3);
217             form = wr.getForms()[0];
218             columnLoggerType = utils.getColumn(TITLE_LOGGER_TYPE, table).intValue();
219             row = utils.getFirstRow("Access log valve engine", table, columnLoggerType).intValue();
220             if (row == -1) {
221                 throw new IllegalThreadStateException JavaDoc("There is no row, which contains 'Access log valve engine'. ");
222             }
223             assertEquals("There is no the correct number of loggers. ", 3, table.getRowCount());
224             assertTrue("There is no remove button. ", wr.getText().indexOf("'remove'") != -1);
225             assertEquals("There is no the correct number of parameters in the form. ", 2, form.getParameterNames().length);
226             assertTrue("There is no checkbox", wr.getText().indexOf(INPUT_CHECKBOX) != -1);
227             assertEquals("The container type is not 'Engine'. ", "Engine", table.getTableCell(row, utils.getColumn(TITLE_CONTAINER_TYPE, table).intValue()).getText());
228             assertEquals("The container name is not 'engine'. ", "engine", table.getTableCell(row, utils.getColumn(TITLE_CONTAINER_NAME, table).intValue()).getText());
229
230             LoggerUtils.createNewHostLogger(wc);
231             // Verify
232
wr = LoggerUtils.getLoggerPage(wc);
233             table = utils.getTable(wr, 3);
234             form = wr.getForms()[0];
235             row = utils.getFirstRow("Access log valve " + host, table, columnLoggerType).intValue();
236             if (row == -1) {
237                 throw new IllegalThreadStateException JavaDoc("There is no row, which contains 'Access log valve " + host + "'. ");
238             }
239             assertEquals("There is no the correct number of loggers. ", 4, table.getRowCount());
240             assertTrue("There is no remove button. ", wr.getText().indexOf("'remove'") != -1);
241             assertEquals("There is no the correct number of parameters in the form. ", 2, form.getParameterNames().length);
242             assertEquals("The container type is not 'Host'. ", "Host", table.getTableCell(row, utils.getColumn(TITLE_CONTAINER_TYPE, table).intValue()).getText());
243             assertEquals("The container name is not '" + host + "'. ", host, table.getTableCell(row, utils.getColumn(TITLE_CONTAINER_NAME, table).intValue()).getText());
244
245             // 2-Modify loggers
246
LoggerUtils.modifyLogger(wc, null, "dir", "true", "false", ".suffix", "prefix_", "%v");
247             // Verify
248
wr = wc.getFrameContents(FRAME_CONTENT);
249             verifyAccessLogValve(wr, "dir", "true", "false", ".suffix", "prefix_", "%v");
250
251             LoggerUtils.modifyLogger(wc, host, "dir", "true", "false", ".suffix", "prefix_", "%v");
252             // Verify
253
wr = wc.getFrameContents(FRAME_CONTENT);
254             verifyAccessLogValve(wr, "dir", "true", "false", ".suffix", "prefix_", "%v");
255
256
257             // 3-Remove loggers
258
LoggerUtils.deleteLogger(wc, null);
259             // Verify
260
wr = LoggerUtils.getLoggerPage(wc);
261             table = utils.getTable(wr, 3);
262             assertEquals("The number of loggers is not 2. ", 3, table.getRowCount());
263
264             LoggerUtils.deleteLogger(wc, host);
265             // Verify
266
wr = LoggerUtils.getLoggerPage(wc);
267             table = utils.getTable(wr, 3);
268             assertEquals("The number of loggers is not 1. ", 2, table.getRowCount());
269         }
270     }
271
272     /**
273      * Test JOnAS logger
274      * @throws Exception if error occurs
275      */

276     public void testJonasLogger() throws Exception JavaDoc {
277
278         if (jProp.isCatalina()) {
279             WebResponse wr;
280             WebLink link;
281             WebTable table;
282             WebForm form;
283             SubmitButton button = null;
284             int row;
285             int columnLevels;
286             int columnTopics;
287             JonasAdminUtils utils = new JonasAdminUtils();
288
289             // Disable errors of javascript
290
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
291             // Disable exception thrown on error status
292
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
293
294             wr = LoggerUtils.getLoggerPage(wc);
295             link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING, URL_JONASADMIN_JONAS_LOGGER);
296             if (link != null) {
297                 link.click();
298             } else {
299                 throw new IllegalThreadStateException JavaDoc("The link " + URL_JONASADMIN_JONAS_LOGGER + " is not found. ");
300             }
301
302             // Verify topics
303
wr = wc.getFrameContents(FRAME_CONTENT);
304             table = utils.getTable(wr, 3);
305             form = wr.getForms()[0];
306             String JavaDoc[] topics = form.getOptions("topic");
307             columnTopics = utils.getColumn("Topics", table).intValue();
308             if (columnTopics == -1) {
309                 throw new IllegalThreadStateException JavaDoc("There is no column, which contains 'Topics'. ");
310             }
311             assertEquals("Number of topics is not the same in the select menu and in the table.", topics.length, table.getRowCount() - 1);
312             for (int i = 0; i < topics.length; i++) {
313                 assertEquals("Topic " + topics[i] + " is not found in the table. ", topics[i], table.getTableCell(i+1, columnTopics).getText());
314             }
315
316             // Modify level of a topic
317
columnLevels = utils.getColumn("Levels", table).intValue();
318             if (columnLevels == -1) {
319                 throw new IllegalThreadStateException JavaDoc("There is no column, which contains 'Levels'. ");
320             }
321             String JavaDoc level = table.getTableCell(topics.length, columnLevels).getText();
322             String JavaDoc topic = "org.objectweb.jonas.web";
323             if (level.equals("WARN")) {
324                 level = "INFO";
325             } else if (level.equals("INFO")) {
326                 level = "ERROR";
327             } else if (level.equals("FATAL")) {
328                 level = "INHERIT";
329             } else if (level.equals("INHERIT")) {
330                 level = "DEBUG";
331             } else if (level.equals("DEBUG")) {
332                 level = "WARN";
333             }
334
335             form.setParameter("topic", topic);
336             form.setParameter("level", level);
337
338             button = form.getSubmitButtons()[0];
339             button.click();
340
341             // Verify topics
342
wr = wc.getFrameContents(FRAME_CONTENT);
343             table = utils.getTable(wr, 3);
344             if (level.equals("ERROR")) {
345                 level = "FATAL";
346             }
347             row = utils.getFirstRow(topic, table, columnTopics).intValue();
348             if (row == -1) {
349                 throw new IllegalThreadStateException JavaDoc("There is no row, which contains '" + topic + "' in the topic column. ");
350             }
351             assertEquals("The level of " + topic + " is not " + level + ". ", level, table.getTableCell(row, columnLevels).getText());
352         }
353     }
354
355     /**
356      * Verify access log valve properties
357      * @param wr Content frame
358      * @param directory value of the directory parameter
359      * @param resolveHosts value of the resolveHosts parameter
360      * @param rotatable value of the rotatable parameter
361      * @param suffix value of the suffix parameter
362      * @param prefix value of the prefix parameter
363      * @param pattern value of the pattern parameter
364      * @throws SAXException if an error occurs while form is got
365      */

366     private void verifyAccessLogValve(WebResponse wr, String JavaDoc directory, String JavaDoc resolveHosts,
367             String JavaDoc rotatable, String JavaDoc suffix, String JavaDoc prefix, String JavaDoc pattern) throws SAXException JavaDoc {
368         WebForm form = wr.getForms()[0];
369         assertEquals("Directory value is not" + directory + ". ", directory, form.getParameterValue("directory"));
370         assertEquals("Resolve hosts value is not" + resolveHosts + ". ", resolveHosts, form.getParameterValue("resolveHosts"));
371         assertEquals("Rotatable value is not" + rotatable + ". ", rotatable, form.getParameterValue("rotatable"));
372         assertEquals("Suffix value is not" + suffix + ". ", suffix, form.getParameterValue("suffix"));
373         assertEquals("Prefix value is not" + prefix + ". ", prefix, form.getParameterValue("prefix"));
374         assertEquals("Pattern value is not" + pattern + ". ", pattern, form.getParameterValue("pattern"));
375     }
376 }
377
Popular Tags