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;
039:
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.io.PrintWriter;
043: import java.io.StringWriter;
044: import java.net.URL;
045:
046: import org.apache.commons.io.IOUtils;
047:
048: import com.gargoylesoftware.htmlunit.html.ClickableElement;
049: import com.gargoylesoftware.htmlunit.html.HtmlPage;
050:
051: /**
052: * Tests for {@link ScriptException}.
053: *
054: * @version $Revision: 2132 $
055: * @author Marc Guillemot
056: * @author Ahmed Ashour
057: */
058: public final class ScriptExceptionTest extends WebTestCase {
059:
060: /**
061: * Create an instance.
062: *
063: * @param name The name of the test.
064: */
065: public ScriptExceptionTest(final String name) {
066: super (name);
067: }
068:
069: /**
070: * @throws Exception if the test fails
071: */
072: public void testConstructor() throws Exception {
073: final String message = "bla bla";
074: final Throwable t = new RuntimeException(message);
075: final HtmlPage page = loadPage("<html></html>");
076: final ScriptException exception = new ScriptException(page, t);
077:
078: assertEquals(t, exception.getCause());
079: assertEquals(message, exception.getMessage());
080: }
081:
082: /**
083: * Test access to the page where the exception occurred from the exception
084: * @throws Exception if the test fails
085: */
086: public void testGetPage() throws Exception {
087: final String html = "<html><script>notExisting()</script></html>";
088:
089: try {
090: loadPage(html);
091: } catch (final ScriptException e) {
092: assertEquals(URL_GARGOYLE, e.getPage().getWebResponse()
093: .getUrl());
094: }
095: }
096:
097: /**
098: * Test the JavaScript stacktrace.
099: * Note: this test will fail when the information (line, col, source name) provided to execute
100: * scripts is improved. In this case this unit test should be adapted IF the provided information
101: * in the script stack trace gets better and provides more useful information to understand the
102: * cause of a problem.
103: * @throws Exception if the test fails
104: */
105: public void testScriptStackTrace() throws Exception {
106: testScriptStackTrace("ScriptExceptionTest1");
107: testScriptStackTrace("ScriptExceptionTest2");
108: }
109:
110: private void testScriptStackTrace(final String baseFileName)
111: throws Exception {
112: try {
113: loadPage(getFileContent(baseFileName + ".html"));
114: } catch (final ScriptException e) {
115: final StringWriter stringWriter = new StringWriter();
116: final PrintWriter printWriter = new PrintWriter(
117: stringWriter);
118: e.printScriptStackTrace(printWriter);
119:
120: final String expectedTrace = getFileContent(baseFileName
121: + ".txt");
122: assertEquals(expectedTrace, stringWriter.toString());
123: return;
124: }
125: fail("Exception not thrown");
126: }
127:
128: private String getFileContent(final String fileName)
129: throws IOException {
130: final InputStream stream = getClass().getClassLoader()
131: .getResourceAsStream(fileName);
132: assertNotNull(fileName, stream);
133: return IOUtils.toString(stream);
134: }
135:
136: /**
137: *
138: * @throws Exception if the test fails
139: */
140: public void testErrorLineNumber() throws Exception {
141: testErrorLineNumber("testJsError1.html", 6);
142: testErrorLineNumber("testJsError2.html", 7);
143: testErrorLineNumber("testJsError3.html", 6);
144: testErrorLineNumber("testJsError4.html", 8);
145: }
146:
147: private void testErrorLineNumber(final String fileName,
148: final int errorLine) throws Exception {
149: final WebClient webClient = new WebClient();
150: final URL url = getClass().getClassLoader().getResource(
151: fileName);
152: assertNotNull(url);
153: try {
154: final HtmlPage page = (HtmlPage) webClient.getPage(url);
155: ((ClickableElement) page.getHtmlElementById("clickMe"))
156: .click();
157: fail();
158: } catch (final ScriptException e) {
159: assertEquals(errorLine, e.getFailingLineNumber());
160: }
161: }
162: }
|