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.libraries;
039:
040: import java.io.StringReader;
041: import java.util.List;
042:
043: import javax.xml.parsers.DocumentBuilderFactory;
044: import javax.xml.transform.Source;
045: import javax.xml.transform.Transformer;
046: import javax.xml.transform.TransformerFactory;
047: import javax.xml.transform.dom.DOMResult;
048: import javax.xml.transform.stream.StreamSource;
049:
050: import org.mortbay.jetty.Server;
051: import org.w3c.dom.Document;
052: import org.w3c.dom.Element;
053:
054: import com.gargoylesoftware.htmlunit.BrowserVersion;
055: import com.gargoylesoftware.htmlunit.HttpWebConnectionTest;
056: import com.gargoylesoftware.htmlunit.WebClient;
057: import com.gargoylesoftware.htmlunit.WebTestCase;
058: import com.gargoylesoftware.htmlunit.html.HtmlButton;
059: import com.gargoylesoftware.htmlunit.html.HtmlDivision;
060: import com.gargoylesoftware.htmlunit.html.HtmlPage;
061:
062: /**
063: * Tests for 0.9.9 version of <a href="http://sarissa.sourceforge.net">Sarissa</a>.
064: *
065: * @version $Revision: 2171 $
066: * @author Ahmed Ashour
067: */
068: public class Sarissa099Test extends WebTestCase {
069:
070: private Server server_;
071:
072: /**
073: * Creates an instance.
074: *
075: * @param name The name of the test.
076: */
077: public Sarissa099Test(final String name) {
078: super (name);
079: }
080:
081: /**
082: * Returns the Sarissa version being tested.
083: *
084: * @return the Sarissa version being tested.
085: */
086: protected String getVersion() {
087: return "0.9.9";
088: }
089:
090: /**
091: * @throws Exception If an error occurs.
092: */
093: public void testSarissa() throws Exception {
094: test("SarissaTestCase");
095: }
096:
097: /**
098: * @throws Exception If an error occurs.
099: */
100: public void testXmlHttpRequest() throws Exception {
101: test("XmlHttpRequestTestCase");
102: }
103:
104: /**
105: * @throws Exception If an error occurs.
106: */
107: public void testXMLSerializer() throws Exception {
108: test("XMLSerializerTestCase");
109: }
110:
111: /**
112: * @throws Exception If an error occurs.
113: */
114: public void testDOMParser() throws Exception {
115: test("DOMParserTestCase");
116: }
117:
118: /**
119: * @throws Exception If an error occurs.
120: */
121: public void testXMLDocument() throws Exception {
122: test("XMLDocumentTestCase");
123: }
124:
125: /**
126: * @throws Exception If an error occurs.
127: */
128: public void testXMLElement() throws Exception {
129: test("XMLElementTestCase");
130: }
131:
132: /**
133: * Fails because of XSL, Jaxen and Maven 2, see {@link #testXSLTWithJaxen()}.
134: * @throws Exception If an error occurs.
135: */
136: public void testXSLTProcessor() throws Exception {
137: if (notYetImplemented()) {
138: return;
139: }
140: test("XSLTProcessorTestCase");
141: }
142:
143: private void test(final String testName) throws Exception {
144: server_ = HttpWebConnectionTest
145: .startWebServer("src/test/resources/sarissa/"
146: + getVersion());
147: test(BrowserVersion.INTERNET_EXPLORER_7_0, testName);
148: test(BrowserVersion.FIREFOX_2, testName);
149: }
150:
151: private void test(final BrowserVersion browserVersion,
152: final String testName) throws Exception {
153: final WebClient client = new WebClient(browserVersion);
154:
155: final String url = "http://localhost:"
156: + HttpWebConnectionTest.PORT + "/test/testsarissa.html";
157: final HtmlPage page = (HtmlPage) client.getPage(url);
158: final HtmlButton button = (HtmlButton) page
159: .getFirstByXPath("//button");
160: button.click();
161:
162: final List divList = page
163: .getByXPath("//div[@class='placeholder']/a[@name='#"
164: + testName + "']/../div[last()]");
165: assertEquals(1, divList.size());
166: final HtmlDivision div = (HtmlDivision) divList.get(0);
167: assertEquals("OK!", div.asText());
168: }
169:
170: /**
171: * {@inheritDoc}
172: */
173: protected void tearDown() throws Exception {
174: super .tearDown();
175: HttpWebConnectionTest.stopWebServer(server_);
176: server_ = null;
177: }
178:
179: /**
180: * @throws Exception If the test fails.
181: */
182: public void testXSLTWithJaxen() throws Exception {
183: final String input = "<root><element attribute=\"value\"/></root>";
184: final String style = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
185: + "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\"/>"
186: + "<xsl:param select=\"'anonymous'\" name=\"user\"/>"
187: + "<xsl:template match=\"/\">"
188: + "<p id=\"user\">User: <xsl:value-of select=\"$user\"/>"
189: + "</p>"
190: + "<xsl:apply-templates/>"
191: + "<hr/>"
192: + "</xsl:template>"
193: + "<xsl:template match=\"greeting\">"
194: + "<p>"
195: + "<xsl:apply-templates/>"
196: + "</p>"
197: + "</xsl:template>"
198: + "</xsl:stylesheet>";
199:
200: final Source xmlSource = new StreamSource(new StringReader(
201: input));
202: final Source xsltSource = new StreamSource(new StringReader(
203: style));
204:
205: final Document containerDocument = DocumentBuilderFactory
206: .newInstance().newDocumentBuilder().newDocument();
207: final Element containerElement = containerDocument
208: .createElement("container");
209: containerDocument.appendChild(containerElement);
210:
211: final DOMResult result = new DOMResult(containerElement);
212:
213: final Transformer transformer = TransformerFactory
214: .newInstance().newTransformer(xsltSource);
215: transformer.transform(xmlSource, result);
216: }
217: }
|