xref: /aosp_15_r20/external/snakeyaml/src/test/java/examples/DiceExampleTest.java (revision ac2a7c1bf4e14d82f3bd566dcc2d76d5b42faf34)
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package examples;
15 
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.regex.Pattern;
20 import junit.framework.TestCase;
21 import org.yaml.snakeyaml.DumperOptions;
22 import org.yaml.snakeyaml.Yaml;
23 import org.yaml.snakeyaml.constructor.AbstractConstruct;
24 import org.yaml.snakeyaml.constructor.Constructor;
25 import org.yaml.snakeyaml.nodes.Node;
26 import org.yaml.snakeyaml.nodes.ScalarNode;
27 import org.yaml.snakeyaml.nodes.Tag;
28 import org.yaml.snakeyaml.representer.Represent;
29 import org.yaml.snakeyaml.representer.Representer;
30 
31 public class DiceExampleTest extends TestCase {
32 
testRepresenter()33   public void testRepresenter() {
34     Dice dice = new Dice(3, 6);
35     DumperOptions options = new DumperOptions();
36     options.setAllowReadOnlyProperties(true);
37     Yaml yaml = new Yaml(options);
38     String output = yaml.dump(dice);
39     assertEquals("!!examples.Dice {a: 3, b: 6}\n", output);
40   }
41 
testDiceRepresenter()42   public void testDiceRepresenter() {
43     Dice dice = new Dice(3, 6);
44     Map<String, Dice> data = new HashMap<String, Dice>();
45     data.put("gold", dice);
46     Yaml yaml = new Yaml(new DiceRepresenter(), new DumperOptions());
47     String output = yaml.dump(data);
48     assertEquals("{gold: !dice '3d6'}\n", output);
49   }
50 
51   class DiceRepresenter extends Representer {
52 
DiceRepresenter()53     public DiceRepresenter() {
54       this.representers.put(Dice.class, new RepresentDice());
55     }
56 
57     private class RepresentDice implements Represent {
58 
representData(Object data)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 Constructor {
68 
DiceConstructor()69     public DiceConstructor() {
70       this.yamlConstructors.put(new Tag("!dice"), new ConstructDice());
71     }
72 
73     private class ConstructDice extends AbstractConstruct {
74 
construct(Node node)75       public Object construct(Node node) {
76         String val = constructScalar((ScalarNode) node);
77         int position = val.indexOf('d');
78         Integer a = Integer.valueOf(val.substring(0, position));
79         Integer b = Integer.valueOf(val.substring(position + 1));
80         return new Dice(a, b);
81       }
82     }
83   }
84 
85   @SuppressWarnings("unchecked")
testConstructor()86   public void testConstructor() {
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   // the tag must start with a digit
94   @SuppressWarnings("unchecked")
testImplicitResolver()95   public void testImplicitResolver() {
96     Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
97     // the tag must start with a digit
98     yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), "123456789");
99     // dump
100     Map<String, Dice> treasure = 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     // load
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   // the tag may start with anything
111   @SuppressWarnings("unchecked")
testImplicitResolverWithNull()112   public void testImplicitResolverWithNull() {
113     Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
114     // the tag may start with anything
115     yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), null);
116     // dump
117     Map<String, Dice> treasure = 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     // load
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 
127   static class DiceBean {
128 
129     public Dice treasure;
130 
131     @Override
equals(Object o)132     public boolean equals(Object o) {
133       if (this == o) {
134         return true;
135       }
136       if (!(o instanceof DiceBean)) {
137         return false;
138       }
139 
140       DiceBean diceBean = (DiceBean) o;
141       return Objects.equals(treasure, diceBean.treasure);
142     }
143 
144     @Override
hashCode()145     public int hashCode() {
146       return treasure != null ? treasure.hashCode() : 0;
147     }
148   }
149 
testImplicitResolverJavaBean()150   public void testImplicitResolverJavaBean() {
151     Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter());
152     // the tag must start with a digit
153     yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), "123456789");
154     // dump
155     DiceBean bean = new DiceBean();
156     bean.treasure = new Dice(10, 20);
157     String output = yaml.dump(bean);
158     assertEquals("!!examples.DiceExampleTest$DiceBean {treasure: 10d20}\n", output);
159     // load
160     Object loaded = yaml.load(output);
161     assertEquals(loaded, bean);
162   }
163 }
164