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.List;
19  import java.util.Map;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * Test Chapter 2.2 from the YAML specification
25   * 
26   * @author py4fun
27   * @see http://yaml.org/spec/1.1/
28   */
29  public class Chapter2_2Test extends TestCase {
30  
31      @SuppressWarnings("unchecked")
32      public void testExample_2_7() {
33          YamlStream resource = new YamlStream("example2_7.yaml");
34          List<Object> list = (List<Object>) resource.getNativeData();
35          assertEquals(2, list.size());
36          List<String> list1 = (List<String>) list.get(0);
37          assertEquals(3, list1.size());
38          assertEquals("Mark McGwire", list1.get(0));
39          assertEquals("Sammy Sosa", list1.get(1));
40          assertEquals("Ken Griffey", list1.get(2));
41          List<String> list2 = (List<String>) list.get(1);
42          assertEquals(2, list2.size());
43          assertEquals("Chicago Cubs", list2.get(0));
44          assertEquals("St Louis Cardinals", list2.get(1));
45      }
46  
47      @SuppressWarnings("unchecked")
48      public void testExample_2_8() {
49          YamlStream resource = new YamlStream("example2_8.yaml");
50          List<Object> list = (List<Object>) resource.getNativeData();
51          assertEquals(2, list.size());
52          Map<String, String> map1 = (Map<String, String>) list.get(0);
53          assertEquals(3, map1.size());
54          assertEquals(new Integer(72200), map1.get("time"));
55          assertEquals("Sammy Sosa", map1.get("player"));
56          assertEquals("strike (miss)", map1.get("action"));
57          Map<String, String> map2 = (Map<String, String>) list.get(1);
58          assertEquals(3, map2.size());
59          assertEquals(new Integer(72227), map2.get("time"));
60          assertEquals("Sammy Sosa", map2.get("player"));
61          assertEquals("grand slam", map2.get("action"));
62      }
63  
64      @SuppressWarnings("unchecked")
65      public void testExample_2_9() {
66          YamlDocument document = new YamlDocument("example2_9.yaml");
67          Map<String, Object> map = (Map<String, Object>) document.getNativeData();
68          assertEquals(map.toString(), 2, map.size());
69          List<String> list1 = (List<String>) map.get("hr");
70          assertEquals(2, list1.size());
71          assertEquals("Mark McGwire", list1.get(0));
72          assertEquals("Sammy Sosa", list1.get(1));
73          List<String> list2 = (List<String>) map.get("rbi");
74          assertEquals(2, list2.size());
75          assertEquals("Sammy Sosa", list2.get(0));
76          assertEquals("Ken Griffey", list2.get(1));
77      }
78  
79      @SuppressWarnings("unchecked")
80      public void testExample_2_10() {
81          YamlDocument document = new YamlDocument("example2_10.yaml");
82          Map<String, Object> map = (Map<String, Object>) document.getNativeData();
83          assertEquals("Examples 2.9 and 2.10 must be identical.",
84                  new YamlDocument("example2_9.yaml").getNativeData(), map);
85      }
86  
87      @SuppressWarnings("unchecked")
88      public void testExample_2_11() {
89          YamlDocument document = new YamlDocument("example2_11.yaml");
90          Map<Object, Object> map = (Map<Object, Object>) document.getNativeData();
91          assertEquals(2, map.size());
92          for (Object key : map.keySet()) {
93              List<String> list = (List<String>) key;
94              assertEquals(2, list.size());
95          }
96      }
97  
98      public void testExample_2_12() {
99          YamlDocument document = new YamlDocument("example2_12.yaml");
100         @SuppressWarnings("unchecked")
101         List<Map<Object, Object>> list = (List<Map<Object, Object>>) document.getNativeData();
102         assertEquals(3, list.size());
103         Map<Object, Object> map1 = (Map<Object, Object>) list.get(0);
104         assertEquals(2, map1.size());
105         assertEquals("Super Hoop", map1.get("item"));
106         Map<Object, Object> map2 = (Map<Object, Object>) list.get(1);
107         assertEquals(2, map2.size());
108         assertEquals("Basketball", map2.get("item"));
109         Map<Object, Object> map3 = (Map<Object, Object>) list.get(2);
110         assertEquals(2, map3.size());
111         assertEquals("Big Shoes", map3.get("item"));
112     }
113 }