KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jonasadmin > test > monitoring > F_JonasAdminNumberOfMeasures


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

25
26 package org.objectweb.jonas.jonasadmin.test.monitoring;
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
33 import com.meterware.httpunit.HttpUnitOptions;
34 import com.meterware.httpunit.WebConversation;
35 import com.meterware.httpunit.WebForm;
36 import com.meterware.httpunit.WebImage;
37 import com.meterware.httpunit.WebResponse;
38
39
40 /**
41  * Test the modification of the number of measures
42  * @author kemlerp
43  *
44  */

45 public class F_JonasAdminNumberOfMeasures extends JonasAdminTestCase {
46
47     /**
48      * URL of the deployment of EARs
49      */

50     private static final String JavaDoc URL_JONASADMIN_MEMORY = "/jonasAdmin/EditMemory.do";
51
52     /**
53      * CONSTANT which is used to calculate the width of the Memory image
54      */

55     private static final int CONSTANT = 190;
56
57     /**
58      *
59      */

60     private static final String JavaDoc INITIAL_NB_MEASURES = "120";
61
62     /**
63      * Constructor with a specified name
64      * @param s name
65      */

66     public F_JonasAdminNumberOfMeasures(String JavaDoc s) {
67         super(s, URL_JONASADMIN);
68     }
69
70     /**
71      * Constructor with a specified name
72      * @param wc the WebConversation of the suite test
73      * @param s name
74      */

75     public F_JonasAdminNumberOfMeasures(WebConversation wc, String JavaDoc s) {
76         super(wc, s, URL_JONASADMIN);
77     }
78
79     /**
80      * Main method
81      * @param args the arguments
82      */

83     public static void main(String JavaDoc[] args) {
84
85         String JavaDoc testtorun = null;
86         // Get args
87
for (int argn = 0; argn < args.length; argn++) {
88             String JavaDoc sArg = args[argn];
89             if (sArg.equals("-n")) {
90                 testtorun = args[++argn];
91             }
92         }
93         if (testtorun == null) {
94             junit.textui.TestRunner.run(suite());
95         } else {
96             junit.textui.TestRunner.run(new F_JonasAdminNumberOfMeasures(testtorun));
97         }
98     }
99
100     /**
101      * Get a new TestSuite for this class
102      * @return a new TestSuite for this class
103      */

104     public static TestSuite suite() {
105         return new TestSuite(F_JonasAdminNumberOfMeasures.class);
106     }
107
108     /**
109      * Get a new TestSuite for this class
110      * @param wc the WebConversation
111      * @return a new TestSuite for this class with the WebConversation instance
112      */

113     public static TestSuite suite(WebConversation wc) {
114         TestSuite suite = new TestSuite();
115         suite.addTest(new F_JonasAdminNumberOfMeasures(wc, "testValidNbMeasures"));
116         suite.addTest(new F_JonasAdminNumberOfMeasures(wc, "testNotIntegerNbMeasures"));
117         suite.addTest(new F_JonasAdminNumberOfMeasures(wc, "testNbMeasuresLessThanOne"));
118         return suite;
119     }
120
121     /**
122      * Setup need for these tests
123      * jonasAdmin is required
124      * @throws Exception if it fails
125      */

126     protected void setUp() throws Exception JavaDoc {
127         super.setUp();
128
129         if (wc.getCurrentPage().getURL() == null) {
130             useWar("jonasAdmin");
131             // login to jonas admin
132
try {
133                 JonasAdminAuth.doValidAuth(wc, url);
134             } catch (Exception JavaDoc e) {
135                 fail("authentification failed : " + e);
136             }
137         } else {
138             // if there was an error, the connection must be restablished
139
try {
140                 wc.getFrameContents(FRAME_TREE);
141             } catch (Exception JavaDoc e) {
142                 wc.getResponse(urlLogOut);
143                 // login to jonas admin
144
try {
145                     JonasAdminAuth.doValidAuth(wc, url);
146                 } catch (Exception JavaDoc auth) {
147                     fail("authentification failed : " + auth);
148                 }
149             }
150         }
151     }
152
153     /**
154      * Setup need for these tests
155      * jonasAdmin is required
156      * @throws Exception if it fails
157      */

158     protected void tearDown() throws Exception JavaDoc {
159         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
160         WebForm[] webForms = wr.getForms();
161         WebForm webForm = webForms[0];
162
163         webForm.setParameter("numberOfMeasures", INITIAL_NB_MEASURES);
164         wr = webForm.submit();
165     }
166
167     /**
168      * Test image width with a good value
169      * @throws Exception if error occurs
170      */

171     public void testValidNbMeasures() throws Exception JavaDoc {
172
173         String JavaDoc nbMeasures = "300";
174         WebResponse wr;
175
176         // Disable errors of javascript
177
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
178         // Disable exception thrown on error status
179
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
180
181         // Modify number of measures
182
wr = modifyNbMeasures(nbMeasures);
183
184         // Verify image width
185
WebImage imageMemory = wr.getImageWithName("memory");
186         assertEquals("the width of the Memory image is wrong. ", "" + calculateWidth(nbMeasures), imageMemory.getAttribute("width"));
187
188     }
189
190     /**
191      * Test image width with a number of measures is not an integer
192      * @throws Exception if error occurs
193      */

194     public void testNotIntegerNbMeasures() throws Exception JavaDoc {
195
196         String JavaDoc nbMeasures = "100.2";
197         WebResponse wr;
198
199         // Disable errors of javascript
200
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
201         // Disable exception thrown on error status
202
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
203
204         // Modify number of measures
205
wr = modifyNbMeasures(nbMeasures);
206
207         // Verify no image
208
assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
209
210         // Verify error message
211
assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);
212     }
213
214     /**
215      * Test image width with a number of measures is 1
216      * @throws Exception if error occurs
217      */

218     public void testNbMeasuresLessThanOne() throws Exception JavaDoc {
219
220         // Disable errors of javascript
221
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
222         // Disable exception thrown on error status
223
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
224
225
226         String JavaDoc nbMeasures = "1";
227         WebResponse wr;
228
229         // Modify number of measures
230
wr = modifyNbMeasures(nbMeasures);
231         // Verify no image
232
assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
233         // Verify error message
234
assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);
235
236
237         nbMeasures = "0";
238
239         // Modify number of measures
240
wr = modifyNbMeasures(nbMeasures);
241         // Verify no image
242
assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
243         // Verify error message
244
assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);
245
246
247         nbMeasures = "-1";
248
249         // Modify number of measures
250
wr = modifyNbMeasures(nbMeasures);
251         // Verify no image
252
assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
253         // Verify error message
254
assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);
255
256
257         nbMeasures = "-99999";
258
259         // Modify number of measures
260
wr = modifyNbMeasures(nbMeasures);
261         // Verify no image
262
assertNull("there is the Memory image. ", wr.getImageWithName("memory"));
263         // Verify error message
264
assertTrue("there is not error message. ", wr.getText().indexOf("<li>") != -1);
265     }
266
267     /**
268      * Calculate the width of Memory image
269      * @param nbMeasures number of measures
270      * @return width
271      */

272     public int calculateWidth(String JavaDoc nbMeasures) {
273         return Integer.parseInt(nbMeasures) * 2 + CONSTANT;
274     }
275
276     /**
277      * Modify number of measures
278      * @param nbMeasures number of measures
279      * @return response
280      * @throws Exception if error occurs
281      */

282     public WebResponse modifyNbMeasures(String JavaDoc nbMeasures) throws Exception JavaDoc {
283
284         // Go to Monitoring Memory
285
WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
286         WebForm[] webForms = wr.getForms();
287         WebForm webForm = webForms[0];
288
289         webForm.setParameter("numberOfMeasures", nbMeasures);
290         return wr = webForm.submit();
291     }
292 }
293
Popular Tags