2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.usecases.systest;
19  
20  import com.gargoylesoftware.htmlunit.ElementNotFoundException;
21  import com.gargoylesoftware.htmlunit.WebClient;
22  import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
23  import com.gargoylesoftware.htmlunit.html.HtmlBody;
24  import com.gargoylesoftware.htmlunit.html.HtmlElement;
25  import com.gargoylesoftware.htmlunit.html.HtmlForm;
26  import com.gargoylesoftware.htmlunit.html.HtmlHead;
27  import com.gargoylesoftware.htmlunit.html.HtmlPage;
28  import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
29  
30  import java.io.IOException;
31  import java.net.URL;
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  import junit.framework.Test;
37  import junit.framework.TestCase;
38  import junit.framework.TestSuite;
39  
40  
41  /***
42   * <p>Abstract base class for system integration tests based on HtmlUnit.</p>
43   */
44  
45  public abstract class AbstractTestCase extends TestCase {
46  
47  
48      // ------------------------------------------------------------ Constructors
49  
50  
51      /***
52       * <p>Construct a new instance of this test case.</p>
53       *
54       * @param name Name of the new test case
55       */
56      public AbstractTestCase(String name) {
57  
58          super(name);
59  
60      }
61  
62  
63      // ------------------------------------------------------ Instance Variables
64  
65  
66      /***
67       * <p>The most recently retrieved page from the server.</p>
68       */
69      protected HtmlPage page = null;
70  
71  
72      /***
73       * <p>The calculated URL for the installed "systest" web application.
74       * This value is based on a system property named <code>url</code>,
75       * which must be defined as part of the command line that executes
76       * each test case.</p>
77       */
78      protected URL url = null;
79  
80  
81      /***
82       * <p>The web client for this test case.</p>
83       */
84      protected WebClient webClient = null;
85  
86  
87      // ------------------------------------------------------ Test Setup Methods
88  
89  
90      /***
91       * <p>Set up the instance variables required for this test case.</p>
92       */
93      protected void setUp() throws Exception {
94  
95          // Calculate the URL for the installed "systest" web application
96          String url = System.getProperty("url");
97          this.url = new URL(url + "/");
98  
99          // Initialize HtmlUnit constructs for this test case
100         webClient = new WebClient();
101 
102     }
103 
104 
105     /***
106      * <p>Return the set of tests included in this test suite.</p>
107      */
108     public static Test suite() {
109 
110         return (new TestSuite(AbstractTestCase.class));
111 
112     }
113 
114 
115     /***
116      * <p>Tear down instance variables required by this test case.</p>
117      */
118     protected void tearDown() throws Exception {
119 
120         page = null;
121         // sessionId = null;
122         url = null;
123         webClient = null;
124 
125     }
126 
127 
128 
129     // ------------------------------------------------------- Protected Methods
130 
131 
132     /***
133      * <p>Return the body element for the most recently retrieved page.
134      * If there is no such element, return <code>null</code>.</p>
135      */
136     protected HtmlBody body() throws Exception {
137 
138         Iterator elements = page.getAllHtmlChildElements();
139         while (elements.hasNext()) {
140             HtmlElement element = (HtmlElement) elements.next();
141             if (element instanceof HtmlBody) {
142                 return ((HtmlBody) element);
143             }
144         }
145         return (null);
146 
147     }
148 
149 
150     /***
151      * <p>Return the HTML element with the specified <code>id</code> from the
152      * most recently retrieved page.  If there is no such element, return
153      * <code>null</code>.</p>
154      *
155      * @param id Identifier of the requested element.
156      */
157     protected HtmlElement element(String id) throws Exception {
158 
159         try {
160             return (page.getHtmlElementById(id));
161         } catch (ElementNotFoundException e) {
162             return (null);
163         }
164 
165     }
166 
167 
168     /***
169      * <p>Return the form with the specified <code>id</code> from the most
170      * recently retrieved page.  If there is no such form, return
171      * <code>null</code>.<p>
172      *
173      * @param id Identifier of the requested form.
174      */
175     protected HtmlForm form(String id) throws Exception {
176 
177         Iterator forms = page.getForms().iterator();
178         while (forms.hasNext()) {
179             HtmlForm form = (HtmlForm) forms.next();
180             if (id.equals(form.getAttributeValue("id"))) {
181                 return (form);
182             }
183         }
184         return (null);
185 
186     }
187 
188 
189     /***
190      * <p>Return the head element for the most recently retrieved page.
191      * If there is no such element, return <code>null</code>.</p>
192      */
193     protected HtmlHead head() throws Exception {
194 
195         Iterator elements = page.getAllHtmlChildElements();
196         while (elements.hasNext()) {
197             HtmlElement element = (HtmlElement) elements.next();
198             if (element instanceof HtmlHead) {
199                 return ((HtmlHead) element);
200             }
201         }
202         return (null);
203 
204     }
205 
206 
207     /***
208      * <p>Click the specified hyperlink, and retrieve the subsequent page,
209      * saving a reference so that other utility methods may be used to
210      * retrieve information from it.</p>
211      *
212      * @param anchor Anchor component to click
213      *
214      * @exception IOException if an input/output error occurs
215      */
216     protected HtmlPage link(HtmlAnchor anchor) throws IOException {
217 
218         HtmlPage page = (HtmlPage) anchor.click();
219         this.page = page;
220         return page;
221 
222     }
223 
224 
225     /***
226      * <p>Retrieve and return the page at the specified context relative path.
227      * Save a reference to this page so that other utility methods may be used
228      * to retrieve information from it.</p>
229      *
230      * @param path Context relative path
231      *
232      * @exception IllegalArgumentException if the context relative path
233      *  does not begin with a '/' character
234      */
235     protected HtmlPage page(String path) throws Exception {
236 
237         HtmlPage page = (HtmlPage) webClient.getPage(url(path));
238         /*
239         if (sessionId == null) {
240             saveSessionId(page);
241         }
242         */
243         this.page = page;
244         return (page);
245 
246     }
247 
248 
249     /***
250      * <p>Submit the current page, using the specified component, and retrieve
251      * the subsequent page, saving a reference so that other utility methods
252      * may be used to retrieve information from it.</p>
253      *
254      * @param submit Submit button component to click
255      *
256      * @exception IOException if an input/output error occurs
257      */
258     protected HtmlPage submit(HtmlSubmitInput submit) throws IOException {
259 
260         HtmlPage page = (HtmlPage) submit.click();
261         this.page = page;
262         return page;
263 
264     }
265 
266 
267     /***
268      * <p>Return the page title from the most recently retrieved page.
269      * Any leading and trailing whitespace will be trimmed.</p>
270      */
271     protected String title() throws Exception {
272 
273         return (page.getTitleText().trim());
274 
275     }
276 
277 
278     /***
279      * <p>Calculate and return an absolute URL for the specified context
280      * relative path, which must begin with a '/' character.</p>
281      *
282      * @param path Context relative path
283      *
284      * @exception IllegalArgumentException if the context relative path
285      *  does not begin with a '/' character
286      */
287     protected URL url(String path) throws Exception {
288 
289         if (path.charAt(0) != '/') {
290             throw new IllegalArgumentException("Context path '" + path +
291                                                "' does not start with '/'");
292         }
293         return new URL(url, path.substring(1));
294 
295     }
296 
297 
298 }