| 1 |
package ezvcard; |
| 2 |
|
| 3 |
import static ezvcard.util.StringUtils.NEWLINE; |
| 4 |
import static ezvcard.util.TestUtils.assertParseWarnings; |
| 5 |
import static ezvcard.util.TestUtils.assertVersion; |
| 6 |
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; |
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
import static org.junit.Assert.assertFalse; |
| 9 |
import static org.junit.Assert.assertTrue; |
| 10 |
import static org.junit.Assert.fail; |
| 11 |
|
| 12 |
import java.io.Writer; |
| 13 |
import java.nio.file.Files; |
| 14 |
import java.nio.file.Path; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.Iterator; |
| 17 |
import java.util.List; |
| 18 |
|
| 19 |
import javax.xml.xpath.XPath; |
| 20 |
import javax.xml.xpath.XPathConstants; |
| 21 |
import javax.xml.xpath.XPathFactory; |
| 22 |
|
| 23 |
import org.jsoup.Jsoup; |
| 24 |
import org.junit.Rule; |
| 25 |
import org.junit.Test; |
| 26 |
import org.junit.rules.TemporaryFolder; |
| 27 |
import org.w3c.dom.Document; |
| 28 |
import org.w3c.dom.NodeList; |
| 29 |
|
| 30 |
import ezvcard.io.LuckyNumProperty; |
| 31 |
import ezvcard.io.LuckyNumProperty.LuckyNumScribe; |
| 32 |
import ezvcard.io.ParseWarning; |
| 33 |
import ezvcard.io.text.TargetApplication; |
| 34 |
import ezvcard.io.xml.XCardNamespaceContext; |
| 35 |
import ezvcard.parameter.ImageType; |
| 36 |
import ezvcard.property.FormattedName; |
| 37 |
import ezvcard.property.Photo; |
| 38 |
import ezvcard.util.XCardBuilder; |
| 39 |
import ezvcard.util.XmlUtils; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
public class EzvcardTest { |
| 74 |
private final XPath xpath = XPathFactory.newInstance().newXPath(); |
| 75 |
{ |
| 76 |
xpath.setNamespaceContext(new XCardNamespaceContext(VCardVersion.V4_0, "v")); |
| 77 |
} |
| 78 |
|
| 79 |
@Rule |
| 80 |
public TemporaryFolder folder = new TemporaryFolder(); |
| 81 |
|
| 82 |
@Test |
| 83 |
public void parse_first() throws Exception { |
| 84 |
|
| 85 |
String str = |
| 86 |
"BEGIN:VCARD\r\n" + |
| 87 |
"VERSION:2.1\r\n" + |
| 88 |
"FN:John Doe\r\n" + |
| 89 |
"END:VCARD\r\n"; |
| 90 |
|
| 91 |
|
| 92 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 93 |
VCard vcard = Ezvcard.parse(str).warnings(warnings).first(); |
| 94 |
assertVersion(VCardVersion.V2_1, vcard); |
| 95 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 96 |
|
| 97 |
assertEquals(1, warnings.size()); |
| 98 |
assertParseWarnings(warnings.get(0)); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void parse_all() throws Exception { |
| 103 |
|
| 104 |
String str = |
| 105 |
"BEGIN:VCARD\r\n" + |
| 106 |
"VERSION:2.1\r\n" + |
| 107 |
"FN:John Doe\r\n" + |
| 108 |
"END:VCARD\r\n" + |
| 109 |
"BEGIN:VCARD\r\n" + |
| 110 |
"VERSION:3.0\r\n" + |
| 111 |
"FN:Jane Doe\r\n" + |
| 112 |
"END:VCARD\r\n"; |
| 113 |
|
| 114 |
|
| 115 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 116 |
List<VCard> vcards = Ezvcard.parse(str).warnings(warnings).all(); |
| 117 |
Iterator<VCard> it = vcards.iterator(); |
| 118 |
|
| 119 |
VCard vcard = it.next(); |
| 120 |
assertVersion(VCardVersion.V2_1, vcard); |
| 121 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 122 |
|
| 123 |
vcard = it.next(); |
| 124 |
assertVersion(VCardVersion.V3_0, vcard); |
| 125 |
assertEquals("Jane Doe", vcard.getFormattedName().getValue()); |
| 126 |
|
| 127 |
assertEquals(2, warnings.size()); |
| 128 |
assertParseWarnings(warnings.get(0)); |
| 129 |
assertParseWarnings(warnings.get(1)); |
| 130 |
|
| 131 |
assertFalse(it.hasNext()); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void parse_register() throws Exception { |
| 136 |
|
| 137 |
String str = |
| 138 |
"BEGIN:VCARD\r\n" + |
| 139 |
"VERSION:2.1\r\n" + |
| 140 |
"X-LUCKY-NUM:22\r\n" + |
| 141 |
"END:VCARD\r\n"; |
| 142 |
|
| 143 |
|
| 144 |
VCard vcard = Ezvcard.parse(str).register(new LuckyNumScribe()).first(); |
| 145 |
assertVersion(VCardVersion.V2_1, vcard); |
| 146 |
List<LuckyNumProperty> ext = vcard.getProperties(LuckyNumProperty.class); |
| 147 |
assertEquals(1, ext.size()); |
| 148 |
assertEquals(22, ext.get(0).luckyNum); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void parse_caretDecoding() throws Exception { |
| 153 |
|
| 154 |
String str = |
| 155 |
"BEGIN:VCARD\r\n" + |
| 156 |
"VERSION:4.0\r\n" + |
| 157 |
"FN;X-TEST=George Herman ^'Babe^' Ruth:John Doe\r\n" + |
| 158 |
"END:VCARD\r\n"; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
VCard vcard = Ezvcard.parse(str).first(); |
| 163 |
assertEquals("George Herman \"Babe\" Ruth", vcard.getFormattedName().getParameter("X-TEST")); |
| 164 |
|
| 165 |
vcard = Ezvcard.parse(str).caretDecoding(true).first(); |
| 166 |
assertEquals("George Herman \"Babe\" Ruth", vcard.getFormattedName().getParameter("X-TEST")); |
| 167 |
|
| 168 |
vcard = Ezvcard.parse(str).caretDecoding(false).first(); |
| 169 |
assertEquals("George Herman ^'Babe^' Ruth", vcard.getFormattedName().getParameter("X-TEST")); |
| 170 |
} |
| 171 |
|
| 172 |
@Test |
| 173 |
public void parseXml_first() throws Exception { |
| 174 |
XCardBuilder xb = new XCardBuilder(); |
| 175 |
xb.prop("fn", "<text>John Doe</text>"); |
| 176 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 177 |
|
| 178 |
VCard vcard = Ezvcard.parseXml(xb.toString()).warnings(warnings).first(); |
| 179 |
assertVersion(VCardVersion.V4_0, vcard); |
| 180 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 181 |
|
| 182 |
assertEquals(1, warnings.size()); |
| 183 |
assertParseWarnings(warnings.get(0)); |
| 184 |
} |
| 185 |
|
| 186 |
@Test |
| 187 |
public void parseXml_all() throws Exception { |
| 188 |
XCardBuilder xb = new XCardBuilder(); |
| 189 |
xb.prop("fn", "<text>John Doe</text>"); |
| 190 |
xb.begin(); |
| 191 |
xb.prop("fn", "<text>Jane Doe</text>"); |
| 192 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 193 |
|
| 194 |
List<VCard> vcards = Ezvcard.parseXml(xb.toString()).warnings(warnings).all(); |
| 195 |
Iterator<VCard> it = vcards.iterator(); |
| 196 |
|
| 197 |
VCard vcard = it.next(); |
| 198 |
assertVersion(VCardVersion.V4_0, vcard); |
| 199 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 200 |
|
| 201 |
vcard = it.next(); |
| 202 |
assertVersion(VCardVersion.V4_0, vcard); |
| 203 |
assertEquals("Jane Doe", vcard.getFormattedName().getValue()); |
| 204 |
|
| 205 |
assertEquals(2, warnings.size()); |
| 206 |
assertParseWarnings(warnings.get(0)); |
| 207 |
assertParseWarnings(warnings.get(1)); |
| 208 |
|
| 209 |
assertFalse(it.hasNext()); |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void parseXml_register() throws Exception { |
| 214 |
XCardBuilder xb = new XCardBuilder(); |
| 215 |
xb.prop("http://luckynum.com", "lucky-num", "<num>22</num>"); |
| 216 |
|
| 217 |
VCard vcard = Ezvcard.parseXml(xb.toString()).register(new LuckyNumScribe()).first(); |
| 218 |
assertVersion(VCardVersion.V4_0, vcard); |
| 219 |
List<LuckyNumProperty> ext = vcard.getProperties(LuckyNumProperty.class); |
| 220 |
assertEquals(1, ext.size()); |
| 221 |
assertEquals(22, ext.get(0).luckyNum); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void parseHtml_first() throws Exception { |
| 226 |
|
| 227 |
String html = |
| 228 |
"<div class=\"vcard\">" + |
| 229 |
"<div class=\"fn\">John Doe</div>" + |
| 230 |
"</div>"; |
| 231 |
|
| 232 |
|
| 233 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 234 |
|
| 235 |
VCard vcard = Ezvcard.parseHtml(html).warnings(warnings).first(); |
| 236 |
assertVersion(VCardVersion.V3_0, vcard); |
| 237 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 238 |
|
| 239 |
assertEquals(1, warnings.size()); |
| 240 |
assertParseWarnings(warnings.get(0)); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void parseHtml_all() throws Exception { |
| 245 |
|
| 246 |
String html = |
| 247 |
"<html>" + |
| 248 |
"<div class=\"vcard\">" + |
| 249 |
"<div class=\"fn\">John Doe</div>" + |
| 250 |
"</div>" + |
| 251 |
"<div class=\"vcard\">" + |
| 252 |
"<div class=\"fn\">Jane Doe</div>" + |
| 253 |
"</div>" + |
| 254 |
"</html>"; |
| 255 |
|
| 256 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 257 |
|
| 258 |
List<VCard> vcards = Ezvcard.parseHtml(html).warnings(warnings).all(); |
| 259 |
Iterator<VCard> it = vcards.iterator(); |
| 260 |
|
| 261 |
VCard vcard = it.next(); |
| 262 |
assertVersion(VCardVersion.V3_0, vcard); |
| 263 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 264 |
|
| 265 |
vcard = it.next(); |
| 266 |
assertVersion(VCardVersion.V3_0, vcard); |
| 267 |
assertEquals("Jane Doe", vcard.getFormattedName().getValue()); |
| 268 |
|
| 269 |
assertEquals(2, warnings.size()); |
| 270 |
assertParseWarnings(warnings.get(0)); |
| 271 |
assertParseWarnings(warnings.get(1)); |
| 272 |
|
| 273 |
assertFalse(it.hasNext()); |
| 274 |
} |
| 275 |
|
| 276 |
@Test |
| 277 |
public void parseHtml_register() throws Exception { |
| 278 |
|
| 279 |
String html = |
| 280 |
"<div class=\"vcard\">" + |
| 281 |
"<div class=\"x-lucky-num\">22</div>" + |
| 282 |
"</div>"; |
| 283 |
|
| 284 |
|
| 285 |
VCard vcard = Ezvcard.parseHtml(html).register(new LuckyNumScribe()).first(); |
| 286 |
assertVersion(VCardVersion.V3_0, vcard); |
| 287 |
List<LuckyNumProperty> ext = vcard.getProperties(LuckyNumProperty.class); |
| 288 |
assertEquals(1, ext.size()); |
| 289 |
assertEquals(22, ext.get(0).luckyNum); |
| 290 |
} |
| 291 |
|
| 292 |
@Test |
| 293 |
public void parseHtml_pageUrl() throws Exception { |
| 294 |
|
| 295 |
String html = |
| 296 |
"<div class=\"vcard\">" + |
| 297 |
"<a href=\"profile.html\" class=\"url fn\">John Doe</a>" + |
| 298 |
"</div>"; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
VCard vcard = Ezvcard.parseHtml(html).first(); |
| 303 |
assertVersion(VCardVersion.V3_0, vcard); |
| 304 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 305 |
assertTrue(vcard.getSources().isEmpty()); |
| 306 |
assertEquals("profile.html", vcard.getUrls().get(0).getValue()); |
| 307 |
|
| 308 |
|
| 309 |
vcard = Ezvcard.parseHtml(html).pageUrl("http://www.example.com/index.html").first(); |
| 310 |
assertVersion(VCardVersion.V3_0, vcard); |
| 311 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 312 |
assertEquals("http://www.example.com/index.html", vcard.getSources().get(0).getValue()); |
| 313 |
assertEquals("http://www.example.com/profile.html", vcard.getUrls().get(0).getValue()); |
| 314 |
} |
| 315 |
|
| 316 |
@Test |
| 317 |
public void parseJson_first() { |
| 318 |
|
| 319 |
String json = |
| 320 |
"[\"vcard\"," + |
| 321 |
"[" + |
| 322 |
"[\"version\", {}, \"text\", \"4.0\"]," + |
| 323 |
"[\"fn\", {}, \"text\", \"John Doe\"]" + |
| 324 |
"]" + |
| 325 |
"]"; |
| 326 |
|
| 327 |
|
| 328 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 329 |
|
| 330 |
VCard vcard = Ezvcard.parseJson(json).warnings(warnings).first(); |
| 331 |
assertVersion(VCardVersion.V4_0, vcard); |
| 332 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 333 |
|
| 334 |
assertEquals(1, warnings.size()); |
| 335 |
assertParseWarnings(warnings.get(0)); |
| 336 |
} |
| 337 |
|
| 338 |
@Test |
| 339 |
public void parseJson_all() { |
| 340 |
|
| 341 |
String json = |
| 342 |
"[\"vcardstream\"," + |
| 343 |
"[\"vcard\"," + |
| 344 |
"[" + |
| 345 |
"[\"version\", {}, \"text\", \"4.0\"]," + |
| 346 |
"[\"fn\", {}, \"text\", \"John Doe\"]" + |
| 347 |
"]" + |
| 348 |
"]," + |
| 349 |
"[\"vcard\"," + |
| 350 |
"[" + |
| 351 |
"[\"version\", {}, \"text\", \"4.0\"]," + |
| 352 |
"[\"fn\", {}, \"text\", \"Jane Doe\"]" + |
| 353 |
"]" + |
| 354 |
"]" + |
| 355 |
"]"; |
| 356 |
|
| 357 |
|
| 358 |
List<List<ParseWarning>> warnings = new ArrayList<>(); |
| 359 |
|
| 360 |
List<VCard> vcards = Ezvcard.parseJson(json).warnings(warnings).all(); |
| 361 |
Iterator<VCard> it = vcards.iterator(); |
| 362 |
|
| 363 |
VCard vcard = it.next(); |
| 364 |
assertVersion(VCardVersion.V4_0, vcard); |
| 365 |
assertEquals("John Doe", vcard.getFormattedName().getValue()); |
| 366 |
|
| 367 |
vcard = it.next(); |
| 368 |
assertVersion(VCardVersion.V4_0, vcard); |
| 369 |
assertEquals("Jane Doe", vcard.getFormattedName().getValue()); |
| 370 |
|
| 371 |
assertEquals(2, warnings.size()); |
| 372 |
assertParseWarnings(warnings.get(0)); |
| 373 |
assertParseWarnings(warnings.get(1)); |
| 374 |
|
| 375 |
assertFalse(it.hasNext()); |
| 376 |
} |
| 377 |
|
| 378 |
@Test |
| 379 |
public void parseJson_register() { |
| 380 |
|
| 381 |
String json = |
| 382 |
"[\"vcard\"," + |
| 383 |
"[" + |
| 384 |
"[\"version\", {}, \"text\", \"4.0\"]," + |
| 385 |
"[\"x-lucky-num\", {}, \"text\", \"22\"]" + |
| 386 |
"]" + |
| 387 |
"]"; |
| 388 |
|
| 389 |
|
| 390 |
VCard vcard = Ezvcard.parseJson(json).register(new LuckyNumScribe()).first(); |
| 391 |
assertVersion(VCardVersion.V4_0, vcard); |
| 392 |
List<LuckyNumProperty> ext = vcard.getProperties(LuckyNumProperty.class); |
| 393 |
assertEquals(1, ext.size()); |
| 394 |
assertEquals(22, ext.get(0).luckyNum); |
| 395 |
} |
| 396 |
|
| 397 |
@Test |
| 398 |
public void write_one() throws Exception { |
| 399 |
VCard vcard = new VCard(); |
| 400 |
vcard.setVersion(VCardVersion.V2_1); |
| 401 |
vcard.setFormattedName(new FormattedName("John Doe")); |
| 402 |
|
| 403 |
String actual = Ezvcard.write(vcard).go(); |
| 404 |
assertTrue(actual.contains("VERSION:2.1")); |
| 405 |
assertTrue(actual.contains("FN:John Doe")); |
| 406 |
} |
| 407 |
|
| 408 |
@Test |
| 409 |
public void write_multiple() throws Exception { |
| 410 |
VCard vcard1 = new VCard(); |
| 411 |
vcard1.setVersion(VCardVersion.V2_1); |
| 412 |
vcard1.setFormattedName(new FormattedName("John Doe")); |
| 413 |
VCard vcard2 = new VCard(); |
| 414 |
vcard2.setVersion(VCardVersion.V3_0); |
| 415 |
vcard2.setFormattedName(new FormattedName("Jane Doe")); |
| 416 |
VCard vcard3 = new VCard(); |
| 417 |
vcard3.setVersion(VCardVersion.V4_0); |
| 418 |
vcard3.setFormattedName(new FormattedName("Janet Doe")); |
| 419 |
|
| 420 |
String actual = Ezvcard.write(vcard1, vcard2, vcard3).go(); |
| 421 |
assertTrue(actual.matches("(?s)BEGIN:VCARD.*?VERSION:2\\.1.*?FN:John Doe.*?END:VCARD.*?BEGIN:VCARD.*?VERSION:3\\.0.*?FN:Jane Doe.*?END:VCARD.*?BEGIN:VCARD.*?VERSION:4\\.0.*?FN:Janet Doe.*?END:VCARD.*")); |
| 422 |
} |
| 423 |
|
| 424 |
@Test |
| 425 |
public void write_version() throws Exception { |
| 426 |
VCard vcard1 = new VCard(); |
| 427 |
vcard1.setVersion(VCardVersion.V2_1); |
| 428 |
VCard vcard2 = new VCard(); |
| 429 |
vcard2.setVersion(VCardVersion.V3_0); |
| 430 |
VCard vcard3 = new VCard(); |
| 431 |
vcard3.setVersion(VCardVersion.V4_0); |
| 432 |
|
| 433 |
String actual = Ezvcard.write(vcard1, vcard2, vcard3).version(VCardVersion.V4_0).go(); |
| 434 |
assertTrue(actual.matches("(?s)(.*?VERSION:4\\.0){3}.*")); |
| 435 |
} |
| 436 |
|
| 437 |
@Test |
| 438 |
public void write_prodId() throws Exception { |
| 439 |
VCard vcard = new VCard(); |
| 440 |
vcard.setVersion(VCardVersion.V3_0); |
| 441 |
|
| 442 |
String actual = Ezvcard.write(vcard).go(); |
| 443 |
assertTrue(actual.contains("\r\nPRODID:")); |
| 444 |
|
| 445 |
actual = Ezvcard.write(vcard).prodId(true).go(); |
| 446 |
assertTrue(actual.contains("\r\nPRODID:")); |
| 447 |
|
| 448 |
actual = Ezvcard.write(vcard).prodId(false).go(); |
| 449 |
assertFalse(actual.contains("\r\nPRODID:")); |
| 450 |
} |
| 451 |
|
| 452 |
@Test |
| 453 |
public void write_caretEncoding() throws Exception { |
| 454 |
VCard vcard = new VCard(); |
| 455 |
vcard.setVersion(VCardVersion.V4_0); |
| 456 |
FormattedName fn = vcard.setFormattedName("test"); |
| 457 |
fn.getParameters().put("X-TEST", "George Herman \"Babe\" Ruth"); |
| 458 |
|
| 459 |
|
| 460 |
try { |
| 461 |
Ezvcard.write(vcard).go(); |
| 462 |
fail("IllegalArgumentException expected."); |
| 463 |
} catch (IllegalArgumentException e) { |
| 464 |
|
| 465 |
} |
| 466 |
|
| 467 |
String actual = Ezvcard.write(vcard).caretEncoding(true).go(); |
| 468 |
assertTrue(actual.contains("\r\nFN;X-TEST=George Herman ^'Babe^' Ruth:")); |
| 469 |
|
| 470 |
try { |
| 471 |
Ezvcard.write(vcard).caretEncoding(false).go(); |
| 472 |
fail("IllegalArgumentException expected."); |
| 473 |
} catch (IllegalArgumentException e) { |
| 474 |
|
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
@Test |
| 479 |
public void write_foldLines() throws Exception { |
| 480 |
VCard vcard = new VCard(); |
| 481 |
vcard.addNote("In the beginning God created the heavens and the earth. Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters."); |
| 482 |
|
| 483 |
|
| 484 |
String actual = Ezvcard.write(vcard).go(); |
| 485 |
assertTrue(actual.contains("\r\nNOTE:In the beginning God created the heavens and the earth. Now the earth \r\n was formless and empty\\, darkness was over the surface of the deep\\, and t\r\n he Spirit of God was hovering over the waters.")); |
| 486 |
|
| 487 |
actual = Ezvcard.write(vcard).foldLines(false).go(); |
| 488 |
assertTrue(actual.contains("\r\nNOTE:In the beginning God created the heavens and the earth. Now the earth was formless and empty\\, darkness was over the surface of the deep\\, and the Spirit of God was hovering over the waters.")); |
| 489 |
|
| 490 |
actual = Ezvcard.write(vcard).foldLines(true).go(); |
| 491 |
assertTrue(actual.contains("\r\nNOTE:In the beginning God created the heavens and the earth. Now the earth \r\n was formless and empty\\, darkness was over the surface of the deep\\, and t\r\n he Spirit of God was hovering over the waters.")); |
| 492 |
} |
| 493 |
|
| 494 |
@Test |
| 495 |
public void write_versionStrict() throws Exception { |
| 496 |
VCard vcard = new VCard(); |
| 497 |
vcard.setVersion(VCardVersion.V4_0); |
| 498 |
vcard.setMailer("mailer"); |
| 499 |
|
| 500 |
String actual = Ezvcard.write(vcard).go(); |
| 501 |
assertFalse(actual.contains("\r\nMAILER:")); |
| 502 |
|
| 503 |
actual = Ezvcard.write(vcard).versionStrict(true).go(); |
| 504 |
assertFalse(actual.contains("\r\nMAILER:")); |
| 505 |
|
| 506 |
actual = Ezvcard.write(vcard).versionStrict(false).go(); |
| 507 |
assertTrue(actual.contains("\r\nMAILER:")); |
| 508 |
} |
| 509 |
|
| 510 |
@Test |
| 511 |
public void write_targetApplication() throws Exception { |
| 512 |
byte data[] = "data".getBytes(); |
| 513 |
VCard vcard = new VCard(); |
| 514 |
vcard.addPhoto(new Photo(data, ImageType.JPEG)); |
| 515 |
|
| 516 |
|
| 517 |
{ |
| 518 |
String actual = Ezvcard.write(vcard).prodId(false).version(VCardVersion.V2_1).go(); |
| 519 |
|
| 520 |
|
| 521 |
String expected = |
| 522 |
"BEGIN:VCARD\r\n" + |
| 523 |
"VERSION:2.1\r\n" + |
| 524 |
"PHOTO;ENCODING=BASE64;TYPE=jpeg:ZGF0YQ==\r\n" + |
| 525 |
"END:VCARD\r\n"; |
| 526 |
|
| 527 |
|
| 528 |
assertEquals(expected, actual); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
{ |
| 533 |
String actual = Ezvcard.write(vcard).prodId(false).version(VCardVersion.V2_1).targetApplication(TargetApplication.OUTLOOK).go(); |
| 534 |
|
| 535 |
|
| 536 |
String expected = |
| 537 |
"BEGIN:VCARD\r\n" + |
| 538 |
"VERSION:2.1\r\n" + |
| 539 |
"PHOTO;ENCODING=BASE64;TYPE=jpeg:ZGF0YQ==\r\n" + |
| 540 |
"\r\n" + |
| 541 |
"END:VCARD\r\n"; |
| 542 |
|
| 543 |
|
| 544 |
assertEquals(expected, actual); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
@Test |
| 549 |
public void write_file() throws Exception { |
| 550 |
VCard vcard = new VCard(); |
| 551 |
Path file = folder.newFile().toPath(); |
| 552 |
Ezvcard.write(vcard).go(file); |
| 553 |
assertTrue(Files.size(file) > 0); |
| 554 |
} |
| 555 |
|
| 556 |
@Test |
| 557 |
public void writeXml_go() throws Exception { |
| 558 |
VCard vcard = new VCard(); |
| 559 |
vcard.setFormattedName("John Doe"); |
| 560 |
|
| 561 |
String xml = Ezvcard.writeXml(vcard).go(); |
| 562 |
assertTrue(xml.contains("<fn><text>John Doe</text></fn>")); |
| 563 |
} |
| 564 |
|
| 565 |
@Test |
| 566 |
public void writeXml_dom() throws Exception { |
| 567 |
VCard vcard = new VCard(); |
| 568 |
vcard.setFormattedName(new FormattedName("John Doe")); |
| 569 |
|
| 570 |
Document actual = Ezvcard.writeXml(vcard).prodId(false).dom(); |
| 571 |
|
| 572 |
|
| 573 |
String html = |
| 574 |
"<vcards xmlns=\"" + VCardVersion.V4_0.getXmlNamespace() + "\">" + |
| 575 |
"<vcard>" + |
| 576 |
"<fn><text>John Doe</text></fn>" + |
| 577 |
"</vcard>" + |
| 578 |
"</vcards>"; |
| 579 |
Document expected = XmlUtils.toDocument(html); |
| 580 |
|
| 581 |
|
| 582 |
assertXMLEqual(expected, actual); |
| 583 |
} |
| 584 |
|
| 585 |
@Test |
| 586 |
public void writeXml_one() throws Exception { |
| 587 |
VCard vcard = new VCard(); |
| 588 |
vcard.setFormattedName("John Doe"); |
| 589 |
|
| 590 |
Document dom = Ezvcard.writeXml(vcard).dom(); |
| 591 |
String actual = (String) xpath.evaluate("/v:vcards/v:vcard/v:fn/v:text", dom, XPathConstants.STRING); |
| 592 |
assertEquals(vcard.getFormattedName().getValue(), actual); |
| 593 |
} |
| 594 |
|
| 595 |
@Test |
| 596 |
public void writeXml_multiple() throws Exception { |
| 597 |
VCard vcard1 = new VCard(); |
| 598 |
vcard1.setFormattedName("John Doe"); |
| 599 |
VCard vcard2 = new VCard(); |
| 600 |
vcard2.setFormattedName("Jane Doe"); |
| 601 |
VCard vcard3 = new VCard(); |
| 602 |
vcard3.setFormattedName("Janet Doe"); |
| 603 |
|
| 604 |
Document dom = Ezvcard.writeXml(vcard1, vcard2, vcard3).dom(); |
| 605 |
NodeList nl = (NodeList) xpath.evaluate("/v:vcards/v:vcard/v:fn/v:text", dom, XPathConstants.NODESET); |
| 606 |
|
| 607 |
assertEquals(vcard1.getFormattedName().getValue(), nl.item(0).getTextContent()); |
| 608 |
assertEquals(vcard2.getFormattedName().getValue(), nl.item(1).getTextContent()); |
| 609 |
assertEquals(vcard3.getFormattedName().getValue(), nl.item(2).getTextContent()); |
| 610 |
} |
| 611 |
|
| 612 |
@Test |
| 613 |
public void writeXml_prodId() throws Exception { |
| 614 |
VCard vcard = new VCard(); |
| 615 |
|
| 616 |
Document dom = Ezvcard.writeXml(vcard).dom(); |
| 617 |
Double count = (Double) xpath.evaluate("count(/v:vcards/v:vcard/v:prodid)", dom, XPathConstants.NUMBER); |
| 618 |
assertEquals(Double.valueOf(1), count); |
| 619 |
|
| 620 |
dom = Ezvcard.writeXml(vcard).prodId(true).dom(); |
| 621 |
count = (Double) xpath.evaluate("count(/v:vcards/v:vcard/v:prodid)", dom, XPathConstants.NUMBER); |
| 622 |
assertEquals(Double.valueOf(1), count); |
| 623 |
|
| 624 |
dom = Ezvcard.writeXml(vcard).prodId(false).dom(); |
| 625 |
count = (Double) xpath.evaluate("count(/v:vcards/v:vcard/v:prodid)", dom, XPathConstants.NUMBER); |
| 626 |
assertEquals(Double.valueOf(0), count); |
| 627 |
} |
| 628 |
|
| 629 |
@Test |
| 630 |
public void writeXml_versionStrict() throws Exception { |
| 631 |
VCard vcard = new VCard(); |
| 632 |
vcard.setMailer("mailer"); |
| 633 |
|
| 634 |
Document dom = Ezvcard.writeXml(vcard).dom(); |
| 635 |
Double count = (Double) xpath.evaluate("count(/v:vcards/v:vcard/v:mailer)", dom, XPathConstants.NUMBER); |
| 636 |
assertEquals(Double.valueOf(0), count); |
| 637 |
|
| 638 |
dom = Ezvcard.writeXml(vcard).versionStrict(true).dom(); |
| 639 |
count = (Double) xpath.evaluate("count(/v:vcards/v:vcard/v:mailer)", dom, XPathConstants.NUMBER); |
| 640 |
assertEquals(Double.valueOf(0), count); |
| 641 |
|
| 642 |
dom = Ezvcard.writeXml(vcard).versionStrict(false).dom(); |
| 643 |
count = (Double) xpath.evaluate("count(/v:vcards/v:vcard/v:mailer)", dom, XPathConstants.NUMBER); |
| 644 |
assertEquals(Double.valueOf(1), count); |
| 645 |
} |
| 646 |
|
| 647 |
@Test |
| 648 |
public void writeXml_indent() throws Exception { |
| 649 |
VCard vcard = new VCard(); |
| 650 |
vcard.setFormattedName(new FormattedName("John Doe")); |
| 651 |
|
| 652 |
String actual = Ezvcard.writeXml(vcard).indent(2).go(); |
| 653 |
assertTrue(actual.contains(" <fn>" + NEWLINE + " <text>John Doe</text>" + NEWLINE + " </fn>")); |
| 654 |
} |
| 655 |
|
| 656 |
@Test |
| 657 |
public void writeHtml_one() throws Exception { |
| 658 |
VCard vcard = new VCard(); |
| 659 |
vcard.setFormattedName(new FormattedName("John Doe")); |
| 660 |
|
| 661 |
String actual = Ezvcard.writeHtml(vcard).go(); |
| 662 |
org.jsoup.nodes.Document document = Jsoup.parse(actual); |
| 663 |
assertEquals(1, document.select(".vcard").size()); |
| 664 |
assertEquals(1, document.select(".vcard .fn").size()); |
| 665 |
} |
| 666 |
|
| 667 |
@Test |
| 668 |
public void writeHtml_multiple() throws Exception { |
| 669 |
VCard vcard1 = new VCard(); |
| 670 |
vcard1.setFormattedName(new FormattedName("John Doe")); |
| 671 |
VCard vcard2 = new VCard(); |
| 672 |
vcard2.setFormattedName(new FormattedName("Jane Doe")); |
| 673 |
VCard vcard3 = new VCard(); |
| 674 |
vcard3.setFormattedName(new FormattedName("Janet Doe")); |
| 675 |
|
| 676 |
String actual = Ezvcard.writeHtml(vcard1, vcard2, vcard3).go(); |
| 677 |
org.jsoup.nodes.Document document = Jsoup.parse(actual); |
| 678 |
assertEquals(3, document.select(".vcard").size()); |
| 679 |
assertEquals(3, document.select(".vcard .fn").size()); |
| 680 |
} |
| 681 |
|
| 682 |
@Test |
| 683 |
public void writeJson_one() { |
| 684 |
VCard vcard = new VCard(); |
| 685 |
vcard.setFormattedName("John Doe"); |
| 686 |
|
| 687 |
|
| 688 |
String expected = |
| 689 |
"[\"vcard\"," + |
| 690 |
"[" + |
| 691 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 692 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 693 |
"]" + |
| 694 |
"]"; |
| 695 |
|
| 696 |
String actual = Ezvcard.writeJson(vcard).prodId(false).go(); |
| 697 |
assertEquals(expected, actual); |
| 698 |
} |
| 699 |
|
| 700 |
@Test |
| 701 |
public void writeJson_multiple() { |
| 702 |
VCard vcard1 = new VCard(); |
| 703 |
vcard1.setFormattedName(new FormattedName("John Doe")); |
| 704 |
VCard vcard2 = new VCard(); |
| 705 |
vcard2.setFormattedName(new FormattedName("Jane Doe")); |
| 706 |
VCard vcard3 = new VCard(); |
| 707 |
vcard3.setFormattedName(new FormattedName("Janet Doe")); |
| 708 |
|
| 709 |
|
| 710 |
String expected = |
| 711 |
"[" + |
| 712 |
"[\"vcard\"," + |
| 713 |
"[" + |
| 714 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 715 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 716 |
"]" + |
| 717 |
"]," + |
| 718 |
"[\"vcard\"," + |
| 719 |
"[" + |
| 720 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 721 |
"[\"fn\",{},\"text\",\"Jane Doe\"]" + |
| 722 |
"]" + |
| 723 |
"]," + |
| 724 |
"[\"vcard\"," + |
| 725 |
"[" + |
| 726 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 727 |
"[\"fn\",{},\"text\",\"Janet Doe\"]" + |
| 728 |
"]" + |
| 729 |
"]" + |
| 730 |
"]"; |
| 731 |
|
| 732 |
String actual = Ezvcard.writeJson(vcard1, vcard2, vcard3).prodId(false).go(); |
| 733 |
assertEquals(expected, actual); |
| 734 |
} |
| 735 |
|
| 736 |
@Test |
| 737 |
public void writeJson_prodId() { |
| 738 |
VCard vcard = new VCard(); |
| 739 |
|
| 740 |
String actual = Ezvcard.writeJson(vcard).go(); |
| 741 |
assertTrue(actual.contains("[\"prodid\",")); |
| 742 |
|
| 743 |
actual = Ezvcard.writeJson(vcard).prodId(true).go(); |
| 744 |
assertTrue(actual.contains("[\"prodid\",")); |
| 745 |
|
| 746 |
actual = Ezvcard.writeJson(vcard).prodId(false).go(); |
| 747 |
assertFalse(actual.contains("[\"prodid\",")); |
| 748 |
} |
| 749 |
|
| 750 |
@Test |
| 751 |
public void writeJson_versionStrict() { |
| 752 |
VCard vcard = new VCard(); |
| 753 |
vcard.setMailer("mailer"); |
| 754 |
|
| 755 |
String actual = Ezvcard.writeJson(vcard).go(); |
| 756 |
assertFalse(actual.contains("[\"mailer\",")); |
| 757 |
|
| 758 |
actual = Ezvcard.writeJson(vcard).versionStrict(true).go(); |
| 759 |
assertFalse(actual.contains("[\"mailer\",")); |
| 760 |
|
| 761 |
actual = Ezvcard.writeJson(vcard).versionStrict(false).go(); |
| 762 |
assertTrue(actual.contains("[\"mailer\",")); |
| 763 |
} |
| 764 |
|
| 765 |
@Test |
| 766 |
public void writeJson_indent() { |
| 767 |
VCard vcard = new VCard(); |
| 768 |
|
| 769 |
|
| 770 |
String actual = Ezvcard.writeJson(vcard).go(); |
| 771 |
assertTrue(actual.startsWith("[\"vcard\",[[\"")); |
| 772 |
|
| 773 |
actual = Ezvcard.writeJson(vcard).prettyPrint(true).go(); |
| 774 |
assertTrue(actual.startsWith("[" + NEWLINE + " \"vcard\"," + NEWLINE + " [" + NEWLINE + " [")); |
| 775 |
|
| 776 |
actual = Ezvcard.writeJson(vcard).prettyPrint(false).go(); |
| 777 |
assertTrue(actual.startsWith("[\"vcard\",[[\"")); |
| 778 |
} |
| 779 |
|
| 780 |
@Test |
| 781 |
public void writeJson_does_not_close_stream() throws Exception { |
| 782 |
VCard vcard = new VCard(); |
| 783 |
|
| 784 |
Path file = folder.newFile().toPath(); |
| 785 |
try (Writer writer = Files.newBufferedWriter(file)) { |
| 786 |
Ezvcard.writeJson(vcard).go(writer); |
| 787 |
writer.write("test"); |
| 788 |
} |
| 789 |
} |
| 790 |
} |