001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.xml;
039:
040: import java.net.URL;
041: import java.util.ArrayList;
042: import java.util.List;
043:
044: import org.apache.commons.httpclient.HttpStatus;
045: import org.w3c.dom.Node;
046:
047: import com.gargoylesoftware.htmlunit.BrowserVersion;
048: import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
049: import com.gargoylesoftware.htmlunit.MockWebConnection;
050: import com.gargoylesoftware.htmlunit.Page;
051: import com.gargoylesoftware.htmlunit.WebClient;
052: import com.gargoylesoftware.htmlunit.WebTestCase;
053:
054: /**
055: * Tests for {@link XmlPage}.
056: *
057: * @version $Revision: 2132 $
058: * @author Marc Guillemot
059: * @author Ahmed Ashour
060: */
061: public class XmlPageTest extends WebTestCase {
062: /**
063: * Create an instance
064: * @param name The name of the test
065: */
066: public XmlPageTest(final String name) {
067: super (name);
068: }
069:
070: /**
071: * Tests namespace
072: * @throws Exception if the test fails
073: */
074: public void testNamespace() throws Exception {
075: final String content = "<?xml version='1.0'?>\n"
076: + "<RDF xmlns='http://www.w3.org/1999/02/22-rdf-syntax-ns#' "
077: + "xmlns:em='http://www.mozilla.org/2004/em-rdf#'>"
078: + "<Description about='urn:mozilla:install-manifest'>"
079: + "<em:name>My Plugin</em:name>" + "</Description>\n"
080: + "</RDF>";
081:
082: final XmlPage xmlPage = testXmlDocument(content, "text/xml");
083: final Node node = xmlPage.getXmlDocument().getFirstChild()
084: .getFirstChild().getFirstChild();
085: assertEquals("em:name", node.getNodeName());
086: assertEquals("name", node.getLocalName());
087: assertEquals("http://www.mozilla.org/2004/em-rdf#", node
088: .getNamespaceURI());
089: }
090:
091: /**
092: * Tests a simple valid xml document
093: * @throws Exception if the test fails
094: */
095: public void testValidDocument() throws Exception {
096: final String content = "<?xml version=\"1.0\"?>\n" + "<foo>\n"
097: + " <foofoo name='first'>something</foofoo>\n"
098: + " <foofoo name='second'>something else</foofoo>\n"
099: + "</foo>";
100:
101: final XmlPage xmlPage = testXmlDocument(content, "text/xml");
102: assertEquals("foo", xmlPage.getXmlDocument().getFirstChild()
103: .getNodeName());
104: }
105:
106: /**
107: * Utility method to test xml page of different mime types
108: * @param content the xml content
109: * @param mimeType The mime type
110: * @return the page returned by the WebClient
111: * @throws Exception if a problem occurs
112: */
113: private XmlPage testXmlDocument(final String content,
114: final String mimeType) throws Exception {
115: final WebClient client = new WebClient();
116: final MockWebConnection webConnection = new MockWebConnection(
117: client);
118: webConnection.setDefaultResponse(content, 200, "OK", mimeType);
119: client.setWebConnection(webConnection);
120: final Page page = client.getPage(URL_FIRST);
121: assertEquals(URL_FIRST, page.getWebResponse().getUrl());
122: assertEquals("OK", page.getWebResponse().getStatusMessage());
123: assertEquals(HttpStatus.SC_OK, page.getWebResponse()
124: .getStatusCode());
125: assertEquals(mimeType, page.getWebResponse().getContentType());
126: assertInstanceOf(page, XmlPage.class);
127: final XmlPage xmlPage = (XmlPage) page;
128: assertEquals(content, xmlPage.getContent());
129: assertNotNull(xmlPage.getXmlDocument());
130: return xmlPage;
131: }
132:
133: /**
134: * Tests a simple invalid (badly formed) xml document
135: * @throws Exception if the test fails
136: */
137: public void testInvalidDocument() throws Exception {
138: final WebClient client = new WebClient();
139: final MockWebConnection webConnection = new MockWebConnection(
140: client);
141:
142: final String content = "<?xml version=\"1.0\"?>\n" + "<foo>\n"
143: + " <foofoo invalid\n"
144: + " <foofoo name='first'>something</foofoo>\n"
145: + " <foofoo name='second'>something else</foofoo>\n"
146: + "</foo>";
147:
148: webConnection
149: .setDefaultResponse(content, 200, "OK", "text/xml");
150: client.setWebConnection(webConnection);
151:
152: final Page page = client.getPage(URL_FIRST);
153: assertEquals(URL_FIRST, page.getWebResponse().getUrl());
154: assertEquals("OK", page.getWebResponse().getStatusMessage());
155: assertEquals(HttpStatus.SC_OK, page.getWebResponse()
156: .getStatusCode());
157: assertEquals("text/xml", page.getWebResponse().getContentType());
158:
159: assertInstanceOf(page, XmlPage.class);
160: final XmlPage xmlPage = (XmlPage) page;
161: assertEquals(content, xmlPage.getContent());
162: assertNull(xmlPage.getXmlDocument());
163: }
164:
165: /**
166: * @throws Exception if the test fails
167: */
168: public void testVoiceXML() throws Exception {
169: final String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
170: + "<vxml xmlns=\"http://www.w3.org/2001/vxml\""
171: + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
172: + " xsi:schemaLocation=\"http://www.w3.org/2001/vxml "
173: + " http://www.w3.org/TR/voicexml20/vxml.xsd\""
174: + " version=\"2.0\">\n"
175: + " <form>\n"
176: + " <block>Hello World!</block>\n"
177: + " </form>\n"
178: + "</vxml>";
179:
180: final XmlPage xmlPage = testXmlDocument(content,
181: "application/voicexml+xml");
182: assertEquals("vxml", xmlPage.getXmlDocument().getFirstChild()
183: .getNodeName());
184: }
185:
186: /**
187: * @throws Exception if the test fails
188: */
189: public void testLoad_XMLComment() throws Exception {
190: testLoad_XMLComment(BrowserVersion.INTERNET_EXPLORER_7_0,
191: new String[] { "true", "8" });
192: testLoad_XMLComment(BrowserVersion.FIREFOX_2, new String[] {
193: "true", "8" });
194: }
195:
196: private void testLoad_XMLComment(
197: final BrowserVersion browserVersion,
198: final String[] expectedAlerts) throws Exception {
199: final URL firstURL = new URL("http://htmlunit/first.html");
200: final URL secondURL = new URL("http://htmlunit/second.xml");
201:
202: final String html = "<html><head><title>foo</title><script>\n"
203: + " function test() {\n"
204: + " var doc = createXmlDocument();\n"
205: + " doc.async = false;\n"
206: + " alert(doc.load('"
207: + "second.xml"
208: + "'));\n"
209: + " alert(doc.documentElement.childNodes[0].nodeType);\n"
210: + " }\n"
211: + " function createXmlDocument() {\n"
212: + " if (document.implementation && document.implementation.createDocument)\n"
213: + " return document.implementation.createDocument('', '', null);\n"
214: + " else if (window.ActiveXObject)\n"
215: + " return new ActiveXObject('Microsoft.XMLDOM');\n"
216: + " }\n" + "</script></head><body onload='test()'>\n"
217: + "</body></html>";
218:
219: final String xml = "<test><!-- --></test>";
220:
221: final List collectedAlerts = new ArrayList();
222: final WebClient client = new WebClient(browserVersion);
223: client.setAlertHandler(new CollectingAlertHandler(
224: collectedAlerts));
225: final MockWebConnection conn = new MockWebConnection(client);
226: conn.setResponse(firstURL, html);
227: conn.setResponse(secondURL, xml, "text/xml");
228: client.setWebConnection(conn);
229:
230: client.getPage(firstURL);
231: assertEquals(expectedAlerts, collectedAlerts);
232: }
233:
234: /**
235: * @throws Exception if the test fails
236: */
237: public void testCreateElement() throws Exception {
238: testCreateElement(BrowserVersion.INTERNET_EXPLORER_7_0,
239: new String[] { "true", "16" });
240: testCreateElement(BrowserVersion.FIREFOX_2, new String[] {
241: "true", "14" });
242: }
243:
244: private void testCreateElement(final BrowserVersion browserVersion,
245: final String[] expectedAlerts) throws Exception {
246: final String content = "<html><head><title>foo</title><script>\n"
247: + " function test() {\n"
248: + " var doc = createXmlDocument();\n"
249: + " doc.appendChild(doc.createElement('elementName'));\n"
250: + " var xml;\n"
251: + " if (window.ActiveXObject)\n"
252: + " xml = doc.xml;\n"
253: + " else\n"
254: + " xml = new XMLSerializer().serializeToString(doc.documentElement);\n"
255: + " alert(xml.indexOf('<elementName/>') != -1);\n"
256: + " alert(xml.length);"
257: + " }\n"
258: + " function createXmlDocument() {\n"
259: + " if (document.implementation && document.implementation.createDocument)\n"
260: + " return document.implementation.createDocument('', '', null);\n"
261: + " else if (window.ActiveXObject)\n"
262: + " return new ActiveXObject('Microsoft.XMLDOM');\n"
263: + " }\n"
264: + "</script></head><body onload='test()'>\n"
265: + "</body></html>";
266:
267: final List collectedAlerts = new ArrayList();
268: loadPage(browserVersion, content, collectedAlerts);
269: assertEquals(expectedAlerts, collectedAlerts);
270: }
271: }
|