1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.File;
18 import java.net.URL;
19 import java.net.UnknownHostException;
20
21 import org.htmlunit.MockWebConnection;
22 import org.htmlunit.SimpleWebTestCase;
23 import org.htmlunit.WebClient;
24 import org.htmlunit.junit.BrowserRunner;
25 import org.htmlunit.util.MimeType;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.TemporaryFolder;
29 import org.junit.runner.RunWith;
30
31
32
33
34
35
36
37 @RunWith(BrowserRunner.class)
38 public class XmlSerializerTest extends SimpleWebTestCase {
39
40
41
42
43
44 @Rule
45 public final TemporaryFolder tmpFolderProvider_ = new TemporaryFolder();
46
47
48
49
50 @Test
51 public void notExistingLink() throws Exception {
52 final String html = DOCTYPE_HTML
53 + "<html>\n"
54 + "<body>"
55 + " <link rel='alternate' href='none.jpg'>\n"
56 + "</body>\n"
57 + "</html>";
58
59 final WebClient client = getWebClient();
60 final MockWebConnection connection = getMockWebConnection();
61 connection.setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
62 connection.setResponse(URL_FIRST, html);
63 client.setWebConnection(connection);
64
65 final HtmlPage page = client.getPage(URL_FIRST);
66
67 final File tmpFolder = tmpFolderProvider_.newFolder("hu");
68 final File file = new File(tmpFolder, "hu_XmlSerializerTest_notExistingLink.html");
69 page.save(file);
70 assertTrue(file.exists());
71 assertTrue(file.isFile());
72 }
73
74
75
76
77 @Test
78 public void unknownHostExceptionLink() throws Exception {
79 final URL imageUrl = new URL(URL_FIRST, "none.jpg");
80 final String html = DOCTYPE_HTML
81 + "<html>\n"
82 + "<body>"
83 + " <link rel='alternate' href='" + imageUrl.toExternalForm() + "'>\n"
84 + "</body>\n"
85 + "</html>";
86
87 final WebClient client = getWebClient();
88 final MockWebConnection connection = getMockWebConnection();
89 connection.setThrowable(imageUrl, new UnknownHostException(imageUrl.toExternalForm()));
90 connection.setResponse(URL_FIRST, html);
91 client.setWebConnection(connection);
92
93 final HtmlPage page = client.getPage(URL_FIRST);
94
95 final File tmpFolder = tmpFolderProvider_.newFolder("hu");
96 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unknownHostExceptionLink.html");
97 page.save(file);
98 assertTrue(file.exists());
99 assertTrue(file.isFile());
100 }
101
102
103
104
105 @Test
106 public void unsupportedProtocolLink() throws Exception {
107 final String html = DOCTYPE_HTML
108 + "<html><head>"
109 + "<link rel='alternate' href='android-app://somehost'>\n"
110 + "</head></html>";
111
112 final WebClient client = getWebClient();
113 final MockWebConnection connection = getMockWebConnection();
114 connection.setResponse(URL_FIRST, html);
115 client.setWebConnection(connection);
116
117 final HtmlPage page = client.getPage(URL_FIRST);
118
119 final File tmpFolder = tmpFolderProvider_.newFolder("hu");
120 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unsupportedProtocol.html");
121 page.save(file);
122 assertTrue(file.exists());
123 assertTrue(file.isFile());
124 }
125
126
127
128
129 @Test
130 public void notExistingImage() throws Exception {
131 final String html = DOCTYPE_HTML
132 + "<html>\n"
133 + "<body>"
134 + " <img src='none.jpg'>\n"
135 + "</body>\n"
136 + "</html>";
137
138 final WebClient client = getWebClient();
139 final MockWebConnection connection = getMockWebConnection();
140 connection.setDefaultResponse("Error: not found", 404, "Not Found", MimeType.TEXT_HTML);
141 connection.setResponse(URL_FIRST, html);
142 client.setWebConnection(connection);
143
144 final HtmlPage page = client.getPage(URL_FIRST);
145
146 final File tmpFolder = tmpFolderProvider_.newFolder("hu");
147 final File file = new File(tmpFolder, "hu_XmlSerializerTest_notExistingImage.html");
148 page.save(file);
149 assertTrue(file.exists());
150 assertTrue(file.isFile());
151 }
152
153
154
155
156 @Test
157 public void unknownHostExceptionImage() throws Exception {
158 final URL imageUrl = new URL(URL_FIRST, "none.jpg");
159 final String html = DOCTYPE_HTML
160 + "<html>\n"
161 + "<body>"
162 + " <img src='" + imageUrl.toExternalForm() + "'>\n"
163 + "</body>\n"
164 + "</html>";
165
166 final WebClient client = getWebClient();
167 final MockWebConnection connection = getMockWebConnection();
168 connection.setThrowable(imageUrl, new UnknownHostException(imageUrl.toExternalForm()));
169 connection.setResponse(URL_FIRST, html);
170 client.setWebConnection(connection);
171
172 final HtmlPage page = client.getPage(URL_FIRST);
173
174 final File tmpFolder = tmpFolderProvider_.newFolder("hu");
175 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unknownHostExceptionImage.html");
176 page.save(file);
177 assertTrue(file.exists());
178 assertTrue(file.isFile());
179 }
180
181
182
183
184 @Test
185 public void unsupportedProtocolImage() throws Exception {
186 final String html = DOCTYPE_HTML
187 + "<html>\n"
188 + "<body>"
189 + " <img src='android-app://somehost'>\n"
190 + "</body>\n"
191 + "</html>";
192
193 final WebClient client = getWebClient();
194 final MockWebConnection connection = getMockWebConnection();
195 connection.setResponse(URL_FIRST, html);
196 client.setWebConnection(connection);
197
198 final HtmlPage page = client.getPage(URL_FIRST);
199
200 final File tmpFolder = tmpFolderProvider_.newFolder("hu");
201 final File file = new File(tmpFolder, "hu_XmlSerializerTest_unsupportedProtocolImage.html");
202 page.save(file);
203 assertTrue(file.exists());
204 assertTrue(file.isFile());
205 }
206 }