| 1 |
package ezvcard.io.xml; |
| 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.custommonkey.xmlunit.XMLAssert.assertXMLEqual; |
| 9 |
import static org.junit.Assert.assertEquals; |
| 10 |
import static org.junit.Assert.assertFalse; |
| 11 |
import static org.junit.Assert.assertTrue; |
| 12 |
import static org.junit.Assert.fail; |
| 13 |
|
| 14 |
import java.io.IOException; |
| 15 |
import java.io.Writer; |
| 16 |
import java.nio.charset.StandardCharsets; |
| 17 |
import java.nio.file.Files; |
| 18 |
import java.nio.file.Path; |
| 19 |
import java.time.ZoneOffset; |
| 20 |
import java.util.Iterator; |
| 21 |
|
| 22 |
import javax.xml.transform.TransformerException; |
| 23 |
|
| 24 |
import org.custommonkey.xmlunit.XMLUnit; |
| 25 |
import org.junit.BeforeClass; |
| 26 |
import org.junit.Rule; |
| 27 |
import org.junit.Test; |
| 28 |
import org.junit.rules.TemporaryFolder; |
| 29 |
import org.xml.sax.SAXException; |
| 30 |
|
| 31 |
import ezvcard.VCard; |
| 32 |
import ezvcard.VCardDataType; |
| 33 |
import ezvcard.io.AgeProperty; |
| 34 |
import ezvcard.io.AgeProperty.AgeScribe; |
| 35 |
import ezvcard.io.LuckyNumProperty; |
| 36 |
import ezvcard.io.LuckyNumProperty.LuckyNumScribe; |
| 37 |
import ezvcard.io.MyFormattedNameProperty; |
| 38 |
import ezvcard.io.MyFormattedNameProperty.MyFormattedNameScribe; |
| 39 |
import ezvcard.io.SalaryProperty; |
| 40 |
import ezvcard.io.SalaryProperty.SalaryScribe; |
| 41 |
import ezvcard.io.scribe.CannotParseScribe; |
| 42 |
import ezvcard.io.scribe.SkipMeScribe; |
| 43 |
import ezvcard.parameter.AddressType; |
| 44 |
import ezvcard.parameter.EmailType; |
| 45 |
import ezvcard.parameter.TelephoneType; |
| 46 |
import ezvcard.property.Anniversary; |
| 47 |
import ezvcard.property.Birthday; |
| 48 |
import ezvcard.property.FormattedName; |
| 49 |
import ezvcard.property.Gender; |
| 50 |
import ezvcard.property.Key; |
| 51 |
import ezvcard.property.Language; |
| 52 |
import ezvcard.property.Note; |
| 53 |
import ezvcard.property.Organization; |
| 54 |
import ezvcard.property.ProductId; |
| 55 |
import ezvcard.property.RawProperty; |
| 56 |
import ezvcard.property.Url; |
| 57 |
import ezvcard.property.Xml; |
| 58 |
import ezvcard.property.asserter.VCardAsserter; |
| 59 |
import ezvcard.util.PartialDate; |
| 60 |
import ezvcard.util.TelUri; |
| 61 |
import ezvcard.util.XmlUtils; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public class XCardReaderTest { |
| 96 |
@Rule |
| 97 |
public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 98 |
|
| 99 |
@BeforeClass |
| 100 |
public static void beforeClass() { |
| 101 |
XMLUnit.setIgnoreWhitespace(true); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void read_single() throws Exception { |
| 106 |
|
| 107 |
VCardAsserter asserter = readXml( |
| 108 |
"<!-- ignore -->" + |
| 109 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 110 |
"<vcard>" + |
| 111 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 112 |
"<n>" + |
| 113 |
"<surname>House</surname>" + |
| 114 |
"<given>Gregory</given>" + |
| 115 |
"<!-- ignore -->" + |
| 116 |
"<additional />" + |
| 117 |
"<prefix>Dr</prefix>" + |
| 118 |
"<prefix>Mr</prefix>" + |
| 119 |
"<suffix>MD</suffix>" + |
| 120 |
"</n>" + |
| 121 |
"</vcard>" + |
| 122 |
"</vcards>" |
| 123 |
); |
| 124 |
|
| 125 |
asserter.next(V4_0); |
| 126 |
|
| 127 |
asserter.simpleProperty(FormattedName.class) |
| 128 |
.value("Dr. Gregory House M.D.") |
| 129 |
.noMore(); |
| 130 |
|
| 131 |
asserter.structuredName() |
| 132 |
.family("House") |
| 133 |
.given("Gregory") |
| 134 |
.prefixes("Dr", "Mr") |
| 135 |
.suffixes("MD") |
| 136 |
.noMore(); |
| 137 |
|
| 138 |
asserter.done(); |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void read_multiple() throws Exception { |
| 144 |
|
| 145 |
VCardAsserter asserter = readXml( |
| 146 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 147 |
"<vcard>" + |
| 148 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 149 |
"<n>" + |
| 150 |
"<surname>House</surname>" + |
| 151 |
"<given>Gregory</given>" + |
| 152 |
"<additional />" + |
| 153 |
"<prefix>Dr</prefix>" + |
| 154 |
"<prefix>Mr</prefix>" + |
| 155 |
"<suffix>MD</suffix>" + |
| 156 |
"</n>" + |
| 157 |
"</vcard>" + |
| 158 |
"<vcard>" + |
| 159 |
"<fn><text>Dr. Lisa Cuddy M.D.</text></fn>" + |
| 160 |
"<n>" + |
| 161 |
"<surname>Cuddy</surname>" + |
| 162 |
"<given>Lisa</given>" + |
| 163 |
"<additional />" + |
| 164 |
"<prefix>Dr</prefix>" + |
| 165 |
"<prefix>Ms</prefix>" + |
| 166 |
"<suffix>MD</suffix>" + |
| 167 |
"</n>" + |
| 168 |
"</vcard>" + |
| 169 |
"</vcards>" |
| 170 |
); |
| 171 |
|
| 172 |
asserter.next(V4_0); |
| 173 |
|
| 174 |
asserter.simpleProperty(FormattedName.class) |
| 175 |
.value("Dr. Gregory House M.D.") |
| 176 |
.noMore(); |
| 177 |
|
| 178 |
asserter.structuredName() |
| 179 |
.family("House") |
| 180 |
.given("Gregory") |
| 181 |
.prefixes("Dr", "Mr") |
| 182 |
.suffixes("MD") |
| 183 |
.noMore(); |
| 184 |
|
| 185 |
asserter.next(V4_0); |
| 186 |
|
| 187 |
asserter.simpleProperty(FormattedName.class) |
| 188 |
.value("Dr. Lisa Cuddy M.D.") |
| 189 |
.noMore(); |
| 190 |
|
| 191 |
asserter.structuredName() |
| 192 |
.family("Cuddy") |
| 193 |
.given("Lisa") |
| 194 |
.prefixes("Dr", "Ms") |
| 195 |
.suffixes("MD") |
| 196 |
.noMore(); |
| 197 |
|
| 198 |
asserter.done(); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
public void read_default_namespace() throws Exception { |
| 204 |
|
| 205 |
VCardAsserter asserter = readXml( |
| 206 |
"<vcards>" + |
| 207 |
"<vcard>" + |
| 208 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 209 |
"</vcard>" + |
| 210 |
"</vcards>" |
| 211 |
); |
| 212 |
|
| 213 |
|
| 214 |
asserter.done(); |
| 215 |
} |
| 216 |
|
| 217 |
@Test |
| 218 |
public void read_wrong_namespace() throws Exception { |
| 219 |
|
| 220 |
VCardAsserter asserter = readXml( |
| 221 |
"<vcards xmlns=\"wrong\">" + |
| 222 |
"<vcard>" + |
| 223 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 224 |
"</vcard>" + |
| 225 |
"</vcards>" |
| 226 |
); |
| 227 |
|
| 228 |
|
| 229 |
asserter.done(); |
| 230 |
} |
| 231 |
|
| 232 |
@Test |
| 233 |
public void read_namespace_prefix() throws Exception { |
| 234 |
|
| 235 |
VCardAsserter asserter = readXml( |
| 236 |
"<v:vcards xmlns:v=\"" + V4_0.getXmlNamespace() + "\">" + |
| 237 |
"<v:vcard>" + |
| 238 |
"<v:fn><x:text xmlns:x=\"" + V4_0.getXmlNamespace() + "\">Dr. Gregory House M.D.</x:text></v:fn>" + |
| 239 |
"</v:vcard>" + |
| 240 |
"</v:vcards>" |
| 241 |
); |
| 242 |
|
| 243 |
asserter.next(V4_0); |
| 244 |
|
| 245 |
asserter.simpleProperty(FormattedName.class) |
| 246 |
.value("Dr. Gregory House M.D.") |
| 247 |
.noMore(); |
| 248 |
|
| 249 |
asserter.done(); |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
@Test |
| 254 |
public void read_preserve_whitespace() throws Exception { |
| 255 |
|
| 256 |
VCardAsserter asserter = readXml( |
| 257 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 258 |
"<vcard>" + |
| 259 |
"<note><text> This \t is \n a note </text></note>" + |
| 260 |
"</vcard>" + |
| 261 |
"</vcards>" |
| 262 |
); |
| 263 |
|
| 264 |
asserter.next(V4_0); |
| 265 |
|
| 266 |
asserter.simpleProperty(Note.class) |
| 267 |
.value(" This \t is \n a note ") |
| 268 |
.noMore(); |
| 269 |
|
| 270 |
asserter.done(); |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
@Test |
| 275 |
public void read_ignore_other_namespaces() throws Exception { |
| 276 |
|
| 277 |
VCardAsserter asserter = readXml( |
| 278 |
"<root>" + |
| 279 |
"<ignore xmlns=\"one\" />" + |
| 280 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 281 |
"<ignore xmlns=\"two\">text</ignore>" + |
| 282 |
"<vcard>" + |
| 283 |
"<fn>" + |
| 284 |
"<parameters>" + |
| 285 |
"<ignore xmlns=\"three\"><foo>bar</foo></ignore>" + |
| 286 |
"<pref><ignore xmlns=\"four\">bar</ignore><integer>1</integer></pref>" + |
| 287 |
"<pref><integer>2</integer></pref>" + |
| 288 |
"</parameters>" + |
| 289 |
"<text>Dr. Gregory House M.D.</text>" + |
| 290 |
"</fn>" + |
| 291 |
"</vcard>" + |
| 292 |
"</vcards>" + |
| 293 |
"</root>" |
| 294 |
); |
| 295 |
|
| 296 |
asserter.next(V4_0); |
| 297 |
|
| 298 |
asserter.simpleProperty(FormattedName.class) |
| 299 |
.value("Dr. Gregory House M.D.") |
| 300 |
.param("PREF", "1") |
| 301 |
.param("PREF", "2") |
| 302 |
.noMore(); |
| 303 |
|
| 304 |
asserter.done(); |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void read_identical_element_names() throws Exception { |
| 310 |
|
| 311 |
VCardAsserter asserter = readXml( |
| 312 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 313 |
"<vcard>" + |
| 314 |
"<vcard>" + |
| 315 |
"<parameters>" + |
| 316 |
"<parameters><text>paramValue1</text></parameters>" + |
| 317 |
"<group><text>paramValue2</text></group>" + |
| 318 |
"</parameters>" + |
| 319 |
"<vcard>propValue</vcard>" + |
| 320 |
"</vcard>" + |
| 321 |
"<group name=\"grp\">" + |
| 322 |
"<group>" + |
| 323 |
"<text>value</text>" + |
| 324 |
"</group>" + |
| 325 |
"</group>" + |
| 326 |
"</vcard>" + |
| 327 |
"</vcards>" |
| 328 |
); |
| 329 |
|
| 330 |
asserter.next(V4_0); |
| 331 |
|
| 332 |
asserter.rawProperty("VCARD") |
| 333 |
.dataType(VCardDataType.get("VCARD")) |
| 334 |
.value("propValue") |
| 335 |
.param("PARAMETERS", "paramValue1") |
| 336 |
.param("GROUP", "paramValue2") |
| 337 |
.noMore(); |
| 338 |
|
| 339 |
asserter.rawProperty("GROUP") |
| 340 |
.dataType(VCardDataType.TEXT) |
| 341 |
.group("grp") |
| 342 |
.value("value") |
| 343 |
.noMore(); |
| 344 |
|
| 345 |
asserter.done(); |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void read_bad_xml() throws Exception { |
| 351 |
|
| 352 |
String xml = |
| 353 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 354 |
"<vcard>" + |
| 355 |
"<fn><text>John Doe</fn>" + |
| 356 |
"</vcard>" + |
| 357 |
"</vcards>"; |
| 358 |
|
| 359 |
|
| 360 |
XCardReader reader = new XCardReader(xml); |
| 361 |
|
| 362 |
try { |
| 363 |
reader.readNext(); |
| 364 |
fail(); |
| 365 |
} catch (IOException e) { |
| 366 |
assertTrue(e.getCause() instanceof TransformerException); |
| 367 |
assertTrue(e.getCause().getCause() instanceof SAXException); |
| 368 |
} |
| 369 |
|
| 370 |
assertNoMoreVCards(reader); |
| 371 |
} |
| 372 |
|
| 373 |
@Test |
| 374 |
public void read_multiple_vcards_elements() throws Exception { |
| 375 |
|
| 376 |
VCardAsserter asserter = readXml( |
| 377 |
"<root>" + |
| 378 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 379 |
"<vcard>" + |
| 380 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 381 |
"</vcard>" + |
| 382 |
"</vcards>" + |
| 383 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 384 |
"<vcard>" + |
| 385 |
"<fn><text>Dr. Lisa Cuddy M.D.</text></fn>" + |
| 386 |
"</vcard>" + |
| 387 |
"</vcards>" + |
| 388 |
"</root>" |
| 389 |
); |
| 390 |
|
| 391 |
asserter.next(V4_0); |
| 392 |
asserter.simpleProperty(FormattedName.class) |
| 393 |
.value("Dr. Gregory House M.D.") |
| 394 |
.noMore(); |
| 395 |
|
| 396 |
asserter.next(V4_0); |
| 397 |
asserter.simpleProperty(FormattedName.class) |
| 398 |
.value("Dr. Lisa Cuddy M.D.") |
| 399 |
.noMore(); |
| 400 |
|
| 401 |
asserter.done(); |
| 402 |
|
| 403 |
} |
| 404 |
|
| 405 |
@Test |
| 406 |
public void read_parameters() throws Exception { |
| 407 |
|
| 408 |
VCardAsserter asserter = readXml( |
| 409 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 410 |
"<vcard>" + |
| 411 |
|
| 412 |
"<note>" + |
| 413 |
"<text>Note 1</text>" + |
| 414 |
"</note>" + |
| 415 |
|
| 416 |
|
| 417 |
"<note>" + |
| 418 |
"<parameters>" + |
| 419 |
"<altid><text>1</text></altid>" + |
| 420 |
"</parameters>" + |
| 421 |
"<text>Hello world!</text>" + |
| 422 |
"</note>" + |
| 423 |
|
| 424 |
|
| 425 |
"<note>" + |
| 426 |
"<parameters>" + |
| 427 |
"<altid>1</altid>" + |
| 428 |
"</parameters>" + |
| 429 |
"<text>Hallo Welt!</text>" + |
| 430 |
"</note>" + |
| 431 |
|
| 432 |
|
| 433 |
"<note>" + |
| 434 |
"<parameters>" + |
| 435 |
"<altid><text>1</text></altid>" + |
| 436 |
"<language><language-tag>fr</language-tag></language>" + |
| 437 |
"</parameters>" + |
| 438 |
"<text>Bonjour tout le monde!</text>" + |
| 439 |
"</note>" + |
| 440 |
|
| 441 |
|
| 442 |
"<tel>" + |
| 443 |
"<parameters>" + |
| 444 |
"<type>" + |
| 445 |
"<text>work</text>" + |
| 446 |
"<text>voice</text>" + |
| 447 |
"</type>" + |
| 448 |
"</parameters>" + |
| 449 |
"<uri>tel:+1-555-555-1234</uri>" + |
| 450 |
"</tel>" + |
| 451 |
"</vcard>" + |
| 452 |
"</vcards>" |
| 453 |
); |
| 454 |
|
| 455 |
asserter.next(V4_0); |
| 456 |
|
| 457 |
asserter.simpleProperty(Note.class) |
| 458 |
.value("Note 1") |
| 459 |
.next() |
| 460 |
.value("Hello world!") |
| 461 |
.param("ALTID", "1") |
| 462 |
.next() |
| 463 |
.value("Hallo Welt!") |
| 464 |
.next() |
| 465 |
.value("Bonjour tout le monde!") |
| 466 |
.param("ALTID", "1") |
| 467 |
.param("LANGUAGE", "fr") |
| 468 |
.noMore(); |
| 469 |
|
| 470 |
asserter.telephone() |
| 471 |
.uri(new TelUri.Builder("+1-555-555-1234").build()) |
| 472 |
.types(TelephoneType.WORK, TelephoneType.VOICE) |
| 473 |
.noMore(); |
| 474 |
|
| 475 |
asserter.done(); |
| 476 |
|
| 477 |
} |
| 478 |
|
| 479 |
@Test |
| 480 |
public void read_groups() throws Exception { |
| 481 |
|
| 482 |
VCardAsserter asserter = readXml( |
| 483 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 484 |
"<vcard>" + |
| 485 |
"<group name=\"item1\">" + |
| 486 |
"<fn><text>John Doe</text></fn>" + |
| 487 |
"<note><text>Hello world!</text></note>" + |
| 488 |
"</group>" + |
| 489 |
"<group>" + |
| 490 |
"<prodid><text>no name attribute</text></prodid>" + |
| 491 |
"</group>" + |
| 492 |
"<note><text>A property without a group</text></note>" + |
| 493 |
"</vcard>" + |
| 494 |
"</vcards>" |
| 495 |
); |
| 496 |
|
| 497 |
asserter.next(V4_0); |
| 498 |
|
| 499 |
asserter.simpleProperty(FormattedName.class) |
| 500 |
.group("item1") |
| 501 |
.value("John Doe") |
| 502 |
.noMore(); |
| 503 |
|
| 504 |
asserter.simpleProperty(Note.class) |
| 505 |
.group("item1") |
| 506 |
.value("Hello world!") |
| 507 |
.next() |
| 508 |
.value("A property without a group") |
| 509 |
.noMore(); |
| 510 |
|
| 511 |
asserter.simpleProperty(ProductId.class) |
| 512 |
.value("no name attribute") |
| 513 |
.noMore(); |
| 514 |
|
| 515 |
asserter.done(); |
| 516 |
|
| 517 |
} |
| 518 |
|
| 519 |
@Test |
| 520 |
public void read_non_standard_properties() throws Exception { |
| 521 |
|
| 522 |
String xml = |
| 523 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 524 |
"<vcard>" + |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
"<foo xmlns=\"http://example.com\">bar</foo>" + |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
"<a:lucky-num xmlns:a=\"http://luckynum.com\"><a:num>21</a:num></a:lucky-num>" + |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
"<x-salary><integer>1000000</integer></x-salary>" + |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
"<x-age><integer>24</integer></x-age>" + |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
"<x-gender><text>m</text></x-gender>" + |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
"<fn><name>John Doe</name></fn>" + |
| 556 |
"</vcard>" + |
| 557 |
"</vcards>"; |
| 558 |
|
| 559 |
|
| 560 |
XCardReader reader = new XCardReader(xml); |
| 561 |
reader.registerScribe(new LuckyNumScribe()); |
| 562 |
reader.registerScribe(new SalaryScribe()); |
| 563 |
reader.registerScribe(new AgeScribe()); |
| 564 |
reader.registerScribe(new MyFormattedNameScribe()); |
| 565 |
|
| 566 |
{ |
| 567 |
VCard vcard = reader.readNext(); |
| 568 |
assertVersion(V4_0, vcard); |
| 569 |
assertPropertyCount(6, vcard); |
| 570 |
|
| 571 |
{ |
| 572 |
Iterator<Xml> xmlIt = vcard.getXmls().iterator(); |
| 573 |
|
| 574 |
Xml xmlType = xmlIt.next(); |
| 575 |
assertXMLEqual(XmlUtils.toDocument("<foo xmlns=\"http://example.com\">bar</foo>"), xmlType.getValue()); |
| 576 |
|
| 577 |
assertFalse(xmlIt.hasNext()); |
| 578 |
} |
| 579 |
|
| 580 |
LuckyNumProperty luckyNum = vcard.getProperty(LuckyNumProperty.class); |
| 581 |
assertEquals(21, luckyNum.luckyNum); |
| 582 |
|
| 583 |
SalaryProperty salary = vcard.getProperty(SalaryProperty.class); |
| 584 |
assertEquals(1000000, salary.salary); |
| 585 |
|
| 586 |
AgeProperty age = vcard.getProperty(AgeProperty.class); |
| 587 |
assertEquals(24, age.age); |
| 588 |
|
| 589 |
RawProperty gender = vcard.getExtendedProperty("X-GENDER"); |
| 590 |
assertEquals(VCardDataType.TEXT, gender.getDataType()); |
| 591 |
assertEquals("m", gender.getValue()); |
| 592 |
|
| 593 |
MyFormattedNameProperty fn = vcard.getProperty(MyFormattedNameProperty.class); |
| 594 |
assertEquals("JOHN DOE", fn.value); |
| 595 |
|
| 596 |
assertParseWarnings(reader); |
| 597 |
} |
| 598 |
|
| 599 |
assertNoMoreVCards(reader); |
| 600 |
} |
| 601 |
|
| 602 |
@Test |
| 603 |
public void read_xml_property() throws Exception { |
| 604 |
|
| 605 |
VCardAsserter asserter = readXml( |
| 606 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 607 |
"<vcard>" + |
| 608 |
"<x:foo xmlns:x=\"http://example.com\">" + |
| 609 |
"<parameters>" + |
| 610 |
"<pref><integer>1</integer></pref>" + |
| 611 |
"</parameters>" + |
| 612 |
"<!-- comment -->" + |
| 613 |
"<x:a />" + |
| 614 |
"<x:b attr=\"value\">text</x:b>" + |
| 615 |
"<x:c>text<x:child>child</x:child></x:c>" + |
| 616 |
"</x:foo>" + |
| 617 |
"</vcard>" + |
| 618 |
"</vcards>" |
| 619 |
); |
| 620 |
|
| 621 |
asserter.next(V4_0); |
| 622 |
|
| 623 |
asserter.xml() |
| 624 |
.param("PREF", "1") |
| 625 |
.value( |
| 626 |
"<foo xmlns=\"http://example.com\">" + |
| 627 |
"<a />" + |
| 628 |
"<b attr=\"value\">text</b>" + |
| 629 |
"<c>text<child>child</child></c>" + |
| 630 |
"</foo>" |
| 631 |
) |
| 632 |
.noMore(); |
| 633 |
|
| 634 |
asserter.done(); |
| 635 |
|
| 636 |
} |
| 637 |
|
| 638 |
@Test |
| 639 |
public void skipMeException() throws Exception { |
| 640 |
|
| 641 |
String xml = |
| 642 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 643 |
"<vcard>" + |
| 644 |
"<skipme><text>value</text></skipme>" + |
| 645 |
"<x-foo><text>value</text></x-foo>" + |
| 646 |
"</vcard>" + |
| 647 |
"</vcards>"; |
| 648 |
|
| 649 |
XCardReader reader = new XCardReader(xml); |
| 650 |
reader.registerScribe(new SkipMeScribe()); |
| 651 |
VCardAsserter asserter = new VCardAsserter(reader); |
| 652 |
|
| 653 |
asserter.next(V4_0); |
| 654 |
|
| 655 |
asserter.rawProperty("x-foo") |
| 656 |
.dataType(VCardDataType.TEXT) |
| 657 |
.value("value") |
| 658 |
.noMore(); |
| 659 |
|
| 660 |
asserter.warnings(22); |
| 661 |
asserter.done(); |
| 662 |
|
| 663 |
} |
| 664 |
|
| 665 |
@Test |
| 666 |
public void cannotParseException() throws Exception { |
| 667 |
|
| 668 |
String xml = |
| 669 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 670 |
"<vcard>" + |
| 671 |
"<group name=\"grp\">" + |
| 672 |
"<cannotparse><text>value1</text></cannotparse>" + |
| 673 |
"</group>" + |
| 674 |
"<cannotparse><text>value2</text></cannotparse>" + |
| 675 |
"<x-foo><text>value</text></x-foo>" + |
| 676 |
"</vcard>" + |
| 677 |
"</vcards>"; |
| 678 |
|
| 679 |
XCardReader reader = new XCardReader(xml); |
| 680 |
reader.registerScribe(new CannotParseScribe()); |
| 681 |
VCardAsserter asserter = new VCardAsserter(reader); |
| 682 |
|
| 683 |
asserter.next(V4_0); |
| 684 |
|
| 685 |
asserter.rawProperty("x-foo") |
| 686 |
.dataType(VCardDataType.TEXT) |
| 687 |
.value("value") |
| 688 |
.noMore(); |
| 689 |
|
| 690 |
asserter.xml() |
| 691 |
.group("grp") |
| 692 |
.value("<cannotparse xmlns=\"" + V4_0.getXmlNamespace() + "\"><text>value1</text></cannotparse>") |
| 693 |
.next() |
| 694 |
.value("<cannotparse xmlns=\"" + V4_0.getXmlNamespace() + "\"><text>value2</text></cannotparse>") |
| 695 |
.noMore(); |
| 696 |
|
| 697 |
asserter.warnings(25, 25); |
| 698 |
asserter.done(); |
| 699 |
|
| 700 |
} |
| 701 |
|
| 702 |
@Test |
| 703 |
public void close_before_stream_ends() throws Exception { |
| 704 |
|
| 705 |
String xml = |
| 706 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 707 |
"<vcard>" + |
| 708 |
"<fn><text>Dr. Gregory House M.D.</text></fn>" + |
| 709 |
"</vcard>" + |
| 710 |
"<vcard>" + |
| 711 |
"<fn><text>Dr. Lisa Cuddy M.D.</text></fn>" + |
| 712 |
"</vcard>" + |
| 713 |
"</vcards>"; |
| 714 |
|
| 715 |
|
| 716 |
XCardReader reader = new XCardReader(xml); |
| 717 |
|
| 718 |
VCard vcard = reader.readNext(); |
| 719 |
assertVersion(V4_0, vcard); |
| 720 |
assertPropertyCount(1, vcard); |
| 721 |
assertEquals("Dr. Gregory House M.D.", vcard.getFormattedName().getValue()); |
| 722 |
assertParseWarnings(reader); |
| 723 |
|
| 724 |
reader.close(); |
| 725 |
|
| 726 |
assertNoMoreVCards(reader); |
| 727 |
} |
| 728 |
|
| 729 |
@Test |
| 730 |
public void read_utf8() throws Exception { |
| 731 |
|
| 732 |
String xml = |
| 733 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| 734 |
"<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\">" + |
| 735 |
"<vcard>" + |
| 736 |
"<note><text>\u019dote</text></note>" + |
| 737 |
"</vcard>" + |
| 738 |
"</vcards>"; |
| 739 |
|
| 740 |
Path file = tempFolder.newFile().toPath(); |
| 741 |
try (Writer writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { |
| 742 |
writer.write(xml); |
| 743 |
} |
| 744 |
|
| 745 |
XCardReader reader = new XCardReader(file); |
| 746 |
VCardAsserter asserter = new VCardAsserter(reader); |
| 747 |
|
| 748 |
asserter.next(V4_0); |
| 749 |
|
| 750 |
asserter.simpleProperty(Note.class) |
| 751 |
.value("\u019dote") |
| 752 |
.noMore(); |
| 753 |
|
| 754 |
asserter.done(); |
| 755 |
|
| 756 |
} |
| 757 |
|
| 758 |
@Test |
| 759 |
public void read_empty() throws Exception { |
| 760 |
VCardAsserter asserter = readXml("<vcards xmlns=\"" + V4_0.getXmlNamespace() + "\" />"); |
| 761 |
asserter.done(); |
| 762 |
} |
| 763 |
|
| 764 |
@Test |
| 765 |
public void read_rfc6351_example() throws Throwable { |
| 766 |
VCardAsserter asserter = read("rfc6351-example.xml"); |
| 767 |
|
| 768 |
asserter.next(V4_0); |
| 769 |
|
| 770 |
|
| 771 |
asserter.simpleProperty(FormattedName.class) |
| 772 |
.value("Simon Perreault") |
| 773 |
.noMore(); |
| 774 |
|
| 775 |
asserter.structuredName() |
| 776 |
.family("Perreault") |
| 777 |
.given("Simon") |
| 778 |
.suffixes("ing. jr", "M.Sc.") |
| 779 |
.noMore(); |
| 780 |
|
| 781 |
asserter.dateProperty(Birthday.class) |
| 782 |
.partialDate(PartialDate.builder().month(2).date(3).build()) |
| 783 |
.noMore(); |
| 784 |
|
| 785 |
asserter.dateProperty(Anniversary.class) |
| 786 |
.partialDate(PartialDate.builder() |
| 787 |
.year(2009) |
| 788 |
.month(8) |
| 789 |
.date(8) |
| 790 |
.hour(14) |
| 791 |
.minute(30) |
| 792 |
.offset(ZoneOffset.ofHours(-5)) |
| 793 |
.build() |
| 794 |
) |
| 795 |
.noMore(); |
| 796 |
|
| 797 |
asserter.property(Gender.class) |
| 798 |
.expected(Gender.male()) |
| 799 |
.noMore(); |
| 800 |
|
| 801 |
asserter.simpleProperty(Language.class) |
| 802 |
.value("fr") |
| 803 |
.param("PREF", "1") |
| 804 |
.next() |
| 805 |
.value("en") |
| 806 |
.param("PREF", "2") |
| 807 |
.noMore(); |
| 808 |
|
| 809 |
asserter.listProperty(Organization.class) |
| 810 |
.values("Viagenie") |
| 811 |
.param("TYPE", "work") |
| 812 |
.noMore(); |
| 813 |
|
| 814 |
asserter.address() |
| 815 |
.streetAddress("2875 boul. Laurier, suite D2-630") |
| 816 |
.locality("Quebec") |
| 817 |
.region("QC") |
| 818 |
.postalCode("G1V 2M2") |
| 819 |
.country("Canada") |
| 820 |
.label("Simon Perreault\n2875 boul. Laurier, suite D2-630\nQuebec, QC, Canada\nG1V 2M2") |
| 821 |
.types(AddressType.WORK) |
| 822 |
.noMore(); |
| 823 |
|
| 824 |
asserter.telephone() |
| 825 |
.uri(new TelUri.Builder("+1-418-656-9254").extension("102").build()) |
| 826 |
.types(TelephoneType.WORK, TelephoneType.VOICE) |
| 827 |
.next() |
| 828 |
.uri(new TelUri.Builder("+1-418-262-6501").build()) |
| 829 |
.types(TelephoneType.WORK, TelephoneType.TEXT, TelephoneType.VOICE, TelephoneType.CELL, TelephoneType.VIDEO) |
| 830 |
.noMore(); |
| 831 |
|
| 832 |
asserter.email() |
| 833 |
.value("[email protected]") |
| 834 |
.types(EmailType.WORK) |
| 835 |
.noMore(); |
| 836 |
|
| 837 |
asserter.geo() |
| 838 |
.latitude(46.766336) |
| 839 |
.longitude(-71.28955) |
| 840 |
.param("TYPE", "work") |
| 841 |
.noMore(); |
| 842 |
|
| 843 |
asserter.binaryProperty(Key.class) |
| 844 |
.url("http://www.viagenie.ca/simon.perreault/simon.asc") |
| 845 |
.param("TYPE", "work") |
| 846 |
.noMore(); |
| 847 |
|
| 848 |
asserter.timezone() |
| 849 |
.text("America/Montreal") |
| 850 |
.noMore(); |
| 851 |
|
| 852 |
asserter.simpleProperty(Url.class) |
| 853 |
.value("http://nomis80.org") |
| 854 |
.param("TYPE", "home") |
| 855 |
.noMore(); |
| 856 |
|
| 857 |
|
| 858 |
asserter.validate().run(); |
| 859 |
asserter.done(); |
| 860 |
} |
| 861 |
|
| 862 |
private static VCardAsserter read(String file) throws SAXException, IOException { |
| 863 |
XCardReader reader = new XCardReader(XCardReaderTest.class.getResourceAsStream(file)); |
| 864 |
return new VCardAsserter(reader); |
| 865 |
} |
| 866 |
|
| 867 |
private static VCardAsserter readXml(String xml) { |
| 868 |
XCardReader reader = new XCardReader(xml); |
| 869 |
return new VCardAsserter(reader); |
| 870 |
} |
| 871 |
} |