View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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 org.yaml.snakeyaml.issues.issue11;
17  
18  import java.util.Map;
19  import java.util.TreeMap;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Yaml;
24  import org.yaml.snakeyaml.constructor.AbstractConstruct;
25  import org.yaml.snakeyaml.constructor.Constructor;
26  import org.yaml.snakeyaml.nodes.Node;
27  import org.yaml.snakeyaml.nodes.ScalarNode;
28  import org.yaml.snakeyaml.nodes.Tag;
29  import org.yaml.snakeyaml.representer.Represent;
30  import org.yaml.snakeyaml.representer.Representer;
31  
32  public class YamlMapTest extends TestCase {
33      public void testYaml() {
34          Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter());
35          String output = yaml.dump(new Custom(123));
36          // System.out.println(output);
37          Custom o = (Custom) yaml.load(output);
38          assertEquals("123", o.getStr());
39      }
40  
41      @SuppressWarnings("unchecked")
42      public void testYamlMap() {
43          Map<String, Object> data = new TreeMap<String, Object>();
44          data.put("customTag", new Custom(123));
45  
46          Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter());
47          String output = yaml.dump(data);
48          // System.out.println(output);
49          Object o = yaml.load(output);
50  
51          assertTrue(o instanceof Map);
52          Map<String, Object> m = (Map<String, Object>) o;
53          assertTrue(m.get("customTag") instanceof Custom);
54      }
55  
56      @SuppressWarnings("unchecked")
57      public void testYamlMapBean() {
58          Map<String, Object> data = new TreeMap<String, Object>();
59          data.put("knownClass", new Wrapper("test", new Custom(456)));
60  
61          Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter());
62          String output = yaml.dump(data);
63          // System.out.println(output);
64          Object o = yaml.load(output);
65  
66          assertTrue(o instanceof Map);
67          Map<String, Object> m = (Map<String, Object>) o;
68          assertEquals(Wrapper.class, m.get("knownClass").getClass());
69      }
70  
71      public static class Wrapper {
72          private String a;
73          private Custom b;
74  
75          public Wrapper(String s, Custom bb) {
76              a = s;
77              b = bb;
78          }
79  
80          public Wrapper() {
81          }
82  
83          public String getA() {
84              return a;
85          }
86  
87          public void setA(String s) {
88              a = s;
89          }
90  
91          public Custom getB() {
92              return b;
93          }
94  
95          public void setB(Custom bb) {
96              b = bb;
97          }
98      }
99  
100     public static class Custom {
101         final private String str;
102 
103         public Custom(Integer i) {
104             str = i.toString();
105         }
106 
107         public Custom(Custom c) {
108             str = c.str;
109         }
110 
111         public String toString() {
112             return str;
113         }
114 
115         public String getStr() {
116             return str;
117         }
118     }
119 
120     public static class ExtendedRepresenter extends Representer {
121         public ExtendedRepresenter() {
122             this.representers.put(Custom.class, new RepresentCustom());
123         }
124 
125         private class RepresentCustom implements Represent {
126             public Node representData(Object data) {
127                 return representScalar(new Tag("!Custom"), ((Custom) data).toString());
128             }
129         }
130     }
131 
132     public static class ExtendedConstructor extends Constructor {
133         public ExtendedConstructor() {
134             this.yamlConstructors.put(new Tag("!Custom"), new ConstructCustom());
135         }
136 
137         private class ConstructCustom extends AbstractConstruct {
138             public Object construct(Node node) {
139                 String str = (String) constructScalar((ScalarNode) node);
140                 return new Custom(new Integer(str));
141             }
142 
143         }
144     }
145 }