1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Calendar;
22 import java.util.Date;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TimeZone;
28
29 import junit.framework.TestCase;
30
31 import org.yaml.snakeyaml.constructor.AbstractConstruct;
32 import org.yaml.snakeyaml.constructor.Constructor;
33 import org.yaml.snakeyaml.nodes.Node;
34 import org.yaml.snakeyaml.nodes.ScalarNode;
35 import org.yaml.snakeyaml.nodes.Tag;
36
37
38
39
40
41
42
43 public class Chapter2_4Test extends TestCase {
44
45 @SuppressWarnings("unchecked")
46 public void testExample_2_19() {
47 YamlDocument document = new YamlDocument("example2_19.yaml");
48 Map<String, Object> map = (Map<String, Object>) document.getNativeData();
49 assertEquals(5, map.size());
50 assertEquals("Expect 12345 to be an Integer.", Integer.class, map.get("canonical")
51 .getClass());
52 assertEquals(new Integer(12345), map.get("canonical"));
53 assertEquals(new Integer(12345), map.get("decimal"));
54 assertEquals(new Integer(3 * 3600 + 25 * 60 + 45), map.get("sexagesimal"));
55 assertEquals(new Integer(014), map.get("octal"));
56 assertEquals(new Integer(0xC), map.get("hexadecimal"));
57 }
58
59 @SuppressWarnings("unchecked")
60 public void testExample_2_20() {
61 YamlDocument document = new YamlDocument("example2_20.yaml");
62 Map<String, Object> map = (Map<String, Object>) document.getNativeData();
63 assertEquals(6, map.size());
64 assertEquals("Expect '1.23015e+3' to be a Double.", Double.class, map.get("canonical")
65 .getClass());
66 assertEquals(new Double(1230.15), map.get("canonical"));
67 assertEquals(new Double(12.3015e+02), map.get("exponential"));
68 assertEquals(new Double(20 * 60 + 30.15), map.get("sexagesimal"));
69 assertEquals(new Double(1230.15), map.get("fixed"));
70 assertEquals(Double.NEGATIVE_INFINITY, map.get("negative infinity"));
71 assertEquals(Double.NaN, map.get("not a number"));
72 }
73
74 @SuppressWarnings("unchecked")
75 public void testExample_2_21() {
76 YamlDocument document = new YamlDocument("example2_21.yaml");
77 Map<String, Object> map = (Map<String, Object>) document.getNativeData();
78 assertEquals(4, map.size());
79 assertNull("'~' must be parsed as 'null': " + map.get(null), map.get(null));
80 assertTrue((Boolean) map.get(Boolean.TRUE));
81 assertFalse((Boolean) map.get(Boolean.FALSE));
82 assertEquals("12345", map.get("string"));
83 }
84
85 @SuppressWarnings("unchecked")
86 public void testExample_2_22() {
87 YamlDocument document = new YamlDocument("example2_22.yaml");
88 Map<String, Object> map = (Map<String, Object>) document.getNativeData();
89 assertEquals(4, map.size());
90 assertEquals("Expect '2001-12-15T02:59:43.1Z' to be a Date.", Date.class,
91 map.get("canonical").getClass());
92 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
93 cal.clear();
94 cal.set(Calendar.YEAR, 2001);
95 cal.set(Calendar.MONTH, 11);
96 cal.set(Calendar.DAY_OF_MONTH, 15);
97 cal.set(Calendar.HOUR_OF_DAY, 2);
98 cal.set(Calendar.MINUTE, 59);
99 cal.set(Calendar.SECOND, 43);
100 cal.set(Calendar.MILLISECOND, 100);
101 Date date = cal.getTime();
102 assertEquals(date, map.get("canonical"));
103 assertEquals("Expect '2001-12-14t21:59:43.10-05:00' to be a Date.", Date.class,
104 map.get("iso8601").getClass());
105 assertEquals("Expect '2001-12-14 21:59:43.10 -5' to be a Date.", Date.class,
106 map.get("spaced").getClass());
107 assertEquals("Expect '2002-12-14' to be a Date.", Date.class, map.get("date").getClass());
108 }
109
110 @SuppressWarnings("unchecked")
111 public void testExample_2_23_non_date() {
112 try {
113 YamlDocument document = new YamlDocument("example2_23_non_date.yaml");
114 Map<String, Object> map = (Map<String, Object>) document.getNativeData();
115 assertEquals(1, map.size());
116 assertEquals("2002-04-28", map.get("not-date"));
117 } catch (RuntimeException e) {
118 fail("Cannot parse '!!str': 'not-date: !!str 2002-04-28'");
119 }
120 }
121
122 @SuppressWarnings("unchecked")
123 public void testExample_2_23_picture() throws Exception {
124 YamlDocument document = new YamlDocument("example2_23_picture.yaml", false);
125 Map<String, Object> map = (Map<String, Object>) document.getNativeData();
126 assertEquals(1, map.size());
127 byte[] picture = (byte[]) map.get("picture");
128 assertEquals((byte) 'G', picture[0]);
129 assertEquals((byte) 'I', picture[1]);
130 assertEquals((byte) 'F', picture[2]);
131 }
132
133 class SomethingConstructor extends Constructor {
134 public SomethingConstructor() {
135 this.yamlConstructors.put(new Tag("!something"), new ConstructSomething());
136 }
137
138 private class ConstructSomething extends AbstractConstruct {
139 public Object construct(Node node) {
140
141 String val = (String) constructScalar((ScalarNode) node);
142 return val.toUpperCase().replace('\n', ' ').trim();
143 }
144 }
145 }
146
147 @SuppressWarnings("unchecked")
148 public void testExample_2_23() throws IOException {
149 YamlDocument document = new YamlDocument("example2_23.yaml", false,
150 new SomethingConstructor());
151 Map<String, Object> map = (Map<String, Object>) document.getNativeData();
152 assertEquals(3, map.size());
153 String special = (String) map.get("application specific tag");
154 assertEquals("THE SEMANTICS OF THE TAG ABOVE MAY BE DIFFERENT FOR DIFFERENT DOCUMENTS.",
155 special);
156 }
157
158 @SuppressWarnings("unchecked")
159 public void testExample_2_25() {
160 YamlDocument document = new YamlDocument("example2_25.yaml");
161 Set<String> set = (Set<String>) document.getNativeData();
162 assertEquals(3, set.size());
163 assertTrue(set.contains("Mark McGwire"));
164 assertTrue(set.contains("Sammy Sosa"));
165 assertTrue(set.contains("Ken Griff"));
166 }
167
168 @SuppressWarnings("unchecked")
169 public void testExample_2_26() {
170 YamlDocument document = new YamlDocument("example2_26.yaml");
171 Map<String, String> map = (Map<String, String>) document.getNativeData();
172 assertEquals(3, map.size());
173 assertTrue(map instanceof LinkedHashMap);
174 assertEquals(new Integer(65), map.get("Mark McGwire"));
175 assertEquals(new Integer(63), map.get("Sammy Sosa"));
176 assertEquals(new Integer(58), map.get("Ken Griffy"));
177 List<String> list = new ArrayList<String>();
178 for (String key : map.keySet()) {
179 list.add(key);
180 }
181 assertEquals("Mark McGwire", list.get(0));
182 assertEquals("Sammy Sosa", list.get(1));
183 assertEquals("Ken Griffy", list.get(2));
184 }
185 }