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.List;
21
22 import junit.framework.TestCase;
23
24 import org.yaml.snakeyaml.DumperOptions;
25 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
26 import org.yaml.snakeyaml.TypeDescription;
27 import org.yaml.snakeyaml.Util;
28 import org.yaml.snakeyaml.Yaml;
29
30 public class ArrayTagsTest extends TestCase {
31
32 public void testDefaultRepresenter() throws IOException {
33 CarWithArray car = new CarWithArray();
34 car.setPlate("12-XP-F4");
35 Wheel[] wheels = new Wheel[5];
36 for (int i = 1; i < 6; i++) {
37 Wheel wheel = new Wheel();
38 wheel.setId(i);
39 wheels[i - 1] = wheel;
40 }
41 car.setWheels(wheels);
42 assertEquals(Util.getLocalResource("constructor/cararray-with-tags-flow-auto.yaml"),
43 new Yaml().dump(car));
44 }
45
46 public void testFlowBlock() throws IOException {
47 CarWithArray car = new CarWithArray();
48 car.setPlate("12-XP-F4");
49 Wheel[] wheels = new Wheel[5];
50 for (int i = 1; i < 6; i++) {
51 Wheel wheel = new Wheel();
52 wheel.setId(i);
53 wheels[i - 1] = wheel;
54 }
55 car.setWheels(wheels);
56 DumperOptions options = new DumperOptions();
57 options.setDefaultFlowStyle(FlowStyle.BLOCK);
58 Yaml yaml = new Yaml(options);
59 assertEquals(Util.getLocalResource("constructor/cararray-with-tags.yaml"), yaml.dump(car));
60 }
61
62 public void testLoadClassTag() throws IOException {
63 Constructor constructor = new Constructor();
64 constructor.addTypeDescription(new TypeDescription(Car.class, "!car"));
65 Yaml yaml = new Yaml(constructor);
66 Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-without-tags.yaml"));
67 assertEquals("12-XP-F4", car.getPlate());
68 List<Wheel> wheels = car.getWheels();
69 assertNotNull(wheels);
70 assertEquals(5, wheels.size());
71 }
72
73 public void testNullDescription() throws IOException {
74 Constructor constructor = new Constructor();
75 try {
76 constructor.addTypeDescription(null);
77 fail("Description is required.");
78 } catch (Exception e) {
79 assertEquals("TypeDescription is required.", e.getMessage());
80 }
81 }
82
83 public void testLoadClassNoRoot() throws IOException {
84 Constructor constructor = new Constructor(new TypeDescription(CarWithArray.class));
85 Yaml yaml = new Yaml(constructor);
86 CarWithArray car = (CarWithArray) yaml.load(Util
87 .getLocalResource("constructor/car-no-root-class.yaml"));
88 assertEquals("12-XP-F4", car.getPlate());
89 Wheel[] wheels = car.getWheels();
90 assertNotNull(wheels);
91 assertEquals(5, wheels.length);
92 }
93
94 public static class CarWithArray {
95 private String plate;
96 private Wheel[] wheels;
97
98 public String getPlate() {
99 return plate;
100 }
101
102 public void setPlate(String plate) {
103 this.plate = plate;
104 }
105
106 public Wheel[] getWheels() {
107 return wheels;
108 }
109
110 public void setWheels(Wheel[] wheels) {
111 this.wheels = wheels;
112 }
113 }
114 }