use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class StreamWriterTest method productId_order.
/**
* Asserts that the PRODID property is always put at the front of the vCard.
*/
@Test
public void productId_order() throws IOException {
vcard.setFormattedName("Name");
{
writer.write(vcard, V2_1);
Iterator<VCardProperty> it = writer.propertiesList.iterator();
VCardProperty property = it.next();
assertTrue(property instanceof RawProperty);
property = it.next();
assertTrue(property instanceof FormattedName);
assertFalse(it.hasNext());
}
for (VCardVersion version : each(V3_0, V4_0)) {
writer.write(vcard, version);
Iterator<VCardProperty> it = writer.propertiesList.iterator();
VCardProperty property = it.next();
assertTrue(property instanceof ProductId);
property = it.next();
assertTrue(property instanceof FormattedName);
assertFalse(it.hasNext());
}
vcard.setProductId("value");
writer.setAddProdId(false);
for (VCardVersion version : each(V3_0, V4_0)) {
writer.write(vcard, version);
Iterator<VCardProperty> it = writer.propertiesList.iterator();
VCardProperty property = it.next();
assertTrue(property instanceof ProductId);
property = it.next();
assertTrue(property instanceof FormattedName);
assertFalse(it.hasNext());
}
}
use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class StreamWriterTest method labels.
@Test
public void labels() throws Throwable {
writer.setAddProdId(false);
// address with label and type
Address adr = new Address();
adr.setLabel("value1");
adr.getTypes().add(AddressType.HOME);
vcard.addAddress(adr);
// address with label
adr = new Address();
adr.setLabel("value2");
vcard.addAddress(adr);
// address
adr = new Address();
vcard.addAddress(adr);
for (VCardVersion version : each(V2_1, V3_0)) {
writer.write(vcard, version);
assertEquals(5, writer.count());
assertEquals(3, writer.count(Address.class));
assertEquals(2, writer.count(Label.class));
Label label = writer.get(Label.class).get(0);
assertEquals("value1", label.getValue());
assertEquals(Arrays.asList(AddressType.HOME), label.getTypes());
label = writer.get(Label.class).get(1);
assertEquals("value2", label.getValue());
assertEquals(Arrays.asList(), label.getTypes());
}
writer.write(vcard, V4_0);
assertEquals(3, writer.count());
assertEquals(3, writer.count(Address.class));
}
use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class VCardPropertyTest method isSupportedBy.
@Test
public void isSupportedBy() {
VCardPropertyImpl withoutSupportedVersions = new VCardPropertyImpl();
for (VCardVersion version : VCardVersion.values()) {
assertTrue(withoutSupportedVersions.isSupportedBy(version));
}
Version3Property withSupportedVersions = new Version3Property();
assertFalse(withSupportedVersions.isSupportedBy(V2_1));
assertTrue(withSupportedVersions.isSupportedBy(V3_0));
assertFalse(withSupportedVersions.isSupportedBy(V4_0));
}
use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class StreamWriter method prepare.
/**
* Determines which properties need to be written.
* @param vcard the vCard to write
* @return the properties to write
* @throws IllegalArgumentException if a scribe hasn't been registered for a
* custom property class (see: {@link #registerScribe(VCardPropertyScribe)
* registerScribe})
*/
private List<VCardProperty> prepare(VCard vcard) {
VCardVersion targetVersion = getTargetVersion();
List<VCardProperty> propertiesToAdd = new ArrayList<VCardProperty>();
Set<Class<? extends VCardProperty>> unregistered = new HashSet<Class<? extends VCardProperty>>();
VCardProperty prodIdProperty = null;
for (VCardProperty property : vcard) {
if (versionStrict && !property.isSupportedBy(targetVersion)) {
// do not add the property to the vCard if it is not supported by the target version
continue;
}
// do not add PRODID to the property list yet
if (property instanceof ProductId) {
prodIdProperty = property;
continue;
}
// check for scribe
if (!index.hasPropertyScribe(property)) {
unregistered.add(property.getClass());
continue;
}
propertiesToAdd.add(property);
// add LABEL properties for each ADR property if the target version is 2.1 or 3.0
if ((targetVersion == VCardVersion.V2_1 || targetVersion == VCardVersion.V3_0) && property instanceof Address) {
Address adr = (Address) property;
String labelStr = adr.getLabel();
if (labelStr == null) {
continue;
}
Label label = new Label(labelStr);
label.getTypes().addAll(adr.getTypes());
propertiesToAdd.add(label);
}
}
if (!unregistered.isEmpty()) {
List<String> classes = new ArrayList<String>(unregistered.size());
for (Class<? extends VCardProperty> clazz : unregistered) {
classes.add(clazz.getName());
}
throw Messages.INSTANCE.getIllegalArgumentException(14, classes);
}
// create a PRODID property, saying the vCard was generated by this library
if (addProdId) {
if (targetVersion == VCardVersion.V2_1) {
prodIdProperty = new RawProperty("X-PRODID", "ez-vcard " + Ezvcard.VERSION);
} else {
prodIdProperty = new ProductId("ez-vcard " + Ezvcard.VERSION);
}
}
// add PRODID to the beginning of the vCard
if (prodIdProperty != null) {
propertiesToAdd.add(0, prodIdProperty);
}
return propertiesToAdd;
}
use of ezvcard.VCardVersion in project ez-vcard by mangstadt.
the class TestUtils method assertVersion.
/**
* Tests the version assigned to a {@link VCard} object.
* @param expected the expected version
* @param vcard the vCard object
*/
public static void assertVersion(VCardVersion expected, VCard vcard) {
VCardVersion actual = vcard.getVersion();
assertEquals(expected, actual);
}
Aggregations