View Javadoc

1   /**
2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      // the tag must start with a digit
94      @SuppressWarnings("unchecked")
95      public void testImplicitResolver() throws IOException {
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 = (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         // 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")
112     public void testImplicitResolverWithNull() throws IOException {
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 = (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         // 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 }