aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/examples/collections/TypeSafeMap2Test.java
blob: 70e7051dd428d58687a79b0c273494b03a142974 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
 * 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 examples.collections;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;

/**
 * Test MapBean->Map<Enum, Developer> developers <br/>
 * Developer class must be properly recognised
 */
public class TypeSafeMap2Test extends TestCase {

  public void testDumpMap() {
    MapBean2 bean = new MapBean2();
    Map<Developer2, Color> data = new LinkedHashMap<Developer2, Color>();
    data.put(new Developer2("Andy", "tester"), Color.BLACK);
    data.put(new Developer2("Lisa", "owner"), Color.RED);
    bean.setData(data);
    Map<Color, Developer2> developers = new LinkedHashMap<Color, Developer2>();
    developers.put(Color.WHITE, new Developer2("Fred", "creator"));
    developers.put(Color.BLACK, new Developer2("John", "committer"));
    bean.setDevelopers(developers);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    String etalon = Util.getLocalResource("examples/map-bean-12.yaml");
    assertEquals(etalon, output);
  }

  public void testMap2() {
    MapBean2 bean = new MapBean2();
    Map<Developer2, Color> data = new LinkedHashMap<Developer2, Color>();
    data.put(new Developer2("Andy", "tester"), Color.BLACK);
    data.put(new SuperMan("Bill", "cleaner", false), Color.BLACK);
    data.put(new Developer2("Lisa", "owner"), Color.RED);
    bean.setData(data);
    Map<Color, Developer2> developers = new LinkedHashMap<Color, Developer2>();
    developers.put(Color.WHITE, new Developer2("Fred", "creator"));
    developers.put(Color.RED, new SuperMan("Jason", "contributor", true));
    developers.put(Color.BLACK, new Developer2("John", "committer"));
    bean.setDevelopers(developers);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    String etalon = Util.getLocalResource("examples/map-bean-13.yaml");
    assertEquals(etalon, output);
    // load
    Yaml beanLoader = new Yaml();
    MapBean2 parsed = beanLoader.loadAs(etalon, MapBean2.class);
    assertNotNull(parsed);
    Map<Developer2, Color> parsedData = parsed.getData();
    assertEquals(3, parsedData.size());
    assertTrue(parsedData.containsKey(new SuperMan("Bill", "cleaner", false)));
    assertEquals(Color.BLACK, parsedData.get(new SuperMan("Bill", "cleaner", false)));
    //
    Map<Color, Developer2> parsedDevelopers = parsed.getDevelopers();
    assertEquals(3, parsedDevelopers.size());
    assertEquals(new SuperMan("Jason", "contributor", true), parsedDevelopers.get(Color.RED));
  }

  public void testLoadMap() {
    String output = Util.getLocalResource("examples/map-bean-12.yaml");
    // System.out.println(output);
    Yaml beanLoader = new Yaml();
    MapBean2 parsed = beanLoader.loadAs(output, MapBean2.class);
    assertNotNull(parsed);
    Map<Developer2, Color> data = parsed.getData();
    assertEquals(2, data.size());
    Iterator<Developer2> iter = data.keySet().iterator();
    Developer2 first = iter.next();
    assertEquals("Andy", first.getName());
    assertEquals("tester", first.getRole());
    assertEquals(Color.BLACK, data.get(first));
    Developer2 second = iter.next();
    assertEquals("Lisa", second.getName());
    assertEquals("owner", second.getRole());
    assertEquals(Color.RED, data.get(second));
    //
    Map<Color, Developer2> developers = parsed.getDevelopers();
    assertEquals(2, developers.size());
    Iterator<Color> iter2 = developers.keySet().iterator();
    Color firstColor = iter2.next();
    assertEquals(Color.WHITE, firstColor);
    Developer2 dev1 = developers.get(firstColor);
    assertEquals("Fred", dev1.getName());
    assertEquals("creator", dev1.getRole());
    Color secondColor = iter2.next();
    assertEquals(Color.BLACK, secondColor);
    Developer2 dev2 = developers.get(secondColor);
    assertEquals("John", dev2.getName());
    assertEquals("committer", dev2.getRole());
  }

  public enum Color {
    WHITE, BLACK, RED
  }

  public static class MapBean2 {

    private Map<Developer2, Color> data;
    private String name;
    private Map<Color, Developer2> developers;

    public MapBean2() {
      name = "Bean123";
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public Map<Color, Developer2> getDevelopers() {
      return developers;
    }

    public void setDevelopers(Map<Color, Developer2> developers) {
      this.developers = developers;
    }

    public Map<Developer2, Color> getData() {
      return data;
    }

    public void setData(Map<Developer2, Color> data) {
      this.data = data;
    }

  }

  public static class Developer2 implements Comparable<Developer2> {

    private String name;
    private String role;

    public Developer2() {}

    private Developer2(String name, String role) {
      this.name = name;
      this.role = role;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getRole() {
      return role;
    }

    public void setRole(String role) {
      this.role = role;
    }

    public int compareTo(Developer2 o) {
      return name.compareTo(o.name);
    }

    @Override
    public boolean equals(Object obj) {
      if (obj instanceof Developer2) {
        return toString().equals(obj.toString());
      } else {
        return false;
      }
    }

    @Override
    public int hashCode() {
      return toString().hashCode();
    }

    @Override
    public String toString() {
      return "Developer " + name + " " + role;
    }

  }

  public static class SuperMan extends Developer2 {

    private boolean smart;

    public SuperMan() {
      super();
    }

    private SuperMan(String name, String role, boolean smart) {
      super(name, role);
      this.smart = smart;
    }

    public boolean isSmart() {
      return smart;
    }

    public void setSmart(boolean smart) {
      this.smart = smart;
    }

    @Override
    public String toString() {
      return "Super" + super.toString();
    }
  }
}