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 org.yaml.snakeyaml.representer;
18  
19  import junit.framework.TestCase;
20  
21  import org.yaml.snakeyaml.Yaml;
22  import org.yaml.snakeyaml.constructor.AbstractConstruct;
23  import org.yaml.snakeyaml.constructor.Constructor;
24  import org.yaml.snakeyaml.nodes.Node;
25  import org.yaml.snakeyaml.nodes.ScalarNode;
26  import org.yaml.snakeyaml.nodes.Tag;
27  
28  public class RepresentTest extends TestCase {
29  
30      public void testCustomRepresenter() {
31          Yaml yaml = new Yaml(new MyConstructor(), new MyRepresenter());
32          CustomBean etalon = new CustomBean("A", 1);
33          String output = yaml.dump(etalon);
34          assertEquals("!!Dice 'Ad1'\n", output);
35          CustomBean bean = (CustomBean) yaml.load(output);
36          assertEquals("A", bean.getPrefix());
37          assertEquals(1, bean.getSuffix());
38          assertEquals(etalon, bean);
39      }
40  
41      class CustomBean {
42          private String prefix;
43          private int suffix;
44  
45          public CustomBean(String prefix, int suffix) {
46              this.prefix = prefix;
47              this.suffix = suffix;
48          }
49  
50          public String getPrefix() {
51              return prefix;
52          }
53  
54          public int getSuffix() {
55              return suffix;
56          }
57  
58          @Override
59          public boolean equals(Object obj) {
60              CustomBean bean = (CustomBean) obj;
61              return prefix.equals(bean.getPrefix()) && suffix == bean.getSuffix();
62          }
63      }
64  
65      class MyRepresenter extends Representer {
66          public MyRepresenter() {
67              this.representers.put(CustomBean.class, new RepresentDice());
68          }
69  
70          private class RepresentDice implements Represent {
71              public Node representData(Object data) {
72                  CustomBean coin = (CustomBean) data;
73                  String value = coin.getPrefix() + "d" + coin.getSuffix();
74                  return representScalar(new Tag("!!Dice"), value);
75              }
76          }
77      }
78  
79      class MyConstructor extends Constructor {
80          public MyConstructor() {
81              this.yamlConstructors.put(new Tag(Tag.PREFIX + "Dice"), new ConstructDice());
82          }
83  
84          private class ConstructDice extends AbstractConstruct {
85              public Object construct(Node node) {
86                  String val = (String) constructScalar((ScalarNode) node);
87                  return new CustomBean(val.substring(0, 1), new Integer(val.substring(2)));
88              }
89          }
90      }
91  }