Search in sources :

Example 21 with SyntaxStyle

use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.

the class VObjectWriterTest method property_value_with_newlines.

/**
 * When the property value contains newlines, it should encode in
 * quoted-printable in old style, and escape newlines in new style.
 */
@Test
public void property_value_with_newlines() throws Exception {
    SyntaxStyle style = SyntaxStyle.OLD;
    {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        writer.getFoldedLineWriter().setLineLength(null);
        VObjectParameters parameters = new VObjectParameters();
        VObjectParameters expectedParams = new VObjectParameters(parameters);
        writer.writeProperty(null, "PROP", parameters, "one\r\ntwo");
        // nothing should be added to the parameters object that was passed into the method
        assertEquals(expectedParams, parameters);
        parameters = new VObjectParameters();
        expectedParams = new VObjectParameters(parameters);
        writer.writeProperty(null, "PROP", parameters, "one\rtwo");
        assertEquals(expectedParams, parameters);
        parameters = new VObjectParameters();
        expectedParams = new VObjectParameters(parameters);
        writer.writeProperty(null, "PROP", parameters, "one\ntwo");
        assertEquals(expectedParams, parameters);
        parameters = new VObjectParameters();
        parameters.put(null, "QUOTED-PRINTABLE");
        expectedParams = new VObjectParameters(parameters);
        writer.writeProperty(null, "PROP", parameters, "one\r\ntwo");
        assertEquals(expectedParams, parameters);
        parameters = new VObjectParameters();
        parameters.put("ENCODING", "QUOTED-PRINTABLE");
        expectedParams = new VObjectParameters(parameters);
        writer.writeProperty(null, "PROP", parameters, "one\r\ntwo");
        assertEquals(expectedParams, parameters);
        parameters = new VObjectParameters();
        parameters.put("CHARSET", "UTF-16");
        expectedParams = new VObjectParameters(parameters);
        writer.writeProperty(null, "PROP", parameters, "one\r\ntwo");
        assertEquals(expectedParams, parameters);
        String actual = sw.toString();
        // @formatter:off
        String expected = "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:one=0D=0Atwo\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:one=0Dtwo\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:one=0Atwo\r\n" + "PROP;QUOTED-PRINTABLE;CHARSET=UTF-8:one=0D=0Atwo\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:one=0D=0Atwo\r\n" + "PROP;CHARSET=UTF-16;ENCODING=QUOTED-PRINTABLE:=FE=FF=00o=00n=00e=00=0D=00=0A=00t=00w=00o\r\n";
        // @formatter:on
        assertEquals(expected, actual);
    }
    style = SyntaxStyle.NEW;
    {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        writer.writeProperty(null, "PROP", new VObjectParameters(), "one\r\ntwo");
        writer.writeProperty(null, "PROP", new VObjectParameters(), "one\rtwo");
        writer.writeProperty(null, "PROP", new VObjectParameters(), "one\ntwo");
        String actual = sw.toString();
        // @formatter:off
        String expected = "PROP:one\\ntwo\r\n" + "PROP:one\\ntwo\r\n" + "PROP:one\\ntwo\r\n";
        // @formatter:on
        assertEquals(expected, actual);
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) Test(org.junit.Test)

Example 22 with SyntaxStyle

use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.

the class VObjectWriterTest method property_value_null.

/**
 * When the property value is null, it should treat the value as an empty
 * string.
 */
@Test
public void property_value_null() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        writer.writeProperty(null, "PROP", new VObjectParameters(), null);
        String actual = sw.toString();
        // @formatter:off
        String expected = "PROP:\r\n";
        // @formatter:on
        assertEquals(expected, actual);
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) Test(org.junit.Test)

Example 23 with SyntaxStyle

use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.

the class VObjectWriterTest method parameters_nameless.

@Test
public void parameters_nameless() throws Exception {
    List<VObjectParameters> list = new ArrayList<VObjectParameters>();
    VObjectParameters parameters = new VObjectParameters();
    parameters.put(null, "one");
    list.add(parameters);
    parameters = new VObjectParameters();
    parameters.put(null, "one");
    parameters.put(null, "two");
    list.add(parameters);
    SyntaxStyle style = SyntaxStyle.OLD;
    {
        for (boolean caretEncoding : new boolean[] { false, true }) {
            StringWriter sw = new StringWriter();
            VObjectWriter writer = new VObjectWriter(sw, style);
            writer.setCaretEncodingEnabled(caretEncoding);
            for (VObjectParameters p : list) {
                writer.writeProperty(null, "PROP", p, "");
            }
            String actual = sw.toString();
            // @formatter:off
            String expected = "PROP;one:\r\n" + "PROP;one;two:\r\n";
            // @formatter:on
            assertEquals(expected, actual);
        }
    }
    style = SyntaxStyle.NEW;
    {
        for (boolean caretEncoding : new boolean[] { false, true }) {
            StringWriter sw = new StringWriter();
            VObjectWriter writer = new VObjectWriter(sw, style);
            writer.setCaretEncodingEnabled(caretEncoding);
            for (VObjectParameters p : list) {
                try {
                    writer.writeProperty(null, "PROP", p, "");
                    fail("IllegalArgumentException expected when property name contains nameless parameter and style is " + style.name());
                } catch (IllegalArgumentException e) {
                // expected
                }
            }
            String actual = sw.toString();
            // @formatter:off
            String expected = "";
            // @formatter:on
            assertEquals(expected, actual);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) ArrayList(java.util.ArrayList) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) Test(org.junit.Test)

Example 24 with SyntaxStyle

use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.

the class VObjectWriterTest method write_VObjectProperty.

@Test
public void write_VObjectProperty() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        VObjectProperty property = new VObjectProperty();
        property.setGroup("group");
        property.setName("PROP");
        property.getParameters().put("PARAM", "pvalue");
        property.setValue("value");
        writer.writeProperty(property);
        String actual = sw.toString();
        // @formatter:off
        String expected = "group.PROP;PARAM=pvalue:value\r\n";
        // @formatter:on
        assertEquals(expected, actual);
    }
}
Also used : VObjectProperty(com.github.mangstadt.vinnie.VObjectProperty) StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) Test(org.junit.Test)

Example 25 with SyntaxStyle

use of com.github.mangstadt.vinnie.SyntaxStyle in project vinnie by mangstadt.

the class VObjectWriterTest method writeEndComponent.

@Test
public void writeEndComponent() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        writer.writeEndComponent("COMP");
        writer.writeEndComponent(" ");
        try {
            writer.writeEndComponent("");
            fail();
        } catch (IllegalArgumentException e) {
        // expected
        }
        try {
            writer.writeEndComponent(null);
            fail();
        } catch (IllegalArgumentException e) {
        // expected
        }
        String actual = sw.toString();
        // @formatter:off
        String expected = "END:COMP\r\n" + "END: \r\n";
        // @formatter:on
        assertEquals(expected, actual);
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) Test(org.junit.Test)

Aggregations

SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)50 Test (org.junit.Test)45 InOrder (org.mockito.InOrder)24 StringWriter (java.io.StringWriter)21 VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)15 VObjectProperty (com.github.mangstadt.vinnie.VObjectProperty)12 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)7 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)7 DecoderException (com.github.mangstadt.vinnie.codec.DecoderException)6 ArrayList (java.util.ArrayList)4 AllowedCharacters (com.github.mangstadt.vinnie.validate.AllowedCharacters)3 ValidationWarning (ezvcard.ValidationWarning)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 VCardDataType (ezvcard.VCardDataType)1 VCardVersion (ezvcard.VCardVersion)1 ClientPidMap (ezvcard.property.ClientPidMap)1 SortString (ezvcard.property.SortString)1 AbstractList (java.util.AbstractList)1 EnumSet (java.util.EnumSet)1