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