KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > clients > alarm > F_Alarm


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: F_Alarm.java,v 1.2 2004/03/19 11:57:20 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.examples.clients.alarm;
28
29 import junit.framework.TestSuite;
30
31 import org.objectweb.jonas.examples.util.JExampleTestCase;
32
33 import com.meterware.httpunit.AuthorizationRequiredException;
34 import com.meterware.httpunit.TableCell;
35 import com.meterware.httpunit.WebForm;
36 import com.meterware.httpunit.WebLink;
37 import com.meterware.httpunit.WebResponse;
38 import com.meterware.httpunit.WebTable;
39
40 /**
41  * Define a class to test the Alarm example
42  * Test authentication, generate alarm and process alarms
43  * @author Florent Benoit
44  */

45 public class F_Alarm extends JExampleTestCase {
46
47
48     /**
49      * URL of the alarm index
50      */

51     private static final String JavaDoc URL_ALARM = "/alarm/secured/list.jsp";
52
53     /**
54      * URL of the list of alarms
55      */

56     private static final String JavaDoc URL_ALARM_LIST = "/alarm/secured/setfilter.jsp?profil=all-I";
57
58     /**
59      * URL of the alarm generator
60      */

61     private static final String JavaDoc URL_ALARM_GENERATE = "/alarm/generator.html";
62
63     /**
64      * Main method
65      * @param args the arguments
66      */

67     public static void main(String JavaDoc[] args) {
68
69         String JavaDoc testtorun = null;
70         // Get args
71
for (int argn = 0; argn < args.length; argn++) {
72             String JavaDoc sArg = args[argn];
73             if (sArg.equals("-n")) {
74                 testtorun = args[++argn];
75             }
76         }
77
78         if (testtorun == null) {
79             junit.textui.TestRunner.run(suite());
80         } else {
81             junit.textui.TestRunner.run(new F_Alarm(testtorun));
82         }
83     }
84
85     /**
86      * Get a new TestSuite for this class
87      * @return a new TestSuite for this class
88      */

89     public static TestSuite suite() {
90         return new TestSuite(F_Alarm.class);
91     }
92
93     /**
94      * Setup need for these tests
95      * alarm is required
96      * @throws Exception if it fails
97      */

98     protected void setUp() throws Exception JavaDoc {
99         super.setUp();
100         useEar("alarm");
101     }
102
103     /**
104      * Constructor with a specified name
105      * @param s name
106      */

107     public F_Alarm(String JavaDoc s) {
108         super(s, URL_ALARM);
109     }
110
111
112     /**
113      * Try to log in without authentication
114      * @throws Exception if an error occurs
115      */

116     public void testTryWithoutAuth() throws Exception JavaDoc {
117         try {
118             WebResponse wr = wc.getResponse(url);
119             fail("Alarm is not protected");
120         } catch (AuthorizationRequiredException e) {
121             ;
122         }
123     }
124
125     /**
126      * Try to authenticate with a bad login/password
127      * @throws Exception if an error occurs
128      */

129     public void testTryWithBadAuth() throws Exception JavaDoc {
130         try {
131             wc.setAuthorization("bad", "bad");
132             WebResponse wr = wc.getResponse(url);
133             fail("Alarm is not protected");
134         } catch (AuthorizationRequiredException e) {
135             ;
136         }
137     }
138
139     /**
140      * Try to authenticate with a right login/password (jetty)
141      * @throws Exception if an error occurs
142      */

143     public void testTryWithJettyAuth() throws Exception JavaDoc {
144         try {
145             wc.setAuthorization("jetty", "jetty");
146             WebResponse wr = wc.getResponse(url);
147         } catch (AuthorizationRequiredException e) {
148             fail("Alarm don't accept l/p jetty/jetty");
149         }
150     }
151
152     /**
153      * Try to authenticate with a right login/password (tomcat)
154      * @throws Exception if an error occurs
155      */

156     public void testTryWithTomcatAuth() throws Exception JavaDoc {
157         try {
158             wc.setAuthorization("tomcat", "tomcat");
159             WebResponse wr = wc.getResponse(url);
160         } catch (AuthorizationRequiredException e) {
161             fail("Alarm don't accept l/p tomcat/tomcat");
162         }
163     }
164     /**
165      * Try to generate alarms. One of each kind of alarm (severe, informative, warning)
166      * @throws Exception if an error occurs
167      */

168     public void testGenerateAlarms() throws Exception JavaDoc {
169         try {
170             wc.setAuthorization("tomcat", "tomcat");
171             WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_ALARM_GENERATE));
172
173             WebForm[] webForms = wr.getForms();
174
175             WebForm webForm = webForms[0];
176             assertNotNull("There must be a form in the html page", webForm);
177
178             String JavaDoc[] levels = new String JavaDoc[] {"S", "W", "I"};
179
180             for (int i = 0; i < 3; i++) {
181                 webForm.setParameter("message", "Automatic test" + i);
182                 webForm.setParameter("device", "aut" + i);
183                 webForm.setParameter("level", levels[i]);
184                 WebResponse wFormRes = webForm.submit();
185                 String JavaDoc txt = wFormRes.getText();
186                 if (txt.indexOf("Message sent") == -1) {
187                     fail("The alarm was not generated : " + txt);
188                 }
189             }
190
191         } catch (AuthorizationRequiredException e) {
192             fail("Alarm don't accept l/p tomcat/tomcat");
193         }
194     }
195
196     /**
197      * Try to process all alarms which were generated
198      * @throws Exception if an error occurs
199      */

200     public void testProcessAlarms() throws Exception JavaDoc {
201         try {
202             // Wait a little if the previous alarms were not generated
203
Thread.sleep(1000);
204
205             wc.setAuthorization("tomcat", "tomcat");
206             WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_ALARM_LIST));
207             WebTable[] webTables = wr.getTables();
208
209             WebTable wTable = webTables[1];
210             assertNotNull("There must be a table in the html page", wTable);
211
212             int rows = wTable.getRowCount();
213             int cols = wTable.getColumnCount();
214
215             assertEquals("The tables must have 6 columns", 6, cols);
216
217             // For each alarm
218
for (int i = 1; i < rows; i++) {
219                 TableCell tableCell = wTable.getTableCell(i, cols - 1);
220                 assertNotNull("There must be a cell in this row", tableCell);
221                 WebLink[] links = tableCell.getLinks();
222                 assertNotNull("There must have links in this cell", links);
223                 for (int l = 0; l < links.length; l++) {
224                    links[l].click();
225                 }
226             }
227             
228             //Now check alarms are empty (rows = 1)
229
wr = wc.getResponse(getAbsoluteUrl(URL_ALARM_LIST));
230             webTables = wr.getTables();
231             wTable = webTables[1];
232             rows = wTable.getRowCount();
233             assertEquals(1, rows);
234
235         } catch (AuthorizationRequiredException e) {
236             fail("Authentication failed");
237         }
238     }
239 }
240
Popular Tags