KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > SanityCheck


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import com.gargoylesoftware.htmlunit.html.HtmlForm;
41 import com.gargoylesoftware.htmlunit.html.HtmlPage;
42 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import junit.textui.TestRunner;
46
47 /**
48  * This class runs live tests against popular webservers just to make sure
49  * nothing obvious has broken. Don't run this very often lest we piss off the
50  * web masters.
51  *
52  * @version $Revision: 1.14 $
53  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
55  */

56 public class SanityCheck extends WebTestCase {
57     private static final BrowserVersion BROWSER_VERSION = BrowserVersion.MOZILLA_1_0;
58
59     /**
60      * Create an instance.
61      * @param name The name of the test.
62      */

63     public SanityCheck( final String name ) {
64         super( name );
65     }
66
67
68     /**
69      * Main entry point for testing.
70      * @param args the arguments.
71      * @throws Exception If a problem occurs.
72      */

73     public static void main( final String args[] ) throws Exception {
74         new MainTestSuite("foo").enableAllLogging();
75         TestRunner.run( SanityCheck.class );
76         System.exit( 0 );
77     }
78
79
80     /**
81      * Test against a live server: Yahoo mail
82      * @throws Exception If something goes wrong.
83      */

84     public void testYahooMail() throws Exception {
85         final WebClient webClient = new WebClient(BROWSER_VERSION);
86         assertInstanceOf(webClient.getPage( new URL( "http://mail.yahoo.com/" ) ), HtmlPage.class);
87     }
88
89
90     /**
91      * Test against a live server: Yahoo
92      * @throws Exception If something goes wrong.
93      */

94     public void testYahoo() throws Exception {
95         final WebClient webClient = new WebClient(BROWSER_VERSION);
96         assertInstanceOf( webClient.getPage( new URL( "http://yahoo.com/" ) ), HtmlPage.class );
97     }
98
99     /**
100      * Test against a live server: Yahoo
101      * @throws Exception If something goes wrong.
102      */

103     public void testYahoo_Spanish() throws Exception {
104         final WebClient webClient = new WebClient(BROWSER_VERSION);
105         assertInstanceOf(
106             webClient.getPage( new URL( "http://edit.europe.yahoo.com/config/mail?.intl=es" ) ), HtmlPage.class );
107     }
108
109
110     /**
111      * Test against a live server: IBM
112      * @throws Exception If something goes wrong.
113      */

114     public void testIBM() throws Exception {
115         final WebClient webClient = new WebClient(BROWSER_VERSION);
116         webClient.setRedirectEnabled( true );
117         final HtmlPage page = (HtmlPage)webClient.getPage( new URL( "http://www.ibm.com/" ) );
118         assertEquals( "http://www.ibm.com/us/", page.getWebResponse().getUrl().toExternalForm() );
119     }
120
121
122     /**
123      * Test against a live server: IBM Alphaworks
124      * @throws Exception If something goes wrong.
125      */

126     public void testAlphaWorks() throws Exception {
127         final WebClient webClient = new WebClient(BROWSER_VERSION);
128         assertInstanceOf(webClient.getPage(new URL( "http://www.alphaworks.ibm.com" ) ), HtmlPage.class);
129     }
130
131
132     /**
133      * Test against a live server: CNN
134      * @throws Exception If something goes wrong.
135      */

136     public void testCNN() throws Exception {
137         final WebClient webClient = new WebClient(BROWSER_VERSION);
138         assertInstanceOf( webClient.getPage( new URL( "http://www.cnn.com" ) ), HtmlPage.class);
139     }
140
141
142     /**
143      * Test against a live server: Toyota Canada
144      * @throws Exception If something goes wrong.
145      */

146     public void testToyotaCanada() throws Exception {
147         final WebClient webClient = new WebClient(BROWSER_VERSION);
148         assertInstanceOf(webClient.getPage( new URL( "http://www.toyota.ca" ) ), HtmlPage.class);
149     }
150
151
152     /**
153      * Test against a live server: HtmlUnit page on sourceforge using https
154      * @throws Exception If something goes wrong.
155      */

156     public void testSourceForge_secure() throws Exception {
157         try {
158             final WebClient webClient = new WebClient(BROWSER_VERSION);
159             webClient.setPrintContentOnFailingStatusCode(true);
160             assertInstanceOf(
161                 webClient.getPage( new URL( "https://sourceforge.net/projects/htmlunit/" ) ),
162                 HtmlPage.class );
163         }
164         catch( final MalformedURLException e ) {
165             System.out.println("Skipping https test: "+getName());
166         }
167     }
168
169
170     /**
171      * Test against a live server: Yahoo secure login
172      * @throws Exception If something goes wrong.
173      */

174     public void testYahooLogin_secure() throws Exception {
175         try {
176             final WebClient webClient = new WebClient(BROWSER_VERSION);
177             final HtmlPage page = (HtmlPage)webClient.getPage( new URL( "https://login.yahoo.com/" ) );
178             final HtmlForm form = page.getFormByName("login_form");
179             assertNotNull(form);
180         }
181         catch( final MalformedURLException e ) {
182             System.out.println("Skipping https test: "+getName());
183         }
184     }
185
186
187     /**
188      * Test against a live server: Amazon Canada
189      * @throws Exception If something goes wrong.
190      */

191     public void testAmazonCanada() throws Exception {
192         final WebClient webClient = new WebClient(BROWSER_VERSION);
193         assertInstanceOf( webClient.getPage( new URL( "http://www.amazon.ca/" ) ), HtmlPage.class );
194     }
195
196
197     /**
198      * Test against a live server: CNN After hours
199      * @throws Exception If something goes wrong.
200      */

201     public void testCnnAfterHours() throws Exception {
202         final WebClient webClient = new WebClient(BROWSER_VERSION);
203         assertInstanceOf( webClient.getPage( new URL( "http://money.cnn.com/markets/afterhours/" ) ), HtmlPage.class );
204     }
205
206
207     /**
208      * Test against a live server: htmlunit.sourceforge.net
209      * @throws Exception If something goes wrong.
210      */

211     public void testHtmlUnitHomepage() throws Exception {
212         final WebClient webClient = new WebClient(BROWSER_VERSION);
213         assertInstanceOf( webClient.getPage( new URL( "http://htmlunit.sourceforge.net" ) ), HtmlPage.class );
214     }
215
216
217     /**
218      * Test against a live server: Adobe Acrobat Reader download step 2
219      * @throws Exception If something goes wrong.
220      */

221     public void testAdobeAcrobatReaderDownloadStep2() throws Exception {
222         final WebClient webClient = new WebClient();
223         assertInstanceOf( webClient.getPage( new URL( "http://www.adobe.com/products/acrobat/readstep2.html" ) ),
224                           HtmlPage.class );
225     }
226
227
228     /**
229      * Print out the name of the test that is running.
230      */

231     public void setUp() {
232         System.out.println();
233         System.out.println( "=====================================" );
234         System.out.println( "== Starting test: " + getName() );
235         System.out.println( "=====================================" );
236     }
237
238
239     private URL getPrintEnvUrl() throws MalformedURLException {
240         return new URL("http://htmlunit.sourceforge.net/cgi-bin/printenv");
241     }
242
243
244     /**
245      * Test against htmlunit.sourceforge.net to make sure parameters are being passed
246      * correctly for GET methods
247      * @throws Exception If something goes wrong.
248      */

249     public void testGetMethodWithParameters() throws Exception {
250         final WebClient webClient = new WebClient();
251         final HtmlPage firstPage = (HtmlPage)webClient.getPage(getPrintEnvUrl());
252
253         assertEquals("GET", firstPage.getHtmlElementById("REQUEST_METHOD").asText());
254
255         final HtmlForm form = firstPage.getFormByName("form1");
256         assertEquals("get", form.getMethodAttribute());
257
258         final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button1");
259         final HtmlPage secondPage = (HtmlPage)button.click();
260         assertEquals("GET", secondPage.getHtmlElementById("REQUEST_METHOD").asText());
261         assertEquals("textfield1=*&button1=PushMe",
262             secondPage.getHtmlElementById("QUERY_STRING").asText());
263         assertEquals("", secondPage.getHtmlElementById("CONTENT").asText());
264     }
265
266
267     /**
268      * Test against htmlunit.sourceforge.net to make sure parameters are being passed
269      * correctly for POST methods.
270      * @throws Exception If something goes wrong.
271      */

272     public void testPostMethodWithDuplicateParameters() throws Exception {
273         final WebClient webClient = new WebClient();
274         final HtmlPage firstPage = (HtmlPage)webClient.getPage(getPrintEnvUrl());
275
276         assertEquals("GET", firstPage.getHtmlElementById("REQUEST_METHOD").asText());
277
278         final HtmlForm form = firstPage.getFormByName("form1");
279         form.setMethodAttribute("post");
280
281         final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button1");
282         button.setAttributeValue("name", "textfield1");
283
284         final HtmlPage secondPage = (HtmlPage)button.click();
285         assertEquals("POST", secondPage.getHtmlElementById("REQUEST_METHOD").asText());
286         assertEquals("", secondPage.getHtmlElementById("QUERY_STRING").asText());
287         assertEquals("textfield1=*&textfield1=PushMe",
288             secondPage.getHtmlElementById("CONTENT").asText());
289     }
290
291     /**
292      * Test against htmlunit.sourceforge.net to make sure parameters are being passed
293      * correctly for POST methods.
294      * @throws Exception If something goes wrong.
295      */

296     public void testPostMethodWithParameters() throws Exception {
297         final WebClient webClient = new WebClient();
298         final HtmlPage firstPage = (HtmlPage)webClient.getPage(getPrintEnvUrl());
299
300         assertEquals("GET", firstPage.getHtmlElementById("REQUEST_METHOD").asText());
301
302         final HtmlForm form = firstPage.getFormByName("form1");
303         form.setMethodAttribute("post");
304
305         final HtmlSubmitInput button = (HtmlSubmitInput)form.getInputByName("button1");
306         final HtmlPage secondPage = (HtmlPage)button.click();
307         assertEquals("POST", secondPage.getHtmlElementById("REQUEST_METHOD").asText());
308         assertEquals("", secondPage.getHtmlElementById("QUERY_STRING").asText());
309         assertEquals("textfield1=*&button1=PushMe",
310             secondPage.getHtmlElementById("CONTENT").asText());
311     }
312 }
313
314
Popular Tags