Debian Codegraph @

src/test/java/ezvcard/io/xml/XCardWriterTest.java

raw symbols SCIP
1 package ezvcard.io.xml;
2
3 import static ezvcard.VCardVersion.V4_0;
4 import static ezvcard.util.TestUtils.assertValidate;
5 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
6 import static org.junit.Assert.assertFalse;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.Assert.fail;
9
10 import java.io.IOException;
11 import java.io.StringWriter;
12 import java.nio.charset.StandardCharsets;
13 import java.nio.file.Path;
14 import java.time.ZoneOffset;
15
16 import org.custommonkey.xmlunit.XMLUnit;
17 import org.junit.Before;
18 import org.junit.BeforeClass;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.junit.rules.TemporaryFolder;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Node;
24 import org.xml.sax.SAXException;
25
26 import ezvcard.VCard;
27 import ezvcard.VCardDataType;
28 import ezvcard.VCardVersion;
29 import ezvcard.io.AgeProperty;
30 import ezvcard.io.AgeProperty.AgeScribe;
31 import ezvcard.io.EmbeddedVCardException;
32 import ezvcard.io.LuckyNumProperty;
33 import ezvcard.io.LuckyNumProperty.LuckyNumScribe;
34 import ezvcard.io.ParseContext;
35 import ezvcard.io.SalaryProperty;
36 import ezvcard.io.SalaryProperty.SalaryScribe;
37 import ezvcard.io.scribe.SkipMeScribe;
38 import ezvcard.io.scribe.VCardPropertyScribe;
39 import ezvcard.io.text.WriteContext;
40 import ezvcard.parameter.AddressType;
41 import ezvcard.parameter.EmailType;
42 import ezvcard.parameter.Encoding;
43 import ezvcard.parameter.ImageType;
44 import ezvcard.parameter.Pid;
45 import ezvcard.parameter.TelephoneType;
46 import ezvcard.parameter.VCardParameters;
47 import ezvcard.property.Address;
48 import ezvcard.property.Anniversary;
49 import ezvcard.property.Birthday;
50 import ezvcard.property.FormattedName;
51 import ezvcard.property.Gender;
52 import ezvcard.property.Geo;
53 import ezvcard.property.Key;
54 import ezvcard.property.Note;
55 import ezvcard.property.Photo;
56 import ezvcard.property.SkipMeProperty;
57 import ezvcard.property.StructuredName;
58 import ezvcard.property.Telephone;
59 import ezvcard.property.Timezone;
60 import ezvcard.property.VCardProperty;
61 import ezvcard.property.Xml;
62 import ezvcard.util.Gobble;
63 import ezvcard.util.PartialDate;
64 import ezvcard.util.TelUri;
65 import ezvcard.util.XmlUtils;
66
67 /*
68 Copyright (c) 2012-2023, Michael Angstadt
69 All rights reserved.
70
71 Redistribution and use in source and binary forms, with or without
72 modification, are permitted provided that the following conditions are met:
73
74 1. Redistributions of source code must retain the above copyright notice, this
75 list of conditions and the following disclaimer.
76 2. Redistributions in binary form must reproduce the above copyright notice,
77 this list of conditions and the following disclaimer in the documentation
78 and/or other materials provided with the distribution.
79
80 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
81 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
82 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
83 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
84 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
85 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
86 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
87 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
88 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
89 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90
91 The views and conclusions contained in the software and documentation are those
92 of the authors and should not be interpreted as representing official policies,
93 either expressed or implied, of the FreeBSD Project.
94 */
95
96 /**
97 * @author Michael Angstadt
98 */
99 public class XCardWriterTest {
100 @Rule
101 public TemporaryFolder tempFolder = new TemporaryFolder();
102
103 private StringWriter sw;
104 private XCardWriter writer;
105
106 @BeforeClass
107 public static void beforeClass() {
108 XMLUnit.setIgnoreAttributeOrder(true);
109 XMLUnit.setIgnoreWhitespace(true);
110 }
111
112 @Before
113 public void before() {
114 sw = new StringWriter();
115 writer = new XCardWriter(sw);
116 writer.setAddProdId(false);
117 }
118
119 @Test
120 public void write_single() throws Exception {
121 VCard vcard = new VCard();
122 vcard.setFormattedName("John Doe");
123 writer.write(vcard);
124
125 writer.close();
126
127 //@formatter:off
128 String expected =
129 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
130 "<vcard>" +
131 "<fn><text>John Doe</text></fn>" +
132 "</vcard>" +
133 "</vcards>";
134 //@formatter:on
135
136 assertOutput(expected);
137 }
138
139 @Test
140 public void write_multiple() throws Exception {
141 VCard vcard = new VCard();
142 vcard.setFormattedName("John Doe");
143 writer.write(vcard);
144
145 vcard = new VCard();
146 vcard.setFormattedName("Jane Doe");
147 writer.write(vcard);
148
149 writer.close();
150
151 //@formatter:off
152 String expected =
153 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
154 "<vcard>" +
155 "<fn><text>John Doe</text></fn>" +
156 "</vcard>" +
157 "<vcard>" +
158 "<fn><text>Jane Doe</text></fn>" +
159 "</vcard>" +
160 "</vcards>";
161 //@formatter:on
162
163 assertOutput(expected);
164 }
165
166 @Test
167 public void write_empty() throws Exception {
168 writer.close();
169
170 //@formatter:off
171 String expected =
172 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\" />";
173 //@formatter:on
174
175 assertOutput(expected);
176 }
177
178 @Test
179 public void write_xml_property() throws Exception {
180 VCard vcard = new VCard();
181 Xml xml = new Xml("<foo xmlns=\"http://example.com\" a=\"b\">bar<car/></foo>");
182 xml.setParameter("x-foo", "bar");
183 vcard.addXml(xml);
184 writer.write(vcard);
185
186 writer.close();
187
188 //@formatter:off
189 String expected =
190 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
191 "<vcard>" +
192 "<foo xmlns=\"http://example.com\" a=\"b\">" +
193 "<parameters xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
194 "<x-foo><unknown>bar</unknown></x-foo>" +
195 "</parameters>" +
196 "bar<car/>" +
197 "</foo>" +
198 "</vcard>" +
199 "</vcards>";
200 //@formatter:on
201
202 assertOutput(expected);
203 }
204
205 @Test
206 public void write_xml_property_null_value() throws Exception {
207 VCard vcard = new VCard();
208 vcard.addXml(new Xml((Document) null));
209 writer.write(vcard);
210
211 writer.close();
212
213 //@formatter:off
214 String expected =
215 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
216 "<vcard />" +
217 "</vcards>";
218 //@formatter:on
219
220 assertOutput(expected);
221 }
222
223 @Test
224 public void write_existing_dom_document() throws Exception {
225 Document document = XmlUtils.toDocument("<root><a /><b /></root>");
226 XCardWriter writer = new XCardWriter(document);
227 writer.setAddProdId(false);
228
229 VCard vcard = new VCard();
230 vcard.setFormattedName("John Doe");
231 writer.write(vcard);
232
233 writer.close();
234
235 //@formatter:off
236 String xml =
237 "<root>" +
238 "<a />" +
239 "<b />" +
240 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
241 "<vcard>" +
242 "<fn><text>John Doe</text></fn>" +
243 "</vcard>" +
244 "</vcards>" +
245 "</root>";
246 Document expected = XmlUtils.toDocument(xml);
247 //@formatter:on
248
249 assertXMLEqual(expected, document);
250 }
251
252 @Test
253 public void write_existing_dom_element() throws Exception {
254 Document document = XmlUtils.toDocument("<root><a /><b /></root>");
255 Node element = document.getFirstChild().getFirstChild();
256 XCardWriter writer = new XCardWriter(element);
257 writer.setAddProdId(false);
258
259 VCard vcard = new VCard();
260 vcard.setFormattedName("John Doe");
261 writer.write(vcard);
262
263 writer.close();
264
265 //@formatter:off
266 String xml =
267 "<root>" +
268 "<a>" +
269 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
270 "<vcard>" +
271 "<fn><text>John Doe</text></fn>" +
272 "</vcard>" +
273 "</vcards>" +
274 "</a>" +
275 "<b />" +
276 "</root>";
277 Document expected = XmlUtils.toDocument(xml);
278 //@formatter:on
279
280 assertXMLEqual(expected, document);
281 }
282
283 @Test
284 public void write_existing_vcards_document() throws Exception {
285 Document document = XmlUtils.toDocument("<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\" />");
286 XCardWriter writer = new XCardWriter(document);
287 writer.setAddProdId(false);
288
289 VCard vcard = new VCard();
290 vcard.setFormattedName("John Doe");
291 writer.write(vcard);
292
293 writer.close();
294
295 //@formatter:off
296 String xml =
297 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
298 "<vcard>" +
299 "<fn><text>John Doe</text></fn>" +
300 "</vcard>" +
301 "</vcards>";
302 Document expected = XmlUtils.toDocument(xml);
303 //@formatter:on
304
305 assertXMLEqual(expected, document);
306 }
307
308 @Test
309 public void write_existing_vcards_element() throws Exception {
310 Document document = XmlUtils.toDocument("<root><a><vcards xmlns=\"" + V4_0.getXmlNamespace() + "\" /></a><b /></root>");
311 Node element = document.getFirstChild().getFirstChild().getFirstChild();
312 XCardWriter writer = new XCardWriter(element);
313 writer.setAddProdId(false);
314
315 VCard vcard = new VCard();
316 vcard.setFormattedName("John Doe");
317 writer.write(vcard);
318
319 writer.close();
320
321 //@formatter:off
322 String xml =
323 "<root>" +
324 "<a>" +
325 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
326 "<vcard>" +
327 "<fn><text>John Doe</text></fn>" +
328 "</vcard>" +
329 "</vcards>" +
330 "</a>" +
331 "<b />" +
332 "</root>";
333 Document expected = XmlUtils.toDocument(xml);
334 //@formatter:on
335
336 assertXMLEqual(expected, document);
337 }
338
339 @Test
340 public void write_parameters() throws Exception {
341 writer.registerParameterDataType("X-INT", VCardDataType.INTEGER);
342
343 VCard vcard = new VCard();
344 Note note = new Note("This is a\nnote.");
345 note.setParameter(VCardParameters.ALTID, "value");
346 note.setParameter(VCardParameters.CALSCALE, "value");
347 note.setParameter(VCardParameters.GEO, "geo:123.456,234.567");
348 note.setParameter(VCardParameters.LABEL, "value");
349 note.setParameter(VCardParameters.LANGUAGE, "en");
350 note.setParameter(VCardParameters.MEDIATYPE, "text/plain");
351 note.getPids().add(new Pid(1, 1));
352 note.setParameter(VCardParameters.PREF, "1");
353 note.setParameter(VCardParameters.SORT_AS, "value");
354 note.setParameter(VCardParameters.TYPE, "home");
355 note.setParameter(VCardParameters.TZ, "America/New_York");
356 note.setParameter("X-CUSTOM", "xxx");
357 note.setParameter("X-INT", "11");
358 vcard.addNote(note);
359 writer.write(vcard);
360
361 writer.close();
362
363 //@formatter:off
364 String expected =
365 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
366 "<vcard>" +
367 "<note>" +
368 "<parameters>" +
369 "<altid><text>value</text></altid>" +
370 "<calscale><text>value</text></calscale>" +
371 "<geo><uri>geo:123.456,234.567</uri></geo>" +
372 "<label><text>value</text></label>" +
373 "<language><language-tag>en</language-tag></language>" +
374 "<mediatype><text>text/plain</text></mediatype>" +
375 "<pid><text>1.1</text></pid>" +
376 "<pref><integer>1</integer></pref>" +
377 "<sort-as><text>value</text></sort-as>" +
378 "<type><text>home</text></type>" +
379 "<tz><uri>America/New_York</uri></tz>" +
380 "<x-custom><unknown>xxx</unknown></x-custom>" +
381 "<x-int><integer>11</integer></x-int>" +
382 "</parameters>" +
383 "<text>This is a\nnote.</text>" +
384 "</note>" +
385 "</vcard>" +
386 "</vcards>";
387 //@formatter:on
388
389 assertOutput(expected);
390 }
391
392 @Test
393 public void write_group() throws Exception {
394 VCard vcard = new VCard();
395
396 vcard.setFormattedName("John Doe");
397
398 Note note = vcard.addNote("This is a\nnote.");
399 note.setGroup("group1");
400 note.setLanguage("en");
401
402 Photo photo = new Photo("http://example.com/image.jpg", ImageType.JPEG);
403 photo.setGroup("group1");
404 vcard.addPhoto(photo);
405
406 note = new Note("Bonjour.");
407 note.setGroup("group2");
408 vcard.addNote(note);
409
410 writer.write(vcard);
411 writer.close();
412
413 //@formatter:off
414 String expected =
415 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
416 "<vcard>" +
417 "<fn><text>John Doe</text></fn>" +
418 "<group name=\"group1\">" +
419 "<photo>" +
420 "<parameters>" +
421 "<mediatype><text>image/jpeg</text></mediatype>" +
422 "</parameters>" +
423 "<uri>http://example.com/image.jpg</uri>" +
424 "</photo>" +
425 "<note>" +
426 "<parameters>" +
427 "<language><language-tag>en</language-tag></language>" +
428 "</parameters>" +
429 "<text>This is a\nnote.</text>" +
430 "</note>" +
431 "</group>" +
432 "<group name=\"group2\">" +
433 "<note><text>Bonjour.</text></note>" +
434 "</group>" +
435 "</vcard>" +
436 "</vcards>";
437 //@formatter:on
438
439 assertOutput(expected);
440 }
441
442 @Test
443 public void write_prodId() throws Exception {
444 //default
445 {
446 StringWriter sw = new StringWriter();
447 XCardWriter writer = new XCardWriter(sw);
448
449 VCard vcard = new VCard();
450 vcard.setFormattedName("John Doe");
451 writer.write(vcard);
452
453 writer.close();
454
455 String actual = sw.toString();
456 assertTrue(actual.matches(".*?<prodid><text>.*?</text></prodid>.*"));
457 }
458
459 //false
460 {
461 StringWriter sw = new StringWriter();
462 XCardWriter writer = new XCardWriter(sw);
463 writer.setAddProdId(false);
464
465 VCard vcard = new VCard();
466 vcard.setFormattedName("John Doe");
467 writer.write(vcard);
468
469 writer.close();
470
471 String actual = sw.toString();
472 assertFalse(actual.matches(".*?<prodid><text>.*?</text></prodid>.*"));
473 }
474
475 //true
476 {
477 StringWriter sw = new StringWriter();
478 XCardWriter writer = new XCardWriter(sw);
479 writer.setAddProdId(true);
480
481 VCard vcard = new VCard();
482 vcard.setFormattedName("John Doe");
483 writer.write(vcard);
484
485 writer.close();
486
487 String actual = sw.toString();
488 assertTrue(actual.matches(".*?<prodid><text>.*?</text></prodid>.*"));
489 }
490 }
491
492 @Test
493 public void skipMeException() throws Exception {
494 writer.registerScribe(new SkipMeScribe());
495
496 VCard vcard = new VCard();
497 vcard.addProperty(new SkipMeProperty());
498 vcard.addExtendedProperty("x-foo", "value");
499 writer.write(vcard);
500 writer.close();
501
502 //@formatter:off
503 String expected =
504 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
505 "<vcard>" +
506 "<x-foo><unknown>value</unknown></x-foo>" +
507 "</vcard>" +
508 "</vcards>";
509 //@formatter:on
510
511 assertOutput(expected);
512 }
513
514 @Test
515 public void write_no_scribe_registered() throws Exception {
516 VCard vcard = new VCard();
517 vcard.setFormattedName("John Doe");
518 writer.write(vcard);
519
520 vcard = new VCard();
521 vcard.setFormattedName("Jane Doe");
522 vcard.addProperty(new SkipMeProperty());
523
524 try {
525 writer.write(vcard);
526 fail();
527 } catch (IllegalArgumentException e) {
528 //should be thrown
529 }
530
531 writer.close();
532
533 //the writer should check for scribes before writing anything to the stream
534 //@formatter:off
535 String expected =
536 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
537 "<vcard>" +
538 "<fn><text>John Doe</text></fn>" +
539 "</vcard>" +
540 "</vcards>";
541 //@formatter:on
542
543 assertOutput(expected);
544 }
545
546 @Test
547 public void write_extended_properties() throws Exception {
548 writer.registerScribe(new LuckyNumScribe());
549 writer.registerScribe(new SalaryScribe());
550 writer.registerScribe(new AgeScribe());
551
552 VCard vcard = new VCard();
553
554 //contains marshal methods and QName
555 LuckyNumProperty num = new LuckyNumProperty(24);
556 vcard.addProperty(num);
557
558 //contains marshal methods, but does not have a QName
559 SalaryProperty salary = new SalaryProperty(1000000);
560 vcard.addProperty(salary);
561
562 //does not contain marshal methods nor QName
563 AgeProperty age = new AgeProperty(22);
564 vcard.addProperty(age);
565
566 writer.write(vcard);
567 writer.close();
568
569 //@formatter:off
570 String expected =
571 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
572 "<vcard>" +
573 "<a:lucky-num xmlns:a=\"http://luckynum.com\">24</a:lucky-num>" +
574 "<x-salary>1000000</x-salary>" +
575 "<x-age><unknown>22</unknown></x-age>" +
576 "</vcard>" +
577 "</vcards>";
578 //@formatter:on
579
580 assertOutput(expected);
581 }
582
583 @Test
584 public void write_prettyPrint() throws Exception {
585 StringWriter sw = new StringWriter();
586 XCardWriter writer = new XCardWriter(sw, 2);
587 writer.setAddProdId(false);
588
589 VCard vcard = new VCard();
590 FormattedName fn = vcard.setFormattedName("John Doe");
591 fn.setParameter("x-foo", "bar");
592 Note note = vcard.addNote("note");
593 note.setGroup("group");
594 writer.write(vcard);
595
596 writer.close();
597
598 String actual = sw.toString();
599
600 String nl = "(\r\n|\n|\r)";
601 //@formatter:off
602 String expectedRegex =
603 "<\\?xml version=\"1.0\" encoding=\"(utf|UTF)-8\"\\?><vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + nl +
604 " <vcard>" + nl +
605 " <fn>" + nl +
606 " <parameters>" + nl +
607 " <x-foo>" + nl +
608 " <unknown>bar</unknown>" + nl +
609 " </x-foo>" + nl +
610 " </parameters>" + nl +
611 " <text>John Doe</text>" + nl +
612 " </fn>" + nl +
613 " <group name=\"group\">" + nl +
614 " <note>" + nl +
615 " <text>note</text>" + nl +
616 " </note>" + nl +
617 " </group>" + nl +
618 " </vcard>" + nl +
619 "</vcards>" + nl;
620 //@formatter:on
621
622 assertTrue(actual.matches(expectedRegex));
623 }
624
625 @Test
626 public void write_xmlVersion_default() throws Exception {
627 StringWriter sw = new StringWriter();
628 XCardWriter writer = new XCardWriter(sw);
629 VCard vcard = new VCard();
630 writer.write(vcard);
631 writer.close();
632
633 String xml = sw.toString();
634 assertTrue(xml.matches("(?i)<\\?xml.*?version=\"1.0\".*?\\?>.*"));
635 }
636
637 @Test
638 public void write_xmlVersion_1_1() throws Exception {
639 StringWriter sw = new StringWriter();
640 XCardWriter writer = new XCardWriter(sw, null, "1.1");
641 VCard vcard = new VCard();
642 writer.write(vcard);
643 writer.close();
644
645 String xml = sw.toString();
646 assertTrue(xml.matches("(?i)<\\?xml.*?version=\"1.1\".*?\\?>.*"));
647 }
648
649 @Test
650 public void write_xmlVersion_invalid() throws Exception {
651 StringWriter sw = new StringWriter();
652 XCardWriter writer = new XCardWriter(sw, null, "10.17");
653 VCard vcard = new VCard();
654 writer.write(vcard);
655 writer.close();
656
657 String xml = sw.toString();
658 assertTrue(xml.matches("(?i)<\\?xml.*?version=\"1.0\".*?\\?>.*"));
659 }
660
661 @Test
662 public void write_utf8() throws Exception {
663 Path file = tempFolder.newFile().toPath();
664 XCardWriter writer = new XCardWriter(file);
665 writer.setAddProdId(false);
666
667 VCard vcard = new VCard();
668 vcard.addNote("\u019dote");
669 writer.write(vcard);
670
671 writer.close();
672
673 String xml = new Gobble(file).asString(StandardCharsets.UTF_8);
674 assertTrue(xml.matches("(?i)<\\?xml.*?encoding=\"utf-8\".*?\\?>.*"));
675 assertTrue(xml.matches(".*?<note><text>\u019dote</text></note>.*"));
676 }
677
678 @Test
679 public void write_embedded_vcards_not_supported() throws Exception {
680 writer.registerScribe(new EmbeddedScribe());
681
682 VCard vcard = new VCard();
683 vcard.addProperty(new EmbeddedProperty());
684 writer.write(vcard);
685
686 writer.close();
687
688 //@formatter:off
689 String expected =
690 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
691 "<vcard />" +
692 "</vcards>";
693 //@formatter:on
694
695 assertOutput(expected);
696 }
697
698 @Test
699 public void write_quoted_printable_encoding_not_supported() throws Exception {
700 VCard vcard = new VCard();
701 FormattedName fn = vcard.setFormattedName("Ömür Öde");
702 fn.getParameters().setEncoding(Encoding.QUOTED_PRINTABLE);
703 fn.getParameters().setCharset("UTF-8");
704
705 writer.write(vcard);
706 writer.close();
707
708 String expected = //@formatter:off
709 "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" +
710 "<vcard>" +
711 "<fn><text>Ömür Öde</text></fn>" +
712 "</vcard>" +
713 "</vcards>"; //@formatter:on
714
715 assertOutput(expected);
716 }
717
718 @Test
719 public void write_rfc6351_example() throws Exception {
720 VCard vcard = new VCard();
721
722 vcard.setFormattedName("Simon Perreault");
723
724 StructuredName n = new StructuredName();
725 n.setFamily("Perreault");
726 n.setGiven("Simon");
727 n.getSuffixes().add("ing. jr");
728 n.getSuffixes().add("M.Sc.");
729 vcard.setStructuredName(n);
730
731 Birthday bday = new Birthday(PartialDate.builder().month(2).date(3).build());
732 vcard.setBirthday(bday);
733
734 Anniversary anniversary = new Anniversary(PartialDate.builder().year(2009).month(8).date(8).hour(14).minute(30).offset(ZoneOffset.ofHours(-5)).build());
735 vcard.setAnniversary(anniversary);
736
737 vcard.setGender(Gender.male());
738
739 vcard.addLanguage("fr").setPref(1);
740 vcard.addLanguage("en").setPref(2);
741
742 vcard.setOrganization("Viagenie").setType("work");
743
744 Address adr = new Address();
745 adr.setStreetAddress("2875 boul. Laurier, suite D2-630");
746 adr.setLocality("Quebec");
747 adr.setRegion("QC");
748 adr.setPostalCode("G1V 2M2");
749 adr.setCountry("Canada");
750 adr.getTypes().add(AddressType.WORK);
751 adr.setLabel("Simon Perreault\n2875 boul. Laurier, suite D2-630\nQuebec, QC, Canada\nG1V 2M2");
752 vcard.addAddress(adr);
753
754 TelUri telUri = new TelUri.Builder("+1-418-656-9254").extension("102").build();
755 Telephone tel = new Telephone(telUri);
756 tel.getTypes().add(TelephoneType.WORK);
757 tel.getTypes().add(TelephoneType.VOICE);
758 vcard.addTelephoneNumber(tel);
759
760 tel = new Telephone(new TelUri.Builder("+1-418-262-6501").build());
761 tel.getTypes().add(TelephoneType.WORK);
762 tel.getTypes().add(TelephoneType.TEXT);
763 tel.getTypes().add(TelephoneType.VOICE);
764 tel.getTypes().add(TelephoneType.CELL);
765 tel.getTypes().add(TelephoneType.VIDEO);
766 vcard.addTelephoneNumber(tel);
767
768 vcard.addEmail("[email protected]", EmailType.WORK);
769
770 Geo geo = new Geo(46.766336, -71.28955);
771 geo.setType("work");
772 vcard.setGeo(geo);
773
774 Key key = new Key("http://www.viagenie.ca/simon.perreault/simon.asc", null);
775 key.setType("work");
776 vcard.addKey(key);
777
778 vcard.setTimezone(new Timezone("America/Montreal"));
779
780 vcard.addUrl("http://nomis80.org").setType("home");
781
782 assertValidate(vcard).versions(V4_0).run();
783
784 assertExample(vcard, "rfc6351-example.xml");
785 }
786
787 private void assertOutput(String expected) throws SAXException, IOException {
788 String actual = sw.toString();
789 assertXMLEqual(expected, actual);
790 }
791
792 private void assertExample(VCard vcard, String exampleFileName) throws IOException, SAXException {
793 writer.write(vcard);
794 writer.close();
795
796 String expected = new Gobble(getClass().getResourceAsStream(exampleFileName)).asString();
797 String actual = sw.toString();
798
799 assertXMLEqual(expected, actual);
800 }
801
802 private static class EmbeddedProperty extends VCardProperty {
803 //empty
804 }
805
806 private static class EmbeddedScribe extends VCardPropertyScribe<EmbeddedProperty> {
807 public EmbeddedScribe() {
808 super(EmbeddedProperty.class, "EMBEDDED");
809 }
810
811 @Override
812 protected VCardDataType _defaultDataType(VCardVersion version) {
813 return null;
814 }
815
816 @Override
817 protected String _writeText(EmbeddedProperty property, WriteContext context) {
818 return null;
819 }
820
821 @Override
822 protected EmbeddedProperty _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
823 return null;
824 }
825
826 @Override
827 protected void _writeXml(EmbeddedProperty property, XCardElement parent) {
828 throw new EmbeddedVCardException(new VCard());
829 }
830 }
831 }