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