Search in sources :

Example 6 with SyntaxStyle

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

the class VObjectReaderTest method parameter_value_escaping.

/**
 * Tests the various escaping mechanisms for parameter values.
 */
@Test
public void parameter_value_escaping() throws Exception {
    SyntaxStyle style = SyntaxStyle.OLD;
    {
        // 1: backslash that doesn't escape anything
        // 2: caret-escaped caret
        // 3: caret-escaped newline (lowercase n)
        // 4: caret-escaped newline (uppercase N)
        // 5: caret-escaped double quote
        // 6: caret that doesn't escape anything
        // 7: backslash-escaped semi-colon (must be escaped in 2.1)
        // 8: un-escaped double quote (no special meaning in 2.1)
        // 9: backslash-escaped backslash
        // @formatter:off
        String string = "PROP;PARAM=1\\ 2^^ 3^n 4^N 5^' 6^ 7\\; 8\" 9\\\\:";
        for (boolean caretDecodingEnabled : new boolean[] { false, true }) {
            // caret decoding has no effect in old style
            VObjectReader reader = reader(string, style);
            reader.setCaretDecodingEnabled(caretDecodingEnabled);
            VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
            reader.parse(listener);
            InOrder inorder = inOrder(listener);
            inorder.verify(listener).onProperty(eq(property().name("PROP").param("PARAM", "1\\ 2^^ 3^n 4^N 5^' 6^ 7; 8\" 9\\").value("").build()), any(Context.class));
            // @formatter:off
            String[] lines = string.split("\r\n");
            int line = 0;
            assertContexts(listener, context(lines[line], ++line));
        // @formatter:on
        }
    }
    style = SyntaxStyle.NEW;
    {
        // 1: backslash that doesn't escape anything
        // 2: caret-escaped caret
        // 3: caret-escaped newline (lowercase n)
        // 4: caret-escaped newline (uppercase N)
        // 5: backslash-escaped newline (lowercase n)
        // 7: caret-escaped double quote
        // 8: backslash-escaped backslash
        // 9: caret that doesn't escape anything
        // @formatter:off
        String string = "PROP;PARAM=1\\ 2^^ 3^n 4^N 5\\n 7^' 8\\\\ 9^ :";
        // @formatter:on
        Map<Boolean, String> expectedParamValues = new HashMap<Boolean, String>();
        expectedParamValues.put(false, "1\\ 2^^ 3^n 4^N 5\\n 7^' 8\\\\ 9^ ");
        expectedParamValues.put(true, "1\\ 2^ 3" + NEWLINE + " 4^N 5\\n 7\" 8\\\\ 9^ ");
        for (Boolean caretDecodingEnabled : expectedParamValues.keySet()) {
            String expectedParamValue = expectedParamValues.get(caretDecodingEnabled);
            VObjectReader reader = reader(string, style);
            reader.setCaretDecodingEnabled(caretDecodingEnabled);
            VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
            reader.parse(listener);
            InOrder inorder = inOrder(listener);
            inorder.verify(listener).onProperty(eq(property().name("PROP").param("PARAM", expectedParamValue).value("").build()), any(Context.class));
            // @formatter:off
            String[] lines = string.split("\r\n");
            int line = 0;
            assertContexts(listener, context(lines[line], ++line));
        // @formatter:on
        }
    }
}
Also used : InOrder(org.mockito.InOrder) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 7 with SyntaxStyle

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

the class VObjectReaderTest method special_characters_in_group_and_name.

/**
 * When there are special characters in the group and property name.
 */
@Test
public void special_characters_in_group_and_name() throws Exception {
    // @formatter:off
    String string = "g=,\"roup.P.=,\"ROP:value";
    for (SyntaxStyle style : SyntaxStyle.values()) {
        VObjectReader reader = reader(string, style);
        VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
        reader.parse(listener);
        InOrder inorder = inOrder(listener);
        inorder.verify(listener).onProperty(eq(new VObjectProperty("g=,\"roup", "P.=,\"ROP", "value")), any(Context.class));
        // @formatter:off
        String[] lines = string.split("\r\n");
        int line = 0;
        assertContexts(listener, context(lines[line], ++line));
    // @formatter:on
    }
}
Also used : InOrder(org.mockito.InOrder) VObjectProperty(com.github.mangstadt.vinnie.VObjectProperty) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) Test(org.junit.Test)

Example 8 with SyntaxStyle

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

the class VObjectReaderTest method parameter_values_in_double_quotes.

/**
 * New style syntax lets you surround parameter values in double quotes.
 * Doing this lets you put special characters like semi-colons in the
 * property value.
 */
@Test
public void parameter_values_in_double_quotes() throws Exception {
    // @formatter:off
    String string = "PROP;PARAM=\"a;b:c,d\":value";
    // @formatter:on
    Map<SyntaxStyle, VObjectProperty> styleToProperty = new HashMap<SyntaxStyle, VObjectProperty>();
    styleToProperty.put(SyntaxStyle.OLD, property().name("PROP").param("PARAM", "\"a").param(null, "b").value("c,d\":value").build());
    styleToProperty.put(SyntaxStyle.NEW, property().name("PROP").param("PARAM", "a;b:c,d").value("value").build());
    for (SyntaxStyle style : styleToProperty.keySet()) {
        VObjectProperty expectedProperty = styleToProperty.get(style);
        VObjectReader reader = reader(string, style);
        VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
        reader.parse(listener);
        InOrder inorder = inOrder(listener);
        inorder.verify(listener).onProperty(eq(expectedProperty), any(Context.class));
        // @formatter:off
        String[] lines = string.split("\r\n");
        int line = 0;
        assertContexts(listener, context(lines[line], ++line));
    // @formatter:on
    }
}
Also used : VObjectProperty(com.github.mangstadt.vinnie.VObjectProperty) InOrder(org.mockito.InOrder) HashMap(java.util.HashMap) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) Test(org.junit.Test)

Example 9 with SyntaxStyle

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

the class VObjectReaderTest method empty_lines.

/**
 * Empty lines should be ignored.
 */
@Test
public void empty_lines() throws Exception {
    // @formatter:off
    String string = "PROP1:value1\r\n" + "\r\n" + "PROP2:value2\r\n" + "\r\n" + "\n" + "\r" + "PROP3:value3";
    for (SyntaxStyle style : SyntaxStyle.values()) {
        VObjectReader reader = reader(string, style);
        VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
        reader.parse(listener);
        InOrder inorder = inOrder(listener);
        inorder.verify(listener).onProperty(eq(property().name("PROP1").value("value1").build()), any(Context.class));
        inorder.verify(listener).onProperty(eq(property().name("PROP2").value("value2").build()), any(Context.class));
        inorder.verify(listener).onProperty(eq(property().name("PROP3").value("value3").build()), any(Context.class));
        // @formatter:off
        assertContexts(listener, context("PROP1:value1", 1), context("PROP2:value2", 3), context("PROP3:value3", 7));
    // @formatter:on
    }
}
Also used : InOrder(org.mockito.InOrder) SyntaxStyle(com.github.mangstadt.vinnie.SyntaxStyle) Test(org.junit.Test)

Example 10 with SyntaxStyle

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

the class VObjectReaderTest method wrong_newlines.

/**
 * Incorrect newline sequences should be accepted.
 */
@Test
public void wrong_newlines() throws Exception {
    // @formatter:off
    String string = "PROP1:value1\r" + "PROP2:value2\n" + "PROP3:value3";
    for (SyntaxStyle style : SyntaxStyle.values()) {
        VObjectReader reader = reader(string, style);
        VObjectDataListenerMock listener = spy(new VObjectDataListenerMock());
        reader.parse(listener);
        InOrder inorder = inOrder(listener);
        inorder.verify(listener).onProperty(eq(property().name("PROP1").value("value1").build()), any(Context.class));
        inorder.verify(listener).onProperty(eq(property().name("PROP2").value("value2").build()), any(Context.class));
        inorder.verify(listener).onProperty(eq(property().name("PROP3").value("value3").build()), any(Context.class));
        // @formatter:off
        int line = 0;
        assertContexts(listener, context("PROP1:value1", ++line), context("PROP2:value2", ++line), context("PROP3:value3", ++line));
    // @formatter:on
    }
}
Also used : InOrder(org.mockito.InOrder) 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