Debian Codegraph @

src/test/java/ezvcard/io/html/HCardElementTest.java

raw symbols SCIP
1 package ezvcard.io.html;
2
3 import static ezvcard.util.StringUtils.NEWLINE;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertFalse;
6 import static org.junit.Assert.assertNull;
7 import static org.junit.Assert.assertTrue;
8
9 import java.util.Arrays;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import org.jsoup.nodes.Element;
14 import org.jsoup.nodes.Node;
15 import org.jsoup.nodes.TextNode;
16 import org.junit.Test;
17
18 import ezvcard.util.HtmlUtils;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
44 The views and conclusions contained in the software and documentation are those
45 of the authors and should not be interpreted as representing official policies,
46 either expressed or implied, of the FreeBSD Project.
47 */
48
49 /**
50 * @author Michael Angstadt
51 */
52 public class HCardElementTest {
53 @Test
54 public void value_text_content() {
55 assertValue("<div>The text</div>", "The text");
56 }
57
58 @Test
59 public void value_line_breaks() {
60 //@formatter:off
61 assertValue(
62 "<div>" +
63 "The<br>text" +
64 "</div>",
65 "The" + NEWLINE + "text");
66
67 //"value" element
68 assertValue(
69 "<div>" +
70 "The "+
71 "<span class=\"value\">"+
72 "real<br>text" +
73 "</span>" +
74 "</div>", "real" + NEWLINE + "text");
75
76 //nested "value" element
77 assertValue(
78 "<div>" +
79 "The " +
80 "<span class=\"value\">" +
81 "real<br>text " +
82 "<span class=\"value\">" +
83 "goes<br>here" +
84 "</span>" +
85 "</span>" +
86 "</div>",
87 "real" + NEWLINE + "text goes" + NEWLINE + "here");
88 //@formatter:on
89 }
90
91 @Test
92 public void value_ignore_child_tags() {
93 //@formatter:off
94 assertValue(
95 "<div>" +
96 "The<b>text</b>" +
97 "</div>",
98 "Thetext");
99 //@formatter:on
100 }
101
102 @Test
103 public void value_ignore_type_text() {
104 //@formatter:off
105 assertValue(
106 "<div>" +
107 "<span class=\"type\">Work</span>" +
108 " is boring." +
109 "</div>",
110 "is boring.");
111
112 assertValue(
113 "<div>" +
114 "<b>" +
115 "All " +
116 "<span class=\"type\">work</span>" +
117 " is boring." +
118 "</b>" +
119 "</div>",
120 "All is boring.");
121 //@formatter:on
122 }
123
124 @Test
125 public void value_value_tag() {
126 //@formatter:off
127 assertValue(
128 "<div>" +
129 "This is " +
130 "<span class=\"value\">the text</span>" +
131 "." +
132 "</div>",
133 "the text");
134 //@formatter:on
135 }
136
137 @Test
138 public void value_multiple_value_tags() {
139 //@formatter:off
140 assertValue(
141 "<div>" +
142 "<span class=\"value\">+1</span>" +
143 "." +
144 "<span class=\"value\">415</span>" +
145 "." +
146 "<span class=\"value\">555</span>" +
147 "." +
148 "<span class=\"value\">1212</span>" +
149 "</div>",
150 "+14155551212");
151 //@formatter:on
152 }
153
154 @Test
155 public void value_multiple_value_tags_not_direct_child() {
156 //@formatter:off
157 assertValue(
158 "<div>" +
159 "<div>Some text</div>" +
160 "<div>" +
161 "<span class=\"value\">This is</span>" +
162 "<div>" +
163 "<div class=\"value\">the value</div>" +
164 "</div>" +
165 "<div class=\"value\">of the element.</div>" +
166 "</div>" +
167 "</div>",
168 "This isthe valueof the element.");
169 //@formatter:on
170 }
171
172 @Test
173 public void value_nested_value_tags() {
174 //@formatter:off
175 assertValue(
176 "<div>" +
177 "<div class=\"value\">" +
178 "the value" +
179 "<span class=\"value\">nested</span>" +
180 "</div>" +
181 "</div>",
182 "the valuenested");
183 //@formatter:on
184 }
185
186 @Test
187 public void value_abbr_value() {
188 //@formatter:off
189 assertValue(
190 "<div>" +
191 "This is " +
192 "<abbr class=\"value\" title=\"1234\">the text</abbr>" +
193 "." +
194 "</div>",
195 "1234");
196 //@formatter:on
197 }
198
199 @Test
200 public void value_abbr_tag_with_title() {
201 assertValue("<abbr class=\"latitude\" title=\"48.816667\">N 48� 81.6667</abbr>", "48.816667");
202 }
203
204 @Test
205 public void value_abbr_tag_without_title() {
206 assertValue("<abbr class=\"latitude\">N 48� 81.6667</abbr>", "N 48� 81.6667");
207 }
208
209 @Test
210 public void value_skip_del_tags() {
211 //@formatter:off
212 assertValue(
213 "<div>" +
214 "This element contains " +
215 "<del>a lot of</del>" +
216 " text" +
217 "</div>",
218 "This element contains text");
219 //@formatter:on
220 }
221
222 @Test
223 public void value_skip_del_tags_in_value() {
224 //@formatter:off
225 assertValue(
226 "<div>" +
227 "This element " +
228 "<span class=\"value\">" +
229 "contains " +
230 "<del>a lot of</del>" +
231 " text" +
232 "</span>" +
233 "</div>",
234 "contains text");
235 //@formatter:on
236 }
237
238 @Test
239 public void firstValue() {
240 //@formatter:off
241 String html =
242 "<div class=\"n\">" +
243 "<div>" +
244 "<span class=\"family-name\"><b>Doe</b></span>" +
245 "</div>" +
246 "</div>";
247 //@formatter:on
248
249 HCardElement element = build(html);
250
251 assertEquals("Doe", element.firstValue("family-name"));
252 assertNull(element.firstValue("non-existant"));
253 }
254
255 @Test
256 public void allValues() {
257 //@formatter:off
258 String html =
259 "<div class=\"n\">" +
260 "<div>" +
261 "<div>" +
262 "<span class=\"additional-name\">Smith</span>" +
263 "</div>" +
264 "</div>" +
265 "<span class=\"additional-name\">" +
266 "(" +
267 "<span class=\"value\">Barney</span>" +
268 ")" +
269 "</span>" +
270 "</div>";
271 //@formatter:on
272
273 HCardElement element = build(html);
274
275 assertEquals(Arrays.asList("Smith", "Barney"), element.allValues("additional-name"));
276 assertTrue(element.allValues("non-existant").isEmpty());
277 }
278
279 @Test
280 public void types_none() {
281 assertTypes("<div class=\"adr\"></div>");
282 }
283
284 @Test
285 public void types_multiple() {
286 //@formatter:off
287 assertTypes(
288 "<div class=\"adr\">" +
289 "<span class=\"type\">work</span>" +
290 "<span class=\"type\">pref</span>" +
291 "</div>",
292 "work", "pref");
293 //@formatter:on
294 }
295
296 @Test
297 public void types_convert_to_lower_case() {
298 //@formatter:off
299 assertTypes(
300 "<div class=\"adr\">" +
301 "<span class=\"type\">wOrk</span>" +
302 "<span class=\"type\">prEf</span>" +
303 "</div>",
304 "work", "pref");
305 //@formatter:on
306 }
307
308 @Test
309 public void types_not_direct_descendant() {
310 //@formatter:off
311 assertTypes(
312 "<div class=\"adr\">" +
313 "<span class=\"type\">work</span>" +
314 "<div>" +
315 "<span class=\"type\">pref</span>" +
316 "<div>" +
317 "</div>",
318 "work", "pref");
319 //@formatter:on
320 }
321
322 @Test
323 public void absUrl() {
324 HCardElement element = build("<a href=\"data:foo\" />", "http://example.com");
325 assertEquals("data:foo", element.absUrl("href"));
326 assertEquals("", element.absUrl("non-existant"));
327
328 element = build("<a href=\"index.html\" />", "http://example.com");
329 assertEquals("http://example.com/index.html", element.absUrl("href"));
330
331 element = build("<a href=\"mailto:[email protected]\" />", "http://example.com");
332 assertEquals("mailto:[email protected]", element.absUrl("href"));
333
334 element = build("<a href=\"http://foobar.com/index.html\" />", "http://example.com");
335 assertEquals("http://foobar.com/index.html", element.absUrl("href"));
336 }
337
338 @Test
339 public void append_with_newlines() {
340 HCardElement element = build("<div />");
341 element.append("Append\rthis\n\ntext\r\nplease.");
342
343 Iterator<Node> it = element.getElement().childNodes().iterator();
344 assertTextNodeValue(it.next(), "Append");
345 assertTagName(it.next(), "br");
346 assertTextNodeValue(it.next(), "this");
347 assertTagName(it.next(), "br");
348 assertTagName(it.next(), "br");
349 assertTextNodeValue(it.next(), "text");
350 assertTagName(it.next(), "br");
351 assertTextNodeValue(it.next(), "please.");
352 assertFalse(it.hasNext());
353 }
354
355 @Test
356 public void append_without_newlines() {
357 HCardElement element = build("<div />");
358 element.append("Without newlines.");
359
360 Iterator<Node> it = element.getElement().childNodes().iterator();
361 assertTextNodeValue(it.next(), "Without newlines.");
362 assertFalse(it.hasNext());
363 }
364
365 private static void assertValue(String html, String expected) {
366 HCardElement element = build(html);
367 String actual = element.value();
368 assertEquals(expected, actual);
369 }
370
371 private static void assertTypes(String html, String... expected) {
372 HCardElement element = build(html);
373 List<String> actual = element.types();
374 assertEquals(Arrays.asList(expected), actual);
375 }
376
377 private static void assertTagName(Node node, String expected) {
378 Element element = (Element) node;
379 String actual = element.tagName();
380 assertEquals(expected, actual);
381 }
382
383 private static void assertTextNodeValue(Node node, String expected) {
384 TextNode textNode = (TextNode) node;
385 String actual = textNode.text();
386 assertEquals(expected, actual);
387 }
388
389 private static HCardElement build(String html) {
390 return build(html, null);
391 }
392
393 private static HCardElement build(String html, String baseUrl) {
394 return new HCardElement(HtmlUtils.toElement(html, baseUrl));
395 }
396 }