| 1 |
package ezvcard.io.xml; |
| 2 |
|
| 3 |
import static ezvcard.VCardVersion.V4_0; |
| 4 |
import static ezvcard.util.StringUtils.NEWLINE; |
| 5 |
import static ezvcard.util.TestUtils.assertValidate; |
| 6 |
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; |
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
import static org.junit.Assert.assertTrue; |
| 9 |
|
| 10 |
import java.io.IOException; |
| 11 |
import java.io.InputStreamReader; |
| 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.BeforeClass; |
| 18 |
import org.junit.Rule; |
| 19 |
import org.junit.Test; |
| 20 |
import org.junit.rules.TemporaryFolder; |
| 21 |
import org.w3c.dom.Document; |
| 22 |
import org.xml.sax.SAXException; |
| 23 |
|
| 24 |
import ezvcard.Ezvcard; |
| 25 |
import ezvcard.VCard; |
| 26 |
import ezvcard.VCardDataType; |
| 27 |
import ezvcard.VCardVersion; |
| 28 |
import ezvcard.io.AgeProperty; |
| 29 |
import ezvcard.io.AgeProperty.AgeScribe; |
| 30 |
import ezvcard.io.EmbeddedVCardException; |
| 31 |
import ezvcard.io.LuckyNumProperty; |
| 32 |
import ezvcard.io.LuckyNumProperty.LuckyNumScribe; |
| 33 |
import ezvcard.io.ParseContext; |
| 34 |
import ezvcard.io.SalaryProperty; |
| 35 |
import ezvcard.io.SalaryProperty.SalaryScribe; |
| 36 |
import ezvcard.io.StreamReader; |
| 37 |
import ezvcard.io.scribe.SkipMeScribe; |
| 38 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 39 |
import ezvcard.io.text.WriteContext; |
| 40 |
import ezvcard.io.xml.XCardDocument.XCardDocumentStreamWriter; |
| 41 |
import ezvcard.parameter.AddressType; |
| 42 |
import ezvcard.parameter.EmailType; |
| 43 |
import ezvcard.parameter.Encoding; |
| 44 |
import ezvcard.parameter.ImageType; |
| 45 |
import ezvcard.parameter.Pid; |
| 46 |
import ezvcard.parameter.TelephoneType; |
| 47 |
import ezvcard.parameter.VCardParameters; |
| 48 |
import ezvcard.property.Address; |
| 49 |
import ezvcard.property.Anniversary; |
| 50 |
import ezvcard.property.Birthday; |
| 51 |
import ezvcard.property.FormattedName; |
| 52 |
import ezvcard.property.Gender; |
| 53 |
import ezvcard.property.Geo; |
| 54 |
import ezvcard.property.Key; |
| 55 |
import ezvcard.property.Language; |
| 56 |
import ezvcard.property.Note; |
| 57 |
import ezvcard.property.Organization; |
| 58 |
import ezvcard.property.Photo; |
| 59 |
import ezvcard.property.ProductId; |
| 60 |
import ezvcard.property.SkipMeProperty; |
| 61 |
import ezvcard.property.StructuredName; |
| 62 |
import ezvcard.property.Telephone; |
| 63 |
import ezvcard.property.Timezone; |
| 64 |
import ezvcard.property.Url; |
| 65 |
import ezvcard.property.VCardProperty; |
| 66 |
import ezvcard.property.Xml; |
| 67 |
import ezvcard.property.asserter.VCardAsserter; |
| 68 |
import ezvcard.util.Gobble; |
| 69 |
import ezvcard.util.PartialDate; |
| 70 |
import ezvcard.util.TelUri; |
| 71 |
import ezvcard.util.XmlUtils; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public class XCardDocumentTest { |
| 106 |
@Rule |
| 107 |
public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 108 |
|
| 109 |
@BeforeClass |
| 110 |
public static void beforeClass() { |
| 111 |
XMLUnit.setIgnoreAttributeOrder(true); |
| 112 |
XMLUnit.setIgnoreWhitespace(true); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void getVCards_multiple() throws Throwable { |
| 117 |
|
| 118 |
VCardAsserter asserter = readXml( |
| 119 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 120 |
"<vcard>" + |
| 121 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 122 |
"</vcard>" + |
| 123 |
"<vcard>" + |
| 124 |
"<fn><text>Dr. Lisa Cuddy M.D.</text></fn>" + |
| 125 |
"</vcard>" + |
| 126 |
"</vcards>" |
| 127 |
); |
| 128 |
|
| 129 |
asserter.next(V4_0); |
| 130 |
asserter.simpleProperty(FormattedName.class) |
| 131 |
.value("Dr. Gregory House M.D.") |
| 132 |
.noMore(); |
| 133 |
|
| 134 |
asserter.next(V4_0); |
| 135 |
asserter.simpleProperty(FormattedName.class) |
| 136 |
.value("Dr. Lisa Cuddy M.D.") |
| 137 |
.noMore(); |
| 138 |
|
| 139 |
asserter.done(); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void getVCards_single() throws Throwable { |
| 145 |
|
| 146 |
VCardAsserter asserter = readXml( |
| 147 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 148 |
"<vcard>" + |
| 149 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 150 |
"</vcard>" + |
| 151 |
"</vcards>" |
| 152 |
); |
| 153 |
|
| 154 |
asserter.next(V4_0); |
| 155 |
asserter.simpleProperty(FormattedName.class) |
| 156 |
.value("Dr. Gregory House M.D.") |
| 157 |
.noMore(); |
| 158 |
|
| 159 |
asserter.done(); |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void add_basicType() throws Throwable { |
| 165 |
VCard vcard = new VCard(); |
| 166 |
vcard.setFormattedName("John Doe"); |
| 167 |
|
| 168 |
XCardDocument xcard = new XCardDocument(); |
| 169 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 170 |
writer.setAddProdId(false); |
| 171 |
writer.write(vcard); |
| 172 |
|
| 173 |
Document actual = xcard.getDocument(); |
| 174 |
|
| 175 |
|
| 176 |
String xml = |
| 177 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 178 |
"<vcard>" + |
| 179 |
"<fn><text>John Doe</text></fn>" + |
| 180 |
"</vcard>" + |
| 181 |
"</vcards>"; |
| 182 |
Document expected = XmlUtils.toDocument(xml); |
| 183 |
|
| 184 |
|
| 185 |
assertXMLEqual(expected, actual); |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void add_parameters() throws Throwable { |
| 190 |
VCard vcard = new VCard(); |
| 191 |
Note note = new Note("This is a\nnote."); |
| 192 |
note.setParameter(VCardParameters.ALTID, "value"); |
| 193 |
note.setParameter(VCardParameters.CALSCALE, "value"); |
| 194 |
note.setParameter(VCardParameters.GEO, "geo:123.456,234.567"); |
| 195 |
note.setParameter(VCardParameters.LABEL, "value"); |
| 196 |
note.setParameter(VCardParameters.LANGUAGE, "en"); |
| 197 |
note.setParameter(VCardParameters.MEDIATYPE, "text/plain"); |
| 198 |
note.getPids().add(new Pid(1, 1)); |
| 199 |
note.setParameter(VCardParameters.PREF, "1"); |
| 200 |
note.setParameter(VCardParameters.SORT_AS, "value"); |
| 201 |
note.setParameter(VCardParameters.TYPE, "home"); |
| 202 |
note.setParameter(VCardParameters.TZ, "America/New_York"); |
| 203 |
note.setParameter("X-CUSTOM", "xxx"); |
| 204 |
note.setParameter("X-INT", "11"); |
| 205 |
vcard.addNote(note); |
| 206 |
|
| 207 |
XCardDocument xcard = new XCardDocument(); |
| 208 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 209 |
writer.setAddProdId(false); |
| 210 |
writer.registerParameterDataType("X-INT", VCardDataType.INTEGER); |
| 211 |
writer.registerParameterDataType("X-CUSTOM", VCardDataType.BOOLEAN); |
| 212 |
writer.registerParameterDataType("X-CUSTOM", null); |
| 213 |
writer.write(vcard); |
| 214 |
|
| 215 |
Document actual = xcard.getDocument(); |
| 216 |
|
| 217 |
|
| 218 |
String xml = |
| 219 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 220 |
"<vcard>" + |
| 221 |
"<note>" + |
| 222 |
"<parameters>" + |
| 223 |
"<altid><text>value</text></altid>" + |
| 224 |
"<calscale><text>value</text></calscale>" + |
| 225 |
"<geo><uri>geo:123.456,234.567</uri></geo>" + |
| 226 |
"<label><text>value</text></label>" + |
| 227 |
"<language><language-tag>en</language-tag></language>" + |
| 228 |
"<mediatype><text>text/plain</text></mediatype>" + |
| 229 |
"<pid><text>1.1</text></pid>" + |
| 230 |
"<pref><integer>1</integer></pref>" + |
| 231 |
"<sort-as><text>value</text></sort-as>" + |
| 232 |
"<type><text>home</text></type>" + |
| 233 |
"<tz><uri>America/New_York</uri></tz>" + |
| 234 |
"<x-custom><unknown>xxx</unknown></x-custom>" + |
| 235 |
"<x-int><integer>11</integer></x-int>" + |
| 236 |
"</parameters>" + |
| 237 |
"<text>This is a\nnote.</text>" + |
| 238 |
"</note>" + |
| 239 |
"</vcard>" + |
| 240 |
"</vcards>"; |
| 241 |
Document expected = XmlUtils.toDocument(xml); |
| 242 |
|
| 243 |
|
| 244 |
assertXMLEqual(expected, actual); |
| 245 |
} |
| 246 |
|
| 247 |
@Test |
| 248 |
public void read_parameters() throws Exception { |
| 249 |
|
| 250 |
VCardAsserter asserter = readXml( |
| 251 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 252 |
"<vcard>" + |
| 253 |
|
| 254 |
"<note>" + |
| 255 |
"<text>Note 1</text>" + |
| 256 |
"</note>" + |
| 257 |
|
| 258 |
|
| 259 |
"<note>" + |
| 260 |
"<parameters>" + |
| 261 |
"<altid><text>1</text></altid>" + |
| 262 |
"</parameters>" + |
| 263 |
"<text>Hello world!</text>" + |
| 264 |
"</note>" + |
| 265 |
|
| 266 |
|
| 267 |
"<note>" + |
| 268 |
"<parameters>" + |
| 269 |
"<altid>1</altid>" + |
| 270 |
"</parameters>" + |
| 271 |
"<text>Hallo Welt!</text>" + |
| 272 |
"</note>" + |
| 273 |
|
| 274 |
|
| 275 |
"<note>" + |
| 276 |
"<parameters>" + |
| 277 |
"<altid><text>1</text></altid>" + |
| 278 |
"<language><language-tag>fr</language-tag></language>" + |
| 279 |
"</parameters>" + |
| 280 |
"<text>Bonjour tout le monde!</text>" + |
| 281 |
"</note>" + |
| 282 |
|
| 283 |
|
| 284 |
"<tel>" + |
| 285 |
"<parameters>" + |
| 286 |
"<type>" + |
| 287 |
"<text>work</text>" + |
| 288 |
"<text>voice</text>" + |
| 289 |
"</type>" + |
| 290 |
"</parameters>" + |
| 291 |
"<uri>tel:+1-555-555-1234</uri>" + |
| 292 |
"</tel>" + |
| 293 |
"</vcard>" + |
| 294 |
"</vcards>" |
| 295 |
); |
| 296 |
|
| 297 |
asserter.next(V4_0); |
| 298 |
|
| 299 |
asserter.simpleProperty(Note.class) |
| 300 |
.value("Note 1") |
| 301 |
.next() |
| 302 |
.value("Hello world!") |
| 303 |
.param("ALTID", "1") |
| 304 |
.next() |
| 305 |
.value("Hallo Welt!") |
| 306 |
.next() |
| 307 |
.value("Bonjour tout le monde!") |
| 308 |
.param("ALTID", "1") |
| 309 |
.param("LANGUAGE", "fr") |
| 310 |
.noMore(); |
| 311 |
|
| 312 |
asserter.telephone() |
| 313 |
.uri(new TelUri.Builder("+1-555-555-1234").build()) |
| 314 |
.types(TelephoneType.WORK, TelephoneType.VOICE) |
| 315 |
.noMore(); |
| 316 |
|
| 317 |
asserter.done(); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
@Test |
| 322 |
public void add_group() throws Throwable { |
| 323 |
VCard vcard = new VCard(); |
| 324 |
|
| 325 |
vcard.setFormattedName("John Doe"); |
| 326 |
|
| 327 |
Note note = vcard.addNote("This is a\nnote."); |
| 328 |
note.setGroup("group1"); |
| 329 |
note.setLanguage("en"); |
| 330 |
|
| 331 |
Photo photo = new Photo("http://example.com/image.jpg", ImageType.JPEG); |
| 332 |
photo.setGroup("group1"); |
| 333 |
vcard.addPhoto(photo); |
| 334 |
|
| 335 |
note = vcard.addNote("Bonjour."); |
| 336 |
note.setGroup("group2"); |
| 337 |
|
| 338 |
XCardDocument xcard = new XCardDocument(); |
| 339 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 340 |
writer.setAddProdId(false); |
| 341 |
writer.write(vcard); |
| 342 |
|
| 343 |
Document actual = xcard.getDocument(); |
| 344 |
|
| 345 |
|
| 346 |
String xml = |
| 347 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 348 |
"<vcard>" + |
| 349 |
"<fn><text>John Doe</text></fn>" + |
| 350 |
"<group name=\"group1\">" + |
| 351 |
"<photo>" + |
| 352 |
"<parameters>" + |
| 353 |
"<mediatype><text>image/jpeg</text></mediatype>" + |
| 354 |
"</parameters>" + |
| 355 |
"<uri>http://example.com/image.jpg</uri>" + |
| 356 |
"</photo>" + |
| 357 |
"<note>" + |
| 358 |
"<parameters>" + |
| 359 |
"<language><language-tag>en</language-tag></language>" + |
| 360 |
"</parameters>" + |
| 361 |
"<text>This is a\nnote.</text>" + |
| 362 |
"</note>" + |
| 363 |
"</group>" + |
| 364 |
"<group name=\"group2\">" + |
| 365 |
"<note><text>Bonjour.</text></note>" + |
| 366 |
"</group>" + |
| 367 |
"</vcard>" + |
| 368 |
"</vcards>"; |
| 369 |
Document expected = XmlUtils.toDocument(xml); |
| 370 |
|
| 371 |
|
| 372 |
assertXMLEqual(expected, actual); |
| 373 |
} |
| 374 |
|
| 375 |
@Test |
| 376 |
public void read_groups() throws Exception { |
| 377 |
|
| 378 |
VCardAsserter asserter = readXml( |
| 379 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 380 |
"<vcard>" + |
| 381 |
"<group name=\"item1\">" + |
| 382 |
"<fn><text>John Doe</text></fn>" + |
| 383 |
"<note><text>Hello world!</text></note>" + |
| 384 |
"</group>" + |
| 385 |
"<group>" + |
| 386 |
"<prodid><text>no name attribute</text></prodid>" + |
| 387 |
"</group>" + |
| 388 |
"<note><text>A property without a group</text></note>" + |
| 389 |
"</vcard>" + |
| 390 |
"</vcards>" |
| 391 |
); |
| 392 |
|
| 393 |
asserter.next(V4_0); |
| 394 |
|
| 395 |
asserter.simpleProperty(FormattedName.class) |
| 396 |
.group("item1") |
| 397 |
.value("John Doe") |
| 398 |
.noMore(); |
| 399 |
|
| 400 |
asserter.simpleProperty(Note.class) |
| 401 |
.group("item1") |
| 402 |
.value("Hello world!") |
| 403 |
.next() |
| 404 |
.value("A property without a group") |
| 405 |
.noMore(); |
| 406 |
|
| 407 |
asserter.simpleProperty(ProductId.class) |
| 408 |
.value("no name attribute") |
| 409 |
.noMore(); |
| 410 |
|
| 411 |
asserter.done(); |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
@Test |
| 416 |
public void add_multiple() throws Throwable { |
| 417 |
VCard vcard1 = new VCard(); |
| 418 |
vcard1.setFormattedName("John Doe"); |
| 419 |
|
| 420 |
VCard vcard2 = new VCard(); |
| 421 |
vcard2.addNote("Hello world!"); |
| 422 |
|
| 423 |
XCardDocument xcard = new XCardDocument(); |
| 424 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 425 |
writer.setAddProdId(false); |
| 426 |
writer.write(vcard1); |
| 427 |
writer.write(vcard2); |
| 428 |
|
| 429 |
Document actual = xcard.getDocument(); |
| 430 |
|
| 431 |
|
| 432 |
String xml = |
| 433 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 434 |
"<vcard>" + |
| 435 |
"<fn><text>John Doe</text></fn>" + |
| 436 |
"</vcard>" + |
| 437 |
"<vcard>" + |
| 438 |
"<note><text>Hello world!</text></note>" + |
| 439 |
"</vcard>" + |
| 440 |
"</vcards>"; |
| 441 |
Document expected = XmlUtils.toDocument(xml); |
| 442 |
|
| 443 |
|
| 444 |
assertXMLEqual(expected, actual); |
| 445 |
} |
| 446 |
|
| 447 |
@Test |
| 448 |
public void add_skipMeException() throws Throwable { |
| 449 |
VCard vcard = new VCard(); |
| 450 |
vcard.addProperty(new SkipMeProperty()); |
| 451 |
vcard.addExtendedProperty("x-foo", "value"); |
| 452 |
|
| 453 |
XCardDocument xcard = new XCardDocument(); |
| 454 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 455 |
writer.setAddProdId(false); |
| 456 |
writer.registerScribe(new SkipMeScribe()); |
| 457 |
writer.write(vcard); |
| 458 |
|
| 459 |
Document actual = xcard.getDocument(); |
| 460 |
|
| 461 |
|
| 462 |
String xml = |
| 463 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 464 |
"<vcard>" + |
| 465 |
"<x-foo><unknown>value</unknown></x-foo>" + |
| 466 |
"</vcard>" + |
| 467 |
"</vcards>"; |
| 468 |
Document expected = XmlUtils.toDocument(xml); |
| 469 |
|
| 470 |
|
| 471 |
assertXMLEqual(expected, actual); |
| 472 |
} |
| 473 |
|
| 474 |
@Test(expected = IllegalArgumentException.class) |
| 475 |
public void add_no_scribe_registered() throws Throwable { |
| 476 |
VCard vcard = new VCard(); |
| 477 |
|
| 478 |
SkipMeProperty property = new SkipMeProperty(); |
| 479 |
vcard.addProperty(property); |
| 480 |
|
| 481 |
XCardDocument xcard = new XCardDocument(); |
| 482 |
xcard.addVCard(vcard); |
| 483 |
} |
| 484 |
|
| 485 |
@Test |
| 486 |
public void add_xml_property() throws Throwable { |
| 487 |
VCard vcard = new VCard(); |
| 488 |
|
| 489 |
Xml xmlProperty = new Xml("<foo xmlns=\"http://example.com\" a=\"b\">bar<car/></foo>"); |
| 490 |
xmlProperty.addParameter("name", "value"); |
| 491 |
vcard.addXml(xmlProperty); |
| 492 |
|
| 493 |
XCardDocument xcard = new XCardDocument(); |
| 494 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 495 |
writer.setAddProdId(false); |
| 496 |
writer.write(vcard); |
| 497 |
|
| 498 |
Document actual = xcard.getDocument(); |
| 499 |
|
| 500 |
|
| 501 |
String xml = |
| 502 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 503 |
"<vcard>" + |
| 504 |
"<foo xmlns=\"http://example.com\" a=\"b\">" + |
| 505 |
"<parameters xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 506 |
"<name><unknown>value</unknown></name>" + |
| 507 |
"</parameters>" + |
| 508 |
"bar<car/>" + |
| 509 |
"</foo>" + |
| 510 |
"</vcard>" + |
| 511 |
"</vcards>"; |
| 512 |
Document expected = XmlUtils.toDocument(xml); |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
assertEquals(XmlUtils.toString(expected), XmlUtils.toString(actual)); |
| 524 |
|
| 525 |
} |
| 526 |
|
| 527 |
@Test |
| 528 |
public void add_xml_property_with_null_value() throws Throwable { |
| 529 |
VCard vcard = new VCard(); |
| 530 |
|
| 531 |
Xml xmlProperty = new Xml((Document) null); |
| 532 |
vcard.addXml(xmlProperty); |
| 533 |
|
| 534 |
XCardDocument xcard = new XCardDocument(); |
| 535 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 536 |
writer.setAddProdId(false); |
| 537 |
writer.write(vcard); |
| 538 |
|
| 539 |
Document actual = xcard.getDocument(); |
| 540 |
|
| 541 |
|
| 542 |
String xml = |
| 543 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 544 |
"<vcard />" + |
| 545 |
"</vcards>"; |
| 546 |
Document expected = XmlUtils.toDocument(xml); |
| 547 |
|
| 548 |
|
| 549 |
assertXMLEqual(expected, actual); |
| 550 |
} |
| 551 |
|
| 552 |
@Test |
| 553 |
public void add_extendedTypes() throws Throwable { |
| 554 |
VCard vcard = new VCard(); |
| 555 |
|
| 556 |
|
| 557 |
LuckyNumProperty num = new LuckyNumProperty(24); |
| 558 |
vcard.addProperty(num); |
| 559 |
|
| 560 |
|
| 561 |
SalaryProperty salary = new SalaryProperty(1000000); |
| 562 |
vcard.addProperty(salary); |
| 563 |
|
| 564 |
|
| 565 |
AgeProperty age = new AgeProperty(22); |
| 566 |
vcard.addProperty(age); |
| 567 |
|
| 568 |
XCardDocument xcard = new XCardDocument(); |
| 569 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 570 |
writer.setAddProdId(false); |
| 571 |
writer.registerScribe(new LuckyNumScribe()); |
| 572 |
writer.registerScribe(new SalaryScribe()); |
| 573 |
writer.registerScribe(new AgeScribe()); |
| 574 |
writer.write(vcard); |
| 575 |
|
| 576 |
Document actual = xcard.getDocument(); |
| 577 |
|
| 578 |
|
| 579 |
String xml = |
| 580 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 581 |
"<vcard>" + |
| 582 |
"<a:lucky-num xmlns:a=\"http://luckynum.com\">24</a:lucky-num>" + |
| 583 |
"<x-salary>1000000</x-salary>" + |
| 584 |
"<x-age><unknown>22</unknown></x-age>" + |
| 585 |
"</vcard>" + |
| 586 |
"</vcards>"; |
| 587 |
Document expected = XmlUtils.toDocument(xml); |
| 588 |
|
| 589 |
|
| 590 |
assertXMLEqual(XmlUtils.toString(actual), expected, actual); |
| 591 |
} |
| 592 |
|
| 593 |
@Test |
| 594 |
public void write_prettyPrint() throws Throwable { |
| 595 |
VCard vcard = new VCard(); |
| 596 |
vcard.setFormattedName("John Doe"); |
| 597 |
|
| 598 |
XCardDocument xcard = new XCardDocument(); |
| 599 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 600 |
writer.setAddProdId(false); |
| 601 |
writer.write(vcard); |
| 602 |
|
| 603 |
String actual = xcard.write(2); |
| 604 |
|
| 605 |
|
| 606 |
String expected = |
| 607 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + NEWLINE + |
| 608 |
" <vcard>" + NEWLINE + |
| 609 |
" <fn>" + NEWLINE + |
| 610 |
" <text>John Doe</text>" + NEWLINE + |
| 611 |
" </fn>" + NEWLINE + |
| 612 |
" </vcard>" + NEWLINE + |
| 613 |
"</vcards>"; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
assertTrue("Expected:" + NEWLINE + expected + NEWLINE + NEWLINE + "Actual:" + NEWLINE + actual, actual.contains(expected)); |
| 618 |
} |
| 619 |
|
| 620 |
@Test |
| 621 |
public void write_prettyPrint_invalid_value() throws Throwable { |
| 622 |
VCard vcard = new VCard(); |
| 623 |
vcard.setFormattedName("John Doe"); |
| 624 |
|
| 625 |
XCardDocument xcard = new XCardDocument(); |
| 626 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 627 |
writer.setAddProdId(false); |
| 628 |
writer.write(vcard); |
| 629 |
|
| 630 |
String actual = xcard.write(); |
| 631 |
|
| 632 |
String expected = "<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\"><vcard><fn><text>John Doe</text></fn></vcard></vcards>"; |
| 633 |
|
| 634 |
|
| 635 |
assertTrue("Expected:" + NEWLINE + expected + NEWLINE + NEWLINE + "Actual:" + NEWLINE + actual, actual.contains(expected)); |
| 636 |
} |
| 637 |
|
| 638 |
@Test |
| 639 |
public void write_xmlVerison_default() throws Throwable { |
| 640 |
VCard vcard = new VCard(); |
| 641 |
XCardDocument xcard = new XCardDocument(); |
| 642 |
xcard.addVCard(vcard); |
| 643 |
|
| 644 |
String xml = xcard.write(); |
| 645 |
assertTrue(xml.matches("(?i)<\\?xml.*?version=\"1.0\".*?\\?>.*")); |
| 646 |
} |
| 647 |
|
| 648 |
@Test |
| 649 |
public void write_xmlVerison_1_1() throws Throwable { |
| 650 |
VCard vcard = new VCard(); |
| 651 |
XCardDocument xcard = new XCardDocument(); |
| 652 |
xcard.addVCard(vcard); |
| 653 |
|
| 654 |
String xml = xcard.write(null, "1.1"); |
| 655 |
assertTrue(xml.matches("(?i)<\\?xml.*?version=\"1.1\".*?\\?>.*")); |
| 656 |
} |
| 657 |
|
| 658 |
@Test |
| 659 |
public void write_xmlVerison_invalid() throws Throwable { |
| 660 |
VCard vcard = new VCard(); |
| 661 |
XCardDocument xcard = new XCardDocument(); |
| 662 |
xcard.addVCard(vcard); |
| 663 |
|
| 664 |
String xml = xcard.write(null, "10.17"); |
| 665 |
assertTrue(xml.matches("(?i)<\\?xml.*?version=\"1.0\".*?\\?>.*")); |
| 666 |
} |
| 667 |
|
| 668 |
@Test |
| 669 |
public void write_utf8() throws Throwable { |
| 670 |
VCard vcard = new VCard(); |
| 671 |
vcard.addNote("\u019dote"); |
| 672 |
|
| 673 |
XCardDocument xcard = new XCardDocument(); |
| 674 |
xcard.addVCard(vcard); |
| 675 |
|
| 676 |
Path file = tempFolder.newFile().toPath(); |
| 677 |
xcard.write(file); |
| 678 |
|
| 679 |
String xml = new Gobble(file).asString(StandardCharsets.UTF_8); |
| 680 |
assertTrue(xml.matches("(?i)<\\?xml.*?encoding=\"utf-8\".*?\\?>.*")); |
| 681 |
assertTrue(xml.matches(".*?<note><text>\u019dote</text></note>.*")); |
| 682 |
} |
| 683 |
|
| 684 |
@Test |
| 685 |
public void add_quoted_printable_encoding_not_supported() throws Throwable { |
| 686 |
VCard vcard = new VCard(); |
| 687 |
FormattedName fn = vcard.setFormattedName("Ömür Öde"); |
| 688 |
fn.getParameters().setEncoding(Encoding.QUOTED_PRINTABLE); |
| 689 |
fn.getParameters().setCharset("UTF-8"); |
| 690 |
|
| 691 |
XCardDocument xcard = new XCardDocument(); |
| 692 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 693 |
writer.setAddProdId(false); |
| 694 |
writer.write(vcard); |
| 695 |
|
| 696 |
Document actual = xcard.getDocument(); |
| 697 |
|
| 698 |
String xml = |
| 699 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 700 |
"<vcard>" + |
| 701 |
"<fn><text>Ömür Öde</text></fn>" + |
| 702 |
"</vcard>" + |
| 703 |
"</vcards>"; |
| 704 |
Document expected = XmlUtils.toDocument(xml); |
| 705 |
|
| 706 |
assertXMLEqual(expected, actual); |
| 707 |
} |
| 708 |
|
| 709 |
@Test |
| 710 |
public void add_embedded_vcards_not_supported() throws Throwable { |
| 711 |
VCard vcard = new VCard(); |
| 712 |
vcard.setFormattedName("John Doe"); |
| 713 |
vcard.addProperty(new EmbeddedProperty()); |
| 714 |
|
| 715 |
XCardDocument xcard = new XCardDocument(); |
| 716 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 717 |
writer.registerScribe(new EmbeddedScribe()); |
| 718 |
writer.write(vcard); |
| 719 |
|
| 720 |
VCard parsedVCard = Ezvcard.parseXml(xcard.write()).first(); |
| 721 |
assertTrue(parsedVCard.getExtendedProperties().isEmpty()); |
| 722 |
} |
| 723 |
|
| 724 |
@Test |
| 725 |
public void add_no_existing_vcards_element() throws Throwable { |
| 726 |
VCard vcard = new VCard(); |
| 727 |
vcard.setFormattedName("John Doe"); |
| 728 |
|
| 729 |
XCardDocument xcard = new XCardDocument("<root><a /></root>"); |
| 730 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 731 |
writer.setAddProdId(false); |
| 732 |
writer.write(vcard); |
| 733 |
|
| 734 |
Document actual = xcard.getDocument(); |
| 735 |
|
| 736 |
|
| 737 |
String xml = |
| 738 |
"<root>" + |
| 739 |
"<a />" + |
| 740 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 741 |
"<vcard>" + |
| 742 |
"<fn><text>John Doe</text></fn>" + |
| 743 |
"</vcard>" + |
| 744 |
"</vcards>" + |
| 745 |
"</root>"; |
| 746 |
Document expected = XmlUtils.toDocument(xml); |
| 747 |
|
| 748 |
|
| 749 |
assertXMLEqual(expected, actual); |
| 750 |
} |
| 751 |
|
| 752 |
@Test |
| 753 |
public void add_existing_vcards_element() throws Throwable { |
| 754 |
VCard vcard = new VCard(); |
| 755 |
vcard.setFormattedName("John Doe"); |
| 756 |
|
| 757 |
XCardDocument xcard = new XCardDocument("<root><vcards xmlns=\"" + V4_0.getXmlNamespace() + "\"><a /></vcards></root>"); |
| 758 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 759 |
writer.setAddProdId(false); |
| 760 |
writer.write(vcard); |
| 761 |
|
| 762 |
Document actual = xcard.getDocument(); |
| 763 |
|
| 764 |
|
| 765 |
String xml = |
| 766 |
"<root>" + |
| 767 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 768 |
"<a />" + |
| 769 |
"<vcard>" + |
| 770 |
"<fn><text>John Doe</text></fn>" + |
| 771 |
"</vcard>" + |
| 772 |
"</vcards>" + |
| 773 |
"</root>"; |
| 774 |
Document expected = XmlUtils.toDocument(xml); |
| 775 |
|
| 776 |
|
| 777 |
assertXMLEqual(expected, actual); |
| 778 |
} |
| 779 |
|
| 780 |
@Test |
| 781 |
public void write_rfc6351_example() throws Throwable { |
| 782 |
VCard vcard = new VCard(); |
| 783 |
|
| 784 |
vcard.setFormattedName("Simon Perreault"); |
| 785 |
|
| 786 |
StructuredName n = new StructuredName(); |
| 787 |
n.setFamily("Perreault"); |
| 788 |
n.setGiven("Simon"); |
| 789 |
n.getSuffixes().add("ing. jr"); |
| 790 |
n.getSuffixes().add("M.Sc."); |
| 791 |
vcard.setStructuredName(n); |
| 792 |
|
| 793 |
Birthday bday = new Birthday(PartialDate.builder().month(2).date(3).build()); |
| 794 |
vcard.setBirthday(bday); |
| 795 |
|
| 796 |
Anniversary anniversary = new Anniversary(PartialDate.builder().year(2009).month(8).date(8).hour(14).minute(30).offset(ZoneOffset.ofHours(-5)).build()); |
| 797 |
vcard.setAnniversary(anniversary); |
| 798 |
|
| 799 |
vcard.setGender(Gender.male()); |
| 800 |
|
| 801 |
vcard.addLanguage("fr").setPref(1); |
| 802 |
vcard.addLanguage("en").setPref(2); |
| 803 |
|
| 804 |
vcard.setOrganization("Viagenie").setType("work"); |
| 805 |
|
| 806 |
Address adr = new Address(); |
| 807 |
adr.setStreetAddress("2875 boul. Laurier, suite D2-630"); |
| 808 |
adr.setLocality("Quebec"); |
| 809 |
adr.setRegion("QC"); |
| 810 |
adr.setPostalCode("G1V 2M2"); |
| 811 |
adr.setCountry("Canada"); |
| 812 |
adr.getTypes().add(AddressType.WORK); |
| 813 |
adr.setLabel("Simon Perreault\n2875 boul. Laurier, suite D2-630\nQuebec, QC, Canada\nG1V 2M2"); |
| 814 |
vcard.addAddress(adr); |
| 815 |
|
| 816 |
TelUri telUri = new TelUri.Builder("+1-418-656-9254").extension("102").build(); |
| 817 |
Telephone tel = new Telephone(telUri); |
| 818 |
tel.getTypes().add(TelephoneType.WORK); |
| 819 |
tel.getTypes().add(TelephoneType.VOICE); |
| 820 |
vcard.addTelephoneNumber(tel); |
| 821 |
|
| 822 |
tel = new Telephone(new TelUri.Builder("+1-418-262-6501").build()); |
| 823 |
tel.getTypes().add(TelephoneType.WORK); |
| 824 |
tel.getTypes().add(TelephoneType.TEXT); |
| 825 |
tel.getTypes().add(TelephoneType.VOICE); |
| 826 |
tel.getTypes().add(TelephoneType.CELL); |
| 827 |
tel.getTypes().add(TelephoneType.VIDEO); |
| 828 |
vcard.addTelephoneNumber(tel); |
| 829 |
|
| 830 |
vcard.addEmail("[email protected]", EmailType.WORK); |
| 831 |
|
| 832 |
Geo geo = new Geo(46.766336, -71.28955); |
| 833 |
geo.setType("work"); |
| 834 |
vcard.setGeo(geo); |
| 835 |
|
| 836 |
Key key = new Key("http://www.viagenie.ca/simon.perreault/simon.asc", null); |
| 837 |
key.setType("work"); |
| 838 |
vcard.addKey(key); |
| 839 |
|
| 840 |
vcard.setTimezone(new Timezone("America/Montreal")); |
| 841 |
|
| 842 |
vcard.addUrl("http://nomis80.org").setType("home"); |
| 843 |
|
| 844 |
assertValidate(vcard).versions(V4_0).run(); |
| 845 |
|
| 846 |
assertExample(vcard, "rfc6351-example.xml"); |
| 847 |
} |
| 848 |
|
| 849 |
@Test |
| 850 |
public void read_rfc6351_example() throws Throwable { |
| 851 |
VCardAsserter asserter = readFile("rfc6351-example.xml"); |
| 852 |
asserter.next(V4_0); |
| 853 |
|
| 854 |
|
| 855 |
asserter.simpleProperty(FormattedName.class) |
| 856 |
.value("Simon Perreault") |
| 857 |
.noMore(); |
| 858 |
|
| 859 |
asserter.structuredName() |
| 860 |
.family("Perreault") |
| 861 |
.given("Simon") |
| 862 |
.suffixes("ing. jr", "M.Sc.") |
| 863 |
.noMore(); |
| 864 |
|
| 865 |
asserter.dateProperty(Birthday.class) |
| 866 |
.partialDate(PartialDate.builder() |
| 867 |
.month(2) |
| 868 |
.date(3) |
| 869 |
.build() |
| 870 |
) |
| 871 |
.noMore(); |
| 872 |
|
| 873 |
asserter.dateProperty(Anniversary.class) |
| 874 |
.partialDate(PartialDate.builder() |
| 875 |
.year(2009) |
| 876 |
.month(8) |
| 877 |
.date(8) |
| 878 |
.hour(14) |
| 879 |
.minute(30) |
| 880 |
.offset(ZoneOffset.ofHours(-5)) |
| 881 |
.build() |
| 882 |
) |
| 883 |
.noMore(); |
| 884 |
|
| 885 |
asserter.property(Gender.class) |
| 886 |
.expected(Gender.male()) |
| 887 |
.noMore(); |
| 888 |
|
| 889 |
asserter.simpleProperty(Language.class) |
| 890 |
.value("fr") |
| 891 |
.param("PREF", "1") |
| 892 |
.next() |
| 893 |
.value("en") |
| 894 |
.param("PREF", "2") |
| 895 |
.noMore(); |
| 896 |
|
| 897 |
asserter.listProperty(Organization.class) |
| 898 |
.values("Viagenie") |
| 899 |
.param("TYPE", "work") |
| 900 |
.noMore(); |
| 901 |
|
| 902 |
asserter.address() |
| 903 |
.streetAddress("2875 boul. Laurier, suite D2-630") |
| 904 |
.locality("Quebec") |
| 905 |
.region("QC") |
| 906 |
.postalCode("G1V 2M2") |
| 907 |
.country("Canada") |
| 908 |
.label("Simon Perreault\n2875 boul. Laurier, suite D2-630\nQuebec, QC, Canada\nG1V 2M2") |
| 909 |
.types(AddressType.WORK) |
| 910 |
.noMore(); |
| 911 |
|
| 912 |
asserter.telephone() |
| 913 |
.uri(new TelUri.Builder("+1-418-656-9254").extension("102").build()) |
| 914 |
.types(TelephoneType.WORK, TelephoneType.VOICE) |
| 915 |
.next() |
| 916 |
.uri(new TelUri.Builder("+1-418-262-6501").build()) |
| 917 |
.types(TelephoneType.WORK, TelephoneType.TEXT, TelephoneType.VOICE, TelephoneType.CELL, TelephoneType.VIDEO) |
| 918 |
.noMore(); |
| 919 |
|
| 920 |
asserter.email() |
| 921 |
.value("[email protected]") |
| 922 |
.types(EmailType.WORK) |
| 923 |
.noMore(); |
| 924 |
|
| 925 |
asserter.geo() |
| 926 |
.latitude(46.766336) |
| 927 |
.longitude(-71.28955) |
| 928 |
.param("TYPE", "work") |
| 929 |
.noMore(); |
| 930 |
|
| 931 |
asserter.binaryProperty(Key.class) |
| 932 |
.url("http://www.viagenie.ca/simon.perreault/simon.asc") |
| 933 |
.param("TYPE", "work") |
| 934 |
.noMore(); |
| 935 |
|
| 936 |
asserter.timezone() |
| 937 |
.text("America/Montreal") |
| 938 |
.noMore(); |
| 939 |
|
| 940 |
asserter.simpleProperty(Url.class) |
| 941 |
.value("http://nomis80.org") |
| 942 |
.param("TYPE", "home") |
| 943 |
.noMore(); |
| 944 |
|
| 945 |
|
| 946 |
asserter.validate().run(); |
| 947 |
asserter.done(); |
| 948 |
} |
| 949 |
|
| 950 |
private static void assertExample(VCard vcard, String exampleFileName) throws IOException, SAXException { |
| 951 |
XCardDocument xcard = new XCardDocument(); |
| 952 |
XCardDocumentStreamWriter writer = xcard.writer(); |
| 953 |
writer.setAddProdId(false); |
| 954 |
writer.write(vcard); |
| 955 |
|
| 956 |
Document expected = XmlUtils.toDocument(new InputStreamReader(XCardDocumentTest.class.getResourceAsStream(exampleFileName))); |
| 957 |
Document actual = xcard.getDocument(); |
| 958 |
|
| 959 |
assertXMLEqual(XmlUtils.toString(actual), expected, actual); |
| 960 |
} |
| 961 |
|
| 962 |
private static VCardAsserter readFile(String file) throws SAXException, IOException { |
| 963 |
XCardDocument document = new XCardDocument(XCardDocumentTest.class.getResourceAsStream(file)); |
| 964 |
StreamReader reader = document.reader(); |
| 965 |
return new VCardAsserter(reader); |
| 966 |
} |
| 967 |
|
| 968 |
private static class EmbeddedProperty extends VCardProperty { |
| 969 |
|
| 970 |
} |
| 971 |
|
| 972 |
private static class EmbeddedScribe extends VCardPropertyScribe<EmbeddedProperty> { |
| 973 |
public EmbeddedScribe() { |
| 974 |
super(EmbeddedProperty.class, "EMBEDDED"); |
| 975 |
} |
| 976 |
|
| 977 |
@Override |
| 978 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 979 |
return null; |
| 980 |
} |
| 981 |
|
| 982 |
@Override |
| 983 |
protected String _writeText(EmbeddedProperty property, WriteContext context) { |
| 984 |
return null; |
| 985 |
} |
| 986 |
|
| 987 |
@Override |
| 988 |
protected EmbeddedProperty _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 989 |
return null; |
| 990 |
} |
| 991 |
|
| 992 |
@Override |
| 993 |
protected void _writeXml(EmbeddedProperty property, XCardElement parent) { |
| 994 |
throw new EmbeddedVCardException(new VCard()); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
private static VCardAsserter readXml(String xml) throws SAXException { |
| 999 |
XCardDocument xcard = new XCardDocument(xml); |
| 1000 |
StreamReader reader = xcard.reader(); |
| 1001 |
return new VCardAsserter(reader); |
| 1002 |
} |
| 1003 |
} |