Search in sources :

Example 1 with VObjectParameters

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

the class VObjectWriterTest method parameters_multivalued_empty_values.

/**
 * When the parameters multimap has a key with an empty list, the parameter
 * should not be written. This should never happen if the user sticks to the
 * API of the VObjectParameters class and does not modify the backing map
 * manually.
 */
@Test
public void parameters_multivalued_empty_values() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        for (boolean caretEncoding : new boolean[] { false, true }) {
            StringWriter sw = new StringWriter();
            VObjectWriter writer = new VObjectWriter(sw, style);
            writer.setCaretEncodingEnabled(caretEncoding);
            VObjectParameters parameters = new VObjectParameters();
            parameters.getMap().put("PARAM", new ArrayList<String>());
            writer.writeProperty(null, "PROP", parameters, "");
            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 2 with VObjectParameters

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

the class VObjectWriterTest method property_name.

@Test
public void property_name() throws Exception {
    for (SyntaxStyle style : SyntaxStyle.values()) {
        StringWriter sw = new StringWriter();
        VObjectWriter writer = new VObjectWriter(sw, style);
        writer.writeProperty(null, "PROP", new VObjectParameters(), "");
        try {
            writer.writeProperty(null, "", new VObjectParameters(), "");
            fail("IllegalArgumentException expected when property name is empty and style is " + style.name());
        } catch (IllegalArgumentException e) {
        // expected
        }
        try {
            writer.writeProperty(null, null, new VObjectParameters(), "");
            fail("NullPointerException expected when property name is null and style is " + style.name());
        } catch (NullPointerException e) {
        // expected
        }
        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 3 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters 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 4 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters 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 5 with VObjectParameters

use of com.github.mangstadt.vinnie.VObjectParameters 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)

Aggregations

VObjectParameters (com.github.mangstadt.vinnie.VObjectParameters)18 StringWriter (java.io.StringWriter)16 SyntaxStyle (com.github.mangstadt.vinnie.SyntaxStyle)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)2 VCard (ezvcard.VCard)1 VCardVersion (ezvcard.VCardVersion)1 EmbeddedVCardException (ezvcard.io.EmbeddedVCardException)1 SkipMeException (ezvcard.io.SkipMeException)1 VCardPropertyScribe (ezvcard.io.scribe.VCardPropertyScribe)1 VCardParameters (ezvcard.parameter.VCardParameters)1 VCardProperty (ezvcard.property.VCardProperty)1 IOException (java.io.IOException)1