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.issue73;
17  
18  import java.util.TreeSet;
19  
20  import junit.framework.TestCase;
21  
22  import org.yaml.snakeyaml.Yaml;
23  
24  /**
25   * Test bean when the implementation is defined: TreeSet instead of just the
26   * interface Set
27   */
28  public class TreeSetTest extends TestCase {
29      public void testSetImplementation() {
30          Bean1 bean = new Bean1();
31          bean.setId("ID123");
32          TreeSet<String> list = new TreeSet<String>();
33          list.add("zzz");
34          list.add("xxx");
35          list.add("ccc");
36          bean.setSet(list);
37          Yaml yaml = new Yaml();
38          String doc = yaml.dump(bean);
39          // System.out.println(doc);
40          //
41          Bean1 loaded = (Bean1) yaml.load(doc);
42          assertEquals(3, loaded.getSet().size());
43          assertEquals(TreeSet.class, loaded.getSet().getClass());
44          assertTrue(loaded.getSet().contains("zzz"));
45          assertTrue(loaded.getSet().contains("xxx"));
46          assertTrue(loaded.getSet().contains("ccc"));
47      }
48  
49      public static class Bean1 {
50          private TreeSet<String> set;
51          private String id;
52  
53          public TreeSet<String> getSet() {
54              return set;
55          }
56  
57          public void setSet(TreeSet<String> set) {
58              this.set = set;
59          }
60  
61          public String getId() {
62              return id;
63          }
64  
65          public void setId(String id) {
66              this.id = id;
67          }
68      }
69  }