View Javadoc

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