use of ezvcard.VCard in project ez-vcard by mangstadt.
the class VCardReaderTest method extended_properties_override_standard_property_scribes.
@Test
public void extended_properties_override_standard_property_scribes() throws Exception {
for (VCardVersion version : VCardVersion.values()) {
// @formatter:off
String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "FN:John Doe\r\n" + "END:VCARD\r\n";
// @formatter:on
VCardReader reader = new VCardReader(str);
reader.registerScribe(new MyFormattedNameScribe());
VCard vcard = reader.readNext();
assertVersion(version, vcard);
assertPropertyCount(1, vcard);
// read a type that has a type class
MyFormattedNameProperty fn = vcard.getProperty(MyFormattedNameProperty.class);
assertEquals("JOHN DOE", fn.value);
assertParseWarnings(reader);
assertNoMoreVCards(reader);
}
}
use of ezvcard.VCard in project ez-vcard by mangstadt.
the class VCardReaderTest method embedded_vcard.
@Test
public void embedded_vcard() throws Exception {
for (VCardVersion version : VCardVersion.values()) {
/*
* Test against all versions, even though embedded vCards are only
* supported by 3.0.
*/
// @formatter:off
String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "AGENT:" + "BEGIN:VCARD\\n" + "VERSION:" + version + "\\n" + "FN:Agent 007\\n" + "AGENT:" + "BEGIN:VCARD\\\\n" + "VERSION:" + version + "\\\\n" + "FN:Agent 009\\\\n" + "END:VCARD\\\\n" + "END:VCARD\r\n" + "FN:John Doe\r\n" + "END:VCARD\r\n";
// @formatter:on
VCardReader reader = new VCardReader(str);
VCard vcard = reader.readNext();
assertVersion(version, vcard);
assertPropertyCount(2, vcard);
assertEquals("John Doe", vcard.getFormattedName().getValue());
{
VCard agentVCard = vcard.getAgent().getVCard();
assertVersion(version, agentVCard);
assertPropertyCount(2, agentVCard);
assertEquals("Agent 007", agentVCard.getFormattedName().getValue());
{
VCard agentAgentVCard = agentVCard.getAgent().getVCard();
assertVersion(version, agentAgentVCard);
assertPropertyCount(1, agentAgentVCard);
assertEquals("Agent 009", agentAgentVCard.getFormattedName().getValue());
}
}
assertParseWarnings(reader);
assertNoMoreVCards(reader);
}
}
use of ezvcard.VCard in project ez-vcard by mangstadt.
the class VCardWriterTest method rfc6350_example.
@Test
public void rfc6350_example() throws Throwable {
VCard vcard = new VCard();
vcard.setFormattedName("Simon Perreault");
StructuredName n = new StructuredName();
n.setFamily("Perreault");
n.setGiven("Simon");
n.getSuffixes().add("ing. jr");
n.getSuffixes().add("M.Sc.");
vcard.setStructuredName(n);
Birthday bday = new Birthday(PartialDate.builder().month(2).date(3).build());
vcard.setBirthday(bday);
Anniversary anniversary = new Anniversary(PartialDate.builder().year(2009).month(8).date(8).hour(14).minute(30).offset(new UtcOffset(false, -5, 0)).build());
vcard.setAnniversary(anniversary);
vcard.setGender(Gender.male());
vcard.addLanguage("fr").setPref(1);
vcard.addLanguage("en").setPref(2);
vcard.setOrganization("Viagenie").setType("work");
Address adr = new Address();
adr.setExtendedAddress("Suite D2-630");
adr.setStreetAddress("2875 Laurier");
adr.setLocality("Quebec");
adr.setRegion("QC");
adr.setPostalCode("G1V 2M2");
adr.setCountry("Canada");
adr.getTypes().add(AddressType.WORK);
vcard.addAddress(adr);
TelUri telUri = new TelUri.Builder("+1-418-656-9254").extension("102").build();
Telephone tel = new Telephone(telUri);
tel.setPref(1);
tel.getTypes().add(TelephoneType.WORK);
tel.getTypes().add(TelephoneType.VOICE);
vcard.addTelephoneNumber(tel);
tel = new Telephone(new TelUri.Builder("+1-418-262-6501").build());
tel.getTypes().add(TelephoneType.WORK);
tel.getTypes().add(TelephoneType.VOICE);
tel.getTypes().add(TelephoneType.CELL);
tel.getTypes().add(TelephoneType.VIDEO);
tel.getTypes().add(TelephoneType.TEXT);
vcard.addTelephoneNumber(tel);
vcard.addEmail("simon.perreault@viagenie.ca", EmailType.WORK);
Geo geo = new Geo(46.772673, -71.282945);
geo.setType("work");
vcard.setGeo(geo);
Key key = new Key("http://www.viagenie.ca/simon.perreault/simon.asc", null);
key.setType("work");
vcard.addKey(key);
vcard.setTimezone(new Timezone(new UtcOffset(false, -5, 0)));
vcard.addUrl("http://nomis80.org").setType("home");
assertValidate(vcard).versions(VCardVersion.V4_0).run();
StringWriter sw = new StringWriter();
VCardWriter writer = new VCardWriter(sw, VCardVersion.V4_0);
writer.setAddProdId(false);
writer.write(vcard);
writer.close();
// @formatter:off
String expected = "BEGIN:VCARD\r\n" + "VERSION:4.0\r\n" + "FN:Simon Perreault\r\n" + "N:Perreault;Simon;;;ing. jr,M.Sc.\r\n" + "BDAY:--0203\r\n" + "ANNIVERSARY:20090808T1430-0500\r\n" + "GENDER:M\r\n" + "LANG;PREF=1:fr\r\n" + "LANG;PREF=2:en\r\n" + "ORG;TYPE=work:Viagenie\r\n" + "ADR;TYPE=work:;Suite D2-630;2875 Laurier;Quebec;QC;G1V 2M2;Canada\r\n" + "TEL;PREF=1;TYPE=work,voice;VALUE=uri:tel:+1-418-656-9254;ext=102\r\n" + "TEL;TYPE=work,voice,cell,video,text;VALUE=uri:tel:+1-418-262-6501\r\n" + "EMAIL;TYPE=work:simon.perreault@viagenie.ca\r\n" + "GEO;TYPE=work:geo:46.772673,-71.282945\r\n" + "KEY;TYPE=work:http://www.viagenie.ca/simon.perreault/simon.asc\r\n" + "TZ;VALUE=utc-offset:-0500\r\n" + "URL;TYPE=home:http://nomis80.org\r\n" + "END:VCARD\r\n";
// @formatter:on
String actual = sw.toString();
assertEquals(expected, actual);
}
use of ezvcard.VCard in project ez-vcard by mangstadt.
the class VCardWriterTest method utf8.
@Test
public void utf8() throws Throwable {
VCard vcard = new VCard();
vcard.addNote("\u019dote");
File file = tempFolder.newFile();
// should be written as UTF-8
{
VCardWriter writer = new VCardWriter(file, false, VCardVersion.V4_0);
writer.setAddProdId(false);
writer.write(vcard);
writer.close();
// @formatter:off
String expected = "BEGIN:VCARD\r\n" + "VERSION:4.0\r\n" + "NOTE:\u019dote\r\n" + "END:VCARD\r\n";
// @formatter:on
String actual = new Gobble(file).asString("UTF-8");
assertEquals(expected, actual);
}
// should be written using default encoding
if (!Charset.defaultCharset().name().equalsIgnoreCase("UTF-8")) {
// don't test if the local machine's default encoding is UTF-8
VCardWriter writer = new VCardWriter(file, VCardVersion.V3_0);
writer.setAddProdId(false);
writer.write(vcard);
writer.close();
// @formatter:off
String expected = "BEGIN:VCARD\r\n" + "VERSION:3.0\r\n" + "NOTE:?ote\r\n" + "END:VCARD\r\n";
// @formatter:on
String actual = new Gobble(file).asString("UTF-8");
assertEquals(expected, actual);
}
}
use of ezvcard.VCard in project ez-vcard by mangstadt.
the class VCardWriterTest method nestedVCard.
@Test
public void nestedVCard() throws Throwable {
VCard vcard = new VCard();
vcard.setFormattedName("Michael Angstadt");
VCard agentVcard = new VCard();
agentVcard.setFormattedName("Agent 007");
agentVcard.addNote("Make sure that it properly folds long lines which are part of a nested AGENT type in a version 2.1 vCard.");
Agent agent = new Agent(agentVcard);
vcard.setAgent(agent);
// i herd u liek AGENTs...
VCard secondAgentVCard = new VCard();
secondAgentVCard.setFormattedName("Agent 009");
secondAgentVCard.addNote("Make sure that it ALSO properly folds THIS long line because it's part of an AGENT that's inside of an AGENT.");
Agent secondAgent = new Agent(secondAgentVCard);
agentVcard.setAgent(secondAgent);
StringWriter sw = new StringWriter();
VCardWriter vcw = new VCardWriter(sw, VCardVersion.V2_1);
vcw.setAddProdId(false);
vcw.write(vcard);
String actual = sw.toString();
// @formatter:off
String expected = "BEGIN:VCARD\r\n" + "VERSION:2.1\r\n" + "FN:Michael Angstadt\r\n" + // nested types should not have X-GENERATOR
"AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:2.1\r\n" + "FN:Agent 007\r\n" + "NOTE:Make sure that it properly folds long lines which are part of a nested \r\n" + " AGENT type in a version 2.1 vCard.\r\n" + "AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:2.1\r\n" + "FN:Agent 009\r\n" + "NOTE:Make sure that it ALSO properly folds THIS long line because it's part \r\n" + " of an AGENT that's inside of an AGENT.\r\n" + "END:VCARD\r\n" + "END:VCARD\r\n" + "END:VCARD\r\n";
// @formatter:on
assertEquals(expected, actual);
}
Aggregations