1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit;
16
17 import static org.junit.Assert.fail;
18
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21 import java.net.URL;
22
23 import org.htmlunit.html.HtmlPage;
24 import org.htmlunit.junit.BrowserRunner;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27
28
29
30
31
32
33
34 @RunWith(BrowserRunner.class)
35 public final class ScriptExceptionTest extends SimpleWebTestCase {
36
37
38
39
40 @Test
41 public void constructor() throws Exception {
42 final String message = "bla bla";
43 final Throwable t = new RuntimeException(message);
44 final HtmlPage page = loadPage(DOCTYPE_HTML + "<html></html>");
45 final ScriptException exception = new ScriptException(page, t);
46
47 assertEquals(t, exception.getCause());
48 assertEquals(message, exception.getMessage());
49 }
50
51
52
53
54
55 @Test
56 public void getPage() throws Exception {
57 final String html = DOCTYPE_HTML + "<html><script>notExisting()</script></html>";
58 try {
59 loadPage(html);
60 }
61 catch (final ScriptException e) {
62 assertEquals(URL_FIRST, e.getPage().getUrl());
63 }
64 }
65
66
67
68
69
70
71
72
73
74 @Test
75 public void scriptStackTrace() throws Exception {
76 testScriptStackTrace("ScriptExceptionTest1");
77 testScriptStackTrace("ScriptExceptionTest2");
78 }
79
80 private void testScriptStackTrace(final String baseFileName) throws Exception {
81 try {
82 loadPage(getBrowserVersion(), getFileContent(baseFileName + ".html"), null,
83 new URL("http://www.gargoylesoftware.com/"));
84 }
85 catch (final ScriptException e) {
86 final StringWriter stringWriter = new StringWriter();
87 final PrintWriter printWriter = new PrintWriter(stringWriter);
88 e.printScriptStackTrace(printWriter);
89
90 final String expectedTrace = getFileContent(baseFileName + ".txt");
91 assertEquals(expectedTrace, stringWriter.toString());
92 return;
93 }
94 fail("Exception not thrown");
95 }
96
97
98
99
100 @Test
101 public void errorLineNumber() throws Exception {
102 testErrorLineNumber("testJsError1.html", 6);
103 testErrorLineNumber("testJsError2.html", 7);
104 testErrorLineNumber("testJsError3.html", 6);
105 testErrorLineNumber("testJsError4.html", 8);
106 }
107
108 private void testErrorLineNumber(final String fileName, final int errorLine) throws Exception {
109 final WebClient webClient = getWebClient();
110 final URL url = getClass().getClassLoader().getResource(fileName);
111 assertNotNull(url);
112 try {
113 final HtmlPage page = webClient.getPage(url);
114 page.getHtmlElementById("clickMe").click();
115 fail();
116 }
117 catch (final ScriptException e) {
118 assertEquals(errorLine, e.getFailingLineNumber());
119 }
120 }
121 }