| 1 |
package ezvcard.io.text; |
| 2 |
|
| 3 |
import static ezvcard.VCardDataType.INTEGER; |
| 4 |
import static ezvcard.VCardDataType.TEXT; |
| 5 |
import static ezvcard.VCardVersion.V2_1; |
| 6 |
import static ezvcard.VCardVersion.V3_0; |
| 7 |
import static ezvcard.VCardVersion.V4_0; |
| 8 |
import static ezvcard.util.StringUtils.NEWLINE; |
| 9 |
import static ezvcard.util.TestUtils.assertNoMoreVCards; |
| 10 |
import static ezvcard.util.TestUtils.assertParseWarnings; |
| 11 |
import static ezvcard.util.TestUtils.assertPropertyCount; |
| 12 |
import static ezvcard.util.TestUtils.assertVersion; |
| 13 |
import static ezvcard.util.TestUtils.each; |
| 14 |
import static org.junit.Assert.assertEquals; |
| 15 |
import static org.junit.Assert.assertNull; |
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
import java.nio.charset.Charset; |
| 19 |
import java.util.Arrays; |
| 20 |
import java.util.Iterator; |
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import com.github.mangstadt.vinnie.codec.QuotedPrintableCodec; |
| 26 |
|
| 27 |
import ezvcard.VCard; |
| 28 |
import ezvcard.VCardDataType; |
| 29 |
import ezvcard.VCardVersion; |
| 30 |
import ezvcard.io.EmbeddedVCardException; |
| 31 |
import ezvcard.io.LuckyNumProperty; |
| 32 |
import ezvcard.io.LuckyNumProperty.LuckyNumScribe; |
| 33 |
import ezvcard.io.MyFormattedNameProperty; |
| 34 |
import ezvcard.io.MyFormattedNameProperty.MyFormattedNameScribe; |
| 35 |
import ezvcard.io.ParseContext; |
| 36 |
import ezvcard.io.scribe.CannotParseScribe; |
| 37 |
import ezvcard.io.scribe.SkipMeScribe; |
| 38 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 39 |
import ezvcard.parameter.AddressType; |
| 40 |
import ezvcard.parameter.VCardParameters; |
| 41 |
import ezvcard.property.Address; |
| 42 |
import ezvcard.property.FormattedName; |
| 43 |
import ezvcard.property.Label; |
| 44 |
import ezvcard.property.RawProperty; |
| 45 |
import ezvcard.property.VCardProperty; |
| 46 |
import ezvcard.property.asserter.VCardAsserter; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public class VCardReaderTest { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@Test |
| 86 |
public void type_parameter_enclosed_in_double_quotes() throws Exception { |
| 87 |
VCardVersion version = V2_1; |
| 88 |
{ |
| 89 |
|
| 90 |
VCardAsserter asserter = read( |
| 91 |
"BEGIN:VCARD\r\n" + |
| 92 |
"VERSION:" + version + "\r\n" + |
| 93 |
"PROP;TYPE=\"one,two,,three\";FOO=\"four,five,,six\":\r\n" + |
| 94 |
"END:VCARD\r\n" |
| 95 |
); |
| 96 |
|
| 97 |
asserter.next(version); |
| 98 |
|
| 99 |
asserter.rawProperty("PROP") |
| 100 |
.param("TYPE", "\"one,two,,three\"") |
| 101 |
.param("FOO", "\"four,five,,six\"") |
| 102 |
.value("") |
| 103 |
.noMore(); |
| 104 |
|
| 105 |
asserter.done(); |
| 106 |
|
| 107 |
} |
| 108 |
for (VCardVersion v : each(V3_0, V4_0)) { |
| 109 |
|
| 110 |
VCardAsserter asserter = read( |
| 111 |
"BEGIN:VCARD\r\n" + |
| 112 |
"VERSION:" + v + "\r\n" + |
| 113 |
"PROP;TYPE=\"one,two,,three\";FOO=\"four,five,,six\":\r\n" + |
| 114 |
"END:VCARD\r\n" |
| 115 |
); |
| 116 |
|
| 117 |
asserter.next(v); |
| 118 |
|
| 119 |
asserter.rawProperty("PROP") |
| 120 |
.param("TYPE", "one", "two", "", "three") |
| 121 |
.param("FOO", "four,five,,six") |
| 122 |
.value("") |
| 123 |
.noMore(); |
| 124 |
|
| 125 |
asserter.done(); |
| 126 |
|
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
@Test |
| 134 |
public void nameless_parameters() throws Exception { |
| 135 |
for (VCardVersion version : VCardVersion.values()) { |
| 136 |
|
| 137 |
VCardAsserter asserter = read( |
| 138 |
"BEGIN:VCARD\r\n" + |
| 139 |
"VERSION:" + version + "\r\n" + |
| 140 |
"PROP;TEXT;QUOTED-PRINTABLE;HOME;FOO:\r\n" + |
| 141 |
"END:VCARD\r\n" |
| 142 |
); |
| 143 |
|
| 144 |
asserter.next(version); |
| 145 |
|
| 146 |
asserter.rawProperty("PROP") |
| 147 |
.param("ENCODING", "QUOTED-PRINTABLE") |
| 148 |
.param("TYPE", "HOME", "FOO") |
| 149 |
.dataType(VCardDataType.TEXT) |
| 150 |
.value("") |
| 151 |
.noMore(); |
| 152 |
|
| 153 |
asserter.done(); |
| 154 |
|
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void quoted_printable_encoding_invalid_value() throws Exception { |
| 160 |
for (VCardVersion version : VCardVersion.values()) { |
| 161 |
|
| 162 |
VCardAsserter asserter = read( |
| 163 |
"BEGIN:VCARD\r\n" + |
| 164 |
"VERSION:" + version + "\r\n" + |
| 165 |
"PROP;ENCODING=QUOTED-PRINTABLE:=nnnn\r\n" + |
| 166 |
"END:VCARD\r\n" |
| 167 |
); |
| 168 |
|
| 169 |
asserter.next(version); |
| 170 |
|
| 171 |
asserter.rawProperty("PROP") |
| 172 |
.param("ENCODING", "QUOTED-PRINTABLE") |
| 173 |
.value("=nnnn") |
| 174 |
.noMore(); |
| 175 |
|
| 176 |
asserter.warnings(27); |
| 177 |
asserter.done(); |
| 178 |
|
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void quoted_printable_invalid_charset() throws Exception { |
| 184 |
String decoded = "one=two"; |
| 185 |
|
| 186 |
for (VCardVersion version : VCardVersion.values()) { |
| 187 |
|
| 188 |
{ |
| 189 |
Charset charset = Charset.defaultCharset(); |
| 190 |
QuotedPrintableCodec codec = new QuotedPrintableCodec(charset.name()); |
| 191 |
String encoded = codec.encode(decoded); |
| 192 |
|
| 193 |
|
| 194 |
VCardAsserter asserter = read( |
| 195 |
"BEGIN:VCARD\r\n" + |
| 196 |
"VERSION:" + version + "\r\n" + |
| 197 |
"PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=invalid:" + encoded + "\r\n" + |
| 198 |
"END:VCARD\r\n" |
| 199 |
); |
| 200 |
|
| 201 |
asserter.next(version); |
| 202 |
|
| 203 |
asserter.rawProperty("PROP") |
| 204 |
.param("ENCODING", "QUOTED-PRINTABLE") |
| 205 |
.param("CHARSET", "invalid") |
| 206 |
.value(decoded) |
| 207 |
.noMore(); |
| 208 |
|
| 209 |
asserter.warnings(27); |
| 210 |
asserter.done(); |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
{ |
| 216 |
Charset charset = Charset.forName("UTF-16"); |
| 217 |
QuotedPrintableCodec codec = new QuotedPrintableCodec(charset.name()); |
| 218 |
String encoded = codec.encode(decoded); |
| 219 |
|
| 220 |
|
| 221 |
String str = |
| 222 |
"BEGIN:VCARD\r\n" + |
| 223 |
"VERSION:" + version + "\r\n" + |
| 224 |
"PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=invalid:" + encoded + "\r\n" + |
| 225 |
"END:VCARD\r\n"; |
| 226 |
|
| 227 |
VCardReader reader = new VCardReader(str); |
| 228 |
reader.setDefaultQuotedPrintableCharset(charset); |
| 229 |
VCardAsserter asserter = new VCardAsserter(reader); |
| 230 |
|
| 231 |
asserter.next(version); |
| 232 |
|
| 233 |
asserter.rawProperty("PROP") |
| 234 |
.param("ENCODING", "QUOTED-PRINTABLE") |
| 235 |
.param("CHARSET", "invalid") |
| 236 |
.value(decoded) |
| 237 |
.noMore(); |
| 238 |
|
| 239 |
asserter.warnings(27); |
| 240 |
asserter.done(); |
| 241 |
|
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
@Test |
| 247 |
public void extended_properties() throws Exception { |
| 248 |
for (VCardVersion version : VCardVersion.values()) { |
| 249 |
|
| 250 |
String str = |
| 251 |
"BEGIN:VCARD\r\n" + |
| 252 |
"VERSION:" + version + "\r\n" + |
| 253 |
"X-LUCKY-NUM:24\r\n" + |
| 254 |
"X-GENDER:ma\\,le\r\n" + |
| 255 |
"X-LUCKY-NUM:22\r\n" + |
| 256 |
"END:VCARD\r\n"; |
| 257 |
|
| 258 |
|
| 259 |
VCardReader reader = new VCardReader(str); |
| 260 |
reader.registerScribe(new LuckyNumScribe()); |
| 261 |
VCard vcard = reader.readNext(); |
| 262 |
assertVersion(version, vcard); |
| 263 |
assertPropertyCount(3, vcard); |
| 264 |
|
| 265 |
|
| 266 |
List<LuckyNumProperty> luckyNumTypes = vcard.getProperties(LuckyNumProperty.class); |
| 267 |
assertEquals(2, luckyNumTypes.size()); |
| 268 |
assertEquals(24, luckyNumTypes.get(0).luckyNum); |
| 269 |
assertEquals(22, luckyNumTypes.get(1).luckyNum); |
| 270 |
assertTrue(vcard.getExtendedProperties("X-LUCKY-NUM").isEmpty()); |
| 271 |
|
| 272 |
|
| 273 |
List<RawProperty> genderTypes = vcard.getExtendedProperties("X-GENDER"); |
| 274 |
assertEquals(1, genderTypes.size()); |
| 275 |
assertEquals("ma\\,le", genderTypes.get(0).getValue()); |
| 276 |
|
| 277 |
assertParseWarnings(reader); |
| 278 |
assertNoMoreVCards(reader); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
@Test |
| 283 |
public void extended_properties_override_standard_property_scribes() throws Exception { |
| 284 |
for (VCardVersion version : VCardVersion.values()) { |
| 285 |
|
| 286 |
String str = |
| 287 |
"BEGIN:VCARD\r\n" + |
| 288 |
"VERSION:" + version + "\r\n" + |
| 289 |
"FN:John Doe\r\n" + |
| 290 |
"END:VCARD\r\n"; |
| 291 |
|
| 292 |
|
| 293 |
VCardReader reader = new VCardReader(str); |
| 294 |
reader.registerScribe(new MyFormattedNameScribe()); |
| 295 |
VCard vcard = reader.readNext(); |
| 296 |
assertVersion(version, vcard); |
| 297 |
assertPropertyCount(1, vcard); |
| 298 |
|
| 299 |
|
| 300 |
MyFormattedNameProperty fn = vcard.getProperty(MyFormattedNameProperty.class); |
| 301 |
assertEquals("JOHN DOE", fn.value); |
| 302 |
|
| 303 |
assertParseWarnings(reader); |
| 304 |
assertNoMoreVCards(reader); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void read_multiple() throws Exception { |
| 310 |
|
| 311 |
VCardAsserter asserter = read( |
| 312 |
"BEGIN:VCARD\r\n" + |
| 313 |
"VERSION:2.1\r\n" + |
| 314 |
"FN:John Doe\r\n" + |
| 315 |
"END:VCARD\r\n" + |
| 316 |
"BEGIN:VCARD\r\n" + |
| 317 |
"VERSION:3.0\r\n" + |
| 318 |
"FN:Jane Doe\r\n" + |
| 319 |
"END:VCARD\r\n" |
| 320 |
); |
| 321 |
|
| 322 |
asserter.next(V2_1); |
| 323 |
asserter.simpleProperty(FormattedName.class) |
| 324 |
.value("John Doe") |
| 325 |
.noMore(); |
| 326 |
|
| 327 |
asserter.next(V3_0); |
| 328 |
asserter.simpleProperty(FormattedName.class) |
| 329 |
.value("Jane Doe") |
| 330 |
.noMore(); |
| 331 |
|
| 332 |
asserter.done(); |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
@Test |
| 337 |
public void nested_vcard() throws Exception { |
| 338 |
for (VCardVersion version : VCardVersion.values()) { |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
String str = |
| 346 |
"BEGIN:VCARD\r\n" + |
| 347 |
"VERSION:" + version + "\r\n" + |
| 348 |
"AGENT:\r\n" + |
| 349 |
"BEGIN:VCARD\r\n" + |
| 350 |
"VERSION:" + version + "\r\n" + |
| 351 |
"FN:Agent 007\r\n" + |
| 352 |
"AGENT:\r\n" + |
| 353 |
"BEGIN:VCARD\r\n" + |
| 354 |
"VERSION:" + version + "\r\n" + |
| 355 |
"FN:Agent 009\r\n" + |
| 356 |
"END:VCARD\r\n" + |
| 357 |
"END:VCARD\r\n" + |
| 358 |
"FN:John Doe\r\n" + |
| 359 |
"END:VCARD\r\n"; |
| 360 |
|
| 361 |
|
| 362 |
VCardReader reader = new VCardReader(str); |
| 363 |
|
| 364 |
VCard vcard = reader.readNext(); |
| 365 |
assertVersion(version, vcard); |
| 366 |
assertPropertyCount(2, vcard); |
| 367 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 368 |
{ |
| 369 |
VCard agent1 = vcard.getAgent().getVCard(); |
| 370 |
assertVersion(version, agent1); |
| 371 |
assertPropertyCount(2, agent1); |
| 372 |
assertEquals("Agent 007", agent1.getFormattedName().getValue()); |
| 373 |
{ |
| 374 |
VCard agent2 = agent1.getAgent().getVCard(); |
| 375 |
assertVersion(version, agent2); |
| 376 |
assertPropertyCount(1, agent2); |
| 377 |
assertEquals("Agent 009", agent2.getFormattedName().getValue()); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
assertParseWarnings(reader); |
| 382 |
assertNoMoreVCards(reader); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
@Test |
| 387 |
public void nested_vcard_missing_vcard() throws Exception { |
| 388 |
for (VCardVersion version : VCardVersion.values()) { |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
String str = |
| 396 |
"BEGIN:VCARD\r\n" + |
| 397 |
"VERSION:" + version + "\r\n" + |
| 398 |
"NESTED:\r\n" + |
| 399 |
"FN:John Doe\r\n" + |
| 400 |
"END:VCARD\r\n"; |
| 401 |
|
| 402 |
|
| 403 |
VCardReader reader = new VCardReader(str); |
| 404 |
reader.registerScribe(new NestedScribe()); |
| 405 |
VCard vcard = reader.readNext(); |
| 406 |
assertVersion(version, vcard); |
| 407 |
assertPropertyCount(2, vcard); |
| 408 |
|
| 409 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 410 |
assertNull(vcard.getProperty(Nested.class).vcard); |
| 411 |
|
| 412 |
assertParseWarnings(reader); |
| 413 |
assertNoMoreVCards(reader); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
@Test |
| 418 |
public void nested_vcard_with_labels() throws Exception { |
| 419 |
for (VCardVersion version : VCardVersion.values()) { |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
String str = |
| 427 |
"BEGIN:VCARD\r\n" + |
| 428 |
"VERSION:" + version + "\r\n" + |
| 429 |
"ADR;TYPE=home:;;;;;\r\n" + |
| 430 |
"ADR;TYPE=work:;;;;;\r\n" + |
| 431 |
"AGENT:\r\n" + |
| 432 |
"BEGIN:VCARD\r\n" + |
| 433 |
"VERSION:" + version + "\r\n" + |
| 434 |
"LABEL;TYPE=home:home label\r\n" + |
| 435 |
"AGENT:\r\n" + |
| 436 |
"BEGIN:VCARD\r\n" + |
| 437 |
"VERSION:" + version + "\r\n" + |
| 438 |
"ADR;TYPE=dom:;;;;;\r\n" + |
| 439 |
"LABEL;TYPE=dom:dom label\r\n" + |
| 440 |
"END:VCARD\r\n" + |
| 441 |
"ADR;TYPE=dom:;;;;;\r\n" + |
| 442 |
"END:VCARD\r\n" + |
| 443 |
"LABEL;TYPE=work:work label\r\n" + |
| 444 |
"END:VCARD\r\n"; |
| 445 |
|
| 446 |
|
| 447 |
VCardReader reader = new VCardReader(str); |
| 448 |
VCard vcard = reader.readNext(); |
| 449 |
assertVersion(version, vcard); |
| 450 |
assertPropertyCount(3, vcard); |
| 451 |
|
| 452 |
Iterator<Address> adrs = vcard.getAddresses().iterator(); |
| 453 |
|
| 454 |
Address adr = adrs.next(); |
| 455 |
assertEquals(Arrays.asList(AddressType.HOME), adr.getTypes()); |
| 456 |
assertNull(adr.getLabel()); |
| 457 |
|
| 458 |
adr = adrs.next(); |
| 459 |
assertEquals(Arrays.asList(AddressType.WORK), adr.getTypes()); |
| 460 |
assertEquals("work label", adr.getLabel()); |
| 461 |
|
| 462 |
{ |
| 463 |
VCard agentVCard = vcard.getAgent().getVCard(); |
| 464 |
assertVersion(version, agentVCard); |
| 465 |
assertPropertyCount(3, agentVCard); |
| 466 |
|
| 467 |
adr = agentVCard.getAddresses().get(0); |
| 468 |
assertEquals(Arrays.asList(AddressType.DOM), adr.getTypes()); |
| 469 |
assertNull(adr.getLabel()); |
| 470 |
|
| 471 |
Label label = agentVCard.getOrphanedLabels().get(0); |
| 472 |
assertEquals(Arrays.asList(AddressType.HOME), label.getTypes()); |
| 473 |
|
| 474 |
{ |
| 475 |
VCard agentAgentVCard = agentVCard.getAgent().getVCard(); |
| 476 |
assertVersion(version, agentAgentVCard); |
| 477 |
assertPropertyCount(1, agentAgentVCard); |
| 478 |
|
| 479 |
adr = agentAgentVCard.getAddresses().get(0); |
| 480 |
assertEquals(Arrays.asList(AddressType.DOM), adr.getTypes()); |
| 481 |
assertEquals("dom label", adr.getLabel()); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
assertParseWarnings(reader); |
| 486 |
assertNoMoreVCards(reader); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
@Test |
| 491 |
public void embedded_vcard() throws Exception { |
| 492 |
for (VCardVersion version : VCardVersion.values()) { |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
String str = |
| 500 |
"BEGIN:VCARD\r\n" + |
| 501 |
"VERSION:" + version + "\r\n" + |
| 502 |
"AGENT:" + |
| 503 |
"BEGIN:VCARD\\n" + |
| 504 |
"VERSION:" + version + "\\n" + |
| 505 |
"FN:Agent 007\\n" + |
| 506 |
"AGENT:" + |
| 507 |
"BEGIN:VCARD\\\\n" + |
| 508 |
"VERSION:" + version + "\\\\n" + |
| 509 |
"FN:Agent 009\\\\n" + |
| 510 |
"END:VCARD\\\\n" + |
| 511 |
"END:VCARD\r\n" + |
| 512 |
"FN:John Doe\r\n" + |
| 513 |
"END:VCARD\r\n"; |
| 514 |
|
| 515 |
|
| 516 |
VCardReader reader = new VCardReader(str); |
| 517 |
VCard vcard = reader.readNext(); |
| 518 |
assertVersion(version, vcard); |
| 519 |
assertPropertyCount(2, vcard); |
| 520 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 521 |
{ |
| 522 |
VCard agentVCard = vcard.getAgent().getVCard(); |
| 523 |
assertVersion(version, agentVCard); |
| 524 |
assertPropertyCount(2, agentVCard); |
| 525 |
assertEquals("Agent 007", agentVCard.getFormattedName().getValue()); |
| 526 |
{ |
| 527 |
VCard agentAgentVCard = agentVCard.getAgent().getVCard(); |
| 528 |
assertVersion(version, agentAgentVCard); |
| 529 |
assertPropertyCount(1, agentAgentVCard); |
| 530 |
assertEquals("Agent 009", agentAgentVCard.getFormattedName().getValue()); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
assertParseWarnings(reader); |
| 535 |
assertNoMoreVCards(reader); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
@Test |
| 545 |
public void label_properties() throws Exception { |
| 546 |
for (VCardVersion version : VCardVersion.values()) { |
| 547 |
|
| 548 |
VCardAsserter asserter = read( |
| 549 |
"BEGIN:VCARD\r\n" + |
| 550 |
"VERSION:" + version.getVersion() + "\r\n" + |
| 551 |
"ADR;TYPE=home;TYPE=parcel:;;123 Main St.;Austin;TX;91827;USA\r\n" + |
| 552 |
"LABEL;TYPE=parcel;TYPE=home:123 Main St.\\nAustin\\, TX 91827\\nUSA\r\n" + |
| 553 |
"ADR;TYPE=work:;;200 Broadway;New York;NY;12345;USA\r\n" + |
| 554 |
"LABEL;TYPE=parcel:200 Broadway\\nNew York\\, NY 12345\\nUSA\r\n" + |
| 555 |
"END:VCARD\r\n" |
| 556 |
); |
| 557 |
|
| 558 |
asserter.next(version); |
| 559 |
|
| 560 |
asserter.address() |
| 561 |
.streetAddress("123 Main St.") |
| 562 |
.locality("Austin") |
| 563 |
.region("TX") |
| 564 |
.postalCode("91827") |
| 565 |
.country("USA") |
| 566 |
.label("123 Main St." + NEWLINE + "Austin, TX 91827" + NEWLINE + "USA") |
| 567 |
.types(AddressType.HOME, AddressType.PARCEL) |
| 568 |
.next() |
| 569 |
.streetAddress("200 Broadway") |
| 570 |
.locality("New York") |
| 571 |
.region("NY") |
| 572 |
.postalCode("12345") |
| 573 |
.country("USA") |
| 574 |
.types(AddressType.WORK) |
| 575 |
.noMore(); |
| 576 |
|
| 577 |
asserter.simpleProperty(Label.class) |
| 578 |
.value("200 Broadway" + NEWLINE + "New York, NY 12345" + NEWLINE + "USA") |
| 579 |
.param("TYPE", "parcel") |
| 580 |
.noMore(); |
| 581 |
|
| 582 |
asserter.done(); |
| 583 |
|
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
@Test |
| 592 |
public void label_parameters() throws Exception { |
| 593 |
for (VCardVersion version : VCardVersion.values()) { |
| 594 |
|
| 595 |
VCardAsserter asserter = read( |
| 596 |
"BEGIN:VCARD\r\n" + |
| 597 |
"VERSION:" + version.getVersion() + "\r\n" + |
| 598 |
"ADR;LABEL=one\\ntwo;PARAM=one\\ntwo:\r\n" + |
| 599 |
"PROP;LABEL=one\\ntwo;PARAM=one\\ntwo:\r\n" + |
| 600 |
"END:VCARD\r\n" |
| 601 |
); |
| 602 |
|
| 603 |
asserter.next(version); |
| 604 |
|
| 605 |
asserter.address() |
| 606 |
.label("one" + NEWLINE + "two") |
| 607 |
.param("PARAM", "one\\ntwo") |
| 608 |
.noMore(); |
| 609 |
asserter.rawProperty("PROP") |
| 610 |
.param("LABEL", "one\\ntwo") |
| 611 |
.param("PARAM", "one\\ntwo") |
| 612 |
.value("") |
| 613 |
.noMore(); |
| 614 |
|
| 615 |
asserter.done(); |
| 616 |
|
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
@Test |
| 621 |
public void skipMeException() throws Exception { |
| 622 |
for (VCardVersion version : VCardVersion.values()) { |
| 623 |
|
| 624 |
String str = |
| 625 |
"BEGIN:VCARD\r\n" + |
| 626 |
"VERSION:" + version + "\r\n" + |
| 627 |
"SKIPME:value\r\n" + |
| 628 |
"X-FOO:value\r\n" + |
| 629 |
"END:VCARD\r\n"; |
| 630 |
|
| 631 |
VCardReader reader = new VCardReader(str); |
| 632 |
reader.registerScribe(new SkipMeScribe()); |
| 633 |
VCardAsserter asserter = new VCardAsserter(reader); |
| 634 |
|
| 635 |
asserter.next(version); |
| 636 |
|
| 637 |
asserter.rawProperty("X-FOO") |
| 638 |
.value("value") |
| 639 |
.noMore(); |
| 640 |
|
| 641 |
asserter.warnings(22); |
| 642 |
asserter.done(); |
| 643 |
|
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
@Test |
| 648 |
public void cannotParseException() throws Exception { |
| 649 |
for (VCardVersion version : VCardVersion.values()) { |
| 650 |
|
| 651 |
String str = |
| 652 |
"BEGIN:VCARD\r\n" + |
| 653 |
"VERSION:" + version + "\r\n" + |
| 654 |
"group.CANNOTPARSE;PARAM=value;VALUE=text:value\r\n" + |
| 655 |
"X-FOO:value\r\n" + |
| 656 |
"END:VCARD\r\n"; |
| 657 |
|
| 658 |
VCardReader reader = new VCardReader(str); |
| 659 |
reader.registerScribe(new CannotParseScribe()); |
| 660 |
VCardAsserter asserter = new VCardAsserter(reader); |
| 661 |
|
| 662 |
asserter.next(version); |
| 663 |
|
| 664 |
asserter.rawProperty("X-FOO") |
| 665 |
.value("value") |
| 666 |
.noMore(); |
| 667 |
|
| 668 |
asserter.rawProperty("CANNOTPARSE") |
| 669 |
.group("group") |
| 670 |
.param("PARAM", "value") |
| 671 |
.dataType(VCardDataType.TEXT) |
| 672 |
.value("value") |
| 673 |
.noMore(); |
| 674 |
|
| 675 |
asserter.warnings(25); |
| 676 |
asserter.done(); |
| 677 |
|
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
@Test |
| 682 |
public void invalid_line() throws Exception { |
| 683 |
for (VCardVersion version : VCardVersion.values()) { |
| 684 |
|
| 685 |
VCardAsserter asserter = read( |
| 686 |
"BEGIN:VCARD\r\n" + |
| 687 |
"VERSION:" + version + "\r\n" + |
| 688 |
"bad-line\r\n" + |
| 689 |
"END:VCARD\r\n" |
| 690 |
); |
| 691 |
|
| 692 |
|
| 693 |
asserter.next(version); |
| 694 |
asserter.warnings(27); |
| 695 |
asserter.done(); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
@Test |
| 700 |
public void property_warning() throws Exception { |
| 701 |
for (VCardVersion version : VCardVersion.values()) { |
| 702 |
|
| 703 |
String str = |
| 704 |
"BEGIN:VCARD\r\n" + |
| 705 |
"VERSION:" + version + "\r\n" + |
| 706 |
"WARNINGS:foo\r\n" + |
| 707 |
"END:VCARD\r\n"; |
| 708 |
|
| 709 |
|
| 710 |
VCardReader reader = new VCardReader(str); |
| 711 |
reader.registerScribe(new WarningsScribe()); |
| 712 |
VCard vcard = reader.readNext(); |
| 713 |
assertVersion(version, vcard); |
| 714 |
assertPropertyCount(1, vcard); |
| 715 |
assertParseWarnings(reader, (Integer) null); |
| 716 |
assertNoMoreVCards(reader); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
@Test |
| 721 |
public void warnings_list_cleared() throws Exception { |
| 722 |
for (VCardVersion version : VCardVersion.values()) { |
| 723 |
|
| 724 |
VCardAsserter asserter = read( |
| 725 |
"BEGIN:VCARD\r\n" + |
| 726 |
"VERSION:" + version + "\r\n" + |
| 727 |
"bad-line\r\n" + |
| 728 |
"END:VCARD\r\n" + |
| 729 |
"BEGIN:VCARD\r\n" + |
| 730 |
"VERSION:" + version + "\r\n" + |
| 731 |
"END:VCARD\r\n" |
| 732 |
); |
| 733 |
|
| 734 |
|
| 735 |
asserter.next(version); |
| 736 |
asserter.warnings(27); |
| 737 |
|
| 738 |
asserter.next(version); |
| 739 |
|
| 740 |
asserter.done(); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
@Test |
| 745 |
public void warnings_in_nested_vcard() throws Exception { |
| 746 |
for (VCardVersion version : VCardVersion.values()) { |
| 747 |
|
| 748 |
String str = |
| 749 |
"BEGIN:VCARD\r\n" + |
| 750 |
"VERSION:" + version + "\r\n" + |
| 751 |
"AGENT:\r\n" + |
| 752 |
"BEGIN:VCARD\r\n" + |
| 753 |
"VERSION:" + version + "\r\n" + |
| 754 |
"WARNINGS:value\r\n" + |
| 755 |
"END:VCARD\r\n" + |
| 756 |
"END:VCARD\r\n"; |
| 757 |
|
| 758 |
|
| 759 |
VCardReader reader = new VCardReader(str); |
| 760 |
reader.registerScribe(new WarningsScribe()); |
| 761 |
reader.readNext(); |
| 762 |
|
| 763 |
assertParseWarnings(reader, (Integer) null); |
| 764 |
assertNoMoreVCards(reader); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
@Test |
| 769 |
public void warnings_in_embedded_vcard() throws Exception { |
| 770 |
for (VCardVersion version : VCardVersion.values()) { |
| 771 |
|
| 772 |
String str = |
| 773 |
"BEGIN:VCARD\r\n" + |
| 774 |
"VERSION:" + version + "\r\n" + |
| 775 |
"AGENT:BEGIN:VCARD\\nVERSION:" + version + "\\nWARNINGS:value\\nEND:VCARD\r\n" + |
| 776 |
"END:VCARD\r\n"; |
| 777 |
|
| 778 |
|
| 779 |
VCardReader reader = new VCardReader(str); |
| 780 |
reader.registerScribe(new WarningsScribe()); |
| 781 |
reader.readNext(); |
| 782 |
|
| 783 |
assertParseWarnings(reader, (Integer) null); |
| 784 |
assertNoMoreVCards(reader); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
@Test |
| 789 |
public void value_parameter() throws Exception { |
| 790 |
for (VCardVersion version : VCardVersion.values()) { |
| 791 |
|
| 792 |
String str = |
| 793 |
"BEGIN:VCARD\r\n" + |
| 794 |
"VERSION:" + version + "\r\n" + |
| 795 |
"VAL;VALUE=text;LANGUAGE=en:value\r\n" + |
| 796 |
"VAL;LANGUAGE=en:value\r\n" + |
| 797 |
"VAL;VALUE=text:value\r\n" + |
| 798 |
"VAL:value\r\n" + |
| 799 |
"END:VCARD\r\n"; |
| 800 |
|
| 801 |
|
| 802 |
VCardReader reader = new VCardReader(str); |
| 803 |
reader.registerScribe(new ValueScribe()); |
| 804 |
VCard vcard = reader.readNext(); |
| 805 |
assertVersion(version, vcard); |
| 806 |
assertPropertyCount(4, vcard); |
| 807 |
|
| 808 |
Iterator<ValueProp> it = vcard.getProperties(ValueProp.class).iterator(); |
| 809 |
|
| 810 |
ValueProp prop = it.next(); |
| 811 |
assertEquals(TEXT, prop.dataType); |
| 812 |
assertEquals(1, prop.getParameters().size()); |
| 813 |
assertNull(prop.getParameters().getValue()); |
| 814 |
assertEquals("en", prop.getParameters().getLanguage()); |
| 815 |
|
| 816 |
prop = it.next(); |
| 817 |
assertEquals(INTEGER, prop.dataType); |
| 818 |
assertEquals(1, prop.getParameters().size()); |
| 819 |
assertNull(prop.getParameters().getValue()); |
| 820 |
assertEquals("en", prop.getParameters().getLanguage()); |
| 821 |
|
| 822 |
prop = it.next(); |
| 823 |
assertEquals(TEXT, prop.dataType); |
| 824 |
assertEquals(0, prop.getParameters().size()); |
| 825 |
assertNull(prop.getParameters().getValue()); |
| 826 |
assertNull(prop.getParameters().getLanguage()); |
| 827 |
|
| 828 |
prop = it.next(); |
| 829 |
assertEquals(INTEGER, prop.dataType); |
| 830 |
assertEquals(0, prop.getParameters().size()); |
| 831 |
assertNull(prop.getParameters().getValue()); |
| 832 |
assertNull(prop.getParameters().getLanguage()); |
| 833 |
|
| 834 |
assertParseWarnings(reader); |
| 835 |
assertNoMoreVCards(reader); |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
@Test |
| 840 |
public void invalid_version() throws Exception { |
| 841 |
|
| 842 |
VCardAsserter asserter = read( |
| 843 |
"BEGIN:VCARD\r\n" + |
| 844 |
"VERSION:invalid\r\n" + |
| 845 |
"END:VCARD\r\n" |
| 846 |
); |
| 847 |
|
| 848 |
asserter.next(V2_1); |
| 849 |
asserter.rawProperty("VERSION") |
| 850 |
.value("invalid") |
| 851 |
.noMore(); |
| 852 |
asserter.warnings(27); |
| 853 |
asserter.done(); |
| 854 |
|
| 855 |
} |
| 856 |
|
| 857 |
@Test |
| 858 |
public void skip_non_vcard_components() throws Exception { |
| 859 |
for (VCardVersion version : VCardVersion.values()) { |
| 860 |
|
| 861 |
VCardAsserter asserter = read( |
| 862 |
"BEGIN:VCALENDAR\r\n" + |
| 863 |
"VERSION:2.0\r\n" + |
| 864 |
"invalid line--warning should not be logged\r\n" + |
| 865 |
"PRODID:-//Company//Application//EN\r\n" + |
| 866 |
"END:VCALENDAR\r\n" + |
| 867 |
"invalid line--warning should not be logged\r\n" + |
| 868 |
"BEGIN:VCARD\r\n" + |
| 869 |
"VERSION:" + version + "\r\n" + |
| 870 |
"FN:John Doe\r\n" + |
| 871 |
"END:VCARD\r\n" |
| 872 |
); |
| 873 |
|
| 874 |
asserter.next(version); |
| 875 |
|
| 876 |
asserter.simpleProperty(FormattedName.class) |
| 877 |
.value("John Doe") |
| 878 |
.noMore(); |
| 879 |
|
| 880 |
asserter.done(); |
| 881 |
|
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
private static class ValueProp extends VCardProperty { |
| 886 |
private final VCardDataType dataType; |
| 887 |
|
| 888 |
public ValueProp(VCardDataType dataType) { |
| 889 |
this.dataType = dataType; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
private static class ValueScribe extends VCardPropertyScribe<ValueProp> { |
| 894 |
public ValueScribe() { |
| 895 |
super(ValueProp.class, "VAL"); |
| 896 |
} |
| 897 |
|
| 898 |
@Override |
| 899 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 900 |
return INTEGER; |
| 901 |
} |
| 902 |
|
| 903 |
@Override |
| 904 |
protected String _writeText(ValueProp property, WriteContext context) { |
| 905 |
return null; |
| 906 |
} |
| 907 |
|
| 908 |
@Override |
| 909 |
protected ValueProp _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 910 |
return new ValueProp(dataType); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
private static class WarningsProperty extends VCardProperty { |
| 915 |
|
| 916 |
} |
| 917 |
|
| 918 |
private static class WarningsScribe extends VCardPropertyScribe<WarningsProperty> { |
| 919 |
public WarningsScribe() { |
| 920 |
super(WarningsProperty.class, "WARNINGS"); |
| 921 |
} |
| 922 |
|
| 923 |
@Override |
| 924 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 925 |
return null; |
| 926 |
} |
| 927 |
|
| 928 |
@Override |
| 929 |
protected String _writeText(WarningsProperty property, WriteContext context) { |
| 930 |
return null; |
| 931 |
} |
| 932 |
|
| 933 |
@Override |
| 934 |
protected WarningsProperty _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 935 |
context.addWarning("one"); |
| 936 |
return new WarningsProperty(); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
private static class Nested extends VCardProperty { |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
private VCard vcard = new VCard(); |
| 946 |
} |
| 947 |
|
| 948 |
private static class NestedScribe extends VCardPropertyScribe<Nested> { |
| 949 |
public NestedScribe() { |
| 950 |
super(Nested.class, "NESTED"); |
| 951 |
} |
| 952 |
|
| 953 |
@Override |
| 954 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 955 |
return null; |
| 956 |
} |
| 957 |
|
| 958 |
@Override |
| 959 |
protected String _writeText(Nested property, WriteContext context) { |
| 960 |
return ""; |
| 961 |
} |
| 962 |
|
| 963 |
@Override |
| 964 |
protected Nested _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 965 |
throw new EmbeddedVCardException(new EmbeddedVCardException.InjectionCallback() { |
| 966 |
private final Nested property = new Nested(); |
| 967 |
|
| 968 |
public void injectVCard(VCard vcard) { |
| 969 |
property.vcard = vcard; |
| 970 |
} |
| 971 |
|
| 972 |
public VCardProperty getProperty() { |
| 973 |
return property; |
| 974 |
} |
| 975 |
}); |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
private static VCardAsserter read(String str) { |
| 980 |
VCardReader reader = new VCardReader(str); |
| 981 |
return new VCardAsserter(reader); |
| 982 |
} |
| 983 |
} |