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