View Javadoc

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