View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for {@link XmlSerializer}.
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   */
37  @RunWith(BrowserRunner.class)
38  public class XmlSerializerTest extends SimpleWebTestCase {
39  
40      /**
41       * Utility for temporary folders.
42       * Has to be public due to JUnit's constraints for @Rule.
43       */
44      @Rule
45      public final TemporaryFolder tmpFolderProvider_ = new TemporaryFolder();
46  
47      /**
48       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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      * @throws Exception if the test fails
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      * @throws Exception if the test fails
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      * @throws Exception if the test fails
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      * @throws Exception if the test fails
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 }