Search in sources :

Example 26 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardReaderTest method nested_vcard_missing_vcard.

@Test
public void nested_vcard_missing_vcard() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        /*
			 * Test against all versions, even though nested vCards are only
			 * supported by 2.1.
			 */
        // @formatter:off
        String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "NESTED:\r\n" + "FN:John Doe\r\n" + "END:VCARD\r\n";
        // @formatter:on
        VCardReader reader = new VCardReader(str);
        reader.registerScribe(new NestedScribe());
        VCard vcard = reader.readNext();
        assertVersion(version, vcard);
        assertPropertyCount(2, vcard);
        assertEquals("John Doe", vcard.getFormattedName().getValue());
        assertNull(vcard.getProperty(Nested.class).vcard);
        assertParseWarnings(reader);
        assertNoMoreVCards(reader);
    }
}
Also used : VCardVersion(ezvcard.VCardVersion) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 27 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardReaderTest method nested_vcard_with_labels.

@Test
public void nested_vcard_with_labels() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        /*
			 * Test against all versions, even though nested vCards are only
			 * supported by 2.1.
			 */
        // @formatter:off
        String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "ADR;TYPE=home:;;;;;\r\n" + "ADR;TYPE=work:;;;;;\r\n" + "AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "LABEL;TYPE=home:home label\r\n" + "AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "ADR;TYPE=dom:;;;;;\r\n" + "LABEL;TYPE=dom:dom label\r\n" + "END:VCARD\r\n" + "ADR;TYPE=dom:;;;;;\r\n" + "END:VCARD\r\n" + "LABEL;TYPE=work:work label\r\n" + "END:VCARD\r\n";
        // @formatter:on
        VCardReader reader = new VCardReader(str);
        VCard vcard = reader.readNext();
        assertVersion(version, vcard);
        assertPropertyCount(3, vcard);
        Iterator<Address> adrs = vcard.getAddresses().iterator();
        Address adr = adrs.next();
        assertEquals(Arrays.asList(AddressType.HOME), adr.getTypes());
        assertNull(adr.getLabel());
        adr = adrs.next();
        assertEquals(Arrays.asList(AddressType.WORK), adr.getTypes());
        assertEquals("work label", adr.getLabel());
        {
            VCard agentVCard = vcard.getAgent().getVCard();
            assertVersion(version, agentVCard);
            assertPropertyCount(3, agentVCard);
            adr = agentVCard.getAddresses().get(0);
            assertEquals(Arrays.asList(AddressType.DOM), adr.getTypes());
            assertNull(adr.getLabel());
            Label label = agentVCard.getOrphanedLabels().get(0);
            assertEquals(Arrays.asList(AddressType.HOME), label.getTypes());
            {
                VCard agentAgentVCard = agentVCard.getAgent().getVCard();
                assertVersion(version, agentAgentVCard);
                assertPropertyCount(1, agentAgentVCard);
                adr = agentAgentVCard.getAddresses().get(0);
                assertEquals(Arrays.asList(AddressType.DOM), adr.getTypes());
                assertEquals("dom label", adr.getLabel());
            }
        }
        assertParseWarnings(reader);
        assertNoMoreVCards(reader);
    }
}
Also used : Address(ezvcard.property.Address) Label(ezvcard.property.Label) VCardVersion(ezvcard.VCardVersion) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 28 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardReaderTest method warnings_in_embedded_vcard.

@Test
public void warnings_in_embedded_vcard() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        // @formatter:off
        String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "AGENT:BEGIN:VCARD\\nVERSION:" + version + "\\nWARNINGS:value\\nEND:VCARD\r\n" + "END:VCARD\r\n";
        // @formatter:on
        VCardReader reader = new VCardReader(str);
        reader.registerScribe(new WarningsScribe());
        reader.readNext();
        assertParseWarnings(reader, (Integer) null);
        assertNoMoreVCards(reader);
    }
}
Also used : VCardVersion(ezvcard.VCardVersion) Test(org.junit.Test)

Example 29 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardReaderTest method nested_vcard.

@Test
public void nested_vcard() throws Exception {
    for (VCardVersion version : VCardVersion.values()) {
        /*
			 * Test against all versions, even though nested vCards are only
			 * supported by 2.1.
			 */
        // @formatter:off
        String str = "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "FN:Agent 007\r\n" + "AGENT:\r\n" + "BEGIN:VCARD\r\n" + "VERSION:" + version + "\r\n" + "FN:Agent 009\r\n" + "END:VCARD\r\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 agent1 = vcard.getAgent().getVCard();
            assertVersion(version, agent1);
            assertPropertyCount(2, agent1);
            assertEquals("Agent 007", agent1.getFormattedName().getValue());
            {
                VCard agent2 = agent1.getAgent().getVCard();
                assertVersion(version, agent2);
                assertPropertyCount(1, agent2);
                assertEquals("Agent 009", agent2.getFormattedName().getValue());
            }
        }
        assertParseWarnings(reader);
        assertNoMoreVCards(reader);
    }
}
Also used : VCardVersion(ezvcard.VCardVersion) VCard(ezvcard.VCard) Test(org.junit.Test)

Example 30 with VCardVersion

use of ezvcard.VCardVersion in project ez-vcard by mangstadt.

the class VCardWriterTest method date_time_properties_should_not_have_a_VALUE_parameter.

@Test
public void date_time_properties_should_not_have_a_VALUE_parameter() throws Throwable {
    class DateTestScribe<T extends VCardProperty> extends VCardPropertyScribe<T> {

        private final VCardDataType dataType;

        public DateTestScribe(Class<T> clazz, String name, VCardDataType dataType) {
            super(clazz, name);
            this.dataType = dataType;
        }

        @Override
        protected VCardDataType _defaultDataType(VCardVersion version) {
            return VCardDataType.DATE_AND_OR_TIME;
        }

        @Override
        protected VCardDataType _dataType(T property, VCardVersion version) {
            return dataType;
        }

        @Override
        protected String _writeText(T property, WriteContext context) {
            return "value";
        }

        @Override
        protected T _parseText(String value, VCardDataType dataType, VCardParameters parameters, ParseContext context) {
            return null;
        }
    }
    class DateProperty extends VCardProperty {
        // empty
    }
    class DateTimeProperty extends VCardProperty {
        // empty
    }
    class TimeProperty extends VCardProperty {
        // empty
    }
    class DateAndOrTimeProperty extends VCardProperty {
        // empty
    }
    VCard vcard = new VCard();
    vcard.addProperty(new DateProperty());
    vcard.addProperty(new DateTimeProperty());
    vcard.addProperty(new TimeProperty());
    vcard.addProperty(new DateAndOrTimeProperty());
    StringWriter sw = new StringWriter();
    VCardWriter vcw = new VCardWriter(sw, VCardVersion.V4_0);
    vcw.registerScribe(new DateTestScribe<DateProperty>(DateProperty.class, "DATE", VCardDataType.DATE));
    vcw.registerScribe(new DateTestScribe<DateTimeProperty>(DateTimeProperty.class, "DATETIME", VCardDataType.DATE_TIME));
    vcw.registerScribe(new DateTestScribe<TimeProperty>(TimeProperty.class, "TIME", VCardDataType.TIME));
    vcw.registerScribe(new DateTestScribe<DateAndOrTimeProperty>(DateAndOrTimeProperty.class, "DATEANDORTIME", VCardDataType.DATE_AND_OR_TIME));
    vcw.setAddProdId(false);
    vcw.write(vcard);
    String actual = sw.toString();
    // @formatter:off
    String expected = "BEGIN:VCARD\r\n" + "VERSION:4.0\r\n" + "DATE:value\r\n" + "DATETIME:value\r\n" + "TIME:value\r\n" + "DATEANDORTIME:value\r\n" + "END:VCARD\r\n";
    // @formatter:on
    assertEquals(actual, expected);
}
Also used : VCardPropertyScribe(ezvcard.io.scribe.VCardPropertyScribe) VCardVersion(ezvcard.VCardVersion) VCardParameters(ezvcard.parameter.VCardParameters) StringWriter(java.io.StringWriter) ParseContext(ezvcard.io.ParseContext) VCardProperty(ezvcard.property.VCardProperty) VCardDataType(ezvcard.VCardDataType) VCard(ezvcard.VCard) Test(org.junit.Test)

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