package ezvcard.io.html; import static ezvcard.util.StringUtils.NEWLINE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.jsoup.nodes.Element; import org.jsoup.nodes.Node; import org.jsoup.nodes.TextNode; import org.junit.Test; import ezvcard.util.HtmlUtils; /* Copyright (c) 2012-2023, Michael Angstadt All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ /** * @author Michael Angstadt */ public class HCardElementTest { @Test public void value_text_content() { assertValue("
The text
", "The text"); } @Test public void value_line_breaks() { //@formatter:off assertValue( "
" + "The
text" + "
", "The" + NEWLINE + "text"); //"value" element assertValue( "
" + "The "+ ""+ "real
text" + "
" + "
", "real" + NEWLINE + "text"); //nested "value" element assertValue( "
" + "The " + "" + "real
text " + "" + "goes
here" + "
" + "
" + "
", "real" + NEWLINE + "text goes" + NEWLINE + "here"); //@formatter:on } @Test public void value_ignore_child_tags() { //@formatter:off assertValue( "
" + "Thetext" + "
", "Thetext"); //@formatter:on } @Test public void value_ignore_type_text() { //@formatter:off assertValue( "
" + "Work" + " is boring." + "
", "is boring."); assertValue( "
" + "" + "All " + "work" + " is boring." + "" + "
", "All is boring."); //@formatter:on } @Test public void value_value_tag() { //@formatter:off assertValue( "
" + "This is " + "the text" + "." + "
", "the text"); //@formatter:on } @Test public void value_multiple_value_tags() { //@formatter:off assertValue( "
" + "+1" + "." + "415" + "." + "555" + "." + "1212" + "
", "+14155551212"); //@formatter:on } @Test public void value_multiple_value_tags_not_direct_child() { //@formatter:off assertValue( "
" + "
Some text
" + "
" + "This is" + "
" + "
the value
" + "
" + "
of the element.
" + "
" + "
", "This isthe valueof the element."); //@formatter:on } @Test public void value_nested_value_tags() { //@formatter:off assertValue( "
" + "
" + "the value" + "nested" + "
" + "
", "the valuenested"); //@formatter:on } @Test public void value_abbr_value() { //@formatter:off assertValue( "
" + "This is " + "the text" + "." + "
", "1234"); //@formatter:on } @Test public void value_abbr_tag_with_title() { assertValue("N 48� 81.6667", "48.816667"); } @Test public void value_abbr_tag_without_title() { assertValue("N 48� 81.6667", "N 48� 81.6667"); } @Test public void value_skip_del_tags() { //@formatter:off assertValue( "
" + "This element contains " + "a lot of" + " text" + "
", "This element contains text"); //@formatter:on } @Test public void value_skip_del_tags_in_value() { //@formatter:off assertValue( "
" + "This element " + "" + "contains " + "a lot of" + " text" + "" + "
", "contains text"); //@formatter:on } @Test public void firstValue() { //@formatter:off String html = "
" + "
" + "Doe" + "
" + "
"; //@formatter:on HCardElement element = build(html); assertEquals("Doe", element.firstValue("family-name")); assertNull(element.firstValue("non-existant")); } @Test public void allValues() { //@formatter:off String html = "
" + "
" + "
" + "Smith" + "
" + "
" + "" + "(" + "Barney" + ")" + "" + "
"; //@formatter:on HCardElement element = build(html); assertEquals(Arrays.asList("Smith", "Barney"), element.allValues("additional-name")); assertTrue(element.allValues("non-existant").isEmpty()); } @Test public void types_none() { assertTypes("
"); } @Test public void types_multiple() { //@formatter:off assertTypes( "
" + "work" + "pref" + "
", "work", "pref"); //@formatter:on } @Test public void types_convert_to_lower_case() { //@formatter:off assertTypes( "
" + "wOrk" + "prEf" + "
", "work", "pref"); //@formatter:on } @Test public void types_not_direct_descendant() { //@formatter:off assertTypes( "
" + "work" + "
" + "pref" + "
" + "
", "work", "pref"); //@formatter:on } @Test public void absUrl() { HCardElement element = build("", "http://example.com"); assertEquals("data:foo", element.absUrl("href")); assertEquals("", element.absUrl("non-existant")); element = build("", "http://example.com"); assertEquals("http://example.com/index.html", element.absUrl("href")); element = build("", "http://example.com"); assertEquals("mailto:jdoe@hotmail.com", element.absUrl("href")); element = build("", "http://example.com"); assertEquals("http://foobar.com/index.html", element.absUrl("href")); } @Test public void append_with_newlines() { HCardElement element = build("
"); element.append("Append\rthis\n\ntext\r\nplease."); Iterator it = element.getElement().childNodes().iterator(); assertTextNodeValue(it.next(), "Append"); assertTagName(it.next(), "br"); assertTextNodeValue(it.next(), "this"); assertTagName(it.next(), "br"); assertTagName(it.next(), "br"); assertTextNodeValue(it.next(), "text"); assertTagName(it.next(), "br"); assertTextNodeValue(it.next(), "please."); assertFalse(it.hasNext()); } @Test public void append_without_newlines() { HCardElement element = build("
"); element.append("Without newlines."); Iterator it = element.getElement().childNodes().iterator(); assertTextNodeValue(it.next(), "Without newlines."); assertFalse(it.hasNext()); } private static void assertValue(String html, String expected) { HCardElement element = build(html); String actual = element.value(); assertEquals(expected, actual); } private static void assertTypes(String html, String... expected) { HCardElement element = build(html); List actual = element.types(); assertEquals(Arrays.asList(expected), actual); } private static void assertTagName(Node node, String expected) { Element element = (Element) node; String actual = element.tagName(); assertEquals(expected, actual); } private static void assertTextNodeValue(Node node, String expected) { TextNode textNode = (TextNode) node; String actual = textNode.text(); assertEquals(expected, actual); } private static HCardElement build(String html) { return build(html, null); } private static HCardElement build(String html, String baseUrl) { return new HCardElement(HtmlUtils.toElement(html, baseUrl)); } }