Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import static ezvcard.VCardVersion.V2_1;
4 import static ezvcard.VCardVersion.V3_0;
5 import static ezvcard.VCardVersion.V4_0;
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertNull;
8
9 import java.util.Arrays;
10
11 import org.junit.Test;
12
13 import ezvcard.VCard;
14 import ezvcard.VCardDataType;
15 import ezvcard.io.scribe.Sensei.Check;
16 import ezvcard.parameter.TelephoneType;
17 import ezvcard.property.Telephone;
18 import ezvcard.util.TelUri;
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 TelephoneScribeTest {
53 private final TelephoneScribe scribe = new TelephoneScribe();
54 private final Sensei<Telephone> sensei = new Sensei<>(scribe);
55
56 private final String text = "(555) 555-1234";
57 private final String textWithExt = "+1-555-555-1234 x101";
58 private final String uri = "tel:+1-555-555-1234;ext=101";
59
60 private final Telephone withText = new Telephone(text);
61 private final Telephone withUri = new Telephone(new TelUri.Builder("+1-555-555-1234").extension("101").build());
62 private final Telephone empty = new Telephone((String) null);
63
64 @Test
65 public void dataType() {
66 sensei.assertDataType(withText).run(VCardDataType.TEXT);
67 sensei.assertDataType(withUri).versions(V2_1, V3_0).run(VCardDataType.TEXT);
68 sensei.assertDataType(withUri).versions(V4_0).run(VCardDataType.URI);
69 sensei.assertDataType(empty).run(VCardDataType.TEXT);
70 }
71
72 /**
73 * If a property contains a "TYPE=pref" parameter and it's being marshalled
74 * to 4.0, it should replace "TYPE=pref" with "PREF=1".
75 */
76 @Test
77 public void prepareParameters_type_pref() {
78 //TODO move test to VCardPropertyScribeTest
79
80 Telephone property = new Telephone((String) null);
81 property.getTypes().add(TelephoneType.PREF);
82
83 //2.1 and 3.0 keep it
84 sensei.assertPrepareParams(property).versions(V2_1, V3_0).expected("TYPE", "pref").run();
85
86 //4.0 converts it to "PREF=1"
87 sensei.assertPrepareParams(property).versions(V4_0).expected("PREF", "1").run();
88 }
89
90 /**
91 * If properties contain "PREF" parameters and they're being marshalled to
92 * 2.1/3.0, then it should find the type with the lowest PREF value and add
93 * "TYPE=pref" to it.
94 */
95 @Test
96 public void prepareParameters_pref_parameter() {
97 VCard vcard = new VCard();
98
99 Telephone tel2 = new Telephone((String) null);
100 tel2.setPref(2);
101 vcard.addTelephoneNumber(tel2);
102
103 Telephone tel1 = new Telephone((String) null);
104 tel1.setPref(1);
105 vcard.addTelephoneNumber(tel1);
106
107 Telephone tel3 = new Telephone((String) null);
108 vcard.addTelephoneNumber(tel3);
109
110 //2.1 and 3.0 converts the lowest PREF parameter to "TYPE=pref", and removes all the rest
111 sensei.assertPrepareParams(tel1).versions(V2_1, V3_0).vcard(vcard).expected("TYPE", "pref").run();
112 sensei.assertPrepareParams(tel2).versions(V2_1, V3_0).vcard(vcard).run();
113 sensei.assertPrepareParams(tel3).versions(V2_1, V3_0).vcard(vcard).run();
114
115 //4.0 keeps it
116 sensei.assertPrepareParams(tel1).versions(V4_0).vcard(vcard).expected("PREF", "1").run();
117 sensei.assertPrepareParams(tel2).versions(V4_0).vcard(vcard).expected("PREF", "2").run();
118 sensei.assertPrepareParams(tel3).versions(V4_0).vcard(vcard).run();
119 }
120
121 @Test
122 public void writeText() {
123 sensei.assertWriteText(withText).run(text);
124 sensei.assertWriteText(withUri).versions(V2_1, V3_0).run(textWithExt);
125 sensei.assertWriteText(withUri).versions(V4_0).run(uri);
126 sensei.assertWriteText(empty).run("");
127 }
128
129 @Test
130 public void writeXml() {
131 sensei.assertWriteXml(withText).run("<text>" + text + "</text>");
132 sensei.assertWriteXml(withUri).run("<uri>" + uri + "</uri>");
133 sensei.assertWriteXml(empty).run("<text/>");
134 }
135
136 @Test
137 public void writeJson() {
138 sensei.assertWriteJson(withText).run(text);
139 sensei.assertWriteJson(withUri).run(uri);
140 sensei.assertWriteJson(empty).run("");
141 }
142
143 @Test
144 public void parseText() {
145 sensei.assertParseText(text).run(withText);
146 sensei.assertParseText(text).dataType(VCardDataType.TEXT).run(withText);
147 sensei.assertParseText(text).dataType(VCardDataType.URI).warnings(18).run(withText);
148
149 sensei.assertParseText(uri).run(withUri);
150 sensei.assertParseText(uri).dataType(VCardDataType.TEXT).run(withUri);
151 sensei.assertParseText(uri).dataType(VCardDataType.URI).run(withUri);
152
153 sensei.assertParseText("invalid").run(hasText("invalid"));
154 sensei.assertParseText("invalid").dataType(VCardDataType.TEXT).run(hasText("invalid"));
155 sensei.assertParseText("invalid").dataType(VCardDataType.URI).warnings(18).run(hasText("invalid"));
156
157 sensei.assertParseText("").run(hasText(""));
158 sensei.assertParseText("").dataType(VCardDataType.TEXT).run(hasText(""));
159 sensei.assertParseText("").dataType(VCardDataType.URI).warnings(18).run(hasText(""));
160 }
161
162 @Test
163 public void parseXml() {
164 sensei.assertParseXml("<text>" + text + "</text>").run(withText);
165 sensei.assertParseXml("<uri>" + uri + "</uri>").run(withUri);
166
167 //prefer <text> to <uri>
168 sensei.assertParseXml("<uri>" + uri + "</uri><text>" + text + "</text>").run(withText);
169
170 sensei.assertParseXml("<uri>invalid</uri>").warnings(18).run(hasText("invalid"));
171 sensei.assertParseXml("").cannotParse(0);
172 }
173
174 @Test
175 public void parseHtml() {
176 //@formatter:off
177 sensei.assertParseHtml(
178 "<div>" +
179 "<span class=\"type\">home</span>" +
180 "<span class=\"type\">cell</span>" +
181 "<span class=\"type\">foo</span>" +
182 "<span class=\"value\">" + text + "</span>" +
183 "</div>").run(new Check<Telephone>(){
184 public void check(Telephone property) {
185 assertEquals(text, property.getText());
186 assertEquals(Arrays.asList(TelephoneType.HOME, TelephoneType.CELL, TelephoneType.get("foo")), property.getTypes());
187 }
188 });
189
190 sensei.assertParseHtml("<a href=\"" + uri + "\">Call me</a>").run(withUri);
191 sensei.assertParseHtml("<a href=\"foo\">" + text + "</a>").run(withText);
192 //@formatter:on
193 }
194
195 @Test
196 public void parseJson() {
197 sensei.assertParseJson(text).dataType(VCardDataType.TEXT).run(withText);
198 sensei.assertParseJson(text).dataType(VCardDataType.URI).warnings(18).run(withText);
199
200 sensei.assertParseJson(uri).dataType(VCardDataType.TEXT).run(withUri);
201 sensei.assertParseJson(uri).dataType(VCardDataType.URI).run(withUri);
202
203 sensei.assertParseJson("invalid").dataType(VCardDataType.TEXT).run(hasText("invalid"));
204 sensei.assertParseJson("invalid").dataType(VCardDataType.URI).warnings(18).run(hasText("invalid"));
205
206 sensei.assertParseJson("").dataType(VCardDataType.TEXT).run(hasText(""));
207 sensei.assertParseJson("").dataType(VCardDataType.URI).warnings(18).run(hasText(""));
208 }
209
210 private Check<Telephone> hasText(final String text) {
211 return actual -> {
212 assertEquals(text, actual.getText());
213 assertNull(actual.getUri());
214 };
215 }
216 }