001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: C_alarm.java 10557 2007-06-07 09:21:44Z coqp $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.examples.clients.alarm;
028:
029: import junit.framework.TestSuite;
030:
031: import org.objectweb.jonas.examples.util.JExampleTestCase;
032:
033: import com.meterware.httpunit.AuthorizationRequiredException;
034: import com.meterware.httpunit.TableCell;
035: import com.meterware.httpunit.WebForm;
036: import com.meterware.httpunit.WebLink;
037: import com.meterware.httpunit.WebResponse;
038: import com.meterware.httpunit.WebTable;
039:
040: /**
041: * Define a class to test the Alarm example
042: * Test authentication, generate alarm and process alarms
043: * @author Florent Benoit
044: */
045: public class C_alarm extends JExampleTestCase {
046:
047: /**
048: * URL of the alarm index
049: */
050: private static final String URL_ALARM = "/alarm/secured/list.jsp";
051:
052: /**
053: * URL of the list of alarms
054: */
055: private static final String URL_ALARM_LIST = "/alarm/secured/setfilter.jsp?profil=all-I";
056:
057: /**
058: * URL of the alarm generator
059: */
060: private static final String URL_ALARM_GENERATE = "/alarm/generator.html";
061:
062: /**
063: * Main method
064: * @param args the arguments
065: */
066: public static void main(String[] args) {
067:
068: String testtorun = null;
069: // Get args
070: for (int argn = 0; argn < args.length; argn++) {
071: String sArg = args[argn];
072: if (sArg.equals("-n")) {
073: testtorun = args[++argn];
074: }
075: }
076:
077: if (testtorun == null) {
078: junit.textui.TestRunner.run(suite());
079: } else {
080: junit.textui.TestRunner.run(new C_alarm(testtorun));
081: }
082: }
083:
084: /**
085: * Get a new TestSuite for this class
086: * @return a new TestSuite for this class
087: */
088: public static TestSuite suite() {
089: return new TestSuite(C_alarm.class);
090: }
091:
092: /**
093: * Setup need for these tests
094: * alarm is required
095: * @throws Exception if it fails
096: */
097: protected void setUp() throws Exception {
098: super .setUp();
099: useEar("alarm");
100: }
101:
102: /**
103: * Constructor with a specified name
104: * @param s name
105: */
106: public C_alarm(String s) {
107: super (s, URL_ALARM);
108: }
109:
110: /**
111: * Try to log in without authentication
112: * @throws Exception if an error occurs
113: */
114: public void testTryWithoutAuth() throws Exception {
115: try {
116: WebResponse wr = wc.getResponse(url);
117: fail("Alarm is not protected");
118: } catch (AuthorizationRequiredException e) {
119: ;
120: }
121: }
122:
123: /**
124: * Try to authenticate with a bad login/password
125: * @throws Exception if an error occurs
126: */
127: public void testTryWithBadAuth() throws Exception {
128: try {
129: wc.setAuthorization("bad", "bad");
130: WebResponse wr = wc.getResponse(url);
131: fail("Alarm is not protected");
132: } catch (AuthorizationRequiredException e) {
133: ;
134: }
135: }
136:
137: /**
138: * Try to authenticate with a right login/password (jetty)
139: * @throws Exception if an error occurs
140: */
141: public void testTryWithJettyAuth() throws Exception {
142: try {
143: wc.setAuthorization("jetty", "jetty");
144: WebResponse wr = wc.getResponse(url);
145: } catch (AuthorizationRequiredException e) {
146: fail("Alarm don't accept l/p jetty/jetty");
147: }
148: }
149:
150: /**
151: * Try to authenticate with a right login/password (tomcat)
152: * @throws Exception if an error occurs
153: */
154: public void testTryWithTomcatAuth() throws Exception {
155: try {
156: wc.setAuthorization("tomcat", "tomcat");
157: WebResponse wr = wc.getResponse(url);
158: } catch (AuthorizationRequiredException e) {
159: fail("Alarm don't accept l/p tomcat/tomcat");
160: }
161: }
162:
163: /**
164: * Try to generate alarms. One of each kind of alarm (severe, informative, warning)
165: * @throws Exception if an error occurs
166: */
167: public void testGenerateAlarms() throws Exception {
168: try {
169: wc.setAuthorization("tomcat", "tomcat");
170: WebResponse wr = wc
171: .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",
177: webForm);
178:
179: String[] levels = new String[] { "S", "W", "I" };
180:
181: for (int i = 0; i < 3; i++) {
182: webForm.setParameter("message", "Automatic test" + i);
183: webForm.setParameter("device", "aut" + i);
184: webForm.setParameter("level", levels[i]);
185: WebResponse wFormRes = webForm.submit();
186: String txt = wFormRes.getText();
187: if (txt.indexOf("Message sent") == -1) {
188: fail("The alarm was not generated : " + txt);
189: }
190: }
191:
192: } catch (AuthorizationRequiredException e) {
193: fail("Alarm don't accept l/p tomcat/tomcat");
194: }
195: }
196:
197: /**
198: * Try to process all alarms which were generated
199: * @throws Exception if an error occurs
200: */
201: public void testProcessAlarms() throws Exception {
202: try {
203: // Wait a little if the previous alarms were not generated
204: Thread.sleep(1000);
205:
206: wc.setAuthorization("tomcat", "tomcat");
207: WebResponse wr = wc
208: .getResponse(getAbsoluteUrl(URL_ALARM_LIST));
209: WebTable[] webTables = wr.getTables();
210:
211: WebTable wTable = webTables[1];
212: assertNotNull("There must be a table in the html page",
213: wTable);
214:
215: int rows = wTable.getRowCount();
216: int cols = wTable.getColumnCount();
217:
218: assertEquals("The tables must have 6 columns", 6, cols);
219:
220: // For each alarm
221: for (int i = 1; i < rows; i++) {
222: TableCell tableCell = wTable.getTableCell(i, cols - 1);
223: assertNotNull("There must be a cell in this row",
224: tableCell);
225: WebLink[] links = tableCell.getLinks();
226: assertNotNull("There must have links in this cell",
227: links);
228: for (int l = 0; l < links.length; l++) {
229: links[l].click();
230: }
231: }
232:
233: //Now check alarms are empty (rows = 1)
234: wr = wc.getResponse(getAbsoluteUrl(URL_ALARM_LIST));
235: webTables = wr.getTables();
236: wTable = webTables[1];
237: rows = wTable.getRowCount();
238: assertEquals(1, rows);
239:
240: } catch (AuthorizationRequiredException e) {
241: fail("Authentication failed");
242: }
243: }
244: }
|