KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > xml > XmlPageTest


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.xml;
39
40 import com.gargoylesoftware.htmlunit.MockWebConnection;
41 import com.gargoylesoftware.htmlunit.Page;
42 import com.gargoylesoftware.htmlunit.WebClient;
43 import com.gargoylesoftware.htmlunit.WebTestCase;
44
45 /**
46  * Tests for SimpleScriptable.
47  *
48  * @version $Revision: 1.3 $
49  * @author Marc Guillemot
50  */

51 public class XmlPageTest extends WebTestCase {
52     /**
53      * Create an instance
54      * @param name The name of the test
55      */

56     public XmlPageTest( final String name ) {
57         super(name);
58     }
59
60
61     /**
62      * Tests a simple valid xml document
63      * @throws Exception if the test fails
64      */

65     public void testValidDocument() throws Exception {
66         final WebClient client = new WebClient();
67         final MockWebConnection webConnection = new MockWebConnection( client );
68
69         final String content
70             = "<?xml version=\"1.0\"?>"
71              + "<foo>\n"
72              + " <foofoo name='first'>something</foofoo>\n"
73              + " <foofoo name='second'>something else</foofoo>\n"
74              + "</foo>";
75
76         webConnection.setDefaultResponse(content, 200, "OK", "text/xml");
77         client.setWebConnection( webConnection );
78
79         final Page page = client.getPage(URL_FIRST);
80         assertEquals(URL_FIRST.toExternalForm(), page.getWebResponse().getUrl().toExternalForm());
81         assertEquals("OK", page.getWebResponse().getStatusMessage());
82         assertEquals(200, page.getWebResponse().getStatusCode());
83         assertEquals("text/xml", page.getWebResponse().getContentType());
84
85         assertInstanceOf(page, XmlPage.class);
86         final XmlPage xmlPage = (XmlPage) page;
87         assertEquals(content, xmlPage.getContent());
88         assertNotNull(xmlPage.getXmlDocument());
89         assertEquals("foo", xmlPage.getXmlDocument().getFirstChild().getNodeName());
90     }
91
92
93     /**
94      * Tests a simple invalid (badly formed) xml document
95      * @throws Exception if the test fails
96      */

97     public void testInvalidDocument() throws Exception {
98         final WebClient client = new WebClient();
99         final MockWebConnection webConnection = new MockWebConnection( client );
100
101         final String content
102             = "<?xml version=\"1.0\"?>"
103             + "<foo>\n"
104             + " <foofoo invalid\n"
105             + " <foofoo name='first'>something</foofoo>\n"
106             + " <foofoo name='second'>something else</foofoo>\n"
107             + "</foo>";
108
109         webConnection.setDefaultResponse(content, 200, "OK", "text/xml");
110         client.setWebConnection( webConnection );
111
112         final Page page = client.getPage(URL_FIRST);
113         assertEquals(URL_FIRST.toExternalForm(), page.getWebResponse().getUrl().toExternalForm());
114         assertEquals("OK", page.getWebResponse().getStatusMessage());
115         assertEquals(200, page.getWebResponse().getStatusCode());
116         assertEquals("text/xml", page.getWebResponse().getContentType());
117
118         assertInstanceOf(page, XmlPage.class);
119         final XmlPage xmlPage = (XmlPage) page;
120         assertEquals(content, xmlPage.getContent());
121         assertNull(xmlPage.getXmlDocument());
122     }
123 }
124
Popular Tags