001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 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: * --------------------------------------------------------------------------
022: * $Id: F_JonasAdminNumberOfMeasures.java 7381 2005-09-14 16:00:02Z kemlerp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jonasadmin.test.monitoring;
027:
028: import junit.framework.TestSuite;
029:
030: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
031: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
032:
033: import com.meterware.httpunit.HttpUnitOptions;
034: import com.meterware.httpunit.WebForm;
035: import com.meterware.httpunit.WebImage;
036: import com.meterware.httpunit.WebResponse;
037:
038: /**
039: * Test the modification of the number of measures
040: * @author kemlerp
041: *
042: */
043: public class F_JonasAdminNumberOfMeasures extends JonasAdminTestCase {
044:
045: /**
046: * URL of the deployment of EARs
047: */
048: private static final String URL_JONASADMIN_MEMORY = "/jonasAdmin/EditMemory.do";
049:
050: /**
051: * CONSTANT which is used to calculate the width of the Memory image
052: */
053: private static final int CONSTANT = 190;
054:
055: /**
056: *
057: */
058: private static final String INITIAL_NB_MEASURES = "120";
059:
060: /**
061: * Constructor with a specified name
062: * @param s name
063: */
064: public F_JonasAdminNumberOfMeasures(String s) {
065: super (s, URL_JONASADMIN);
066: }
067:
068: /**
069: * Main method
070: * @param args the arguments
071: */
072: public static void main(String[] args) {
073:
074: String testtorun = null;
075: // Get args
076: for (int argn = 0; argn < args.length; argn++) {
077: String sArg = args[argn];
078: if (sArg.equals("-n")) {
079: testtorun = args[++argn];
080: }
081: }
082: if (testtorun == null) {
083: junit.textui.TestRunner.run(suite());
084: } else {
085: junit.textui.TestRunner
086: .run(new F_JonasAdminNumberOfMeasures(testtorun));
087: }
088: }
089:
090: /**
091: * Get a new TestSuite for this class
092: * @return a new TestSuite for this class
093: */
094: public static TestSuite suite() {
095: return new TestSuite(F_JonasAdminNumberOfMeasures.class);
096: }
097:
098: /**
099: * Setup need for these tests
100: * jonasAdmin is required
101: * @throws Exception if it fails
102: */
103: protected void setUp() throws Exception {
104: super .setUp();
105:
106: if (wc.getCurrentPage().getURL() == null) {
107: useWar("jonasAdmin");
108: // login to jonas admin
109: try {
110: JonasAdminAuth.doValidAuth(wc, url);
111: } catch (Exception e) {
112: fail("authentification failed : " + e);
113: }
114: } else {
115: // if there was an error, the connection must be restablished
116: try {
117: wc.getFrameContents(FRAME_TREE);
118: } catch (Exception e) {
119: wc.getResponse(urlLogOut);
120: // login to jonas admin
121: try {
122: JonasAdminAuth.doValidAuth(wc, url);
123: } catch (Exception auth) {
124: fail("authentification failed : " + auth);
125: }
126: }
127: }
128: }
129:
130: /**
131: * Setup need for these tests
132: * jonasAdmin is required
133: * @throws Exception if it fails
134: */
135: public void tearDown() throws Exception {
136:
137: super .tearDown();
138:
139: WebResponse wr = wc
140: .getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
141: WebForm[] webForms = wr.getForms();
142: WebForm webForm = webForms[0];
143:
144: webForm.setParameter("numberOfMeasures", INITIAL_NB_MEASURES);
145: wr = webForm.submit();
146: }
147:
148: /**
149: * Test image width with a good value
150: * @throws Exception if error occurs
151: */
152: public void testValidNbMeasures() throws Exception {
153:
154: String nbMeasures = "300";
155: WebResponse wr;
156:
157: // Disable errors of javascript
158: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
159: // Disable exception thrown on error status
160: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
161:
162: // Modify number of measures
163: wr = modifyNbMeasures(nbMeasures);
164:
165: // Verify image width
166: WebImage imageMemory = wr.getImageWithName("memory");
167: assertEquals("the width of the Memory image is wrong. ", ""
168: + calculateWidth(nbMeasures), imageMemory
169: .getAttribute("width"));
170:
171: }
172:
173: /**
174: * Test image width with a number of measures is not an integer
175: * @throws Exception if error occurs
176: */
177: public void testNotIntegerNbMeasures() throws Exception {
178:
179: String nbMeasures = "100.2";
180: WebResponse wr;
181:
182: // Disable errors of javascript
183: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
184: // Disable exception thrown on error status
185: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
186:
187: // Modify number of measures
188: wr = modifyNbMeasures(nbMeasures);
189:
190: // Verify no image
191: assertNull("there is the Memory image. ", wr
192: .getImageWithName("memory"));
193:
194: // Verify error message
195: assertTrue("there is not error message. ", wr.getText()
196: .indexOf("<li>") != -1);
197: }
198:
199: /**
200: * Test image width with a number of measures is 1
201: * @throws Exception if error occurs
202: */
203: public void testNbMeasuresLessThanOne() throws Exception {
204:
205: // Disable errors of javascript
206: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
207: // Disable exception thrown on error status
208: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
209:
210: String nbMeasures = "1";
211: WebResponse wr;
212:
213: // Modify number of measures
214: wr = modifyNbMeasures(nbMeasures);
215: // Verify no image
216: assertNull("there is the Memory image. ", wr
217: .getImageWithName("memory"));
218: // Verify error message
219: assertTrue("there is not error message. ", wr.getText()
220: .indexOf("<li>") != -1);
221:
222: nbMeasures = "0";
223:
224: // Modify number of measures
225: wr = modifyNbMeasures(nbMeasures);
226: // Verify no image
227: assertNull("there is the Memory image. ", wr
228: .getImageWithName("memory"));
229: // Verify error message
230: assertTrue("there is not error message. ", wr.getText()
231: .indexOf("<li>") != -1);
232:
233: nbMeasures = "-1";
234:
235: // Modify number of measures
236: wr = modifyNbMeasures(nbMeasures);
237: // Verify no image
238: assertNull("there is the Memory image. ", wr
239: .getImageWithName("memory"));
240: // Verify error message
241: assertTrue("there is not error message. ", wr.getText()
242: .indexOf("<li>") != -1);
243:
244: nbMeasures = "-99999";
245:
246: // Modify number of measures
247: wr = modifyNbMeasures(nbMeasures);
248: // Verify no image
249: assertNull("there is the Memory image. ", wr
250: .getImageWithName("memory"));
251: // Verify error message
252: assertTrue("there is not error message. ", wr.getText()
253: .indexOf("<li>") != -1);
254: }
255:
256: /**
257: * Calculate the width of Memory image
258: * @param nbMeasures number of measures
259: * @return width
260: */
261: public int calculateWidth(String nbMeasures) {
262: return Integer.parseInt(nbMeasures) * 2 + CONSTANT;
263: }
264:
265: /**
266: * Modify number of measures
267: * @param nbMeasures number of measures
268: * @return response
269: * @throws Exception if error occurs
270: */
271: public WebResponse modifyNbMeasures(String nbMeasures)
272: throws Exception {
273:
274: // Go to Monitoring Memory
275: WebResponse wr = wc
276: .getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
277: WebForm[] webForms = wr.getForms();
278: WebForm webForm = webForms[0];
279:
280: webForm.setParameter("numberOfMeasures", nbMeasures);
281: return wr = webForm.submit();
282: }
283: }
|