Debian Codegraph @

src/test/java/ezvcard/io/scribe/VCardPropertyScribeTest.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 ezvcard.util.TestUtils.assertIntEquals;
7 import static ezvcard.util.TestUtils.each;
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.assertNotSame;
10 import static org.junit.Assert.assertNull;
11 import static org.junit.Assert.assertSame;
12
13 import java.time.LocalDate;
14 import java.time.LocalDateTime;
15 import java.time.OffsetDateTime;
16 import java.time.ZoneOffset;
17 import java.time.temporal.Temporal;
18 import java.util.Arrays;
19
20 import javax.xml.namespace.QName;
21
22 import org.junit.ClassRule;
23 import org.junit.Test;
24
25 import ezvcard.VCard;
26 import ezvcard.VCardDataType;
27 import ezvcard.VCardVersion;
28 import ezvcard.io.ParseContext;
29 import ezvcard.io.json.JCardValue;
30 import ezvcard.io.scribe.Sensei.Check;
31 import ezvcard.io.text.WriteContext;
32 import ezvcard.parameter.VCardParameters;
33 import ezvcard.property.VCardProperty;
34 import ezvcard.util.DefaultTimezoneRule;
35
36 /*
37 Copyright (c) 2012-2023, Michael Angstadt
38 All rights reserved.
39
40 Redistribution and use in source and binary forms, with or without
41 modification, are permitted provided that the following conditions are met:
42
43 1. Redistributions of source code must retain the above copyright notice, this
44 list of conditions and the following disclaimer.
45 2. Redistributions in binary form must reproduce the above copyright notice,
46 this list of conditions and the following disclaimer in the documentation
47 and/or other materials provided with the distribution.
48
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
50 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
53 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 /**
62 * @author Michael Angstadt
63 */
64 public class VCardPropertyScribeTest {
65 @ClassRule
66 public static final DefaultTimezoneRule tzRule = new DefaultTimezoneRule(1, 0);
67
68 private final VCardPropertyScribeImpl scribe = new VCardPropertyScribeImpl();
69 private final Sensei<TestProperty> sensei = new Sensei<>(scribe);
70
71 @Test
72 public void date_parse_utc() {
73 String value = "20130611T134302Z";
74 Temporal expected = LocalDateTime.of(2013, 6, 11, 13, 43, 2).toInstant(ZoneOffset.UTC);
75 Temporal actual = VCardPropertyScribe.date(value);
76
77 assertEquals(expected, actual);
78 }
79
80 @Test
81 public void date_parse_offset() {
82 String value = "20130611T134302+0100";
83 Temporal expected = OffsetDateTime.of(2013, 6, 11, 13, 43, 2, 0, ZoneOffset.ofHours(1));
84 Temporal actual = VCardPropertyScribe.date(value);
85
86 assertEquals(expected, actual);
87 }
88
89 @Test
90 public void date_parse_local() {
91 String value = "20130611T134302";
92 Temporal expected = LocalDateTime.of(2013, 6, 11, 13, 43, 2);
93 Temporal actual = VCardPropertyScribe.date(value);
94
95 assertEquals(expected, actual);
96 }
97
98 @Test
99 public void date_write() {
100 LocalDate date = LocalDate.of(2013, 6, 11);
101 assert_date_write(date, "20130611", false);
102 assert_date_write(date, "2013-06-11", true);
103 }
104
105 private void assert_date_write(Temporal temporal, String expected, boolean extended) {
106 String actual = VCardPropertyScribe.date(temporal).extended(extended).write();
107 assertEquals(expected, actual);
108 }
109
110 @Test
111 public void dataType_default() {
112 TestProperty property = new TestProperty("value");
113 sensei.assertDataType(property).run(VCardDataType.TEXT);
114 }
115
116 @Test
117 public void dataType_custom() {
118 VCardPropertyScribeImpl scribe = new VCardPropertyScribeImpl() {
119 @Override
120 protected VCardDataType _dataType(TestProperty property, VCardVersion version) {
121 return VCardDataType.URI;
122 }
123 };
124 TestProperty property = new TestProperty("value");
125 new Sensei<>(scribe).assertDataType(property).run(VCardDataType.URI);
126 }
127
128 @Test
129 public void prepareParameters() {
130 VCardPropertyScribeImpl m = new VCardPropertyScribeImpl() {
131 @Override
132 protected void _prepareParameters(TestProperty property, VCardParameters copy, VCardVersion version, VCard vcard) {
133 copy.put("PARAM", "value");
134 }
135 };
136
137 TestProperty property = new TestProperty("value");
138 VCardParameters copy = m.prepareParameters(property, V4_0, new VCard());
139
140 assertNotSame(property.getParameters(), copy);
141 assertEquals("value", copy.first("PARAM"));
142 }
143
144 @Test
145 public void handlePrefParam_to_4() {
146 VCard vcard = new VCard();
147
148 TestProperty two = new TestProperty("");
149 two.getParameters().setType("HOME");
150 vcard.addProperty(two);
151
152 TestProperty one = new TestProperty("");
153 one.getParameters().setType("PREF");
154 vcard.addProperty(one);
155
156 TestProperty three = new TestProperty("");
157 vcard.addProperty(three);
158
159 //no change
160 for (VCardVersion version : each(V2_1, V3_0)) {
161 for (TestProperty p : vcard.getProperties(TestProperty.class)) {
162 VCardParameters copy = new VCardParameters(p.getParameters());
163 VCardPropertyScribe.handlePrefParam(p, copy, version, vcard);
164 assertEquals(p.getParameters(), copy);
165 }
166 }
167
168 {
169 VCardVersion version = V4_0;
170
171 VCardParameters copy = new VCardParameters(one.getParameters());
172 VCardPropertyScribe.handlePrefParam(one, copy, version, vcard);
173 assertIntEquals(1, copy.getPref());
174 assertNull(copy.getType());
175
176 for (TestProperty p : each(two, three)) {
177 copy = new VCardParameters(p.getParameters());
178 VCardPropertyScribe.handlePrefParam(p, copy, version, vcard);
179 assertEquals(p.getParameters(), copy);
180 }
181 }
182 }
183
184 @Test
185 public void handlePrefParam_from_4() {
186 VCard vcard = new VCard();
187
188 TestProperty two = new TestProperty("");
189 two.getParameters().setPref(2);
190 vcard.addProperty(two);
191
192 TestProperty nullPref = new TestProperty("");
193 vcard.addProperty(nullPref);
194
195 TestProperty invalidPref = new TestProperty("");
196 invalidPref.getParameters().replace("PREF", "invalid");
197 vcard.addProperty(invalidPref);
198
199 TestProperty one = new TestProperty("");
200 one.getParameters().setPref(1);
201 vcard.addProperty(one);
202
203 TestProperty three = new TestProperty("");
204 three.getParameters().setPref(3);
205 vcard.addProperty(three);
206
207 //no change
208 {
209 VCardVersion version = V4_0;
210 for (TestProperty p : vcard.getProperties(TestProperty.class)) {
211 VCardParameters copy = new VCardParameters(p.getParameters());
212 VCardPropertyScribe.handlePrefParam(p, copy, version, vcard);
213 assertEquals(p.getParameters(), copy);
214 }
215 }
216
217 for (VCardVersion version : each(V2_1, V3_0)) {
218 VCardParameters copy = new VCardParameters(one.getParameters());
219 VCardPropertyScribe.handlePrefParam(one, copy, version, vcard);
220 assertEquals("pref", copy.getType());
221 assertNull(copy.getPref());
222
223 for (TestProperty p : each(nullPref, invalidPref, two, three)) {
224 copy = new VCardParameters(p.getParameters());
225 VCardPropertyScribe.handlePrefParam(p, copy, version, vcard);
226 assertNull(copy.getType());
227 assertNull(copy.getPref());
228 }
229 }
230 }
231
232 @Test
233 public void writeText() {
234 TestProperty property = new TestProperty("value");
235 sensei.assertWriteText(property).run("value");
236 }
237
238 @Test
239 public void parseText() {
240 final VCardParameters params = new VCardParameters();
241 sensei.assertParseText("value").dataType(VCardDataType.TEXT).warnings((Integer) null).params(params).run(property -> {
242 assertEquals("value", property.value);
243 assertEquals(VCardDataType.TEXT, property.parsedDataType);
244 assertSame(params, property.getParameters());
245 });
246 }
247
248 @Test
249 public void writeXml() {
250 TestProperty prop = new TestProperty("value");
251 sensei.assertWriteXml(prop).run("<text>value</text>");
252 }
253
254 @Test
255 public void parseXml() {
256 final VCardParameters params = new VCardParameters();
257
258 //@formatter:off
259 sensei.assertParseXml(
260 "<ignore xmlns=\"http://example.com\">ignore-me</ignore>" +
261 "<integer>value</integer>" +
262 "<text>ignore-me</text>").params(params).warnings((Integer)null).run(new Check<TestProperty>(){
263 public void check(TestProperty property){
264 assertEquals("value", property.value);
265 assertEquals(VCardDataType.INTEGER, property.parsedDataType);
266 assertSame(params,property.getParameters());
267 }
268 });
269 //@formatter:on
270 }
271
272 @Test
273 public void parseXml_no_xcard_element() {
274 sensei.assertParseXml("<one xmlns=\"http://example.com\">1</one><two xmlns=\"http://example.com\">2</two>").warnings((Integer) null).run(property -> {
275 assertEquals("12", property.value);
276 assertNull(property.parsedDataType);
277 });
278 }
279
280 @Test
281 public void parseXml_no_child_elements() {
282 sensei.assertParseXml("value").warnings((Integer) null).run(property -> {
283 assertEquals("value", property.value);
284 assertNull(property.parsedDataType);
285 });
286 }
287
288 @Test
289 public void parseXml_unknown() {
290 sensei.assertParseXml("<unknown>value</unknown>").warnings((Integer) null).run(property -> {
291 assertEquals("value", property.value);
292 assertEquals(null, property.parsedDataType);
293 });
294 sensei.assertParseXml("<one xmlns=\"http://example.com\">1</one><unknown>value</unknown>").warnings((Integer) null).run(property -> {
295 assertEquals("value", property.value);
296 assertEquals(null, property.parsedDataType);
297 });
298 sensei.assertParseXml("<unknown />").warnings((Integer) null).run(property -> {
299 assertEquals("", property.value);
300 assertEquals(null, property.parsedDataType);
301 });
302 }
303
304 @Test
305 public void getQName_default() {
306 QName expected = new QName(V4_0.getXmlNamespace(), "test");
307 QName actual = scribe.getQName();
308 assertEquals(expected, actual);
309 }
310
311 @Test
312 public void getQName_custom() {
313 QName expected = new QName("http://example.com", "foo");
314 VCardPropertyScribeImpl m = new VCardPropertyScribeImpl(expected);
315 QName actual = m.getQName();
316 assertEquals(expected, actual);
317 }
318
319 @Test
320 public void parseHtml() {
321 sensei.assertParseHtml("<div>value<del>ignore</del>value</div>").warnings((Integer) null).run(property -> {
322 assertEquals("valuevalue", property.value);
323 assertNull(property.parsedDataType);
324 });
325 }
326
327 @Test
328 public void writeJson() {
329 TestProperty property = new TestProperty("value");
330 sensei.assertWriteJson(property).run("value");
331 }
332
333 @Test
334 public void parseJson_single() {
335 final VCardParameters params = new VCardParameters();
336 JCardValue value = JCardValue.single("value");
337 sensei.assertParseJson(value).dataType(VCardDataType.TEXT).params(params).warnings((Integer) null).run(property -> {
338 assertEquals("value", property.value);
339 assertSame(params, property.getParameters());
340 });
341 }
342
343 @Test
344 public void parseJson_list() {
345 JCardValue value = JCardValue.multi("value1", "val,;ue2");
346 sensei.assertParseJson(value).dataType(VCardDataType.TEXT).warnings((Integer) null).run(property -> {
347 assertEquals("value1,val\\,\\;ue2", property.value);
348 });
349 }
350
351 @Test
352 public void parseJson_structured() {
353 JCardValue value = JCardValue.structured(null, "value1", "val,;ue2", Arrays.asList("value3", "value4"));
354 sensei.assertParseJson(value).dataType(VCardDataType.TEXT).warnings((Integer) null).run(property -> {
355 assertEquals(";value1;val\\,\\;ue2;value3,value4", property.value);
356 });
357 }
358
359 private class VCardPropertyScribeImpl extends VCardPropertyScribe<TestProperty> {
360 private VCardPropertyScribeImpl() {
361 super(TestProperty.class, "TEST");
362 }
363
364 private VCardPropertyScribeImpl(QName qname) {
365 super(TestProperty.class, "TEST", qname);
366 }
367
368 @Override
369 protected VCardDataType _defaultDataType(VCardVersion version) {
370 return VCardDataType.TEXT;
371 }
372
373 @Override
374 protected String _writeText(TestProperty property, WriteContext context) {
375 return property.value;
376 }
377
378 @Override
379 protected TestProperty _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
380 context.addWarning("parseText");
381 return new TestProperty(value, dataType);
382 }
383 }
384
385 private class TestProperty extends VCardProperty {
386 public String value;
387 public VCardDataType parsedDataType;
388
389 public TestProperty(String value) {
390 this(value, null);
391 }
392
393 public TestProperty(String value, VCardDataType parsedDataType) {
394 this.value = value;
395 this.parsedDataType = parsedDataType;
396 }
397 }
398 }