Debian Codegraph @

src/test/java/ezvcard/io/scribe/ImppScribeTest.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.net.URI;
6
7 import org.junit.Test;
8
9 import ezvcard.VCard;
10 import ezvcard.VCardVersion;
11 import ezvcard.property.Impp;
12
13 /*
14 Copyright (c) 2012-2023, Michael Angstadt
15 All rights reserved.
16
17 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions are met:
19
20 1. Redistributions of source code must retain the above copyright notice, this
21 list of conditions and the following disclaimer.
22 2. Redistributions in binary form must reproduce the above copyright notice,
23 this list of conditions and the following disclaimer in the documentation
24 and/or other materials provided with the distribution.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37 The views and conclusions contained in the software and documentation are those
38 of the authors and should not be interpreted as representing official policies,
39 either expressed or implied, of the FreeBSD Project.
40 */
41
42 /**
43 * @author Michael Angstadt
44 */
45 public class ImppScribeTest {
46 private final ImppScribe scribe = new ImppScribe();
47 private final Sensei<Impp> sensei = new Sensei<>(scribe);
48
49 private final String uri = "aim:johndoe";
50 private final String badUri = ":::";
51
52 private final Impp withValue = new Impp(uri);
53 private final Impp empty = new Impp((URI) null);
54
55 @Test
56 public void prepareParameters() {
57 Impp withPref = new Impp(uri);
58 withPref.setPref(1);
59 VCard vcard = new VCard();
60 vcard.addImpp(withPref);
61
62 sensei.assertPrepareParams(withPref).vcard(vcard).versions(VCardVersion.V2_1, VCardVersion.V3_0).expected("TYPE", "pref").run();
63 sensei.assertPrepareParams(withPref).vcard(vcard).versions(VCardVersion.V4_0).expected("PREF", "1").run();
64 }
65
66 @Test
67 public void writeText() {
68 sensei.assertWriteText(withValue).run(uri);
69 sensei.assertWriteText(empty).run("");
70 }
71
72 @Test
73 public void writeXml() {
74 sensei.assertWriteXml(withValue).run("<uri>" + uri + "</uri>");
75 sensei.assertWriteXml(empty).run("<uri/>");
76 }
77
78 @Test
79 public void writeJson() {
80 sensei.assertWriteJson(withValue).run(uri);
81 sensei.assertWriteJson(empty).run("");
82 }
83
84 @Test
85 public void escape_special_chars() {
86 Impp impp = new Impp("special", "öffentlich");
87 String expected = "special:%C3%B6ffentlich";
88
89 sensei.assertWriteText(impp).run(expected);
90 sensei.assertWriteXml(impp).run("<uri>" + expected + "</uri>");
91 sensei.assertWriteJson(impp).run(expected);
92 }
93
94 @Test
95 public void parseText() {
96 sensei.assertParseText(uri).run(withValue);
97 sensei.assertParseText(badUri).cannotParse(15);
98 sensei.assertParseText("").run(empty);
99 }
100
101 @Test
102 public void parseXml() {
103 sensei.assertParseXml("<uri>" + uri + "</uri>").run(withValue);
104 sensei.assertParseXml("<uri>" + badUri + "</uri>").cannotParse(15);
105 sensei.assertParseXml("").cannotParse(0);
106 }
107
108 @Test
109 public void parseHtml() {
110 sensei.assertParseHtml("<a href=\"aim:goim?screenname=johndoe\">IM me</a>").run(withValue);
111 sensei.assertParseHtml("<div>aim:goim?screenname=johndoe</div>").run(withValue);
112 sensei.assertParseHtml("<div>johndoe</div>").cannotParse(14);
113 }
114
115 @Test
116 public void parseJson() {
117 sensei.assertParseJson(uri).run(withValue);
118 sensei.assertParseJson(badUri).cannotParse(15);
119 sensei.assertParseJson("").run(empty);
120 }
121
122 @Test
123 public void parseHtmlLink() {
124 //@formatter:off
125 assertParseHtmlLink("aim:theuser",
126 "aim:goim?screenname=theuser",
127 "aim:goim?screenname=theuser&message=hello",
128 "aim:addbuddy?screenname=theuser"
129 );
130
131 assertParseHtmlLink("ymsgr:theuser",
132 "ymsgr:sendim?theuser",
133 "ymsgr:addfriend?theuser",
134 "ymsgr:sendfile?theuser",
135 "ymsgr:call?theuser"
136 );
137
138 assertParseHtmlLink("skype:theuser",
139 "skype:theuser",
140 "skype:theuser?call",
141 "skype:theuser?call&topic=theTopic"
142 );
143
144 assertParseHtmlLink("msnim:theuser",
145 "msnim:chat?contact=theuser",
146 "msnim:add?contact=theuser",
147 "msnim:voice?contact=theuser",
148 "msnim:video?contact=theuser"
149 );
150
151 assertParseHtmlLink("xmpp:theuser",
152 "xmpp:theuser",
153 "xmpp:theuser?message"
154 );
155
156 assertParseHtmlLink("icq:123456789",
157 "icq:message?uin=123456789"
158 );
159
160 assertParseHtmlLink("sip:username:password@host:port",
161 "sip:username:password@host:port"
162 );
163
164 assertParseHtmlLink("irc://foobar.org/theuser,isnick",
165 "irc://foobar.org/theuser,isnick"
166 );
167
168 assertParseHtmlLink(null,
169 "foo:theuser",
170 "theuser",
171 "aim:invalid?screenname=theuser"
172 );
173 //@formatter:on
174 }
175
176 private void assertParseHtmlLink(String expectedUri, String... linksToTest) {
177 URI expected = (expectedUri == null) ? null : URI.create(expectedUri);
178 for (String link : linksToTest) {
179 URI actual = scribe.parseHtmlLink(link);
180 assertEquals(expected, actual);
181 }
182 }
183
184 @Test
185 public void writeHtmlLink() {
186 assertWriteHtmlLink(new Impp((String) null), null);
187 assertWriteHtmlLink(Impp.aim("theuser"), "aim:goim?screenname=theuser");
188 assertWriteHtmlLink(Impp.skype("theuser"), "skype:theuser");
189 assertWriteHtmlLink(Impp.icq("123456789"), "icq:message?uin=123456789");
190 assertWriteHtmlLink(Impp.msn("theuser"), "msnim:chat?contact=theuser");
191 assertWriteHtmlLink(Impp.yahoo("theuser"), "ymsgr:sendim?theuser");
192 assertWriteHtmlLink(Impp.irc("theuser"), "irc:theuser");
193 assertWriteHtmlLink(Impp.sip("theuser"), "sip:theuser");
194 assertWriteHtmlLink(Impp.xmpp("theuser"), "xmpp:theuser?message");
195 assertWriteHtmlLink(new Impp("foo", "bar"), "foo:bar");
196 }
197
198 private void assertWriteHtmlLink(Impp property, String expectedUri) {
199 String actualUri = scribe.writeHtmlLink(property);
200 assertEquals(expectedUri, actualUri);
201 }
202 }