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_JonasAdminRange.java 7381 2005-09-14 16:00:02Z kemlerp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jonasadmin.test.monitoring;
025:
026: import junit.framework.TestSuite;
027:
028: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminAuth;
029: import org.objectweb.jonas.jonasadmin.test.util.JonasAdminTestCase;
030:
031: import com.meterware.httpunit.HttpUnitOptions;
032: import com.meterware.httpunit.WebForm;
033: import com.meterware.httpunit.WebLink;
034: import com.meterware.httpunit.WebResponse;
035:
036: public class F_JonasAdminRange extends JonasAdminTestCase {
037:
038: /**
039: * URL of the Monitoring
040: */
041: private static final String URL_JONASADMIN_MONITORING = "EditMonitoring.do";
042:
043: /**
044: * URL of the Memory
045: */
046: private static final String URL_JONASADMIN_MEMORY = "/jonasAdmin/EditMemory.do";
047:
048: /**
049: * CONSTANT which is used to calculate the timer Graphic
050: */
051: private static final int CONSTANT = 2;
052:
053: /**
054: * INITIAL RANGE VALUE
055: */
056: private static final String INITIAL_RANGE = "10";
057:
058: /**
059: * Constructor with a specified name
060: * @param s name
061: */
062: public F_JonasAdminRange(String s) {
063: super (s, URL_JONASADMIN);
064: }
065:
066: /**
067: * Main method
068: * @param args the arguments
069: */
070: public static void main(String[] args) {
071:
072: String testtorun = null;
073: // Get args
074: for (int argn = 0; argn < args.length; argn++) {
075: String sArg = args[argn];
076: if (sArg.equals("-n")) {
077: testtorun = args[++argn];
078: }
079: }
080: if (testtorun == null) {
081: junit.textui.TestRunner.run(suite());
082: } else {
083: junit.textui.TestRunner
084: .run(new F_JonasAdminRange(testtorun));
085: }
086: }
087:
088: /**
089: * Get a new TestSuite for this class
090: * @return a new TestSuite for this class
091: */
092: public static TestSuite suite() {
093: return new TestSuite(F_JonasAdminRange.class);
094: }
095:
096: /**
097: * Setup need for these tests
098: * jonasAdmin is required
099: * @throws Exception if it fails
100: */
101: protected void setUp() throws Exception {
102: super .setUp();
103:
104: if (wc.getCurrentPage().getURL() == null) {
105: useWar("jonasAdmin");
106: // login to jonas admin
107: try {
108: JonasAdminAuth.doValidAuth(wc, url);
109: } catch (Exception e) {
110: fail("authentification failed : " + e);
111: }
112: } else {
113: // if there was an error, the connection must be restablished
114: try {
115: wc.getFrameContents(FRAME_TREE);
116: } catch (Exception e) {
117: wc.getResponse(urlLogOut);
118: // login to jonas admin
119: try {
120: JonasAdminAuth.doValidAuth(wc, url);
121: } catch (Exception auth) {
122: fail("authentification failed : " + auth);
123: }
124: }
125: }
126: }
127:
128: /**
129: * Test monitoring range with a correct value
130: * @throws Exception if error occurs
131: *
132: */
133: public void testSuccessful() throws Exception {
134:
135: String range = "22";
136: WebResponse wr;
137:
138: // Disable errors of javascript
139: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
140: // Disable exception thrown on error status
141: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
142:
143: // Modify number of measures
144: wr = modifyNbMeasures(range);
145:
146: // Verify range
147: String text = wr.getText();
148: int start = text.indexOf("function refresh()");
149: int end = text.indexOf("}", start);
150: // Get script
151: String refreshScript = text.substring(start, end);
152:
153: // Get value
154: start = refreshScript.indexOf("setTimeout(\"refresh()\", ")
155: + "setTimeout(\"refresh()\", ".length();
156: end = refreshScript.indexOf(");", start);
157: String value = refreshScript.substring(start, end);
158:
159: // Verify refresh value in javascript function
160: assertEquals("the range of the Memory image is wrong. ", ""
161: + calculateTimerGraphic(range), value);
162:
163: // Verify error message
164: assertFalse("there is an error message. ", wr
165: .getElementsWithAttribute("class", "errors").length > 0);
166: }
167:
168: /**
169: * Test monitoring range with too small value
170: * @throws Exception if error occurs
171: *
172: */
173: public void testTooSmallValue() throws Exception {
174:
175: String range = "9";
176: WebResponse wr;
177:
178: // Disable errors of javascript
179: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
180: // Disable exception thrown on error status
181: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
182:
183: // Modify number of measures
184: wr = modifyNbMeasures(range);
185:
186: // Verify error message
187: assertTrue("there is not error message. ", wr
188: .getElementsWithAttribute("class", "errors").length > 0);
189: }
190:
191: /**
192: * Test the memory servlet
193: * @throws Exception if error occurs
194: *
195: */
196: public void testMemoryServlet() throws Exception {
197: WebResponse wr;
198: WebLink link;
199:
200: // Disable errors of javascript
201: HttpUnitOptions.setExceptionsThrownOnScriptError(false);
202: // Disable exception thrown on error status
203: HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
204:
205: // Go to Monitoring
206: wr = wc.getFrameContents(FRAME_TREE);
207: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
208: URL_JONASADMIN_MONITORING);
209: link.click();
210: wr = wc.getFrameContents(FRAME_CONTENT);
211:
212: // Go to Memory
213: link = wr.getFirstMatchingLink(WebLink.MATCH_URL_STRING,
214: URL_JONASADMIN_MEMORY);
215: link.click();
216: wr = wc.getFrameContents(FRAME_CONTENT);
217:
218: // Refresh the image
219: String servletUrl = prefixUrl
220: + "/jonasAdmin/MemoryGraphServlet?1";
221: WebResponse imgResponse = wc.getResponse(servletUrl);
222:
223: // Verify image
224: assertEquals("'" + servletUrl + "' doesn't send a png image. ",
225: "image/png", imgResponse.getHeaderField("CONTENT-TYPE"));
226:
227: }
228:
229: /**
230: * Calculate the timer graphic
231: * @param range range
232: * @return timer graphic
233: */
234: public int calculateTimerGraphic(String range) {
235: return (Integer.parseInt(range) + CONSTANT) * 1000;
236: }
237:
238: /**
239: * Modify range
240: * @param range range
241: * @return response
242: * @throws Exception if error occurs
243: */
244: public WebResponse modifyNbMeasures(String range) throws Exception {
245:
246: // Go to Monitoring Memory
247: WebResponse wr = wc
248: .getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
249:
250: WebForm[] webForms = wr.getForms();
251: WebForm webForm = webForms[0];
252:
253: webForm.setParameter("range", range);
254: return wr = webForm.submit();
255: }
256:
257: /**
258: * Tear Down
259: * cleanUp action
260: */
261: public void tearDown() throws Exception {
262: super .tearDown();
263: WebResponse wr = wc
264: .getResponse(getAbsoluteUrl(URL_JONASADMIN_MEMORY));
265: WebForm[] webForms = wr.getForms();
266: WebForm webForm = webForms[0];
267:
268: webForm.setParameter("range", INITIAL_RANGE);
269: wr = webForm.submit();
270: }
271: }
|