aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/yaml/snakeyaml/issues/issue11/YamlMapTest.java
blob: 8b6468aaabcfc6f437dd5bc6b82709a86608fd57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.issues.issue11;

import java.util.Map;
import java.util.TreeMap;
import junit.framework.TestCase;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.AbstractConstruct;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;

public class YamlMapTest extends TestCase {

  public void testYaml() {
    Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter());
    String output = yaml.dump(new Custom(123));
    // System.out.println(output);
    Custom o = yaml.load(output);
    assertEquals("123", o.getStr());
  }

  @SuppressWarnings("unchecked")
  public void testYamlMap() {
    Map<String, Object> data = new TreeMap<String, Object>();
    data.put("customTag", new Custom(123));

    Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter());
    String output = yaml.dump(data);
    // System.out.println(output);
    Object o = yaml.load(output);

    assertTrue(o instanceof Map);
    Map<String, Object> m = (Map<String, Object>) o;
    assertTrue(m.get("customTag") instanceof Custom);
  }

  @SuppressWarnings("unchecked")
  public void testYamlMapBean() {
    Map<String, Object> data = new TreeMap<String, Object>();
    data.put("knownClass", new Wrapper("test", new Custom(456)));

    Yaml yaml = new Yaml(new ExtendedConstructor(), new ExtendedRepresenter());
    String output = yaml.dump(data);
    // System.out.println(output);
    Object o = yaml.load(output);

    assertTrue(o instanceof Map);
    Map<String, Object> m = (Map<String, Object>) o;
    assertEquals(Wrapper.class, m.get("knownClass").getClass());
  }

  public static class Wrapper {

    private String a;
    private Custom b;

    public Wrapper(String s, Custom bb) {
      a = s;
      b = bb;
    }

    public Wrapper() {}

    public String getA() {
      return a;
    }

    public void setA(String s) {
      a = s;
    }

    public Custom getB() {
      return b;
    }

    public void setB(Custom bb) {
      b = bb;
    }
  }

  public static class Custom {

    final private String str;

    public Custom(Integer i) {
      str = i.toString();
    }

    public Custom(Custom c) {
      str = c.str;
    }

    public String toString() {
      return str;
    }

    public String getStr() {
      return str;
    }
  }

  public static class ExtendedRepresenter extends Representer {

    public ExtendedRepresenter() {
      this.representers.put(Custom.class, new RepresentCustom());
    }

    private class RepresentCustom implements Represent {

      public Node representData(Object data) {
        return representScalar(new Tag("!Custom"), data.toString());
      }
    }
  }

  public static class ExtendedConstructor extends Constructor {

    public ExtendedConstructor() {
      this.yamlConstructors.put(new Tag("!Custom"), new ConstructCustom());
    }

    private class ConstructCustom extends AbstractConstruct {

      public Object construct(Node node) {
        String str = constructScalar((ScalarNode) node);
        return new Custom(Integer.valueOf(str));
      }

    }
  }
}