KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > faces > sysclient > AbstractTestCase


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.faces.sysclient;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.httpclient.HttpState;
28
29 import com.gargoylesoftware.htmlunit.ElementNotFoundException;
30 import com.gargoylesoftware.htmlunit.WebClient;
31 import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
32 import com.gargoylesoftware.htmlunit.html.HtmlBody;
33 import com.gargoylesoftware.htmlunit.html.HtmlElement;
34 import com.gargoylesoftware.htmlunit.html.HtmlForm;
35 import com.gargoylesoftware.htmlunit.html.HtmlHead;
36 import com.gargoylesoftware.htmlunit.html.HtmlPage;
37 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
38
39
40
41 /**
42  * <p>Abstract base class for system integration tests based on HtmlUnit.</p>
43  *
44  * @version $Rev: 165410 $ $Date: 2005-04-30 17:25:14 +0100 (Sat, 30 Apr 2005) $
45  */

46
47 public abstract class AbstractTestCase extends TestCase {
48
49
50     // ------------------------------------------------------------ Constructors
51

52
53     /**
54      * <p>Construct a new instance of this test case.</p>
55      *
56      * @param name Name of the new test case
57      */

58     public AbstractTestCase(String JavaDoc name) {
59
60         super(name);
61
62     }
63
64
65     // ------------------------------------------------------ Instance Variables
66

67
68     /**
69      * <p>The HTTP state information for this test case.</p>
70      */

71     protected HttpState httpState = null;
72
73
74     /**
75      * <p>The most recently retrieved page from the server.</p>
76      */

77     protected HtmlPage page = null;
78
79
80     /**
81      * <p>The calculated URL for the installed "systest" web application.
82      * This value is based on a system property named <code>systest</code>,
83      * which must be defined as part of the command line that executes
84      * each test case.</p>
85      */

86     protected URL JavaDoc url = null;
87
88
89     /**
90      * <p>The web client for this test case.</p>
91      */

92     protected WebClient webClient = null;
93
94
95     // ------------------------------------------------------ Test Setup Methods
96

97
98     /**
99      * <p>Set up the instance variables required for this test case.</p>
100      */

101     public void setUp() throws Exception JavaDoc {
102
103         // Calculate the URL for the installed "systest" web application
104
String JavaDoc systest = System.getProperty("systest");
105         url = new URL JavaDoc(systest + "/");
106
107         // Initialize HtmlUnit constructs for this test case
108
webClient = new WebClient();
109         httpState = webClient.getWebConnection().getStateForUrl(url("/"));
110
111     }
112
113
114     /**
115      * <p>Return the set of tests included in this test suite.</p>
116      */

117     public static Test suite() {
118
119         return (new TestSuite(AbstractTestCase.class));
120
121     }
122
123
124     /**
125      * <p>Tear down instance variables required by this test case.</p>
126      */

127     public void tearDown() {
128
129         httpState = null;
130         page = null;
131         // sessionId = null;
132
url = null;
133         webClient = null;
134
135     }
136
137
138
139     // ------------------------------------------------------- Protected Methods
140

141
142     /**
143      * <p>Return the body element for the most recently retrieved page.
144      * If there is no such element, return <code>null</code>.</p>
145      */

146     protected HtmlBody body() throws Exception JavaDoc {
147
148         Iterator JavaDoc elements = page.getAllHtmlChildElements();
149         while (elements.hasNext()) {
150             HtmlElement element = (HtmlElement) elements.next();
151             if (element instanceof HtmlBody) {
152                 return ((HtmlBody) element);
153             }
154         }
155         return (null);
156
157     }
158
159
160     /**
161      * <p>Return the HTML element with the specified <code>id</code> from the
162      * most recently retrieved page. If there is no such element, return
163      * <code>null</code>.</p>
164      *
165      * @param id Identifier of the requested element.
166      */

167     protected HtmlElement element(String JavaDoc id) throws Exception JavaDoc {
168
169         try {
170             return (page.getHtmlElementById(id));
171         } catch (ElementNotFoundException e) {
172             return (null);
173         }
174
175     }
176
177
178     /**
179      * <p>Return the form with the specified <code>id</code> from the most
180      * recently retrieved page. If there is no such form, return
181      * <code>null</code>.<p>
182      *
183      * @param id Identifier of the requested form.
184      */

185     protected HtmlForm form(String JavaDoc id) throws Exception JavaDoc {
186
187         Iterator JavaDoc forms = page.getAllForms().iterator();
188         while (forms.hasNext()) {
189             HtmlForm form = (HtmlForm) forms.next();
190             if (id.equals(form.getAttributeValue("id"))) {
191                 return (form);
192             }
193         }
194         return (null);
195
196     }
197
198
199     /**
200      * <p>Return the head element for the most recently retrieved page.
201      * If there is no such element, return <code>null</code>.</p>
202      */

203     protected HtmlHead head() throws Exception JavaDoc {
204
205         Iterator JavaDoc elements = page.getAllHtmlChildElements();
206         while (elements.hasNext()) {
207             HtmlElement element = (HtmlElement) elements.next();
208             if (element instanceof HtmlHead) {
209                 return ((HtmlHead) element);
210             }
211         }
212         return (null);
213
214     }
215
216
217     /**
218      * <p>Click the specified hyperlink, and retrieve the subsequent page,
219      * saving a reference so that other utility methods may be used to
220      * retrieve information from it.</p>
221      *
222      * @param anchor Anchor component to click
223      *
224      * @exception IOException if an input/output error occurs
225      */

226     protected HtmlPage link(HtmlAnchor anchor) throws IOException JavaDoc {
227
228         HtmlPage page = (HtmlPage) anchor.click();
229         this.page = page;
230         return page;
231
232     }
233
234
235     /**
236      * <p>Retrieve and return the page at the specified context relative path.
237      * Save a reference to this page so that other utility methods may be used
238      * to retrieve information from it.</p>
239      *
240      * @param path Context relative path
241      *
242      * @exception IllegalArgumentException if the context relative path
243      * does not begin with a '/' character
244      */

245     protected HtmlPage page(String JavaDoc path) throws Exception JavaDoc {
246
247         HtmlPage page = (HtmlPage) webClient.getPage(url(path));
248         /*
249         if (sessionId == null) {
250             saveSessionId(page);
251         }
252         */

253         this.page = page;
254         return (page);
255
256     }
257
258
259     /**
260      * <p>Submit the current page, using the specified component, and retrieve
261      * the subsequent page, saving a reference so that other utility methods
262      * may be used to retrieve information from it.</p>
263      *
264      * @param submit Submit button component to click
265      *
266      * @exception IOException if an input/output error occurs
267      */

268     protected HtmlPage submit(HtmlSubmitInput submit) throws IOException JavaDoc {
269
270         HtmlPage page = (HtmlPage) submit.click();
271         this.page = page;
272         return page;
273
274     }
275
276
277     /**
278      * <p>Return the page title from the most recently retrieved page.
279      * Any leading and trailing whitespace will be trimmed.</p>
280      */

281     protected String JavaDoc title() throws Exception JavaDoc {
282
283         return (page.getTitleText().trim());
284
285     }
286
287
288     /**
289      * <p>Calculate and return an absolute URL for the specified context
290      * relative path, which must begin with a '/' character.</p>
291      *
292      * @param path Context relative path
293      *
294      * @exception IllegalArgumentException if the context relative path
295      * does not begin with a '/' character
296      */

297     protected URL JavaDoc url(String JavaDoc path) throws Exception JavaDoc {
298
299         if (path.charAt(0) != '/') {
300             throw new IllegalArgumentException JavaDoc("Context path '" + path +
301                                                "' does not start with '/'");
302         }
303         return new URL JavaDoc(url, path.substring(1));
304
305     }
306
307
308 }
309
Popular Tags