aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/examples/collections/TypeSafeSetImplementationsTest.java
blob: 56ba7855870af4760ac524f3b251a062d2d37abf (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
 * 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.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import junit.framework.TestCase;
import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;

/**
 * Test different Map implementations as JavaBean properties
 */
public class TypeSafeSetImplementationsTest extends TestCase {

  public void testDumpSet() {
    SetBean bean = new SetBean();
    SortedSet<String> sortedSet = new TreeSet<String>();
    sortedSet.add("two");
    sortedSet.add("one");
    sortedSet.add("three");
    bean.setSorted(sortedSet);
    SortedSet<Developer> developers = new TreeSet<Developer>();
    developers.add(new Developer("John", "founder"));
    developers.add(new Developer("Karl", "user"));
    bean.setDevelopers(developers);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    String etalon = Util.getLocalResource("examples/set-bean-1.yaml");
    assertEquals(etalon, output);
  }

  public void testDumpSet2() {
    SetBean bean = new SetBean();
    SortedSet<String> sortedSet = new TreeSet<String>();
    sortedSet.add("two");
    sortedSet.add("one");
    sortedSet.add("three");
    bean.setSorted(sortedSet);
    SortedSet<Developer> developers = new TreeSet<Developer>();
    developers.add(new Developer("John", "founder"));
    developers.add(new Developer("Karl", "user"));
    developers.add(new SuperDeveloper("Bill", "super"));
    bean.setDevelopers(developers);
    Yaml yaml = new Yaml();
    String output = yaml.dumpAsMap(bean);
    // System.out.println(output);
    String etalon = Util.getLocalResource("examples/set-bean-6.yaml");
    assertEquals(etalon, output);
  }

  public void testLoadSet() {
    String output = Util.getLocalResource("examples/set-bean-1.yaml");
    // System.out.println(output);
    Yaml beanLoader = new Yaml();
    SetBean parsed = beanLoader.loadAs(output, SetBean.class);
    assertNotNull(parsed);
    SortedSet<String> sortedMap = parsed.getSorted();
    assertEquals(3, sortedMap.size());
    assertTrue(sortedMap.contains("one"));
    assertTrue(sortedMap.contains("two"));
    assertTrue(sortedMap.contains("three"));
    String first = sortedMap.iterator().next();
    assertEquals("one", first);
    //
    SortedSet<Developer> developers = parsed.getDevelopers();
    assertEquals(2, developers.size());
    assertEquals("John", developers.first().getName());
    assertEquals("Karl", developers.last().getName());
  }

  public void testLoadSetReversed() {
    String output = Util.getLocalResource("examples/set-bean-2.yaml");
    // System.out.println(output);
    Yaml beanLoader = new Yaml();
    SetBean parsed = beanLoader.loadAs(output, SetBean.class);
    assertNotNull(parsed);
    SortedSet<String> sortedMap = parsed.getSorted();
    assertEquals(3, sortedMap.size());
    assertTrue(sortedMap.contains("one"));
    assertTrue(sortedMap.contains("two"));
    assertTrue(sortedMap.contains("three"));
    // alphabetically: one, three, two
    assertEquals("one", sortedMap.first());
    assertEquals("two", sortedMap.last());
    // the order is not from YAML (must be sorted)
    SortedSet<Developer> developers = parsed.getDevelopers();
    assertEquals(2, developers.size());
    assertEquals("John", developers.first().getName());
    assertEquals("Karl", developers.last().getName());
  }

  public static class SetBean {

    private SortedSet<String> sorted;
    private SortedSet<Developer> developers;
    private String name;

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

    public SortedSet<String> getSorted() {
      return sorted;
    }

    public void setSorted(SortedSet<String> sorted) {
      this.sorted = sorted;
    }

    public String getName() {
      return name;
    }

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

    public SortedSet<Developer> getDevelopers() {
      return developers;
    }

    public void setDevelopers(SortedSet<Developer> developers) {
      this.developers = developers;
    }
  }

  public static class Developer implements Comparable<Developer> {

    private String name;
    private String role;

    public Developer() {}

    public Developer(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(Developer o) {
      return name.compareTo(o.name);
    }
  }

  public static class SuperDeveloper extends Developer {

    public SuperDeveloper() {
      super();
    }

    public SuperDeveloper(String string, String string2) {
      super(string, string2);
    }

  }

  @SuppressWarnings("unchecked")
  public void testNoJavaBeanSetRecursive() {
    Set<Object> set = new HashSet<Object>(3);
    set.add("aaa");
    set.add(111);
    Box box = new Box();
    box.setId("id123");
    box.setSet(set);
    set.add(box);
    Yaml yaml = new Yaml();
    String output = yaml.dump(set);
    // System.out.println(output);
    // the order may differ on different JVMs
    // String etalon = Util.getLocalResource("examples/set-bean-3.yaml");
    // assertEquals(etalon, output);
    assertTrue(output.contains("&id001 !!set"));
    assertTrue(output.contains("? !!examples.collections.TypeSafeSetImplementationsTest$Box"));
    assertTrue(output.contains("set: *id001"));
    assertTrue(output.contains("111: null"));
    // load
    Set<Object> list2 = yaml.load(output);
    assertEquals(3, list2.size());
    assertTrue(list2.contains("aaa"));
    assertTrue(list2.contains(111));
  }

  public static class Box {

    private String id;
    private Set<Object> set;

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public Set<Object> getSet() {
      return set;
    }

    public void setSet(Set<Object> set) {
      this.set = set;
    }
  }

  @SuppressWarnings("unchecked")
  public void testNoJavaBeanSet() {
    Yaml yaml = new Yaml();
    String output = Util.getLocalResource("examples/set-bean-4.yaml");
    // System.out.println(output);
    // load
    Set<String> set = yaml.load(output);
    assertEquals(3, set.size());
    assertTrue(set.contains("aaa"));
    assertTrue(set.contains("bbb"));
    assertTrue(set.contains("zzz"));
    Iterator<String> iter = set.iterator();
    assertEquals("bbb", iter.next());
    assertEquals("aaa", iter.next());
    assertEquals("zzz", iter.next());
  }

  @SuppressWarnings("unchecked")
  public void testNoJavaBeanSet2() {
    Yaml yaml = new Yaml();
    String output = Util.getLocalResource("examples/set-bean-5.yaml");
    // System.out.println(output);
    // load and sort
    Set<String> set = yaml.load(output);
    assertEquals(3, set.size());
    assertTrue(set.contains("aaa"));
    assertTrue(set.contains("bbb"));
    assertTrue(set.contains("zzz"));
    Iterator<String> iter = set.iterator();
    assertEquals("aaa", iter.next());
    assertEquals("bbb", iter.next());
    assertEquals("zzz", iter.next());
  }
}