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.constructor; 17 18 import java.util.Date; 19 import java.util.List; 20 import java.util.Map; 21 import java.util.TreeMap; 22 23 import junit.framework.TestCase; 24 25 import org.yaml.snakeyaml.TypeDescription; 26 import org.yaml.snakeyaml.Util; 27 import org.yaml.snakeyaml.Yaml; 28 import org.yaml.snakeyaml.nodes.Tag; 29 import org.yaml.snakeyaml.representer.Representer; 30 31 public class TypeSafeCollectionsTest extends TestCase { 32 33 public void testTypeSafeList() { 34 Constructor constructor = new Constructor(Car.class); 35 TypeDescription carDescription = new TypeDescription(Car.class); 36 carDescription.putListPropertyType("wheels", Wheel.class); 37 constructor.addTypeDescription(carDescription); 38 Yaml yaml = new Yaml(constructor); 39 Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-no-root-class.yaml")); 40 assertEquals("12-XP-F4", car.getPlate()); 41 List<Wheel> wheels = car.getWheels(); 42 assertNotNull(wheels); 43 assertEquals(5, wheels.size()); 44 for (Wheel wheel : wheels) { 45 assertTrue(wheel.getId() > 0); 46 } 47 } 48 49 public void testTypeSafeMap() { 50 Constructor constructor = new Constructor(MyCar.class); 51 TypeDescription carDescription = new TypeDescription(MyCar.class); 52 carDescription.putMapPropertyType("wheels", MyWheel.class, Object.class); 53 constructor.addTypeDescription(carDescription); 54 Yaml yaml = new Yaml(constructor); 55 MyCar car = (MyCar) yaml.load(Util 56 .getLocalResource("constructor/car-no-root-class-map.yaml")); 57 assertEquals("00-FF-Q2", car.getPlate()); 58 Map<MyWheel, Date> wheels = car.getWheels(); 59 assertNotNull(wheels); 60 assertEquals(5, wheels.size()); 61 for (MyWheel wheel : wheels.keySet()) { 62 assertTrue(wheel.getId() > 0); 63 Date date = wheels.get(wheel); 64 long time = date.getTime(); 65 assertTrue("It must be midnight.", time % 10000 == 0); 66 } 67 } 68 69 public void testWithGlobalTag() { 70 Map<MyWheel, Date> wheels = new TreeMap<MyWheel, Date>(); 71 long time = 1248212168084L; 72 for (int i = 1; i < 6; i++) { 73 MyWheel mw = new MyWheel(); 74 mw.setId(i); 75 mw.setBrand(mw.getBrand() + String.valueOf(i)); 76 wheels.put(mw, new Date(time + i)); 77 } 78 MyCar c = new MyCar(); 79 c.setPlate("00-FF-Q2"); 80 c.setWheels(wheels); 81 Representer representer = new Representer(); 82 representer.addClassTag(MyWheel.class, Tag.MAP); 83 Yaml yaml = new Yaml(representer); 84 String output = yaml.dump(c); 85 assertEquals(Util.getLocalResource("javabeans/mycar-with-global-tag1.yaml"), output); 86 // load 87 Yaml beanLoader = new Yaml(); 88 MyCar car = beanLoader.loadAs(output, MyCar.class); 89 assertNotNull(car); 90 assertEquals("00-FF-Q2", car.getPlate()); 91 assertEquals(5, car.getWheels().size()); 92 for (Date d : car.getWheels().values()) { 93 // give a day for any timezone 94 assertTrue(d.before(new Date(time + 1000 * 60 * 60 * 24))); 95 assertTrue(d.after(new Date(time))); 96 } 97 Object wheel = car.getWheels().keySet().iterator().next(); 98 assertTrue(wheel instanceof MyWheel); 99 MyWheel w = (MyWheel) wheel; 100 assertEquals(1, w.getId()); 101 assertEquals("Pirelli1", w.getBrand()); 102 } 103 } 104