1 /** 2 * Copyright (c) 2008, 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 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.Constructor; 28 import org.yaml.snakeyaml.constructor.SafeConstructor; 29 import org.yaml.snakeyaml.nodes.Node; 30 import org.yaml.snakeyaml.nodes.ScalarNode; 31 import org.yaml.snakeyaml.nodes.Tag; 32 import org.yaml.snakeyaml.representer.Represent; 33 import org.yaml.snakeyaml.representer.Representer; 34 35 public class DiceExampleTest extends TestCase { testRepresenter()36 public void testRepresenter() { 37 Dice dice = new Dice(3, 6); 38 DumperOptions options = new DumperOptions(); 39 options.setAllowReadOnlyProperties(true); 40 Yaml yaml = new Yaml(options); 41 String output = yaml.dump(dice); 42 assertEquals("!!examples.Dice {a: 3, b: 6}\n", output); 43 } 44 testDiceRepresenter()45 public void testDiceRepresenter() { 46 Dice dice = new Dice(3, 6); 47 Map<String, Dice> data = new HashMap<String, Dice>(); 48 data.put("gold", dice); 49 Yaml yaml = new Yaml(new DiceRepresenter(), new DumperOptions()); 50 String output = yaml.dump(data); 51 assertEquals("{gold: !dice '3d6'}\n", output); 52 } 53 54 class DiceRepresenter extends Representer { DiceRepresenter()55 public DiceRepresenter() { 56 this.representers.put(Dice.class, new RepresentDice()); 57 } 58 59 private class RepresentDice implements Represent { representData(Object data)60 public Node representData(Object data) { 61 Dice dice = (Dice) data; 62 String value = dice.getA() + "d" + dice.getB(); 63 return representScalar(new Tag("!dice"), value); 64 } 65 } 66 } 67 68 class DiceConstructor extends Constructor { DiceConstructor()69 public DiceConstructor() { 70 this.yamlConstructors.put(new Tag("!dice"), new ConstructDice()); 71 } 72 73 private class ConstructDice extends AbstractConstruct { construct(Node node)74 public Object construct(Node node) { 75 String val = (String) constructScalar((ScalarNode) node); 76 int position = val.indexOf('d'); 77 Integer a = new Integer(val.substring(0, position)); 78 Integer b = new Integer(val.substring(position + 1)); 79 return new Dice(a, b); 80 } 81 } 82 } 83 84 @SuppressWarnings("unchecked") testConstructor()85 public void testConstructor() { 86 Yaml yaml = new Yaml(new DiceConstructor()); 87 Object data = yaml.load("{initial hit points: !dice '8d4'}"); 88 Map<String, Dice> map = (Map<String, Dice>) data; 89 assertEquals(new Dice(8, 4), map.get("initial hit points")); 90 } 91 92 // the tag must start with a digit 93 @SuppressWarnings("unchecked") testImplicitResolver()94 public void testImplicitResolver() { 95 Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter()); 96 // the tag must start with a digit 97 yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), "123456789"); 98 // dump 99 Map<String, Dice> treasure = new HashMap<String, Dice>(); 100 treasure.put("treasure", new Dice(10, 20)); 101 String output = yaml.dump(treasure); 102 assertEquals("{treasure: 10d20}\n", output); 103 // load 104 Object data = yaml.load("{damage: 5d10}"); 105 Map<String, Dice> map = (Map<String, Dice>) data; 106 assertEquals(new Dice(5, 10), map.get("damage")); 107 } 108 109 // the tag may start with anything 110 @SuppressWarnings("unchecked") testImplicitResolverWithNull()111 public void testImplicitResolverWithNull() { 112 Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter()); 113 // the tag may start with anything 114 yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), null); 115 // dump 116 Map<String, Dice> treasure = new HashMap<String, Dice>(); 117 treasure.put("treasure", new Dice(10, 20)); 118 String output = yaml.dump(treasure); 119 assertEquals("{treasure: 10d20}\n", output); 120 // load 121 Object data = yaml.load("{damage: 5d10}"); 122 Map<String, Dice> map = (Map<String, Dice>) data; 123 assertEquals(new Dice(5, 10), map.get("damage")); 124 } 125 126 static class DiceBean { 127 public Dice treasure; 128 129 @Override equals(Object o)130 public boolean equals(Object o) { 131 if (this == o) 132 return true; 133 if (!(o instanceof DiceBean)) 134 return false; 135 136 DiceBean diceBean = (DiceBean) o; 137 if (treasure != null ? !treasure.equals(diceBean.treasure) : diceBean.treasure != null) 138 return false; 139 return true; 140 } 141 142 @Override hashCode()143 public int hashCode() { 144 return treasure != null ? treasure.hashCode() : 0; 145 } 146 } 147 testImplicitResolverJavaBean()148 public void testImplicitResolverJavaBean() { 149 Yaml yaml = new Yaml(new DiceConstructor(), new DiceRepresenter()); 150 // the tag must start with a digit 151 yaml.addImplicitResolver(new Tag("!dice"), Pattern.compile("\\d+d\\d+"), "123456789"); 152 // dump 153 DiceBean bean = new DiceBean(); 154 bean.treasure = new Dice(10, 20); 155 String output = yaml.dump(bean); 156 assertEquals("!!examples.DiceExampleTest$DiceBean {treasure: 10d20}\n", output); 157 // load 158 Object loaded = yaml.load(output); 159 assertEquals(loaded, bean); 160 } 161 } 162