aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/extensions/compactnotation/CompactConstructorExampleTest.java
blob: d8e355c0dbfbb6f7e057286cab0380938d23d064 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * Copyright (c) 2008, http://www.snakeyaml.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.yaml.snakeyaml.extensions.compactnotation;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

public class CompactConstructorExampleTest extends TestCase {

    private Object load(String fileName) {
        CompactConstructor compact = new CompactConstructor();
        Yaml yaml = new Yaml(compact);
        String doc = Util.getLocalResource("compactnotation/" + fileName);
        Object obj = yaml.load(doc);
        assertNotNull(obj);
        return obj;
    }

    public void test1() {
        Object obj = load("example1.yaml");
        assertEquals(new Container(), obj);
    }

    public void test2() {
        Object obj = load("example2.yaml");
        assertEquals(new Container("title"), obj);
    }

    public void test3() {
        Container obj = (Container) load("example3.yaml");
        assertEquals(new Container("title3"), obj);
        assertEquals("title3", obj.getTitle());
        assertEquals("parent", obj.getName());
        assertEquals("123", obj.getId());
    }

    public void test4() {
        Object obj = load("example4.yaml");
        // System.out.println(obj);
        Container container = (Container) obj;
        assertNotNull(obj);
        assertEquals(new Container("title4"), obj);
        assertEquals("title4", container.getTitle());
        assertEquals("child4", container.getName());
        assertEquals("444", container.getId());
    }

    public void test5() {
        Object obj = load("example5.yaml");
        // System.out.println(obj);
        Container container = (Container) obj;
        assertNotNull(obj);
        assertEquals(new Container("title4"), obj);
        assertEquals("title4", container.getTitle());
        assertEquals("child5", container.getName());
        assertEquals("ID555", container.getId());
    }

    public void test6() {
        Object obj = load("example6.yaml");
        // System.out.println(obj);
        Container container = (Container) obj;
        assertNotNull(obj);
        assertEquals(new Container("title4"), obj);
        assertEquals("title4", container.getTitle());
        assertEquals("child6", container.getName());
        assertEquals("ID6", container.getId());
    }

    public void test7() {
        Object obj = load("example7.yaml");
        // System.out.println(obj);
        Container container = (Container) obj;
        assertNotNull(obj);
        assertEquals(new Container("The title"), obj);
        assertEquals("The title", container.getTitle());
        assertEquals("child7", container.getName());
        assertEquals("id7", container.getId());
    }

    @SuppressWarnings("unchecked")
    // TODO it is unclear how the result should look like for CON
    public void test9() {
        Map<String, Object> map = (Map<String, Object>) load("example9.yaml");
        assertEquals(1, map.size());
        Map<Container, Map<String, String>> containers = (Map<Container, Map<String, String>>) map
                .get("something");
        // System.out.println(obj);
        assertEquals(2, containers.size());
        for (Container c : containers.keySet()) {
            assertTrue(c.getId().matches("id\\d"));
            assertEquals(1, containers.get(c).size());
        }
    }

    @SuppressWarnings("unchecked")
    public void test10() {
        Map<String, Object> map = (Map<String, Object>) load("example10.yaml");
        assertEquals(1, map.size());
        List<Container> containers = (List<Container>) map.get("something");
        // System.out.println(obj);
        assertEquals(3, containers.size());
        for (Container c : containers) {
            assertTrue(c.toString(), c.getId().matches("id\\d+"));
            assertTrue(c.toString(), c.getName().matches("child\\d+"));
            // System.out.println(c);
        }
    }

    public void test11withoutPackageNames() {
        Constructor compact = new PackageCompactConstructor(
                "org.yaml.snakeyaml.extensions.compactnotation");
        Yaml yaml = new Yaml(compact);
        String doc = Util.getLocalResource("compactnotation/example11.yaml");
        Box box = (Box) yaml.load(doc);
        assertNotNull(box);
        assertEquals("id11", box.getId());
        assertEquals("Main box", box.getName());
        Item top = box.getTop();
        assertEquals("id003", top.getId());
        assertEquals("25.0", top.getPrice());
        assertEquals("parrot", top.getName());
        Item bottom = box.getBottom();
        assertEquals("id004", bottom.getId());
        assertEquals("3.5", bottom.getPrice());
        assertEquals("sweet", bottom.getName());
    }

    public void test12withList() {
        Constructor compact = new TableCompactConstructor(
                "org.yaml.snakeyaml.extensions.compactnotation");
        Yaml yaml = new Yaml(compact);
        String doc = Util.getLocalResource("compactnotation/example12.yaml");
        Table table = (Table) yaml.load(doc);
        assertNotNull(table);
        assertEquals("id12", table.getId());
        assertEquals("A table", table.getName());
        List<Row> rows = table.getRows();
        assertEquals(3, rows.size());
        Iterator<Row> iter = rows.iterator();
        Row first = iter.next();
        assertEquals("id111", first.getId());
        assertEquals("I think; therefore I am.", first.getDescription());
        assertEquals(0.125, first.getRatio(), 0.000000001);
        assertEquals(15, first.getSize());
        Row second = iter.next();
        assertEquals("id222", second.getId());
        assertEquals("We do not need new lines here, just replace them all with spaces\n",
                second.getDescription());
        assertEquals(0.333, second.getRatio(), 0.000000001);
        assertEquals(17, second.getSize());
        Row third = iter.next();
        assertEquals("id333", third.getId());
        assertEquals(
                "Please preserve all\nthe lines because they may be\nimportant, but do not include the last one !!!",
                third.getDescription());
        assertEquals(0.88, third.getRatio(), 0.000000001);
        assertEquals(52, third.getSize());
    }
}