1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package examples;
18
19 import java.io.IOException;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.regex.Pattern;
23
24 import junit.framework.TestCase;
25
26 import org.yaml.snakeyaml.DumperOptions;
27 import org.yaml.snakeyaml.Yaml;
28 import org.yaml.snakeyaml.constructor.AbstractConstruct;
29 import org.yaml.snakeyaml.constructor.SafeConstructor;
30 import org.yaml.snakeyaml.nodes.Node;
31 import org.yaml.snakeyaml.nodes.ScalarNode;
32 import org.yaml.snakeyaml.nodes.Tag;
33 import org.yaml.snakeyaml.representer.Represent;
34 import org.yaml.snakeyaml.representer.Representer;
35
36 public class DiceExampleTest extends TestCase {
37 public void testRepresenter() throws IOException {
38 Dice dice = new Dice(3, 6);
39 DumperOptions options = new DumperOptions();
40 options.setAllowReadOnlyProperties(true);
41 Yaml yaml = new Yaml(options);
42 String output = yaml.dump(dice);
43 assertEquals("!!examples.Dice {a: 3, b: 6}\n", output);
44 }
45
46 public void testDiceRepresenter() throws IOException {
47 Dice dice = new Dice(3, 6);
48 Map<String, Dice> data = new HashMap<String, Dice>();
49 data.put("gold", dice);
50 Yaml yaml = new Yaml(new DiceRepresenter(), new DumperOptions());
51 String output = yaml.dump(data);
52 assertEquals("{gold: !dice '3d6'}\n", output);
53 }
54
55 class DiceRepresenter extends Representer {
56 public DiceRepresenter() {
57 this.representers.put(Dice.class, new RepresentDice());
58 }
59
60 private class RepresentDice implements Represent {
61 public Node representData(Object data) {
62 Dice dice = (Dice) data;
63 String value = dice.getA() + "d" + dice.getB();
64 return representScalar(new Tag("!dice"), value);
65 }
66 }
67 }
68
69 class DiceConstructor extends SafeConstructor {
70 public DiceConstructor() {
71 this.yamlConstructors.put(new Tag("!dice"), new ConstructDice());
72 }
73
74 private class ConstructDice extends AbstractConstruct {
75 public Object construct(Node node) {
76 String val = (String) constructScalar((ScalarNode) node);
77 int position = val.indexOf('d');
78 Integer a = new Integer(val.substring(0, position));
79 Integer b = new Integer(val.substring(position + 1));
80 return new Dice(a, b);
81 }
82 }
83 }
84
85 @SuppressWarnings("unchecked")
86 public void testConstructor() throws IOException {
87 Yaml yaml = new Yaml(new DiceConstructor());
88 Object data = yaml.load("{initial hit points: !dice '8d4'}");
89 Map<String, Dice> map = (Map<String, Dice>) data;
90 assertEquals(new Dice(8, 4), map.get("initial hit points"));
91 }
92
93
94 @SuppressWarnings("unchecked")
95 public void testImplicitResolver() throws IOException {
96 Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
97
98 yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), "123456789");
99
100 Map<String, Dice> treasure = (Map<String, Dice>) new HashMap<String, Dice>();
101 treasure.put("treasure", new Dice(10, 20));
102 String output = yaml.dump(treasure);
103 assertEquals("{treasure: 10d20}\n", output);
104
105 Object data = yaml.load("{damage: 5d10}");
106 Map<String, Dice> map = (Map<String, Dice>) data;
107 assertEquals(new Dice(5, 10), map.get("damage"));
108 }
109
110
111 @SuppressWarnings("unchecked")
112 public void testImplicitResolverWithNull() throws IOException {
113 Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
114
115 yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), null);
116
117 Map<String, Dice> treasure = (Map<String, Dice>) new HashMap<String, Dice>();
118 treasure.put("treasure", new Dice(10, 20));
119 String output = yaml.dump(treasure);
120 assertEquals("{treasure: 10d20}\n", output);
121
122 Object data = yaml.load("{damage: 5d10}");
123 Map<String, Dice> map = (Map<String, Dice>) data;
124 assertEquals(new Dice(5, 10), map.get("damage"));
125 }
126 }