| 1 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 45 public class F_Alarm extends JExampleTestCase { 46 47 48 51 private static final String URL_ALARM = "/alarm/secured/list.jsp"; 52 53 56 private static final String URL_ALARM_LIST = "/alarm/secured/setfilter.jsp?profil=all-I"; 57 58 61 private static final String URL_ALARM_GENERATE = "/alarm/generator.html"; 62 63 67 public static void main(String [] args) { 68 69 String testtorun = null; 70 for (int argn = 0; argn < args.length; argn++) { 72 String 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 89 public static TestSuite suite() { 90 return new TestSuite(F_Alarm.class); 91 } 92 93 98 protected void setUp() throws Exception { 99 super.setUp(); 100 useEar("alarm"); 101 } 102 103 107 public F_Alarm(String s) { 108 super(s, URL_ALARM); 109 } 110 111 112 116 public void testTryWithoutAuth() throws Exception { 117 try { 118 WebResponse wr = wc.getResponse(url); 119 fail("Alarm is not protected"); 120 } catch (AuthorizationRequiredException e) { 121 ; 122 } 123 } 124 125 129 public void testTryWithBadAuth() throws Exception { 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 143 public void testTryWithJettyAuth() throws Exception { 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 156 public void testTryWithTomcatAuth() throws Exception { 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 168 public void testGenerateAlarms() throws Exception { 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 [] levels = new String [] {"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 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 200 public void testProcessAlarms() throws Exception { 201 try { 202 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 (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 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 |