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.1 from the YAML specification
25   * 
26   * @author py4fun
27   * @see http://yaml.org/spec/1.1/
28   */
29  public class Chapter2_1Test extends TestCase {
30  
31      @SuppressWarnings("unchecked")
32      public void testExample_2_1() {
33          YamlDocument document = new YamlDocument("example2_1.yaml");
34          List<String> list = (List<String>) document.getNativeData();
35          assertEquals(3, list.size());
36          assertEquals("Mark McGwire", list.get(0));
37          assertEquals("Sammy Sosa", list.get(1));
38          assertEquals("Ken Griffey", list.get(2));
39          assertEquals("[Mark McGwire, Sammy Sosa, Ken Griffey]\n", document.getPresentation());
40      }
41  
42      @SuppressWarnings("unchecked")
43      public void testExample_2_2() {
44          YamlDocument document = new YamlDocument("example2_2.yaml");
45          Map<String, Object> map = (Map<String, Object>) document.getNativeData();
46          assertEquals(3, map.size());
47          assertEquals("Expect 65 to be a Integer", Integer.class, map.get("hr").getClass());
48          assertEquals(new Integer(65), map.get("hr"));
49          assertEquals(new Float(0.278), new Float("0.278"));
50          assertEquals("Expect 0.278 to be a Float", Double.class, map.get("avg").getClass());
51          assertEquals(new Double(0.278), map.get("avg"));
52          assertEquals("Expect 147 to be an Integer", Integer.class, map.get("rbi").getClass());
53          assertEquals(new Integer(147), map.get("rbi"));
54      }
55  
56      @SuppressWarnings("unchecked")
57      public void testExample_2_3() {
58          YamlDocument document = new YamlDocument("example2_3.yaml");
59          Map<String, List<String>> map = (Map<String, List<String>>) document.getNativeData();
60          assertEquals(2, map.size());
61          List<String> list1 = map.get("american");
62          assertEquals(3, list1.size());
63          assertEquals("Boston Red Sox", list1.get(0));
64          assertEquals("Detroit Tigers", list1.get(1));
65          assertEquals("New York Yankees", list1.get(2));
66          List<String> list2 = map.get("national");
67          assertEquals(3, list2.size());
68          assertEquals("New York Mets", list2.get(0));
69          assertEquals("Chicago Cubs", list2.get(1));
70          assertEquals("Atlanta Braves", list2.get(2));
71      }
72  
73      @SuppressWarnings("unchecked")
74      public void testExample_2_4() {
75          YamlDocument document = new YamlDocument("example2_4.yaml");
76          List<Map<String, Object>> list = (List<Map<String, Object>>) document.getNativeData();
77          assertEquals(2, list.size());
78          Map<String, Object> map1 = list.get(0);
79          assertEquals(3, map1.size());
80          assertEquals("Mark McGwire", map1.get("name"));
81      }
82  
83      @SuppressWarnings("unchecked")
84      public void testExample_2_5() {
85          YamlDocument document = new YamlDocument("example2_5.yaml");
86          List<List<Object>> list = (List<List<Object>>) document.getNativeData();
87          assertEquals(3, list.size());
88          List<Object> list1 = list.get(0);
89          assertEquals(3, list1.size());
90          assertEquals("name", list1.get(0));
91          assertEquals("hr", list1.get(1));
92          assertEquals("avg", list1.get(2));
93          assertEquals(3, list.get(1).size());
94          assertEquals(3, list.get(2).size());
95      }
96  
97      @SuppressWarnings("unchecked")
98      public void testExample_2_6() {
99          YamlDocument document = new YamlDocument("example2_6.yaml");
100         Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) document
101                 .getNativeData();
102         assertEquals(2, map.size());
103         Map<String, Object> map1 = map.get("Mark McGwire");
104         assertEquals(2, map1.size());
105         Map<String, Object> map2 = map.get("Sammy Sosa");
106         assertEquals(2, map2.size());
107     }
108 }