1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package examples; 15 16 import java.util.List; 17 import java.util.Map; 18 import junit.framework.TestCase; 19 import org.yaml.snakeyaml.Util; 20 import org.yaml.snakeyaml.Yaml; 21 22 public class AnyObjectExampleTest extends TestCase { 23 24 @SuppressWarnings("unchecked") testLoad()25 public void testLoad() { 26 String doc = Util.getLocalResource("examples/any-object-example.yaml"); 27 Yaml yaml = new Yaml(); 28 Map<String, Object> object = yaml.load(doc); 29 assertEquals(6, object.size()); 30 assertEquals("[null, null]", object.get("none").toString()); 31 List<?> list1 = (List<?>) object.get("none"); 32 assertEquals(2, list1.size()); 33 for (Object object2 : list1) { 34 assertNull(object2); 35 } 36 // 37 assertEquals("[true, false, true, false]", object.get("bool").toString()); 38 assertEquals(4, ((List<?>) object.get("bool")).size()); 39 // 40 assertEquals(Integer.valueOf(42), object.get("int")); 41 assertEquals(Double.valueOf(3.14159), object.get("float")); 42 // 43 assertEquals("[LITE, RES_ACID, SUS_DEXT]", object.get("list").toString()); 44 List<?> list2 = (List<?>) object.get("list"); 45 assertEquals(3, list2.size()); 46 for (Object object2 : list2) { 47 assertEquals(object2.toString(), object2.toString().toUpperCase()); 48 } 49 // 50 assertEquals("{hp=13, sp=5}", object.get("dict").toString()); 51 Map<String, Integer> map = (Map<String, Integer>) object.get("dict"); 52 assertEquals(2, map.keySet().size()); 53 assertEquals(Integer.valueOf(13), map.get("hp")); 54 assertEquals(Integer.valueOf(5), map.get("sp")); 55 } 56 } 57