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