KickJava   Java API By Example, From Geeks To Geeks.

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


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_JonasAdminRange.java,v 1.3 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.WebResponse;
37
38
39 public class F_JonasAdminRange extends JonasAdminTestCase {
40
41     /**
42      * URL of the deployment of EARs
43      */

44     private static final String JavaDoc URL_JONASADMIN_MEMORY = "/jonasAdmin/EditMemory.do";
45
46     /**
47      * CONSTANT which is used to calculate the timer Graphic
48      */

49     private static final int CONSTANT = 2;
50
51     /**
52      * INITIAL RANGE VALUE
53      */

54     private static final String JavaDoc INITIAL_RANGE = "10";
55
56     /**
57      * Constructor with a specified name
58      * @param s name
59      */

60     public F_JonasAdminRange(String JavaDoc s) {
61         super(s, URL_JONASADMIN);
62     }
63
64     /**
65      * Constructor with a specified name
66      * @param wc the WebConversation of the suite test
67      * @param s name
68      */

69     public F_JonasAdminRange(WebConversation wc, String JavaDoc s) {
70         super(wc, s, URL_JONASADMIN);
71     }
72
73     /**
74      * Main method
75      * @param args the arguments
76      */

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

98     public static TestSuite suite() {
99         return new TestSuite(F_JonasAdminRange.class);
100     }
101
102     /**
103      * Get a new TestSuite for this class
104      * @param wc the WebConversation
105      * @return a new TestSuite for this class with the WebConversation instance
106      */

107     public static TestSuite suite(WebConversation wc) {
108         TestSuite suite = new TestSuite();
109         suite.addTest(new F_JonasAdminRange(wc, "testSuccessful"));
110         suite.addTest(new F_JonasAdminRange(wc, "testTooSmallValue"));
111         return suite;
112     }
113
114     /**
115      * Setup need for these tests
116      * jonasAdmin is required
117      * @throws Exception if it fails
118      */

119     protected void setUp() throws Exception JavaDoc {
120         super.setUp();
121
122         if (wc.getCurrentPage().getURL() == null) {
123             useWar("jonasAdmin");
124             // login to jonas admin
125
try {
126                 JonasAdminAuth.doValidAuth(wc, url);
127             } catch (Exception JavaDoc e) {
128                 fail("authentification failed : " + e);
129             }
130         } else {
131             // if there was an error, the connection must be restablished
132
try {
133                 wc.getFrameContents(FRAME_TREE);
134             } catch (Exception JavaDoc e) {
135                 wc.getResponse(urlLogOut);
136                 // login to jonas admin
137
try {
138                     JonasAdminAuth.doValidAuth(wc, url);
139                 } catch (Exception JavaDoc auth) {
140                     fail("authentification failed : " + auth);
141                 }
142             }
143         }
144     }
145
146     /**
147      * Test monitoring range with a correct value
148      * @throws Exception if error occurs
149      *
150      */

151     public void testSuccessful() throws Exception JavaDoc {
152
153         String JavaDoc range = "22";
154         WebResponse wr;
155
156         // Disable errors of javascript
157
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
158         // Disable exception thrown on error status
159
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
160
161         // Modify number of measures
162
wr = modifyNbMeasures(range);
163
164         // Verify range
165
String JavaDoc text = wr.getText();
166         int start = text.indexOf("function refresh()");
167         int end = text.indexOf("}", start);
168         // Get script
169
String JavaDoc refreshScript = text.substring(start, end);
170
171         // Get value
172
start = refreshScript.indexOf("setTimeout(\"refresh()\", ") + "setTimeout(\"refresh()\", ".length();
173         end = refreshScript.indexOf(");", start);
174         String JavaDoc value = refreshScript.substring(start, end);
175
176         // Verify refresh value in javascript function
177
assertEquals("the range of the Memory image is wrong. ", "" + calculateTimerGraphic(range), value);
178
179         // Verify error message
180
assertFalse("there is an error message. ", wr.getElementsWithAttribute("class", "errors").length > 0);
181     }
182
183     /**
184      * Test monitoring range with too small value
185      * @throws Exception if error occurs
186      *
187      */

188     public void testTooSmallValue() throws Exception JavaDoc {
189
190         String JavaDoc range = "9";
191         WebResponse wr;
192
193         // Disable errors of javascript
194
HttpUnitOptions.setExceptionsThrownOnScriptError(false);
195         // Disable exception thrown on error status
196
HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
197
198         // Modify number of measures
199
wr = modifyNbMeasures(range);
200
201         // Verify error message
202
assertTrue("there is not error message. ", wr.getElementsWithAttribute("class", "errors").length > 0);
203     }
204
205     /**
206      * Calculate the timer graphic
207      * @param range range
208      * @return timer graphic
209      */

210     public int calculateTimerGraphic(String JavaDoc range) {
211         return (Integer.parseInt(range) + CONSTANT) * 1000;
212     }
213
214     /**
215      * Modify range
216      * @param range range
217      * @return response
218      * @throws Exception if error occurs
219      */

220     public WebResponse modifyNbMeasures(String JavaDoc range) throws Exception JavaDoc {
221
222         // Go to Monitoring Memory
223
WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
224
225         WebForm[] webForms = wr.getForms();
226         WebForm webForm = webForms[0];
227
228         webForm.setParameter("range", range);
229         return wr = webForm.submit();
230     }
231
232     /**
233      * Tear Down
234      * cleanUp action
235      */

236     public void tearDown() throws Exception JavaDoc {
237         WebResponse wr = wc.getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
238         WebForm[] webForms = wr.getForms();
239         WebForm webForm = webForms[0];
240
241         webForm.setParameter("range", INITIAL_RANGE);
242         wr = webForm.submit();
243     }
244 }
Popular Tags