| 1 |
package ezvcard.io.json; |
| 2 |
|
| 3 |
import static ezvcard.util.StringUtils.NEWLINE; |
| 4 |
import static ezvcard.util.TestUtils.assertValidate; |
| 5 |
import static org.junit.Assert.assertEquals; |
| 6 |
|
| 7 |
import java.io.IOException; |
| 8 |
import java.io.StringWriter; |
| 9 |
import java.nio.charset.StandardCharsets; |
| 10 |
import java.nio.file.Path; |
| 11 |
import java.time.OffsetDateTime; |
| 12 |
import java.time.ZoneOffset; |
| 13 |
|
| 14 |
import org.junit.Rule; |
| 15 |
import org.junit.Test; |
| 16 |
import org.junit.rules.TemporaryFolder; |
| 17 |
|
| 18 |
import ezvcard.VCard; |
| 19 |
import ezvcard.VCardDataType; |
| 20 |
import ezvcard.VCardVersion; |
| 21 |
import ezvcard.io.ParseContext; |
| 22 |
import ezvcard.io.scribe.SkipMeScribe; |
| 23 |
import ezvcard.io.scribe.VCardPropertyScribe; |
| 24 |
import ezvcard.io.text.WriteContext; |
| 25 |
import ezvcard.parameter.AddressType; |
| 26 |
import ezvcard.parameter.EmailType; |
| 27 |
import ezvcard.parameter.Encoding; |
| 28 |
import ezvcard.parameter.TelephoneType; |
| 29 |
import ezvcard.parameter.VCardParameters; |
| 30 |
import ezvcard.property.Address; |
| 31 |
import ezvcard.property.Anniversary; |
| 32 |
import ezvcard.property.Birthday; |
| 33 |
import ezvcard.property.FormattedName; |
| 34 |
import ezvcard.property.Gender; |
| 35 |
import ezvcard.property.Geo; |
| 36 |
import ezvcard.property.Key; |
| 37 |
import ezvcard.property.SkipMeProperty; |
| 38 |
import ezvcard.property.StructuredName; |
| 39 |
import ezvcard.property.Telephone; |
| 40 |
import ezvcard.property.Timezone; |
| 41 |
import ezvcard.property.VCardProperty; |
| 42 |
import ezvcard.util.Gobble; |
| 43 |
import ezvcard.util.PartialDate; |
| 44 |
import ezvcard.util.TelUri; |
| 45 |
|
| 46 |
|
| 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 |
public class JCardWriterTest { |
| 79 |
@Rule |
| 80 |
public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 81 |
|
| 82 |
@Test |
| 83 |
public void write_single_vcard() throws Throwable { |
| 84 |
StringWriter sw = new StringWriter(); |
| 85 |
try (JCardWriter writer = new JCardWriter(sw)) { |
| 86 |
writer.setAddProdId(false); |
| 87 |
|
| 88 |
VCard vcard = new VCard(); |
| 89 |
vcard.setFormattedName("John Doe"); |
| 90 |
writer.write(vcard); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
String expected = |
| 95 |
"[\"vcard\"," + |
| 96 |
"[" + |
| 97 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 98 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 99 |
"]" + |
| 100 |
"]"; |
| 101 |
|
| 102 |
assertEquals(expected, sw.toString()); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void write_multiple_vcards() throws Throwable { |
| 107 |
StringWriter sw = new StringWriter(); |
| 108 |
try (JCardWriter writer = new JCardWriter(sw, true)) { |
| 109 |
writer.setAddProdId(false); |
| 110 |
|
| 111 |
VCard vcard = new VCard(); |
| 112 |
vcard.setFormattedName("John Doe"); |
| 113 |
writer.write(vcard); |
| 114 |
|
| 115 |
vcard = new VCard(); |
| 116 |
vcard.setFormattedName("Jane Doe"); |
| 117 |
writer.write(vcard); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
String expected = |
| 122 |
"[" + |
| 123 |
"[\"vcard\"," + |
| 124 |
"[" + |
| 125 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 126 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 127 |
"]" + |
| 128 |
"]," + |
| 129 |
"[\"vcard\"," + |
| 130 |
"[" + |
| 131 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 132 |
"[\"fn\",{},\"text\",\"Jane Doe\"]" + |
| 133 |
"]" + |
| 134 |
"]" + |
| 135 |
"]"; |
| 136 |
|
| 137 |
assertEquals(expected, sw.toString()); |
| 138 |
} |
| 139 |
|
| 140 |
@Test |
| 141 |
public void setPrettyPrint() throws Throwable { |
| 142 |
StringWriter sw = new StringWriter(); |
| 143 |
try (JCardWriter writer = new JCardWriter(sw, true)) { |
| 144 |
writer.setAddProdId(false); |
| 145 |
writer.setPrettyPrint(true); |
| 146 |
|
| 147 |
VCard vcard = new VCard(); |
| 148 |
vcard.setFormattedName("John Doe"); |
| 149 |
writer.write(vcard); |
| 150 |
|
| 151 |
vcard = new VCard(); |
| 152 |
vcard.setFormattedName("John Doe"); |
| 153 |
writer.write(vcard); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
String expected = |
| 158 |
"[" + NEWLINE + |
| 159 |
" [" + NEWLINE + |
| 160 |
" \"vcard\"," + NEWLINE + |
| 161 |
" [" + NEWLINE + |
| 162 |
" [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE + |
| 163 |
" [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE + |
| 164 |
" ]" + NEWLINE + |
| 165 |
" ]," + NEWLINE + |
| 166 |
" [" + NEWLINE + |
| 167 |
" \"vcard\"," + NEWLINE + |
| 168 |
" [" + NEWLINE + |
| 169 |
" [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE + |
| 170 |
" [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE + |
| 171 |
" ]" + NEWLINE + |
| 172 |
" ]" + NEWLINE + |
| 173 |
"]"; |
| 174 |
|
| 175 |
assertEquals(expected, sw.toString()); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void setPrettyPrinter() throws Throwable { |
| 180 |
StringWriter sw = new StringWriter(); |
| 181 |
try (JCardWriter writer = new JCardWriter(sw, true)) { |
| 182 |
writer.setAddProdId(false); |
| 183 |
writer.setPrettyPrinter(new JCardPrettyPrinter()); |
| 184 |
|
| 185 |
VCard vcard = new VCard(); |
| 186 |
vcard.setFormattedName("John Doe"); |
| 187 |
writer.write(vcard); |
| 188 |
|
| 189 |
vcard = new VCard(); |
| 190 |
vcard.setFormattedName("John Doe"); |
| 191 |
writer.write(vcard); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
String expected = |
| 196 |
"[" + NEWLINE + |
| 197 |
" [" + NEWLINE + |
| 198 |
" \"vcard\"," + NEWLINE + |
| 199 |
" [" + NEWLINE + |
| 200 |
" [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE + |
| 201 |
" [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE + |
| 202 |
" ]" + NEWLINE + |
| 203 |
" ]," + NEWLINE + |
| 204 |
" [" + NEWLINE + |
| 205 |
" \"vcard\"," + NEWLINE + |
| 206 |
" [" + NEWLINE + |
| 207 |
" [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE + |
| 208 |
" [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE + |
| 209 |
" ]" + NEWLINE + |
| 210 |
" ]" + NEWLINE + |
| 211 |
"]"; |
| 212 |
|
| 213 |
assertEquals(expected, sw.toString()); |
| 214 |
} |
| 215 |
|
| 216 |
@Test |
| 217 |
public void write_no_vcards() throws Throwable { |
| 218 |
StringWriter sw = new StringWriter(); |
| 219 |
try (JCardWriter writer = new JCardWriter(sw)) { |
| 220 |
} |
| 221 |
assertEquals("", sw.toString()); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void write_raw_property() throws Throwable { |
| 226 |
StringWriter sw = new StringWriter(); |
| 227 |
try (JCardWriter writer = new JCardWriter(sw)) { |
| 228 |
writer.setAddProdId(false); |
| 229 |
|
| 230 |
VCard vcard = new VCard(); |
| 231 |
vcard.setFormattedName("John Doe"); |
| 232 |
vcard.addExtendedProperty("x-type", "value"); |
| 233 |
writer.write(vcard); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
String expected = |
| 238 |
"[\"vcard\"," + |
| 239 |
"[" + |
| 240 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 241 |
"[\"fn\",{},\"text\",\"John Doe\"]," + |
| 242 |
"[\"x-type\",{},\"unknown\",\"value\"]" + |
| 243 |
"]" + |
| 244 |
"]"; |
| 245 |
|
| 246 |
assertEquals(expected, sw.toString()); |
| 247 |
} |
| 248 |
|
| 249 |
@Test |
| 250 |
public void write_extended_property() throws Throwable { |
| 251 |
StringWriter sw = new StringWriter(); |
| 252 |
try (JCardWriter writer = new JCardWriter(sw)) { |
| 253 |
writer.registerScribe(new TestScribe()); |
| 254 |
writer.setAddProdId(false); |
| 255 |
|
| 256 |
VCard vcard = new VCard(); |
| 257 |
vcard.setFormattedName("John Doe"); |
| 258 |
vcard.addProperty(new TestProperty(JCardValue.single("value"))); |
| 259 |
writer.write(vcard); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
String expected = |
| 264 |
"[\"vcard\"," + |
| 265 |
"[" + |
| 266 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 267 |
"[\"fn\",{},\"text\",\"John Doe\"]," + |
| 268 |
"[\"x-type\",{},\"text\",\"value\"]" + |
| 269 |
"]" + |
| 270 |
"]"; |
| 271 |
|
| 272 |
assertEquals(expected, sw.toString()); |
| 273 |
} |
| 274 |
|
| 275 |
@Test |
| 276 |
public void write_quoted_printable_encoding_not_supported() throws Exception { |
| 277 |
StringWriter sw = new StringWriter(); |
| 278 |
try (JCardWriter writer = new JCardWriter(sw)) { |
| 279 |
writer.setAddProdId(false); |
| 280 |
|
| 281 |
VCard vcard = new VCard(); |
| 282 |
FormattedName fn = vcard.setFormattedName("Ömür Öde"); |
| 283 |
fn.getParameters().setEncoding(Encoding.QUOTED_PRINTABLE); |
| 284 |
fn.getParameters().setCharset("UTF-8"); |
| 285 |
writer.write(vcard); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
String expected = |
| 290 |
"[\"vcard\"," + |
| 291 |
"[" + |
| 292 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 293 |
"[\"fn\",{},\"text\",\"Ömür Öde\"]" + |
| 294 |
"]" + |
| 295 |
"]"; |
| 296 |
|
| 297 |
assertEquals(expected, sw.toString()); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void skipMeException() throws Throwable { |
| 302 |
StringWriter sw = new StringWriter(); |
| 303 |
try (JCardWriter writer = new JCardWriter(sw)) { |
| 304 |
writer.registerScribe(new SkipMeScribe()); |
| 305 |
writer.setAddProdId(false); |
| 306 |
|
| 307 |
VCard vcard = new VCard(); |
| 308 |
vcard.setFormattedName("John Doe"); |
| 309 |
vcard.addProperty(new SkipMeProperty()); |
| 310 |
writer.write(vcard); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
String expected = |
| 315 |
"[\"vcard\"," + |
| 316 |
"[" + |
| 317 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 318 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 319 |
"]" + |
| 320 |
"]"; |
| 321 |
|
| 322 |
assertEquals(expected, sw.toString()); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void utf8() throws Throwable { |
| 327 |
VCard vcard = new VCard(); |
| 328 |
vcard.addNote("\u019dote"); |
| 329 |
|
| 330 |
Path file = tempFolder.newFile().toPath(); |
| 331 |
try (JCardWriter writer = new JCardWriter(file)) { |
| 332 |
writer.setAddProdId(false); |
| 333 |
writer.write(vcard); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
String expected = |
| 338 |
"[\"vcard\"," + |
| 339 |
"[" + |
| 340 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 341 |
"[\"note\",{},\"text\",\"\u019dote\"]" + |
| 342 |
"]" + |
| 343 |
"]"; |
| 344 |
|
| 345 |
String actual = new Gobble(file).asString(StandardCharsets.UTF_8); |
| 346 |
assertEquals(expected, actual); |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void jcard_example() throws Throwable { |
| 351 |
VCard vcard = createExample(); |
| 352 |
|
| 353 |
assertValidate(vcard).versions(VCardVersion.V4_0).run(); |
| 354 |
StringWriter sw = new StringWriter(); |
| 355 |
try (JCardWriter writer = new JCardWriter(sw)) { |
| 356 |
writer.setAddProdId(false); |
| 357 |
writer.write(vcard); |
| 358 |
} |
| 359 |
String actual = sw.toString(); |
| 360 |
|
| 361 |
assertExample(actual, "jcard-example.json"); |
| 362 |
} |
| 363 |
|
| 364 |
public static VCard createExample() { |
| 365 |
VCard vcard = new VCard(); |
| 366 |
|
| 367 |
vcard.setFormattedName("SimonPerreault"); |
| 368 |
|
| 369 |
StructuredName n = new StructuredName(); |
| 370 |
n.setFamily("Perreault"); |
| 371 |
n.setGiven("Simon"); |
| 372 |
n.getSuffixes().add("ing.jr"); |
| 373 |
n.getSuffixes().add("M.Sc."); |
| 374 |
vcard.setStructuredName(n); |
| 375 |
|
| 376 |
Birthday bday = new Birthday(PartialDate.builder().month(2).date(3).build()); |
| 377 |
vcard.setBirthday(bday); |
| 378 |
|
| 379 |
Anniversary anniversary = new Anniversary(OffsetDateTime.of(2009, 8, 8, 14, 30, 0, 0, ZoneOffset.ofHours(-5))); |
| 380 |
vcard.setAnniversary(anniversary); |
| 381 |
|
| 382 |
vcard.setGender(Gender.male()); |
| 383 |
|
| 384 |
vcard.addLanguage("fr").setPref(1); |
| 385 |
vcard.addLanguage("en").setPref(2); |
| 386 |
|
| 387 |
vcard.setOrganization("Viagenie").setType("work"); |
| 388 |
|
| 389 |
Address adr = new Address(); |
| 390 |
adr.setExtendedAddress("SuiteD2-630"); |
| 391 |
adr.setStreetAddress("2875Laurier"); |
| 392 |
adr.setLocality("Quebec"); |
| 393 |
adr.setRegion("QC"); |
| 394 |
adr.setPostalCode("G1V2M2"); |
| 395 |
adr.setCountry("Canada"); |
| 396 |
adr.getTypes().add(AddressType.WORK); |
| 397 |
vcard.addAddress(adr); |
| 398 |
|
| 399 |
TelUri telUri = new TelUri.Builder("+1-418-656-9254").extension("102").build(); |
| 400 |
Telephone tel = new Telephone(telUri); |
| 401 |
tel.getTypes().add(TelephoneType.WORK); |
| 402 |
tel.getTypes().add(TelephoneType.VOICE); |
| 403 |
tel.setPref(1); |
| 404 |
vcard.addTelephoneNumber(tel); |
| 405 |
|
| 406 |
tel = new Telephone(new TelUri.Builder("+1-418-262-6501").build()); |
| 407 |
tel.getTypes().add(TelephoneType.WORK); |
| 408 |
tel.getTypes().add(TelephoneType.CELL); |
| 409 |
tel.getTypes().add(TelephoneType.VOICE); |
| 410 |
tel.getTypes().add(TelephoneType.VIDEO); |
| 411 |
tel.getTypes().add(TelephoneType.TEXT); |
| 412 |
vcard.addTelephoneNumber(tel); |
| 413 |
|
| 414 |
vcard.addEmail("[email protected]", EmailType.WORK); |
| 415 |
|
| 416 |
Geo geo = new Geo(46.772673, -71.282945); |
| 417 |
geo.setType("work"); |
| 418 |
vcard.setGeo(geo); |
| 419 |
|
| 420 |
Key key = new Key("http://www.viagenie.ca/simon.perreault/simon.asc", null); |
| 421 |
key.setType("work"); |
| 422 |
vcard.addKey(key); |
| 423 |
|
| 424 |
vcard.setTimezone(new Timezone(ZoneOffset.ofHours(-5))); |
| 425 |
|
| 426 |
vcard.addUrl("http://nomis80.org").setType("home"); |
| 427 |
return vcard; |
| 428 |
} |
| 429 |
|
| 430 |
public static void assertExample(String actual, String exampleFileName) throws IOException { |
| 431 |
Filter filter = new Filter() { |
| 432 |
public String filter(String json) { |
| 433 |
|
| 434 |
|
| 435 |
json = json.replaceAll("\"bday\",\\{\\},\"date-and-or-time\"", "\"bday\",{},\"date\""); |
| 436 |
json = json.replaceAll("\"anniversary\",\\{\\},\"date-and-or-time\"", "\"anniversary\",{},\"date-time\""); |
| 437 |
return json; |
| 438 |
} |
| 439 |
}; |
| 440 |
|
| 441 |
String expected = new Gobble(JCardWriterTest.class.getResourceAsStream(exampleFileName)).asString(); |
| 442 |
expected = expected.replaceAll("\\s", ""); |
| 443 |
if (filter != null) { |
| 444 |
expected = filter.filter(expected); |
| 445 |
} |
| 446 |
|
| 447 |
assertEquals(expected, actual); |
| 448 |
} |
| 449 |
|
| 450 |
private interface Filter { |
| 451 |
String filter(String json); |
| 452 |
} |
| 453 |
|
| 454 |
private static class TestProperty extends VCardProperty { |
| 455 |
public JCardValue value; |
| 456 |
|
| 457 |
public TestProperty(JCardValue value) { |
| 458 |
this.value = value; |
| 459 |
} |
| 460 |
|
| 461 |
@Override |
| 462 |
public TestProperty copy() { |
| 463 |
throw new UnsupportedOperationException("Copy method should not be used."); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
private static class TestScribe extends VCardPropertyScribe<TestProperty> { |
| 468 |
public TestScribe() { |
| 469 |
super(TestProperty.class, "X-TYPE"); |
| 470 |
} |
| 471 |
|
| 472 |
@Override |
| 473 |
protected VCardDataType _defaultDataType(VCardVersion version) { |
| 474 |
return VCardDataType.TEXT; |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
protected String _writeText(TestProperty property, WriteContext context) { |
| 479 |
return null; |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
protected TestProperty _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) { |
| 484 |
return null; |
| 485 |
} |
| 486 |
|
| 487 |
@Override |
| 488 |
protected JCardValue _writeJson(TestProperty property) { |
| 489 |
return property.value; |
| 490 |
} |
| 491 |
} |
| 492 |
} |