| 1 |
package ezvcard.io.html; |
| 2 |
|
| 3 |
import static ezvcard.util.StringUtils.NEWLINE; |
| 4 |
import static org.junit.Assert.assertArrayEquals; |
| 5 |
import static org.junit.Assert.assertEquals; |
| 6 |
import static org.junit.Assert.assertNull; |
| 7 |
import static org.junit.Assert.assertTrue; |
| 8 |
|
| 9 |
import java.io.IOException; |
| 10 |
import java.io.InputStream; |
| 11 |
import java.io.Writer; |
| 12 |
import java.nio.file.Files; |
| 13 |
import java.nio.file.Paths; |
| 14 |
import java.time.LocalDate; |
| 15 |
import java.time.LocalDateTime; |
| 16 |
import java.time.ZoneOffset; |
| 17 |
|
| 18 |
import org.jsoup.Jsoup; |
| 19 |
import org.jsoup.nodes.Document; |
| 20 |
import org.jsoup.select.Elements; |
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import ezvcard.Ezvcard; |
| 24 |
import ezvcard.VCard; |
| 25 |
import ezvcard.io.ParseContext; |
| 26 |
import ezvcard.io.scribe.SortStringScribe; |
| 27 |
import ezvcard.parameter.AddressType; |
| 28 |
import ezvcard.parameter.EmailType; |
| 29 |
import ezvcard.parameter.ImageType; |
| 30 |
import ezvcard.parameter.SoundType; |
| 31 |
import ezvcard.parameter.TelephoneType; |
| 32 |
import ezvcard.property.Address; |
| 33 |
import ezvcard.property.Birthday; |
| 34 |
import ezvcard.property.Email; |
| 35 |
import ezvcard.property.Impp; |
| 36 |
import ezvcard.property.Logo; |
| 37 |
import ezvcard.property.Note; |
| 38 |
import ezvcard.property.Organization; |
| 39 |
import ezvcard.property.Photo; |
| 40 |
import ezvcard.property.Revision; |
| 41 |
import ezvcard.property.Role; |
| 42 |
import ezvcard.property.SortString; |
| 43 |
import ezvcard.property.Sound; |
| 44 |
import ezvcard.property.StructuredName; |
| 45 |
import ezvcard.property.Telephone; |
| 46 |
import ezvcard.property.Timezone; |
| 47 |
import ezvcard.property.Title; |
| 48 |
import ezvcard.property.Uid; |
| 49 |
import ezvcard.property.Url; |
| 50 |
import ezvcard.util.TelUri; |
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public class HCardPageTest { |
| 85 |
private byte[] mockData = "data".getBytes(); |
| 86 |
|
| 87 |
@Test |
| 88 |
public void zero_vcards() throws Exception { |
| 89 |
Document document = generate(); |
| 90 |
assertTrue(document.getElementsByClass("vcard").isEmpty()); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void one_vcard() throws Exception { |
| 95 |
Document document = generate(new VCard()); |
| 96 |
assertEquals(1, document.getElementsByClass("vcard").size()); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void multiple_vcards() throws Exception { |
| 101 |
Document document = generate(new VCard(), new VCard()); |
| 102 |
assertEquals(2, document.getElementsByClass("vcard").size()); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void prodId() throws Exception { |
| 107 |
Document document = generate(new VCard()); |
| 108 |
assertEquals(1, document.select(".vcard .prodid").size()); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void logic_for_displaying_a_photo() throws Exception { |
| 113 |
VCard vcard = new VCard(); |
| 114 |
Document document = generate(vcard); |
| 115 |
assertTrue(document.select(".vcard .photo").isEmpty()); |
| 116 |
assertTrue(document.select(".vcard .logo").isEmpty()); |
| 117 |
|
| 118 |
vcard = new VCard(); |
| 119 |
Photo photo = new Photo(mockData, ImageType.JPEG); |
| 120 |
vcard.addPhoto(photo); |
| 121 |
document = generate(vcard); |
| 122 |
assertEquals(1, document.select(".vcard .photo").size()); |
| 123 |
assertTrue(document.select(".vcard .logo").isEmpty()); |
| 124 |
|
| 125 |
vcard = new VCard(); |
| 126 |
Logo logo = new Logo(mockData, ImageType.PNG); |
| 127 |
vcard.addLogo(logo); |
| 128 |
document = generate(vcard); |
| 129 |
assertTrue(document.select(".vcard .photo").isEmpty()); |
| 130 |
assertEquals(1, document.select(".vcard .logo").size()); |
| 131 |
|
| 132 |
vcard = new VCard(); |
| 133 |
photo = new Photo(mockData, ImageType.JPEG); |
| 134 |
vcard.addPhoto(photo); |
| 135 |
logo = new Logo(mockData, ImageType.PNG); |
| 136 |
vcard.addLogo(logo); |
| 137 |
document = generate(vcard); |
| 138 |
assertEquals(1, document.select(".vcard .photo").size()); |
| 139 |
assertTrue(document.select(".vcard .logo").isEmpty()); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void sort_string_logic() throws Exception { |
| 144 |
SortStringScribe scribe = new SortStringScribe(); |
| 145 |
|
| 146 |
|
| 147 |
{ |
| 148 |
VCard vcard = new VCard(); |
| 149 |
StructuredName n = new StructuredName(); |
| 150 |
n.setSortAs("Smith"); |
| 151 |
vcard.setStructuredName(n); |
| 152 |
Organization org = new Organization(); |
| 153 |
org.getSortAs().add("Jones"); |
| 154 |
vcard.setOrganization(org); |
| 155 |
vcard.setSortString(new SortString("Doe")); |
| 156 |
Document document = generate(vcard); |
| 157 |
|
| 158 |
Elements elements = document.select(".vcard .sort-string"); |
| 159 |
SortString ss = scribe.parseHtml(new HCardElement(elements.first()), new ParseContext()); |
| 160 |
assertEquals("Doe", ss.getValue()); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
{ |
| 165 |
VCard vcard = new VCard(); |
| 166 |
StructuredName n = new StructuredName(); |
| 167 |
n.setSortAs("Smith"); |
| 168 |
vcard.setStructuredName(n); |
| 169 |
Organization org = new Organization(); |
| 170 |
org.getSortAs().add("Jones"); |
| 171 |
vcard.setOrganization(org); |
| 172 |
Document document = generate(vcard); |
| 173 |
|
| 174 |
Elements elements = document.select(".vcard .sort-string"); |
| 175 |
SortString ss = scribe.parseHtml(new HCardElement(elements.first()), new ParseContext()); |
| 176 |
assertEquals("Smith", ss.getValue()); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
{ |
| 181 |
VCard vcard = new VCard(); |
| 182 |
Organization org = new Organization(); |
| 183 |
org.getSortAs().add("Jones"); |
| 184 |
vcard.setOrganization(org); |
| 185 |
Document document = generate(vcard); |
| 186 |
|
| 187 |
Elements elements = document.select(".vcard .sort-string"); |
| 188 |
SortString ss = scribe.parseHtml(new HCardElement(elements.first()), new ParseContext()); |
| 189 |
assertEquals("Jones", ss.getValue()); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
@Test |
| 194 |
public void org() throws Exception { |
| 195 |
|
| 196 |
{ |
| 197 |
VCard vcard = new VCard(); |
| 198 |
Organization org = new Organization(); |
| 199 |
org.getValues().add("Google"); |
| 200 |
vcard.setOrganization(org); |
| 201 |
Document document = generate(vcard); |
| 202 |
|
| 203 |
assertEquals(1, document.select(".vcard .org .organization-name").size()); |
| 204 |
assertEquals(0, document.select(".vcard .org .organization-unit").size()); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
{ |
| 209 |
VCard vcard = new VCard(); |
| 210 |
Organization org = new Organization(); |
| 211 |
org.getValues().add("Google"); |
| 212 |
org.getValues().add("GMail Team"); |
| 213 |
vcard.setOrganization(org); |
| 214 |
Document document = generate(vcard); |
| 215 |
|
| 216 |
assertEquals(1, document.select(".vcard .org .organization-name").size()); |
| 217 |
assertEquals(1, document.select(".vcard .org .organization-unit").size()); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
@Test |
| 222 |
public void note() throws Exception { |
| 223 |
VCard vcard = new VCard(); |
| 224 |
vcard.addNote("one\rtwo\nthree\r\nfour"); |
| 225 |
|
| 226 |
HCardPage template = new HCardPage(); |
| 227 |
template.add(vcard); |
| 228 |
String html = template.write(); |
| 229 |
|
| 230 |
try (HCardParser reader = new HCardParser(html)) { |
| 231 |
vcard = reader.readNext(); |
| 232 |
} |
| 233 |
assertEquals("one" + NEWLINE + "two" + NEWLINE + "three" + NEWLINE + "four", vcard.getNotes().get(0).getValue()); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void create_then_parse() throws Exception { |
| 238 |
|
| 239 |
VCard expected = createFullVCard(); |
| 240 |
HCardPage template = new HCardPage(); |
| 241 |
template.add(expected); |
| 242 |
String html = template.write(); |
| 243 |
|
| 244 |
|
| 245 |
try (Writer writer = Files.newBufferedWriter(Paths.get("target", "vcard.html"))) { |
| 246 |
writer.write(html); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
VCard actual; |
| 251 |
try (HCardParser reader = new HCardParser(html)) { |
| 252 |
actual = reader.readNext(); |
| 253 |
} |
| 254 |
|
| 255 |
assertEquals("Claus", actual.getSortString().getValue()); |
| 256 |
|
| 257 |
assertEquals(expected.getClassification().getValue(), actual.getClassification().getValue()); |
| 258 |
|
| 259 |
assertEquals(expected.getMailer().getValue(), actual.getMailer().getValue()); |
| 260 |
|
| 261 |
assertEquals(expected.getFormattedName().getValue(), actual.getFormattedName().getValue()); |
| 262 |
|
| 263 |
assertEquals(expected.getUid().getValue(), actual.getUid().getValue()); |
| 264 |
|
| 265 |
assertEquals(expected.getNickname().getValues(), actual.getNickname().getValues()); |
| 266 |
|
| 267 |
assertEquals(expected.getOrganization().getValues(), actual.getOrganization().getValues()); |
| 268 |
|
| 269 |
assertEquals(expected.getCategories().getValues(), actual.getCategories().getValues()); |
| 270 |
|
| 271 |
assertEquals(expected.getBirthday().getDate(), actual.getBirthday().getDate()); |
| 272 |
|
| 273 |
assertEquals(expected.getRevision().getValue(), actual.getRevision().getValue()); |
| 274 |
|
| 275 |
assertEquals(expected.getGeo().getLatitude(), actual.getGeo().getLatitude()); |
| 276 |
assertEquals(expected.getGeo().getLongitude(), actual.getGeo().getLongitude()); |
| 277 |
|
| 278 |
assertEquals(expected.getTimezone().getOffset(), actual.getTimezone().getOffset()); |
| 279 |
assertNull(actual.getTimezone().getText()); |
| 280 |
|
| 281 |
{ |
| 282 |
StructuredName e = expected.getStructuredName(); |
| 283 |
StructuredName a = actual.getStructuredName(); |
| 284 |
assertEquals(e.getFamily(), a.getFamily()); |
| 285 |
assertEquals(e.getGiven(), a.getGiven()); |
| 286 |
assertEquals(e.getAdditionalNames(), a.getAdditionalNames()); |
| 287 |
assertEquals(e.getPrefixes(), a.getPrefixes()); |
| 288 |
assertEquals(e.getSuffixes(), a.getSuffixes()); |
| 289 |
assertTrue(a.getSortAs().isEmpty()); |
| 290 |
} |
| 291 |
|
| 292 |
assertEquals(expected.getTitles().size(), actual.getTitles().size()); |
| 293 |
for (int i = 0; i < expected.getTitles().size(); i++) { |
| 294 |
Title e = expected.getTitles().get(i); |
| 295 |
Title a = actual.getTitles().get(i); |
| 296 |
assertEquals(e.getValue(), a.getValue()); |
| 297 |
} |
| 298 |
|
| 299 |
assertEquals(expected.getRoles().size(), actual.getRoles().size()); |
| 300 |
for (int i = 0; i < expected.getRoles().size(); i++) { |
| 301 |
Role e = expected.getRoles().get(i); |
| 302 |
Role a = actual.getRoles().get(i); |
| 303 |
assertEquals(e.getValue(), a.getValue()); |
| 304 |
} |
| 305 |
|
| 306 |
assertEquals(expected.getNotes().size(), actual.getNotes().size()); |
| 307 |
for (int i = 0; i < expected.getNotes().size(); i++) { |
| 308 |
Note e = expected.getNotes().get(i); |
| 309 |
Note a = actual.getNotes().get(i); |
| 310 |
assertEquals(e.getValue(), a.getValue()); |
| 311 |
} |
| 312 |
|
| 313 |
assertEquals(expected.getUrls().size(), actual.getUrls().size()); |
| 314 |
for (int i = 0; i < expected.getUrls().size(); i++) { |
| 315 |
Url e = expected.getUrls().get(i); |
| 316 |
Url a = actual.getUrls().get(i); |
| 317 |
assertEquals(e.getValue(), a.getValue()); |
| 318 |
} |
| 319 |
|
| 320 |
assertEquals(expected.getImpps().size(), actual.getImpps().size()); |
| 321 |
for (int i = 0; i < expected.getImpps().size(); i++) { |
| 322 |
Impp e = expected.getImpps().get(i); |
| 323 |
Impp a = actual.getImpps().get(i); |
| 324 |
assertEquals(e.getUri(), a.getUri()); |
| 325 |
} |
| 326 |
|
| 327 |
assertEquals(expected.getEmails().size(), actual.getEmails().size()); |
| 328 |
for (int i = 0; i < expected.getEmails().size(); i++) { |
| 329 |
Email e = expected.getEmails().get(i); |
| 330 |
Email a = actual.getEmails().get(i); |
| 331 |
assertEquals(e.getValue(), a.getValue()); |
| 332 |
assertEquals(e.getTypes(), a.getTypes()); |
| 333 |
} |
| 334 |
|
| 335 |
assertEquals(expected.getTelephoneNumbers().size(), actual.getTelephoneNumbers().size()); |
| 336 |
for (int i = 0; i < expected.getTelephoneNumbers().size(); i++) { |
| 337 |
Telephone e = expected.getTelephoneNumbers().get(i); |
| 338 |
Telephone a = actual.getTelephoneNumbers().get(i); |
| 339 |
if (e.getText() != null) { |
| 340 |
assertEquals(e.getText(), a.getText()); |
| 341 |
} else { |
| 342 |
TelUri uri = e.getUri(); |
| 343 |
if (uri.getExtension() == null) { |
| 344 |
assertEquals(e.getUri().getNumber(), a.getText()); |
| 345 |
} else { |
| 346 |
assertEquals(e.getUri().getNumber() + " x" + uri.getExtension(), a.getText()); |
| 347 |
} |
| 348 |
} |
| 349 |
assertEquals(e.getTypes(), a.getTypes()); |
| 350 |
} |
| 351 |
|
| 352 |
assertEquals(expected.getAddresses().size(), actual.getAddresses().size()); |
| 353 |
for (int i = 0; i < expected.getAddresses().size(); i++) { |
| 354 |
Address e = expected.getAddresses().get(i); |
| 355 |
Address a = actual.getAddresses().get(i); |
| 356 |
assertEquals(e.getPoBox(), a.getPoBox()); |
| 357 |
assertEquals(e.getExtendedAddress(), a.getExtendedAddress()); |
| 358 |
assertEquals(e.getStreetAddress(), a.getStreetAddress()); |
| 359 |
assertEquals(e.getLocality(), a.getLocality()); |
| 360 |
assertEquals(e.getRegion(), a.getRegion()); |
| 361 |
assertEquals(e.getPostalCode(), a.getPostalCode()); |
| 362 |
assertEquals(e.getCountry(), a.getCountry()); |
| 363 |
assertEquals(e.getLabel(), a.getLabel()); |
| 364 |
assertEquals(e.getTypes(), a.getTypes()); |
| 365 |
} |
| 366 |
|
| 367 |
assertEquals(expected.getPhotos().size(), actual.getPhotos().size()); |
| 368 |
for (int i = 0; i < expected.getPhotos().size(); i++) { |
| 369 |
Photo e = expected.getPhotos().get(i); |
| 370 |
Photo a = actual.getPhotos().get(i); |
| 371 |
assertEquals(e.getContentType(), a.getContentType()); |
| 372 |
assertArrayEquals(e.getData(), a.getData()); |
| 373 |
} |
| 374 |
|
| 375 |
assertEquals(expected.getSounds().size(), actual.getSounds().size()); |
| 376 |
for (int i = 0; i < expected.getSounds().size(); i++) { |
| 377 |
Sound e = expected.getSounds().get(i); |
| 378 |
Sound a = actual.getSounds().get(i); |
| 379 |
assertEquals(e.getContentType(), a.getContentType()); |
| 380 |
assertArrayEquals(e.getData(), a.getData()); |
| 381 |
} |
| 382 |
|
| 383 |
assertEquals("ez-vcard " + Ezvcard.VERSION, actual.getProductId().getValue()); |
| 384 |
} |
| 385 |
|
| 386 |
private VCard createFullVCard() throws IOException { |
| 387 |
VCard vcard = new VCard(); |
| 388 |
|
| 389 |
StructuredName n = new StructuredName(); |
| 390 |
n.setFamily("Claus"); |
| 391 |
n.setGiven("Santa"); |
| 392 |
n.getAdditionalNames().add("Saint Nicholas"); |
| 393 |
n.getAdditionalNames().add("Father Christmas"); |
| 394 |
n.getPrefixes().add("Mr"); |
| 395 |
n.getPrefixes().add("Dr"); |
| 396 |
n.getSuffixes().add("M.D."); |
| 397 |
n.setSortAs("Claus"); |
| 398 |
vcard.setStructuredName(n); |
| 399 |
|
| 400 |
vcard.setClassification("public"); |
| 401 |
|
| 402 |
vcard.setMailer("Thunderbird"); |
| 403 |
|
| 404 |
vcard.setFormattedName("Santa Claus"); |
| 405 |
|
| 406 |
vcard.setNickname("Kris Kringle"); |
| 407 |
|
| 408 |
vcard.addTitle("Manager"); |
| 409 |
|
| 410 |
vcard.addRole("Executive"); |
| 411 |
vcard.addRole("Team Builder"); |
| 412 |
|
| 413 |
vcard.addEmail("[email protected]", EmailType.HOME, EmailType.WORK); |
| 414 |
|
| 415 |
vcard.addEmail("[email protected]", EmailType.WORK); |
| 416 |
|
| 417 |
Telephone tel = new Telephone(new TelUri.Builder("+1-555-222-3333").extension("101").build()); |
| 418 |
vcard.addTelephoneNumber(tel); |
| 419 |
|
| 420 |
tel = new Telephone(new TelUri.Builder("+1-555-333-4444").build()); |
| 421 |
tel.getTypes().add(TelephoneType.WORK); |
| 422 |
vcard.addTelephoneNumber(tel); |
| 423 |
|
| 424 |
vcard.addTelephoneNumber("(555) 111-2222", TelephoneType.HOME, TelephoneType.VOICE, TelephoneType.PREF); |
| 425 |
|
| 426 |
Address adr = new Address(); |
| 427 |
adr.setStreetAddress("123 Main St"); |
| 428 |
adr.setExtendedAddress("Apt 11"); |
| 429 |
adr.setLocality("Austin"); |
| 430 |
adr.setRegion("Tx"); |
| 431 |
adr.setPostalCode("12345"); |
| 432 |
adr.setCountry("USA"); |
| 433 |
adr.setLabel("123 Main St." + NEWLINE + "Austin TX, 12345" + NEWLINE + "USA"); |
| 434 |
adr.getTypes().add(AddressType.HOME); |
| 435 |
vcard.addAddress(adr); |
| 436 |
|
| 437 |
adr = new Address(); |
| 438 |
adr.setPoBox("123"); |
| 439 |
adr.setStreetAddress("456 Wall St."); |
| 440 |
adr.setLocality("New York"); |
| 441 |
adr.setRegion("NY"); |
| 442 |
adr.setPostalCode("11111"); |
| 443 |
adr.setCountry("USA"); |
| 444 |
adr.getTypes().add(AddressType.PREF); |
| 445 |
adr.getTypes().add(AddressType.WORK); |
| 446 |
vcard.addAddress(adr); |
| 447 |
|
| 448 |
vcard.setOrganization("Google", "GMail"); |
| 449 |
|
| 450 |
Birthday bday = new Birthday(LocalDate.of(1970, 3, 8)); |
| 451 |
vcard.setBirthday(bday); |
| 452 |
|
| 453 |
vcard.addUrl("http://company.com"); |
| 454 |
|
| 455 |
vcard.setCategories("business owner", "jolly"); |
| 456 |
|
| 457 |
vcard.addImpp(Impp.aim("myhandle")); |
| 458 |
vcard.addImpp(Impp.yahoo("[email protected]")); |
| 459 |
|
| 460 |
vcard.addNote("I am proficient in Tiger-Crane Style," + NEWLINE + "and I am more than proficient in the exquisite art of the Samurai sword."); |
| 461 |
|
| 462 |
vcard.setGeo(123.456, -98.123); |
| 463 |
|
| 464 |
vcard.setTimezone(new Timezone(ZoneOffset.ofHours(-6), "America/Chicago")); |
| 465 |
|
| 466 |
InputStream in = getClass().getResourceAsStream("hcard-portrait.jpg"); |
| 467 |
Photo photo = new Photo(in, ImageType.JPEG); |
| 468 |
vcard.addPhoto(photo); |
| 469 |
|
| 470 |
in = getClass().getResourceAsStream("hcard-sound.ogg"); |
| 471 |
Sound sound = new Sound(in, SoundType.OGG); |
| 472 |
vcard.addSound(sound); |
| 473 |
|
| 474 |
vcard.setUid(new Uid("urn:uuid:ffce1595-cbe9-4418-9d0d-b015655d45f6")); |
| 475 |
|
| 476 |
vcard.setRevision(new Revision(LocalDateTime.of(2000, 3, 15, 13, 22, 44).toInstant(ZoneOffset.UTC))); |
| 477 |
|
| 478 |
return vcard; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
private Document generate(VCard... vcards) { |
| 487 |
HCardPage template = new HCardPage(); |
| 488 |
for (VCard vcard : vcards) { |
| 489 |
template.add(vcard); |
| 490 |
} |
| 491 |
return Jsoup.parse(template.write()); |
| 492 |
} |
| 493 |
} |