1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml.extensions.compactnotation;
17
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21
22 import junit.framework.TestCase;
23
24 import org.yaml.snakeyaml.Util;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.constructor.Constructor;
27
28 public class CompactConstructorExampleTest extends TestCase {
29
30 private Object load(String fileName) {
31 CompactConstructor compact = new CompactConstructor();
32 Yaml yaml = new Yaml(compact);
33 String doc = Util.getLocalResource("compactnotation/" + fileName);
34 Object obj = yaml.load(doc);
35 assertNotNull(obj);
36 return obj;
37 }
38
39 public void test1() {
40 Object obj = load("example1.yaml");
41 assertEquals(new Container(), obj);
42 }
43
44 public void test2() {
45 Object obj = load("example2.yaml");
46 assertEquals(new Container("title"), obj);
47 }
48
49 public void test3() {
50 Container obj = (Container) load("example3.yaml");
51 assertEquals(new Container("title3"), obj);
52 assertEquals("title3", obj.getTitle());
53 assertEquals("parent", obj.getName());
54 assertEquals("123", obj.getId());
55 }
56
57 public void test4() {
58 Object obj = load("example4.yaml");
59
60 Container container = (Container) obj;
61 assertNotNull(obj);
62 assertEquals(new Container("title4"), obj);
63 assertEquals("title4", container.getTitle());
64 assertEquals("child4", container.getName());
65 assertEquals("444", container.getId());
66 }
67
68 public void test5() {
69 Object obj = load("example5.yaml");
70
71 Container container = (Container) obj;
72 assertNotNull(obj);
73 assertEquals(new Container("title4"), obj);
74 assertEquals("title4", container.getTitle());
75 assertEquals("child5", container.getName());
76 assertEquals("ID555", container.getId());
77 }
78
79 public void test6() {
80 Object obj = load("example6.yaml");
81
82 Container container = (Container) obj;
83 assertNotNull(obj);
84 assertEquals(new Container("title4"), obj);
85 assertEquals("title4", container.getTitle());
86 assertEquals("child6", container.getName());
87 assertEquals("ID6", container.getId());
88 }
89
90 public void test7() {
91 Object obj = load("example7.yaml");
92
93 Container container = (Container) obj;
94 assertNotNull(obj);
95 assertEquals(new Container("The title"), obj);
96 assertEquals("The title", container.getTitle());
97 assertEquals("child7", container.getName());
98 assertEquals("id7", container.getId());
99 }
100
101 @SuppressWarnings("unchecked")
102
103 public void test9() {
104 Map<String, Object> map = (Map<String, Object>) load("example9.yaml");
105 assertEquals(1, map.size());
106 Map<Container, Map<String, String>> containers = (Map<Container, Map<String, String>>) map
107 .get("something");
108
109 assertEquals(2, containers.size());
110 for (Container c : containers.keySet()) {
111 assertTrue(c.getId().matches("id\\d"));
112 assertEquals(1, containers.get(c).size());
113 }
114 }
115
116 @SuppressWarnings("unchecked")
117 public void test10() {
118 Map<String, Object> map = (Map<String, Object>) load("example10.yaml");
119 assertEquals(1, map.size());
120 List<Container> containers = (List<Container>) map.get("something");
121
122 assertEquals(3, containers.size());
123 for (Container c : containers) {
124 assertTrue(c.toString(), c.getId().matches("id\\d+"));
125 assertTrue(c.toString(), c.getName().matches("child\\d+"));
126
127 }
128 }
129
130 public void test11withoutPackageNames() {
131 Constructor compact = new PackageCompactConstructor(
132 "org.yaml.snakeyaml.extensions.compactnotation");
133 Yaml yaml = new Yaml(compact);
134 String doc = Util.getLocalResource("compactnotation/example11.yaml");
135 Box box = (Box) yaml.load(doc);
136 assertNotNull(box);
137 assertEquals("id11", box.getId());
138 assertEquals("Main box", box.getName());
139 Item top = box.getTop();
140 assertEquals("id003", top.getId());
141 assertEquals("25.0", top.getPrice());
142 assertEquals("parrot", top.getName());
143 Item bottom = box.getBottom();
144 assertEquals("id004", bottom.getId());
145 assertEquals("3.5", bottom.getPrice());
146 assertEquals("sweet", bottom.getName());
147 }
148
149 public void test12withList() {
150 Constructor compact = new TableCompactConstructor(
151 "org.yaml.snakeyaml.extensions.compactnotation");
152 Yaml yaml = new Yaml(compact);
153 String doc = Util.getLocalResource("compactnotation/example12.yaml");
154 Table table = (Table) yaml.load(doc);
155 assertNotNull(table);
156 assertEquals("id12", table.getId());
157 assertEquals("A table", table.getName());
158 List<Row> rows = table.getRows();
159 assertEquals(3, rows.size());
160 Iterator<Row> iter = rows.iterator();
161 Row first = iter.next();
162 assertEquals("id111", first.getId());
163 assertEquals("I think; therefore I am.", first.getDescription());
164 assertEquals(0.125, first.getRatio(), 0.000000001);
165 assertEquals(15, first.getSize());
166 Row second = iter.next();
167 assertEquals("id222", second.getId());
168 assertEquals("We do not need new lines here, just replace them all with spaces\n",
169 second.getDescription());
170 assertEquals(0.333, second.getRatio(), 0.000000001);
171 assertEquals(17, second.getSize());
172 Row third = iter.next();
173 assertEquals("id333", third.getId());
174 assertEquals(
175 "Please preserve all\nthe lines because they may be\nimportant, but do not include the last one !!!",
176 third.getDescription());
177 assertEquals(0.88, third.getRatio(), 0.000000001);
178 assertEquals(52, third.getSize());
179 }
180 }