Debian Codegraph @

src/test/java/ezvcard/io/json/JCardReaderTest.java

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import static ezvcard.VCardVersion.V4_0;
4 import static ezvcard.util.TestUtils.assertNoMoreVCards;
5 import static ezvcard.util.TestUtils.assertParseWarnings;
6 import static ezvcard.util.TestUtils.assertPropertyCount;
7 import static ezvcard.util.TestUtils.assertVersion;
8 import static org.junit.Assert.assertEquals;
9
10 import java.io.Writer;
11 import java.nio.charset.StandardCharsets;
12 import java.nio.file.Files;
13 import java.nio.file.Path;
14 import java.time.OffsetDateTime;
15 import java.time.ZoneOffset;
16
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.TemporaryFolder;
20
21 import ezvcard.VCard;
22 import ezvcard.VCardDataType;
23 import ezvcard.VCardVersion;
24 import ezvcard.io.MyFormattedNameProperty;
25 import ezvcard.io.MyFormattedNameProperty.MyFormattedNameScribe;
26 import ezvcard.io.ParseContext;
27 import ezvcard.io.scribe.CannotParseScribe;
28 import ezvcard.io.scribe.SkipMeScribe;
29 import ezvcard.io.scribe.VCardPropertyScribe;
30 import ezvcard.io.text.WriteContext;
31 import ezvcard.parameter.AddressType;
32 import ezvcard.parameter.EmailType;
33 import ezvcard.parameter.TelephoneType;
34 import ezvcard.parameter.VCardParameters;
35 import ezvcard.property.Anniversary;
36 import ezvcard.property.Birthday;
37 import ezvcard.property.FormattedName;
38 import ezvcard.property.Gender;
39 import ezvcard.property.Key;
40 import ezvcard.property.Language;
41 import ezvcard.property.Note;
42 import ezvcard.property.Organization;
43 import ezvcard.property.Url;
44 import ezvcard.property.VCardProperty;
45 import ezvcard.property.asserter.VCardAsserter;
46 import ezvcard.util.PartialDate;
47 import ezvcard.util.TelUri;
48
49 /*
50 Copyright (c) 2012-2023, Michael Angstadt
51 All rights reserved.
52
53 Redistribution and use in source and binary forms, with or without
54 modification, are permitted provided that the following conditions are met:
55
56 1. Redistributions of source code must retain the above copyright notice, this
57 list of conditions and the following disclaimer.
58 2. Redistributions in binary form must reproduce the above copyright notice,
59 this list of conditions and the following disclaimer in the documentation
60 and/or other materials provided with the distribution.
61
62 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
63 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
64 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
65 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
66 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
67 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
68 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
69 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
71 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72
73 The views and conclusions contained in the software and documentation are those
74 of the authors and should not be interpreted as representing official policies,
75 either expressed or implied, of the FreeBSD Project.
76 */
77
78 /**
79 * @author Michael Angstadt
80 */
81 public class JCardReaderTest {
82 @Rule
83 public TemporaryFolder tempFolder = new TemporaryFolder();
84
85 @Test
86 public void read_single() throws Throwable {
87 //@formatter:off
88 VCardAsserter asserter = readJson(
89 "[\"vcard\"," +
90 "[" +
91 "[\"version\", {}, \"text\", \"4.0\"]," +
92 "[\"fn\", {}, \"text\", \"John Doe\"]" +
93 "]" +
94 "]"
95 );
96
97 asserter.next(V4_0);
98
99 asserter.simpleProperty(FormattedName.class)
100 .value("John Doe")
101 .noMore();
102
103 asserter.done();
104 //@formatter:on
105 }
106
107 @Test
108 public void read_multiple() throws Throwable {
109 //@formatter:off
110 VCardAsserter asserter = readJson(
111 "[" +
112 "[\"vcard\"," +
113 "[" +
114 "[\"version\", {}, \"text\", \"4.0\"]," +
115 "[\"fn\", {}, \"text\", \"John Doe\"]" +
116 "]" +
117 "]," +
118 "[\"vcard\"," +
119 "[" +
120 "[\"version\", {}, \"text\", \"4.0\"]," +
121 "[\"fn\", {}, \"text\", \"Jane Doe\"]" +
122 "]" +
123 "]" +
124 "]"
125 );
126
127 asserter.next(V4_0);
128 asserter.simpleProperty(FormattedName.class)
129 .value("John Doe")
130 .noMore();
131
132 asserter.next(V4_0);
133 asserter.simpleProperty(FormattedName.class)
134 .value("Jane Doe")
135 .noMore();
136
137 asserter.done();
138 //@formatter:on
139 }
140
141 @Test
142 public void no_version() throws Exception {
143 //@formatter:off
144 VCardAsserter asserter = readJson(
145 "[\"vcard\"," +
146 "[" +
147 "[\"fn\", {}, \"text\", \"John Doe\"]" +
148 "]" +
149 "]"
150 );
151
152 asserter.next(V4_0); //default to 4.0
153
154 asserter.simpleProperty(FormattedName.class)
155 .value("John Doe")
156 .noMore();
157
158 asserter.warnings(29);
159 asserter.done();
160 //@formatter:on
161 }
162
163 @Test
164 public void invalid_version() throws Exception {
165 //@formatter:off
166 VCardAsserter asserter = readJson(
167 "[\"vcard\"," +
168 "[" +
169 "[\"version\", {}, \"text\", \"3.0\"]," +
170 "[\"fn\", {}, \"text\", \"John Doe\"]" +
171 "]" +
172 "]"
173 );
174
175 asserter.next(V4_0); //should still set the version to 4.0
176
177 asserter.simpleProperty(FormattedName.class)
178 .value("John Doe")
179 .noMore();
180
181 asserter.warnings(30);
182 asserter.done();
183 //@formatter:on
184 }
185
186 @Test
187 public void no_properties() throws Throwable {
188 //@formatter:off
189 VCardAsserter asserter = readJson(
190 "[\"vcard\"," +
191 "[" +
192 "]" +
193 "]"
194 );
195
196 asserter.next(V4_0); //default to 4.0
197 asserter.warnings(29); //missing VERSION property
198 asserter.done();
199 //@formatter:on
200 }
201
202 @Test
203 public void no_properties_multiple() throws Throwable {
204 //@formatter:off
205 VCardAsserter asserter = readJson(
206 "[" +
207 "[\"vcard\"," +
208 "[" +
209 "]" +
210 "]," +
211 "[\"vcard\"," +
212 "[" +
213 "]" +
214 "]" +
215 "]"
216 );
217
218 asserter.next(V4_0); //default to 4.0
219 asserter.warnings(29); //missing VERSION property
220
221 asserter.next(V4_0); //default to 4.0
222 asserter.warnings(29); //missing VERSION property
223
224 asserter.done();
225 //@formatter:on
226 }
227
228 @Test
229 public void extendedType() throws Throwable {
230 //@formatter:off
231 VCardAsserter asserter = readJson(
232 "[\"vcard\"," +
233 "[" +
234 "[\"version\", {}, \"text\", \"4.0\"]," +
235 "[\"x-type\", {}, \"text\", \"value\"]" +
236 "]" +
237 "]"
238 );
239
240 asserter.next(V4_0);
241
242 asserter.rawProperty("x-type")
243 .dataType(VCardDataType.TEXT)
244 .value("value")
245 .noMore();
246
247 asserter.done();
248 //@formatter:on
249 }
250
251 @Test
252 public void registerExtendedType() throws Throwable {
253 //@formatter:off
254 String json =
255 "[\"vcard\"," +
256 "[" +
257 "[\"version\", {}, \"text\", \"4.0\"]," +
258 "[\"x-type\", {}, \"text\", \"value\"]" +
259 "]" +
260 "]";
261 //@formatter:on
262
263 JCardReader reader = new JCardReader(json);
264 reader.registerScribe(new TypeForTestingScribe());
265
266 VCard vcard = reader.readNext();
267 assertVersion(V4_0, vcard);
268 assertPropertyCount(1, vcard);
269
270 TypeForTesting prop = vcard.getProperty(TypeForTesting.class);
271 assertEquals("value", prop.value.asSingle());
272
273 assertParseWarnings(reader);
274 assertNoMoreVCards(reader);
275 }
276
277 @Test
278 public void readExtendedType_override_standard_type_classes() throws Throwable {
279 //@formatter:off
280 String json =
281 "[\"vcard\"," +
282 "[" +
283 "[\"version\", {}, \"text\", \"4.0\"]," +
284 "[\"fn\", {}, \"text\", \"John Doe\"]" +
285 "]" +
286 "]";
287 //@formatter:on
288
289 JCardReader reader = new JCardReader(json);
290 reader.registerScribe(new MyFormattedNameScribe());
291
292 VCard vcard = reader.readNext();
293 assertPropertyCount(1, vcard);
294 assertVersion(V4_0, vcard);
295
296 MyFormattedNameProperty prop = vcard.getProperty(MyFormattedNameProperty.class);
297 assertEquals("JOHN DOE", prop.value);
298
299 assertParseWarnings(reader);
300 assertNoMoreVCards(reader);
301 }
302
303 @Test
304 public void skipMeException() throws Throwable {
305 //@formatter:off
306 String json =
307 "[\"vcard\"," +
308 "[" +
309 "[\"version\", {}, \"text\", \"4.0\"]," +
310 "[\"skipme\", {}, \"text\", \"value\"]" +
311 "]" +
312 "]";
313 //@formatter:on
314
315 JCardReader reader = new JCardReader(json);
316 reader.registerScribe(new SkipMeScribe());
317 VCardAsserter asserter = new VCardAsserter(reader);
318
319 asserter.next(V4_0);
320 asserter.warnings(22);
321 asserter.done();
322 }
323
324 @Test
325 public void cannotParseException() throws Throwable {
326 //@formatter:off
327 String json =
328 "[\"vcard\"," +
329 "[" +
330 "[\"version\", {}, \"text\", \"4.0\"]," +
331 "[\"cannotparse\", {}, \"text\", \"value\"]," +
332 "[\"x-foo\", {}, \"text\", \"value\"]" +
333 "]" +
334 "]";
335
336 JCardReader reader = new JCardReader(json);
337 reader.registerScribe(new CannotParseScribe());
338 VCardAsserter asserter = new VCardAsserter(reader);
339
340 asserter.next(V4_0);
341
342 asserter.rawProperty("x-foo")
343 .dataType(VCardDataType.TEXT)
344 .value("value")
345 .noMore();
346
347 asserter.rawProperty("cannotparse")
348 .dataType(VCardDataType.TEXT)
349 .value("value")
350 .noMore();
351
352 asserter.warnings(25);
353 asserter.done();
354 //@formatter:on
355 }
356
357 @Test
358 public void utf8() throws Throwable {
359 //@formatter:off
360 String json =
361 "[\"vcard\"," +
362 "[" +
363 "[\"version\", {}, \"text\", \"4.0\"]," +
364 "[\"note\", {}, \"text\", \"\u019dote\"]" +
365 "]" +
366 "]";
367
368 Path file = tempFolder.newFile().toPath();
369 try (Writer writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
370 writer.write(json);
371 }
372
373 JCardReader reader = new JCardReader(file);
374 VCardAsserter asserter = new VCardAsserter(reader);
375
376 asserter.next(V4_0);
377
378 asserter.simpleProperty(Note.class)
379 .value("\u019dote")
380 .noMore();
381
382 asserter.done();
383 //@formatter:on
384 }
385
386 private static class TypeForTesting extends VCardProperty {
387 public JCardValue value;
388
389 public TypeForTesting(JCardValue value) {
390 this.value = value;
391 }
392
393 @Override
394 public TypeForTesting copy() {
395 throw new UnsupportedOperationException("Copy method should not be used.");
396 }
397 }
398
399 private static class TypeForTestingScribe extends VCardPropertyScribe<TypeForTesting> {
400 public TypeForTestingScribe() {
401 super(TypeForTesting.class, "X-TYPE");
402 }
403
404 @Override
405 protected VCardDataType _defaultDataType(VCardVersion version) {
406 return VCardDataType.TEXT;
407 }
408
409 @Override
410 protected String _writeText(TypeForTesting property, WriteContext context) {
411 return "";
412 }
413
414 @Override
415 protected TypeForTesting _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
416 return new TypeForTesting(null);
417 }
418
419 @Override
420 protected TypeForTesting _parseJson(JCardValue value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
421 return new TypeForTesting(value);
422 }
423
424 }
425
426 @Test
427 public void jcard_example() throws Throwable {
428 JCardReader reader = new JCardReader(getClass().getResourceAsStream("jcard-example.json"));
429 VCardAsserter asserter = new VCardAsserter(reader);
430
431 asserter.next(V4_0);
432
433 //@formatter:off
434 asserter.simpleProperty(FormattedName.class)
435 .value("Simon Perreault")
436 .noMore();
437
438 asserter.structuredName()
439 .family("Perreault")
440 .given("Simon")
441 .suffixes("ing. jr", "M.Sc.")
442 .noMore();
443
444 asserter.dateProperty(Birthday.class)
445 .partialDate(PartialDate.builder().month(2).date(3).build())
446 .noMore();
447
448 asserter.dateProperty(Anniversary.class)
449 .date(OffsetDateTime.of(2009, 8, 8, 14, 30, 0, 0, ZoneOffset.ofHours(-5)))
450 .noMore();
451
452 asserter.property(Gender.class)
453 .expected(Gender.male())
454 .noMore();
455
456 asserter.simpleProperty(Language.class)
457 .value("fr")
458 .param("PREF", "1")
459 .next()
460 .value("en")
461 .param("PREF", "2")
462 .noMore();
463
464 asserter.listProperty(Organization.class)
465 .values("Viagenie")
466 .param("TYPE", "work")
467 .noMore();
468
469 asserter.address()
470 .extendedAddress("Suite D2-630")
471 .streetAddress("2875 Laurier")
472 .locality("Quebec")
473 .region("QC")
474 .postalCode("G1V 2M2")
475 .country("Canada")
476 .types(AddressType.WORK)
477 .noMore();
478
479 asserter.telephone()
480 .uri(new TelUri.Builder("+1-418-656-9254").extension("102").build())
481 .types(TelephoneType.WORK, TelephoneType.VOICE)
482 .param("PREF", "1")
483 .next()
484 .uri(new TelUri.Builder("+1-418-262-6501").build())
485 .types(TelephoneType.WORK, TelephoneType.CELL, TelephoneType.VOICE, TelephoneType.VIDEO, TelephoneType.TEXT)
486 .noMore();
487
488 asserter.email()
489 .value("[email protected]")
490 .types(EmailType.WORK)
491 .noMore();
492
493 asserter.geo()
494 .latitude(46.772673)
495 .longitude(-71.282945)
496 .param("TYPE", "work")
497 .noMore();
498
499 asserter.binaryProperty(Key.class)
500 .url("http://www.viagenie.ca/simon.perreault/simon.asc")
501 .param("TYPE","work")
502 .noMore();
503
504 asserter.timezone()
505 .offset(ZoneOffset.ofHours(-5))
506 .noMore();
507
508 asserter.simpleProperty(Url.class)
509 .value("http://nomis80.org")
510 .param("TYPE", "home")
511 .noMore();
512 //@formatter:on
513
514 asserter.validate().run();
515 asserter.done();
516 }
517
518 private static VCardAsserter readJson(String json) {
519 JCardReader reader = new JCardReader(json);
520 return new VCardAsserter(reader);
521 }
522 }