aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/config/ini/IniTest.java
blob: e9eb4011044fd26d0a81e1cf17fc7a152a47c927 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.ini;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author mpolden
 */
class IniTest {

    @Test
    public void parse() {
        String example = """
                key1 = no section
                []
                key2 = also no section  ; in-line comment
                ; a comment
                # another comment

                [foo]
                key3 =   "with spaces; and an escaped quote: \\"  "    # in-line comment
                key4 = \\"single leading escaped quote
                key1 =    leading whitespace unquoted
                key2 = "   leading whitespace quoted"
                key6 =

                [bar]
                key1=in section

                [foo]
                key5 = quote \\" in the middle
                """;
        Ini ini = parse(example);
        assertEquals(Map.of("", Map.of("key1", "no section",
                                       "key2", "also no section"),
                            "foo", Map.of("key1", "leading whitespace unquoted",
                                          "key2", "   leading whitespace quoted",
                                          "key3", "with spaces; and an escaped quote: \\\"  ",
                                          "key4", "\\\"single leading escaped quote",
                                          "key5", "quote \\\" in the middle",
                                          "key6", ""),
                            "bar", Map.of("key1", "in section")),
                     ini.entries());

        String expected = """
                key1 = no section
                key2 = also no section
                
                [bar]
                key1 = in section
                
                [foo]
                key1 = leading whitespace unquoted
                key2 = "   leading whitespace quoted"
                key3 = "with spaces; and an escaped quote: \\"  "
                key4 = "\\"single leading escaped quote"
                key5 = "quote \\" in the middle"
                key6 = ""
                """;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ini.write(out);
        String serialized = out.toString(StandardCharsets.UTF_8);
        assertEquals(expected, serialized);
        assertEquals(ini, parse(serialized));
    }

    @Test
    public void parse_invalid() {
        var tests = Map.of("key1\n",
                           "Invalid entry on line 1: 'key1': Expected key=[value]",

                           "key0 = ok\nkey1 = \"foo bar\" trailing stuff\n",
                           "Invalid entry on line 2: 'key1 = \"foo bar\" trailing stuff': Additional character(s) after end quote at column 8",

                           "[section1]\nkey0=foo\nkey0=bar\n",
                           "Invalid entry on line 3: 'key0=bar': Key 'key0' duplicated in section 'section1'",

                           "key1 = \"foo",
                           "Invalid entry on line 1: 'key1 = \"foo': Missing closing quote");
        tests.forEach((input, errorMessage) -> {
            try {
                parse(input);
                fail("Expected exception for input '" + input + "'");
            } catch (IllegalArgumentException e) {
                assertEquals(errorMessage, e.getMessage());
            }
        });
    }

    private static Ini parse(String ini) {
        return Ini.parse(new ByteArrayInputStream(ini.getBytes(StandardCharsets.UTF_8)));
    }

}