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.constructor;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.TypeDescription;
24  import org.yaml.snakeyaml.Util;
25  import org.yaml.snakeyaml.Yaml;
26  import org.yaml.snakeyaml.error.YAMLException;
27  import org.yaml.snakeyaml.nodes.Tag;
28  import org.yaml.snakeyaml.representer.Representer;
29  
30  public class ClassTagsTest extends TestCase {
31  
32      public void testDefaultRepresenter() {
33          Car car = new Car();
34          car.setPlate("12-XP-F4");
35          List<Wheel> wheels = new ArrayList<Wheel>();
36          for (int i = 1; i < 6; i++) {
37              Wheel wheel = new Wheel();
38              wheel.setId(i);
39              wheels.add(wheel);
40          }
41          car.setWheels(wheels);
42          assertEquals(Util.getLocalResource("constructor/car-with-tags.yaml"), new Yaml().dump(car));
43      }
44  
45      public void testDumpClassTag() {
46          Car car = new Car();
47          car.setPlate("12-XP-F4");
48          List<Wheel> wheels = new ArrayList<Wheel>();
49          for (int i = 1; i < 6; i++) {
50              Wheel wheel = new Wheel();
51              wheel.setId(i);
52              wheels.add(wheel);
53          }
54          car.setWheels(wheels);
55          Representer representer = new Representer();
56          representer.addClassTag(Car.class, new Tag("!car"));
57          representer.addClassTag(Wheel.class, Tag.MAP);
58          Yaml yaml = new Yaml(representer);
59          String output = yaml.dump(car);
60          assertEquals(Util.getLocalResource("constructor/car-without-tags.yaml"), output);
61      }
62  
63      public void testLoadUnknounClassTag() {
64          try {
65              Yaml yaml = new Yaml();
66              yaml.load(Util.getLocalResource("constructor/car-without-tags.yaml"));
67              fail("Must fail because of unknown tag: !car");
68          } catch (YAMLException e) {
69              assertTrue(e.getMessage().contains("Invalid tag: !car"));
70          }
71  
72      }
73  
74      public void testLoadClassTag() {
75          Constructor constructor = new Constructor();
76          constructor.addTypeDescription(new TypeDescription(Car.class, "!car"));
77          Yaml yaml = new Yaml(constructor);
78          String source = Util.getLocalResource("constructor/car-without-tags.yaml");
79          Car car = (Car) yaml.load(source);
80          assertEquals("12-XP-F4", car.getPlate());
81          List<Wheel> wheels = car.getWheels();
82          assertNotNull(wheels);
83          assertEquals(5, wheels.size());
84      }
85  
86      public void testNullDescription() {
87          Constructor constructor = new Constructor();
88          try {
89              constructor.addTypeDescription(null);
90              fail("Description is required.");
91          } catch (Exception e) {
92              assertEquals("TypeDescription is required.", e.getMessage());
93          }
94      }
95  
96      public void testLoadClassNoRoot() {
97          Constructor constructor = new Constructor(new TypeDescription(Car.class));
98          Yaml yaml = new Yaml(constructor);
99          Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-no-root-class.yaml"));
100         assertEquals("12-XP-F4", car.getPlate());
101         List<Wheel> wheels = car.getWheels();
102         assertNotNull(wheels);
103         assertEquals(5, wheels.size());
104     }
105 }