Debian Codegraph @

src/test/java/ezvcard/io/text/VCardWriterTest.java

raw symbols SCIP
1 package ezvcard.io.text;
2
3 import static ezvcard.util.TestUtils.assertValidate;
4 import static org.junit.Assert.assertEquals;
5
6 import java.io.StringWriter;
7 import java.nio.charset.Charset;
8 import java.nio.charset.StandardCharsets;
9 import java.nio.charset.UnmappableCharacterException;
10 import java.nio.file.Path;
11 import java.time.ZoneOffset;
12
13 import org.junit.Rule;
14 import org.junit.Test;
15 import org.junit.rules.TemporaryFolder;
16
17 import ezvcard.VCard;
18 import ezvcard.VCardDataType;
19 import ezvcard.VCardVersion;
20 import ezvcard.io.ParseContext;
21 import ezvcard.io.scribe.SkipMeScribe;
22 import ezvcard.io.scribe.VCardPropertyScribe;
23 import ezvcard.parameter.AddressType;
24 import ezvcard.parameter.EmailType;
25 import ezvcard.parameter.Encoding;
26 import ezvcard.parameter.ImageType;
27 import ezvcard.parameter.KeyType;
28 import ezvcard.parameter.TelephoneType;
29 import ezvcard.parameter.VCardParameters;
30 import ezvcard.property.Address;
31 import ezvcard.property.Agent;
32 import ezvcard.property.Anniversary;
33 import ezvcard.property.Birthday;
34 import ezvcard.property.FormattedName;
35 import ezvcard.property.Gender;
36 import ezvcard.property.Geo;
37 import ezvcard.property.Key;
38 import ezvcard.property.Logo;
39 import ezvcard.property.Note;
40 import ezvcard.property.Photo;
41 import ezvcard.property.SkipMeProperty;
42 import ezvcard.property.StructuredName;
43 import ezvcard.property.Telephone;
44 import ezvcard.property.Timezone;
45 import ezvcard.property.VCardProperty;
46 import ezvcard.util.Gobble;
47 import ezvcard.util.PartialDate;
48 import ezvcard.util.TelUri;
49
50 /*
51 Copyright (c) 2012-2023, Michael Angstadt
52 All rights reserved.
53
54 Redistribution and use in source and binary forms, with or without
55 modification, are permitted provided that the following conditions are met:
56
57 1. Redistributions of source code must retain the above copyright notice, this
58 list of conditions and the following disclaimer.
59 2. Redistributions in binary form must reproduce the above copyright notice,
60 this list of conditions and the following disclaimer in the documentation
61 and/or other materials provided with the distribution.
62
63 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
64 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
67 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
70 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73
74 The views and conclusions contained in the software and documentation are those
75 of the authors and should not be interpreted as representing official policies,
76 either expressed or implied, of the FreeBSD Project.
77 */
78
79 /**
80 * @author Michael Angstadt
81 */
82 @SuppressWarnings("resource")
83 public class VCardWriterTest {
84 @Rule
85 public TemporaryFolder tempFolder = new TemporaryFolder();
86
87 @Test
88 public void generalStructure() throws Throwable {
89 VCard vcard = new VCard();
90 vcard.setFormattedName("John Doe");
91 vcard.setProductId("ez-vcard");
92
93 StringWriter sw = new StringWriter();
94 VCardWriter vcw = new VCardWriter(sw, VCardVersion.V4_0);
95 vcw.setAddProdId(false);
96 vcw.write(vcard);
97 String actual = sw.toString();
98
99 //@formatter:off
100 String expected =
101 "BEGIN:VCARD\r\n" +
102 "VERSION:4.0\r\n" +
103 "PRODID:ez-vcard\r\n" +
104 "FN:John Doe\r\n" +
105 "END:VCARD\r\n";
106 //@formatter:on
107
108 assertEquals(actual, expected);
109 }
110
111 @Test
112 public void nestedVCard() throws Throwable {
113 VCard vcard = new VCard();
114
115 vcard.setFormattedName("Michael Angstadt");
116
117 VCard agentVcard = new VCard();
118 agentVcard.setFormattedName("Agent 007");
119 agentVcard.addNote("Make sure that it properly folds long lines which are part of a nested AGENT type in a version 2.1 vCard.");
120 Agent agent = new Agent(agentVcard);
121 vcard.setAgent(agent);
122
123 //i herd u liek AGENTs...
124 VCard secondAgentVCard = new VCard();
125 secondAgentVCard.setFormattedName("Agent 009");
126 secondAgentVCard.addNote("Make sure that it ALSO properly folds THIS long line because it's part of an AGENT that's inside of an AGENT.");
127 Agent secondAgent = new Agent(secondAgentVCard);
128 agentVcard.setAgent(secondAgent);
129
130 StringWriter sw = new StringWriter();
131 VCardWriter vcw = new VCardWriter(sw, VCardVersion.V2_1);
132 vcw.setAddProdId(false);
133 vcw.write(vcard);
134 String actual = sw.toString();
135
136 //@formatter:off
137 String expected =
138 "BEGIN:VCARD\r\n" +
139 "VERSION:2.1\r\n" +
140 "FN:Michael Angstadt\r\n" +
141 "AGENT:\r\n" + //nested types should not have X-GENERATOR
142 "BEGIN:VCARD\r\n" +
143 "VERSION:2.1\r\n" +
144 "FN:Agent 007\r\n" +
145 "NOTE:Make sure that it properly folds long lines which are part of a nested \r\n" +
146 " AGENT type in a version 2.1 vCard.\r\n" +
147 "AGENT:\r\n" +
148 "BEGIN:VCARD\r\n" +
149 "VERSION:2.1\r\n" +
150 "FN:Agent 009\r\n" +
151 "NOTE:Make sure that it ALSO properly folds THIS long line because it's part \r\n" +
152 " of an AGENT that's inside of an AGENT.\r\n" +
153 "END:VCARD\r\n" +
154 "END:VCARD\r\n" +
155 "END:VCARD\r\n";
156 //@formatter:on
157
158 assertEquals(expected, actual);
159 }
160
161 @Test
162 public void embeddedVCard() throws Throwable {
163 VCard vcard = new VCard();
164
165 vcard.setFormattedName("Michael Angstadt");
166
167 VCard agentVcard = new VCard();
168 agentVcard.setFormattedName("Agent 007");
169 Note note = agentVcard.addNote("Bonne soir�e, 007.");
170 note.setLanguage("fr");
171 Agent agent = new Agent(agentVcard);
172 vcard.setAgent(agent);
173
174 //i herd u liek AGENTs...
175 VCard secondAgentVCard = new VCard();
176 secondAgentVCard.setFormattedName("Agent 009");
177 note = secondAgentVCard.addNote("Good evening, 009.");
178 note.setLanguage("en");
179 Agent secondAgent = new Agent(secondAgentVCard);
180 agentVcard.setAgent(secondAgent);
181
182 StringWriter sw = new StringWriter();
183 VCardWriter vcw = new VCardWriter(sw, VCardVersion.V3_0);
184 vcw.getVObjectWriter().getFoldedLineWriter().setLineLength(null);
185 vcw.setAddProdId(false);
186 vcw.write(vcard);
187 String actual = sw.toString();
188
189 //@formatter:off
190 String expected =
191 "BEGIN:VCARD\r\n" +
192 "VERSION:3.0\r\n" +
193 "FN:Michael Angstadt\r\n" +
194 "AGENT:" + //nested types should not have PRODID
195 "BEGIN:VCARD\\n" +
196 "VERSION:3.0\\n" +
197 "FN:Agent 007\\n" +
198 "NOTE\\;LANGUAGE=fr:Bonne soir�e\\\\\\, 007.\\n" +
199 "AGENT:" +
200 "BEGIN:VCARD\\\\n" +
201 "VERSION:3.0\\\\n" +
202 "FN:Agent 009\\\\n" +
203 "NOTE\\\\\\;LANGUAGE=en:Good evening\\\\\\\\\\\\\\, 009.\\\\n" +
204 "END:VCARD\\\\n\\n" +
205 "END:VCARD\\n\r\n" +
206 "END:VCARD\r\n";
207 //@formatter:on
208
209 assertEquals(expected, actual);
210 }
211
212 @Test
213 public void skipMeException() throws Throwable {
214 VCard vcard = new VCard();
215 vcard.addProperty(new SkipMeProperty());
216 vcard.addExtendedProperty("X-FOO", "value");
217
218 StringWriter sw = new StringWriter();
219 VCardWriter vcw = new VCardWriter(sw, VCardVersion.V3_0);
220 vcw.setAddProdId(false);
221 vcw.registerScribe(new SkipMeScribe());
222 vcw.write(vcard);
223
224 String actual = sw.toString();
225
226 //@formatter:off
227 String expected =
228 "BEGIN:VCARD\r\n" +
229 "VERSION:3.0\r\n" +
230 "X-FOO:value\r\n" +
231 "END:VCARD\r\n";
232 //@formatter:on
233
234 assertEquals(actual, expected);
235 }
236
237 @Test
238 public void setVersionStrict_nested() throws Throwable {
239 VCard vcard = new VCard();
240
241 VCard agentVCard = new VCard();
242 agentVCard.setGender(Gender.male()); //only supported by 4.0
243 Agent agent = new Agent(agentVCard);
244 vcard.setAgent(agent);
245
246 StringWriter sw = new StringWriter();
247 VCardWriter vcw = new VCardWriter(sw, VCardVersion.V2_1);
248 vcw.setAddProdId(false);
249 vcw.setVersionStrict(false);
250 vcw.write(vcard);
251
252 String actual = sw.toString();
253
254 //@formatter:off
255 String expected =
256 "BEGIN:VCARD\r\n" +
257 "VERSION:2.1\r\n" +
258 "AGENT:\r\n" +
259 "BEGIN:VCARD\r\n" +
260 "VERSION:2.1\r\n" +
261 "GENDER:M\r\n" +
262 "END:VCARD\r\n" +
263 "END:VCARD\r\n";
264 //@formatter:on
265
266 assertEquals(actual, expected);
267 }
268
269 @Test
270 public void setVersionStrict_embedded() throws Throwable {
271 VCard vcard = new VCard();
272
273 VCard agentVCard = new VCard();
274 agentVCard.setGender(Gender.male()); //only supported by 4.0
275 Agent agent = new Agent(agentVCard);
276 vcard.setAgent(agent);
277
278 StringWriter sw = new StringWriter();
279 VCardWriter vcw = new VCardWriter(sw, VCardVersion.V3_0);
280 vcw.setAddProdId(false);
281 vcw.setVersionStrict(false);
282 vcw.write(vcard);
283
284 String actual = sw.toString();
285
286 //@formatter:off
287 String expected =
288 "BEGIN:VCARD\r\n" +
289 "VERSION:3.0\r\n" +
290 "AGENT:BEGIN:VCARD\\nVERSION:3.0\\nGENDER:M\\nEND:VCARD\\n\r\n" +
291 "END:VCARD\r\n";
292 //@formatter:on
293
294 assertEquals(actual, expected);
295 }
296
297 @Test
298 public void utf8() throws Throwable {
299 VCard vcard = new VCard();
300 vcard.addNote("\u019dote");
301
302 Path file = tempFolder.newFile().toPath();
303
304 //should be written as UTF-8
305 {
306 try (VCardWriter writer = new VCardWriter(file, false, VCardVersion.V4_0)) {
307 writer.setAddProdId(false);
308 writer.write(vcard);
309 }
310
311 //@formatter:off
312 String expected =
313 "BEGIN:VCARD\r\n" +
314 "VERSION:4.0\r\n" +
315 "NOTE:\u019dote\r\n" +
316 "END:VCARD\r\n";
317 //@formatter:on
318
319 String actual = new Gobble(file).asString(StandardCharsets.UTF_8);
320 assertEquals(expected, actual);
321 }
322
323 //should be written using default encoding
324 if (!Charset.defaultCharset().name().equalsIgnoreCase("UTF-8")) { //don't test if the local machine's default encoding is UTF-8
325 try (VCardWriter writer = new VCardWriter(file, VCardVersion.V3_0)) {
326 writer.setAddProdId(false);
327 writer.write(vcard);
328 } catch (UnmappableCharacterException e) {
329 /*
330 * This exception may be thrown if the character can't be
331 * written in the system's default character set. If so, end
332 * the test early.
333 */
334 return;
335 }
336
337 //@formatter:off
338 String expected =
339 "BEGIN:VCARD\r\n" +
340 "VERSION:3.0\r\n" +
341 "NOTE:?ote\r\n" +
342 "END:VCARD\r\n";
343 //@formatter:on
344
345 String actual = new Gobble(file).asString(StandardCharsets.UTF_8);
346 assertEquals(expected, actual);
347 }
348 }
349
350 @Test
351 public void date_time_properties_should_not_have_a_VALUE_parameter() throws Throwable {
352 class DateTestScribe<T extends VCardProperty> extends VCardPropertyScribe<T> {
353 private final VCardDataType dataType;
354
355 public DateTestScribe(Class<T> clazz, String name, VCardDataType dataType) {
356 super(clazz, name);
357 this.dataType = dataType;
358 }
359
360 @Override
361 protected VCardDataType _defaultDataType(VCardVersion version) {
362 return VCardDataType.DATE_AND_OR_TIME;
363 }
364
365 @Override
366 protected VCardDataType _dataType(T property, VCardVersion version) {
367 return dataType;
368 }
369
370 @Override
371 protected String _writeText(T property, WriteContext context) {
372 return "value";
373 }
374
375 @Override
376 protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
377 return null;
378 }
379 }
380
381 class DateProperty extends VCardProperty {
382 //empty
383 }
384 class DateTimeProperty extends VCardProperty {
385 //empty
386 }
387 class TimeProperty extends VCardProperty {
388 //empty
389 }
390 class DateAndOrTimeProperty extends VCardProperty {
391 //empty
392 }
393
394 VCard vcard = new VCard();
395 vcard.addProperty(new DateProperty());
396 vcard.addProperty(new DateTimeProperty());
397 vcard.addProperty(new TimeProperty());
398 vcard.addProperty(new DateAndOrTimeProperty());
399
400 StringWriter sw = new StringWriter();
401 VCardWriter vcw = new VCardWriter(sw, VCardVersion.V4_0);
402 vcw.registerScribe(new DateTestScribe<>(DateProperty.class, "DATE", VCardDataType.DATE));
403 vcw.registerScribe(new DateTestScribe<>(DateTimeProperty.class, "DATETIME", VCardDataType.DATE_TIME));
404 vcw.registerScribe(new DateTestScribe<>(TimeProperty.class, "TIME", VCardDataType.TIME));
405 vcw.registerScribe(new DateTestScribe<>(DateAndOrTimeProperty.class, "DATEANDORTIME", VCardDataType.DATE_AND_OR_TIME));
406 vcw.setAddProdId(false);
407 vcw.write(vcard);
408
409 String actual = sw.toString();
410
411 //@formatter:off
412 String expected =
413 "BEGIN:VCARD\r\n" +
414 "VERSION:4.0\r\n" +
415 "DATE:value\r\n" +
416 "DATETIME:value\r\n" +
417 "TIME:value\r\n" +
418 "DATEANDORTIME:value\r\n" +
419 "END:VCARD\r\n";
420 //@formatter:on
421
422 assertEquals(actual, expected);
423 }
424
425 @Test
426 public void setIncludeTrailingSemicolons() throws Throwable {
427 VCard vcard = new VCard();
428 StructuredName n = new StructuredName();
429 n.setFamily("Family");
430 vcard.setStructuredName(n);
431
432 {
433 StringWriter sw = new StringWriter();
434 VCardWriter writer = new VCardWriter(sw, VCardVersion.V2_1);
435 writer.setAddProdId(false);
436
437 writer.write(vcard);
438 writer.setIncludeTrailingSemicolons(true);
439 writer.write(vcard);
440 writer.setIncludeTrailingSemicolons(false);
441 writer.write(vcard);
442
443 String actual = sw.toString();
444
445 //@formatter:off
446 String expected =
447 "BEGIN:VCARD\r\n" +
448 "VERSION:2.1\r\n" +
449 "N:Family\r\n" +
450 "END:VCARD\r\n" +
451 "BEGIN:VCARD\r\n" +
452 "VERSION:2.1\r\n" +
453 "N:Family;;;;\r\n" +
454 "END:VCARD\r\n" +
455 "BEGIN:VCARD\r\n" +
456 "VERSION:2.1\r\n" +
457 "N:Family\r\n" +
458 "END:VCARD\r\n";
459 //@formatter:on
460
461 assertEquals(expected, actual);
462 }
463
464 {
465 StringWriter sw = new StringWriter();
466 VCardWriter writer = new VCardWriter(sw, VCardVersion.V3_0);
467 writer.setAddProdId(false);
468
469 writer.write(vcard);
470 writer.setIncludeTrailingSemicolons(true);
471 writer.write(vcard);
472 writer.setIncludeTrailingSemicolons(false);
473 writer.write(vcard);
474
475 String actual = sw.toString();
476
477 //@formatter:off
478 String expected =
479 "BEGIN:VCARD\r\n" +
480 "VERSION:3.0\r\n" +
481 "N:Family\r\n" +
482 "END:VCARD\r\n" +
483 "BEGIN:VCARD\r\n" +
484 "VERSION:3.0\r\n" +
485 "N:Family;;;;\r\n" +
486 "END:VCARD\r\n" +
487 "BEGIN:VCARD\r\n" +
488 "VERSION:3.0\r\n" +
489 "N:Family\r\n" +
490 "END:VCARD\r\n";
491 //@formatter:on
492
493 assertEquals(expected, actual);
494 }
495
496 {
497 StringWriter sw = new StringWriter();
498 VCardWriter writer = new VCardWriter(sw, VCardVersion.V4_0);
499 writer.setAddProdId(false);
500
501 writer.write(vcard);
502 writer.setIncludeTrailingSemicolons(true);
503 writer.write(vcard);
504 writer.setIncludeTrailingSemicolons(false);
505 writer.write(vcard);
506
507 String actual = sw.toString();
508
509 //@formatter:off
510 String expected =
511 "BEGIN:VCARD\r\n" +
512 "VERSION:4.0\r\n" +
513 "N:Family;;;;\r\n" +
514 "END:VCARD\r\n" +
515 "BEGIN:VCARD\r\n" +
516 "VERSION:4.0\r\n" +
517 "N:Family;;;;\r\n" +
518 "END:VCARD\r\n" +
519 "BEGIN:VCARD\r\n" +
520 "VERSION:4.0\r\n" +
521 "N:Family\r\n" +
522 "END:VCARD\r\n";
523 //@formatter:on
524
525 assertEquals(expected, actual);
526 }
527 }
528
529 @Test
530 public void setTargetApplication_outlook() throws Throwable {
531 VCard vcard = new VCard();
532 byte data[] = "foobar".getBytes();
533 vcard.addKey(new Key(data, KeyType.X509));
534 vcard.addPhoto(new Photo(data, ImageType.JPEG));
535 vcard.addLogo(new Logo("http://www.company.com/logo.png", ImageType.PNG));
536 vcard.addNote("note");
537
538 {
539 StringWriter sw = new StringWriter();
540 VCardWriter writer = new VCardWriter(sw, VCardVersion.V2_1);
541 writer.setAddProdId(false);
542 writer.write(vcard);
543 writer.setTargetApplication(TargetApplication.OUTLOOK);
544 writer.write(vcard);
545
546 String actual = sw.toString();
547
548 //@formatter:off
549 String expected =
550 "BEGIN:VCARD\r\n" +
551 "VERSION:2.1\r\n" +
552 "KEY;ENCODING=BASE64;TYPE=x509:Zm9vYmFy\r\n" +
553 "PHOTO;ENCODING=BASE64;TYPE=jpeg:Zm9vYmFy\r\n" +
554 "LOGO;TYPE=png;VALUE=url:http://www.company.com/logo.png\r\n" +
555 "NOTE:note\r\n" +
556 "END:VCARD\r\n" +
557 "BEGIN:VCARD\r\n" +
558 "VERSION:2.1\r\n" +
559 "KEY;ENCODING=BASE64;TYPE=x509:Zm9vYmFy\r\n" +
560 "\r\n" +
561 "PHOTO;ENCODING=BASE64;TYPE=jpeg:Zm9vYmFy\r\n" +
562 "\r\n" +
563 "LOGO;TYPE=png;VALUE=url:http://www.company.com/logo.png\r\n" +
564 "NOTE:note\r\n" +
565 "END:VCARD\r\n";
566 //@formatter:on
567
568 assertEquals(expected, actual);
569 }
570
571 {
572 StringWriter sw = new StringWriter();
573 VCardWriter writer = new VCardWriter(sw, VCardVersion.V3_0);
574 writer.setAddProdId(false);
575 writer.write(vcard);
576 writer.setTargetApplication(TargetApplication.OUTLOOK);
577 writer.write(vcard);
578
579 String actual = sw.toString();
580
581 //@formatter:off
582 String expected =
583 "BEGIN:VCARD\r\n" +
584 "VERSION:3.0\r\n" +
585 "KEY;ENCODING=b;TYPE=x509:Zm9vYmFy\r\n" +
586 "PHOTO;ENCODING=b;TYPE=jpeg:Zm9vYmFy\r\n" +
587 "LOGO;TYPE=png;VALUE=uri:http://www.company.com/logo.png\r\n" +
588 "NOTE:note\r\n" +
589 "END:VCARD\r\n" +
590 "BEGIN:VCARD\r\n" +
591 "VERSION:3.0\r\n" +
592 "KEY;ENCODING=b;TYPE=x509:Zm9vYmFy\r\n" +
593 "\r\n" +
594 "PHOTO;ENCODING=b;TYPE=jpeg:Zm9vYmFy\r\n" +
595 "\r\n" +
596 "LOGO;TYPE=png;VALUE=uri:http://www.company.com/logo.png\r\n" +
597 "NOTE:note\r\n" +
598 "END:VCARD\r\n";
599 //@formatter:on
600
601 assertEquals(expected, actual);
602 }
603
604 {
605 StringWriter sw = new StringWriter();
606 VCardWriter writer = new VCardWriter(sw, VCardVersion.V4_0);
607 writer.setAddProdId(false);
608 writer.write(vcard);
609 writer.setTargetApplication(TargetApplication.OUTLOOK);
610 writer.write(vcard);
611
612 String actual = sw.toString();
613
614 //@formatter:off
615 String expected =
616 "BEGIN:VCARD\r\n" +
617 "VERSION:4.0\r\n" +
618 "KEY:data:application/x509;base64,Zm9vYmFy\r\n" +
619 "PHOTO:data:image/jpeg;base64,Zm9vYmFy\r\n" +
620 "LOGO;MEDIATYPE=image/png:http://www.company.com/logo.png\r\n" +
621 "NOTE:note\r\n" +
622 "END:VCARD\r\n" +
623 "BEGIN:VCARD\r\n" +
624 "VERSION:4.0\r\n" +
625 "KEY:data:application/x509;base64,Zm9vYmFy\r\n" +
626 "PHOTO:data:image/jpeg;base64,Zm9vYmFy\r\n" +
627 "LOGO;MEDIATYPE=image/png:http://www.company.com/logo.png\r\n" +
628 "NOTE:note\r\n" +
629 "END:VCARD\r\n";
630 //@formatter:on
631
632 assertEquals(expected, actual);
633 }
634 }
635
636 @Test
637 public void quoted_printable_encoding() throws Throwable {
638 VCard vcard = new VCard();
639 FormattedName fn = vcard.setFormattedName("Ömür Öde");
640 fn.getParameters().setEncoding(Encoding.QUOTED_PRINTABLE);
641 fn.getParameters().setCharset("UTF-8");
642
643 VCardVersion version = VCardVersion.V2_1;
644 {
645 StringWriter sw = new StringWriter();
646 try (VCardWriter writer = new VCardWriter(sw, version)) {
647 writer.setAddProdId(false);
648 writer.write(vcard);
649 }
650 String actual = sw.toString();
651
652 String expected = //@formatter:off
653 "BEGIN:VCARD\r\n" +
654 "VERSION:2.1\r\n" +
655 "FN;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:=C3=96m=C3=BCr =C3=96de\r\n" +
656 "END:VCARD\r\n"; //@formatter:on
657
658 assertEquals(expected, actual);
659 }
660
661 version = VCardVersion.V3_0;
662 {
663 StringWriter sw = new StringWriter();
664 try (VCardWriter writer = new VCardWriter(sw, version)) {
665 writer.setAddProdId(false);
666 writer.write(vcard);
667 }
668 String actual = sw.toString();
669
670 String expected = //@formatter:off
671 "BEGIN:VCARD\r\n" +
672 "VERSION:3.0\r\n" +
673 "FN:Ömür Öde\r\n" +
674 "END:VCARD\r\n"; //@formatter:on
675
676 assertEquals(expected, actual);
677 }
678
679 version = VCardVersion.V4_0;
680 {
681 StringWriter sw = new StringWriter();
682 try (VCardWriter writer = new VCardWriter(sw, version)) {
683 writer.setAddProdId(false);
684 writer.write(vcard);
685 }
686 String actual = sw.toString();
687
688 String expected = //@formatter:off
689 "BEGIN:VCARD\r\n" +
690 "VERSION:4.0\r\n" +
691 "FN:Ömür Öde\r\n" +
692 "END:VCARD\r\n"; //@formatter:on
693
694 assertEquals(expected, actual);
695 }
696 }
697
698 @Test
699 public void rfc6350_example() throws Throwable {
700 VCard vcard = new VCard();
701
702 vcard.setFormattedName("Simon Perreault");
703
704 StructuredName n = new StructuredName();
705 n.setFamily("Perreault");
706 n.setGiven("Simon");
707 n.getSuffixes().add("ing. jr");
708 n.getSuffixes().add("M.Sc.");
709 vcard.setStructuredName(n);
710
711 Birthday bday = new Birthday(PartialDate.builder().month(2).date(3).build());
712 vcard.setBirthday(bday);
713
714 Anniversary anniversary = new Anniversary(PartialDate.builder().year(2009).month(8).date(8).hour(14).minute(30).offset(ZoneOffset.ofHours(-5)).build());
715 vcard.setAnniversary(anniversary);
716
717 vcard.setGender(Gender.male());
718
719 vcard.addLanguage("fr").setPref(1);
720 vcard.addLanguage("en").setPref(2);
721
722 vcard.setOrganization("Viagenie").setType("work");
723
724 Address adr = new Address();
725 adr.setExtendedAddress("Suite D2-630");
726 adr.setStreetAddress("2875 Laurier");
727 adr.setLocality("Quebec");
728 adr.setRegion("QC");
729 adr.setPostalCode("G1V 2M2");
730 adr.setCountry("Canada");
731 adr.getTypes().add(AddressType.WORK);
732 vcard.addAddress(adr);
733
734 TelUri telUri = new TelUri.Builder("+1-418-656-9254").extension("102").build();
735 Telephone tel = new Telephone(telUri);
736 tel.setPref(1);
737 tel.getTypes().add(TelephoneType.WORK);
738 tel.getTypes().add(TelephoneType.VOICE);
739 vcard.addTelephoneNumber(tel);
740
741 tel = new Telephone(new TelUri.Builder("+1-418-262-6501").build());
742 tel.getTypes().add(TelephoneType.WORK);
743 tel.getTypes().add(TelephoneType.VOICE);
744 tel.getTypes().add(TelephoneType.CELL);
745 tel.getTypes().add(TelephoneType.VIDEO);
746 tel.getTypes().add(TelephoneType.TEXT);
747 vcard.addTelephoneNumber(tel);
748
749 vcard.addEmail("[email protected]", EmailType.WORK);
750
751 Geo geo = new Geo(46.772673, -71.282945);
752 geo.setType("work");
753 vcard.setGeo(geo);
754
755 Key key = new Key("http://www.viagenie.ca/simon.perreault/simon.asc", null);
756 key.setType("work");
757 vcard.addKey(key);
758
759 vcard.setTimezone(new Timezone(ZoneOffset.ofHours(-5)));
760
761 vcard.addUrl("http://nomis80.org").setType("home");
762
763 assertValidate(vcard).versions(VCardVersion.V4_0).run();
764
765 StringWriter sw = new StringWriter();
766 try (VCardWriter writer = new VCardWriter(sw, VCardVersion.V4_0)) {
767 writer.setAddProdId(false);
768 writer.write(vcard);
769 }
770
771 //@formatter:off
772 String expected =
773 "BEGIN:VCARD\r\n" +
774 "VERSION:4.0\r\n" +
775 "FN:Simon Perreault\r\n" +
776 "N:Perreault;Simon;;;ing. jr,M.Sc.\r\n" +
777 "BDAY:--0203\r\n" +
778 "ANNIVERSARY:20090808T1430-0500\r\n" +
779 "GENDER:M\r\n" +
780 "LANG;PREF=1:fr\r\n" +
781 "LANG;PREF=2:en\r\n" +
782 "ORG;TYPE=work:Viagenie\r\n" +
783 "ADR;TYPE=work:;Suite D2-630;2875 Laurier;Quebec;QC;G1V 2M2;Canada\r\n" +
784 "TEL;PREF=1;TYPE=work,voice;VALUE=uri:tel:+1-418-656-9254;ext=102\r\n" +
785 "TEL;TYPE=work,voice,cell,video,text;VALUE=uri:tel:+1-418-262-6501\r\n" +
786 "EMAIL;TYPE=work:[email protected]\r\n" +
787 "GEO;TYPE=work:geo:46.772673,-71.282945\r\n" +
788 "KEY;TYPE=work:http://www.viagenie.ca/simon.perreault/simon.asc\r\n" +
789 "TZ;VALUE=utc-offset:-0500\r\n" +
790 "URL;TYPE=home:http://nomis80.org\r\n" +
791 "END:VCARD\r\n";
792 //@formatter:on
793
794 String actual = sw.toString();
795
796 assertEquals(expected, actual);
797 }
798
799 @Test
800 public void rfc2426_example() throws Throwable {
801 StringWriter sw = new StringWriter();
802 try (VCardWriter writer = new VCardWriter(sw, VCardVersion.V3_0)) {
803 writer.getVObjectWriter().getFoldedLineWriter().setLineLength(null);
804 writer.setAddProdId(false);
805
806 {
807 VCard vcard = new VCard();
808
809 vcard.setFormattedName("Frank Dawson");
810
811 vcard.setOrganization("Lotus Development Corporation");
812
813 Address adr = new Address();
814 adr.setStreetAddress("6544 Battleford Drive");
815 adr.setLocality("Raleigh");
816 adr.setRegion("NC");
817 adr.setPostalCode("27613-3502");
818 adr.setCountry("U.S.A.");
819 adr.getTypes().add(AddressType.WORK);
820 adr.getTypes().add(AddressType.POSTAL);
821 adr.getTypes().add(AddressType.PARCEL);
822 vcard.addAddress(adr);
823
824 vcard.addTelephoneNumber("+1-919-676-9515", TelephoneType.VOICE, TelephoneType.MSG, TelephoneType.WORK);
825 vcard.addTelephoneNumber("+1-919-676-9564", TelephoneType.FAX, TelephoneType.WORK);
826
827 vcard.addEmail("[email protected]", EmailType.INTERNET, EmailType.PREF);
828 vcard.addEmail("[email protected]", EmailType.INTERNET);
829
830 vcard.addUrl("http://home.earthlink.net/�fdawson");
831
832 assertValidate(vcard).versions(VCardVersion.V3_0).prop(null, 0).run();
833
834 writer.write(vcard);
835 }
836
837 {
838 VCard vcard = new VCard();
839
840 vcard.setFormattedName("Tim Howes");
841
842 vcard.setOrganization("Netscape Communications Corp.");
843
844 Address adr = new Address();
845 adr.setStreetAddress("501 E. Middlefield Rd.");
846 adr.setLocality("Mountain View");
847 adr.setRegion("CA");
848 adr.setPostalCode("94043");
849 adr.setCountry("U.S.A.");
850 adr.getTypes().add(AddressType.WORK);
851 vcard.addAddress(adr);
852
853 vcard.addTelephoneNumber("+1-415-937-3419", TelephoneType.VOICE, TelephoneType.MSG, TelephoneType.WORK);
854 vcard.addTelephoneNumber("+1-415-528-4164", TelephoneType.FAX, TelephoneType.WORK);
855
856 vcard.addEmail("[email protected]", EmailType.INTERNET);
857
858 assertValidate(vcard).versions(VCardVersion.V3_0).prop(null, 0).run();
859
860 writer.write(vcard);
861 }
862 }
863
864 //@formatter:off
865 String expected =
866 "BEGIN:VCARD\r\n" +
867 "VERSION:3.0\r\n" +
868 "FN:Frank Dawson\r\n" +
869 "ORG:Lotus Development Corporation\r\n" +
870 "ADR;TYPE=work,postal,parcel:;;6544 Battleford Drive;Raleigh;NC;27613-3502;U.S.A.\r\n" +
871 "TEL;TYPE=voice,msg,work:+1-919-676-9515\r\n" +
872 "TEL;TYPE=fax,work:+1-919-676-9564\r\n" +
873 "EMAIL;TYPE=internet,pref:[email protected]\r\n" +
874 "EMAIL;TYPE=internet:[email protected]\r\n" +
875 "URL:http://home.earthlink.net/�fdawson\r\n" +
876 "END:VCARD\r\n" +
877 "BEGIN:VCARD\r\n" +
878 "VERSION:3.0\r\n" +
879 "FN:Tim Howes\r\n" +
880 "ORG:Netscape Communications Corp.\r\n" +
881 "ADR;TYPE=work:;;501 E. Middlefield Rd.;Mountain View;CA;94043;U.S.A.\r\n" +
882 "TEL;TYPE=voice,msg,work:+1-415-937-3419\r\n" +
883 "TEL;TYPE=fax,work:+1-415-528-4164\r\n" +
884 "EMAIL;TYPE=internet:[email protected]\r\n" +
885 "END:VCARD\r\n";
886 //@formatter:on
887
888 String actual = sw.toString();
889
890 assertEquals(expected, actual);
891 }
892 }