Search in sources :

Example 16 with SyntaxStyle

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

the class VObjectWriterTest method parameters_escape_special_characters_in_value.

/**
 * When escapable characters exist in a parameter value.
 */
@Test
public void parameters_escape_special_characters_in_value() throws Exception {
    // Old style:
    // Replaces \ with \\
    // Replaces ; with \;
    SyntaxStyle style = SyntaxStyle.OLD;
    {
        for (boolean caretEncoding : new boolean[] { false, true }) {
            StringWriter sw = new StringWriter();
            VObjectWriter writer = new VObjectWriter(sw, style);
            writer.getFoldedLineWriter().setLineLength(null);
            writer.setCaretEncodingEnabled(caretEncoding);
            String input = testString(":\r\n");
            String expectedOutput = input.replace("\\", "\\\\").replace(";", "\\;");
            VObjectParameters parameters = new VObjectParameters();
            parameters.put("PARAM", input);
            writer.writeProperty(null, "PROP", parameters, "");
            String actual = sw.toString();
            // @formatter:off
            String expected = "PROP;PARAM=" + expectedOutput + ":\r\n";
            // @formatter:on
            assertEquals(expected, actual);
        }
    }
    style = SyntaxStyle.NEW;
    {
        // New style without caret escaping
        // surrounds in double quotes, since it contains , ; or :
        boolean caretEncoding = false;
        {
            StringWriter sw = new StringWriter();
            VObjectWriter writer = new VObjectWriter(sw, style);
            writer.getFoldedLineWriter().setLineLength(null);
            writer.setCaretEncodingEnabled(caretEncoding);
            String input = testString("\"\r\n");
            String expectedOutput = input;
            VObjectParameters parameters = new VObjectParameters();
            parameters.put("PARAM", input);
            writer.writeProperty(null, "PROP", parameters, "");
            String actual = sw.toString();
            // @formatter:off
            String expected = "PROP;PARAM=\"" + expectedOutput + "\":\r\n";
            // @formatter:on
            assertEquals(expected, actual);
        }
        // New style with caret escaping
        // replaces ^ with ^^
        // replaces newline with ^n
        // replaces " with ^'
        // surrounds in double quotes, since it contains , ; or :
        caretEncoding = true;
        {
            StringWriter sw = new StringWriter();
            VObjectWriter writer = new VObjectWriter(sw, style);
            writer.getFoldedLineWriter().setLineLength(null);
            writer.setCaretEncodingEnabled(caretEncoding);
            // make sure all three kinds of newline sequences are handled
            String input = testString("\r\n") + "\r\n\n\r";
            String expectedOutput = input.replace("^", "^^").replace("\"", "^'").replace("\r\n", "^n").replace("\r", "^n").replace("\n", "^n");
            VObjectParameters parameters = new VObjectParameters();
            parameters.put("PARAM", input);
            writer.writeProperty(null, "PROP", parameters, "");
            String actual = sw.toString();
            // @formatter:off
            String expected = "PROP;PARAM=\"" + expectedOutput + "\":\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 17 with SyntaxStyle

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

the class VObjectWriterTest method property_value_quoted_printable.

/**
 * When a QUOTED-PRINTABLE parameter value is present, the writer should
 * encode the value in quoted-printable.
 */
@Test
public void property_value_quoted_printable() throws Exception {
    final String propValue = "value \u00e4\u00f6\u00fc\u00df";
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        writer.getFoldedLineWriter().setLineLength(null);
        // no parameters
        VObjectParameters parameters = new VObjectParameters();
        writer.writeProperty(null, "PROP", parameters, propValue);
        // no charset
        parameters = new VObjectParameters();
        parameters.put("ENCODING", "QUOTED-PRINTABLE");
        writer.writeProperty(null, "PROP", parameters, propValue);
        // UTF-8
        parameters = new VObjectParameters();
        parameters.put("ENCODING", "QUOTED-PRINTABLE");
        parameters.put("CHARSET", "UTF-8");
        writer.writeProperty(null, "PROP", parameters, propValue);
        // UTF-16
        parameters = new VObjectParameters();
        parameters.put("ENCODING", "QUOTED-PRINTABLE");
        parameters.put("CHARSET", "UTF-16");
        writer.writeProperty(null, "PROP", parameters, propValue);
        // invalid
        parameters = new VObjectParameters();
        parameters.put("ENCODING", "QUOTED-PRINTABLE");
        parameters.put("CHARSET", "invalid");
        writer.writeProperty(null, "PROP", parameters, propValue);
        String actual = sw.toString();
        // @formatter:off
        String expected = "PROP:" + propValue + "\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:value =C3=A4=C3=B6=C3=BC=C3=9F\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:value =C3=A4=C3=B6=C3=BC=C3=9F\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-16:=FE=FF=00v=00a=00l=00u=00e=00 =00=E4=00=F6=00=FC=00=DF\r\n" + "PROP;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:value =C3=A4=C3=B6=C3=BC=C3=9F\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 18 with SyntaxStyle

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

the class VObjectWriterTest method writeVersion.

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

Example 19 with SyntaxStyle

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

the class VObjectWriterTest method parameters_invalid_characters_in_value.

/**
 * When there are invalid characters in a parameter value.
 */
@Test
public void parameters_invalid_characters_in_value() throws Exception {
    SyntaxStyle style = SyntaxStyle.OLD;
    {
        for (boolean caretEncoding : new boolean[] { false, true }) {
            for (char c : ":\n\r".toCharArray()) {
                StringWriter sw = new StringWriter();
                VObjectWriter writer = new VObjectWriter(sw, style);
                writer.setCaretEncodingEnabled(caretEncoding);
                VObjectParameters parameters = new VObjectParameters();
                parameters.put("PARAM", c + "");
                try {
                    writer.writeProperty(null, "PROP", parameters, "");
                    fail("IllegalArgumentException expected when parameter value contains character " + ch(c) + " and caret encoding is " + caretEncoding + " and style is " + style.name());
                } catch (IllegalArgumentException e) {
                // expected
                }
                String actual = sw.toString();
                // @formatter:off
                String expected = "";
                // @formatter:on
                assertEquals(expected, actual);
            }
        }
    }
    style = SyntaxStyle.NEW;
    {
        boolean caretEncoding = false;
        {
            for (char c : "\"\n\r".toCharArray()) {
                StringWriter sw = new StringWriter();
                VObjectWriter writer = new VObjectWriter(sw, style);
                writer.setCaretEncodingEnabled(caretEncoding);
                VObjectParameters parameters = new VObjectParameters();
                parameters.put("PARAM", c + "");
                try {
                    writer.writeProperty(null, "PROP", parameters, "");
                    fail("IllegalArgumentException expected when parameter value contains character " + ch(c) + " and caret encoding is " + caretEncoding + " and style is " + style.name());
                } catch (IllegalArgumentException e) {
                // expected
                }
                String actual = sw.toString();
                // @formatter:off
                String expected = "";
                // @formatter:on
                assertEquals(expected, actual);
            }
        }
        caretEncoding = true;
        {
        // no characters are disallowed
        }
    }
}
Also used : StringWriter(java.io.StringWriter) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) VObjectParameters(com.github.mangstadt.vinnie.VObjectParameters) Test(org.junit.Test)

Example 20 with SyntaxStyle

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

the class VObjectWriterTest method setSyntaxStyle.

@Test
public void setSyntaxStyle() {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        assertEquals(style, writer.getSyntaxStyle());
        for (SyntaxStyle style2 : SyntaxStyle.values()) {
            writer.setSyntaxStyle(style2);
            assertEquals(style2, writer.getSyntaxStyle());
        }
    }
}
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