Search in sources :

Example 31 with VCardVersion

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());
    }
}
Also used : RawProperty(ezvcard.property.RawProperty) FormattedName(ezvcard.property.FormattedName) Iterator(java.util.Iterator) VCardProperty(ezvcard.property.VCardProperty) VCardVersion(ezvcard.VCardVersion) ProductId(ezvcard.property.ProductId) Test(org.junit.Test)

Example 32 with VCardVersion

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));
}
Also used : Address(ezvcard.property.Address) Label(ezvcard.property.Label) VCardVersion(ezvcard.VCardVersion) Test(org.junit.Test)

Example 33 with VCardVersion

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));
}
Also used : VCardVersion(ezvcard.VCardVersion) Test(org.junit.Test)

Example 34 with VCardVersion

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;
}
Also used : Address(ezvcard.property.Address) ArrayList(java.util.ArrayList) Label(ezvcard.property.Label) VCardVersion(ezvcard.VCardVersion) ProductId(ezvcard.property.ProductId) RawProperty(ezvcard.property.RawProperty) VCardProperty(ezvcard.property.VCardProperty) HashSet(java.util.HashSet)

Example 35 with VCardVersion

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);
}
Also used : VCardVersion(ezvcard.VCardVersion)

Aggregations

VCardVersion (ezvcard.VCardVersion)36 Test (org.junit.Test)30 VCard (ezvcard.VCard)14 VCardAsserter (ezvcard.property.asserter.VCardAsserter)10 VCardParameters (ezvcard.parameter.VCardParameters)5 Label (ezvcard.property.Label)4 RawProperty (ezvcard.property.RawProperty)4 VCardProperty (ezvcard.property.VCardProperty)4 Address (ezvcard.property.Address)3 ProductId (ezvcard.property.ProductId)3 VCardDataType (ezvcard.VCardDataType)2 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)2 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)2 ArrayList (java.util.ArrayList)2 SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)1 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)1 AllowedCharacters (com.github.mangstadt.vinnie.validate.AllowedCharacters)1 ValidationWarning (ezvcard.ValidationWarning)1 LuckyNumProperty (ezvcard.io.LuckyNumProperty)1 LuckyNumScribe (ezvcard.io.LuckyNumProperty.LuckyNumScribe)1