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