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 examples.collections;
    18
    19  import java.util.HashSet;
    20  import java.util.Iterator;
    21  import java.util.Set;
    22  import java.util.SortedSet;
    23  import java.util.TreeSet;
    24
    25  import junit.framework.TestCase;
    26
    27  import org.yaml.snakeyaml.Util;
    28  import org.yaml.snakeyaml.Yaml;
    29
    30  /**
    31   * Test different Map implementations as JavaBean properties
    32   */
    33  public class TypeSafeSetImplementationsTest extends TestCase {
    34      public void testDumpSet() {
    35          SetBean bean = new SetBean();
    36          SortedSet<String> sortedSet = new TreeSet<String>();
    37          sortedSet.add("two");
    38          sortedSet.add("one");
    39          sortedSet.add("three");
    40          bean.setSorted(sortedSet);
    41          SortedSet<Developer> developers = new TreeSet<Developer>();
    42          developers.add(new Developer("John", "founder"));
    43          developers.add(new Developer("Karl", "user"));
    44          bean.setDevelopers(developers);
    45          Yaml yaml = new Yaml();
    46          String output = yaml.dumpAsMap(bean);
    47          // System.out.println(output);
    48          String etalon = Util.getLocalResource("examples/set-bean-1.yaml");
    49          assertEquals(etalon, output);
    50      }
    51
    52      public void testDumpSet2() {
    53          SetBean bean = new SetBean();
    54          SortedSet<String> sortedSet = new TreeSet<String>();
    55          sortedSet.add("two");
    56          sortedSet.add("one");
    57          sortedSet.add("three");
    58          bean.setSorted(sortedSet);
    59          SortedSet<Developer> developers = new TreeSet<Developer>();
    60          developers.add(new Developer("John", "founder"));
    61          developers.add(new Developer("Karl", "user"));
    62          developers.add(new SuperDeveloper("Bill", "super"));
    63          bean.setDevelopers(developers);
    64          Yaml yaml = new Yaml();
    65          String output = yaml.dumpAsMap(bean);
    66          // System.out.println(output);
    67          String etalon = Util.getLocalResource("examples/set-bean-6.yaml");
    68          assertEquals(etalon, output);
    69      }
    70
    71      public void testLoadSet() {
    72          String output = Util.getLocalResource("examples/set-bean-1.yaml");
    73          // System.out.println(output);
    74          Yaml beanLoader = new Yaml();
    75          SetBean parsed = beanLoader.loadAs(output, SetBean.class);
    76          assertNotNull(parsed);
    77          SortedSet<String> sortedMap = parsed.getSorted();
    78          assertEquals(3, sortedMap.size());
    79          assertTrue(sortedMap.contains("one"));
    80          assertTrue(sortedMap.contains("two"));
    81          assertTrue(sortedMap.contains("three"));
    82          String first = sortedMap.iterator().next();
    83          assertEquals("one", first);
    84          //
    85          SortedSet<Developer> developers = parsed.getDevelopers();
    86          assertEquals(2, developers.size());
    87          assertEquals("John", developers.first().getName());
    88          assertEquals("Karl", developers.last().getName());
    89      }
    90
    91      public void testLoadSetReversed() {
    92          String output = Util.getLocalResource("examples/set-bean-2.yaml");
    93          // System.out.println(output);
    94          Yaml beanLoader = new Yaml();
    95          SetBean parsed = beanLoader.loadAs(output, SetBean.class);
    96          assertNotNull(parsed);
    97          SortedSet<String> sortedMap = parsed.getSorted();
    98          assertEquals(3, sortedMap.size());
    99          assertTrue(sortedMap.contains("one"));
    100         assertTrue(sortedMap.contains("two"));
    101         assertTrue(sortedMap.contains("three"));
    102         // alphabetically: one, three, two
    103         assertEquals("one", sortedMap.first());
    104         assertEquals("two", sortedMap.last());
    105         // the order is not from YAML (must be sorted)
    106         SortedSet<Developer> developers = parsed.getDevelopers();
    107         assertEquals(2, developers.size());
    108         assertEquals("John", developers.first().getName());
    109         assertEquals("Karl", developers.last().getName());
    110     }
    111
    112     public static class SetBean {
    113         private SortedSet<String> sorted;
    114         private SortedSet<Developer> developers;
    115         private String name;
    116
    117         public SetBean() {
    118             name = "Bean123";
    119         }
    120
    121         public SortedSet<String> getSorted() {
    122             return sorted;
    123         }
    124
    125         public void setSorted(SortedSet<String> sorted) {
    126             this.sorted = sorted;
    127         }
    128
    129         public String getName() {
    130             return name;
    131         }
    132
    133         public void setName(String name) {
    134             this.name = name;
    135         }
    136
    137         public SortedSet<Developer> getDevelopers() {
    138             return developers;
    139         }
    140
    141         public void setDevelopers(SortedSet<Developer> developers) {
    142             this.developers = developers;
    143         }
    144     }
    145
    146     public static class Developer implements Comparable<Developer> {
    147         private String name;
    148         private String role;
    149
    150         public Developer() {
    151         }
    152
    153         public Developer(String name, String role) {
    154             this.name = name;
    155             this.role = role;
    156         }
    157
    158         public String getName() {
    159             return name;
    160         }
    161
    162         public void setName(String name) {
    163             this.name = name;
    164         }
    165
    166         public String getRole() {
    167             return role;
    168         }
    169
    170         public void setRole(String role) {
    171             this.role = role;
    172         }
    173
    174         public int compareTo(Developer o) {
    175             return name.compareTo(o.name);
    176         }
    177     }
    178
    179     public static class SuperDeveloper extends Developer {
    180
    181         public SuperDeveloper() {
    182             super();
    183         }
    184
    185         public SuperDeveloper(String string, String string2) {
    186             super(string, string2);
    187         }
    188
    189     }
    190
    191     @SuppressWarnings("unchecked")
    192     public void testNoJavaBeanSetRecursive() {
    193         Set<Object> set = new HashSet<Object>(3);
    194         set.add("aaa");
    195         set.add(111);
    196         Box box = new Box();
    197         box.setId("id123");
    198         box.setSet(set);
    199         set.add(box);
    200         Yaml yaml = new Yaml();
    201         String output = yaml.dump(set);
    202         // System.out.println(output);
    203         // the order may differ on different JVMs
    204         // String etalon = Util.getLocalResource("examples/set-bean-3.yaml");
    205         // assertEquals(etalon, output);
    206         assertTrue(output.contains("&id001 !!set"));
    207         assertTrue(output.contains("? !!examples.collections.TypeSafeSetImplementationsTest$Box"));
    208         assertTrue(output.contains("set: *id001"));
    209         assertTrue(output.contains("111: null"));
    210         // load
    211         Set<Object> list2 = (Set<Object>) yaml.load(output);
    212         assertEquals(3, list2.size());
    213         assertTrue(list2.contains("aaa"));
    214         assertTrue(list2.contains(111));
    215     }
    216
    217     public static class Box {
    218         private String id;
    219         private Set<Object> set;
    220
    221         public String getId() {
    222             return id;
    223         }
    224
    225         public void setId(String id) {
    226             this.id = id;
    227         }
    228
    229         public Set<Object> getSet() {
    230             return set;
    231         }
    232
    233         public void setSet(Set<Object> set) {
    234             this.set = set;
    235         }
    236     }
    237
    238     @SuppressWarnings("unchecked")
    239     public void testNoJavaBeanSet() {
    240         Yaml yaml = new Yaml();
    241         String output = Util.getLocalResource("examples/set-bean-4.yaml");
    242         // System.out.println(output);
    243         // load
    244         Set<String> set = (Set<String>) yaml.load(output);
    245         assertEquals(3, set.size());
    246         assertTrue(set.contains("aaa"));
    247         assertTrue(set.contains("bbb"));
    248         assertTrue(set.contains("zzz"));
    249         Iterator<String> iter = set.iterator();
    250         assertEquals("bbb", iter.next());
    251         assertEquals("aaa", iter.next());
    252         assertEquals("zzz", iter.next());
    253     }
    254
    255     @SuppressWarnings("unchecked")
    256     public void testNoJavaBeanSet2() {
    257         Yaml yaml = new Yaml();
    258         String output = Util.getLocalResource("examples/set-bean-5.yaml");
    259         // System.out.println(output);
    260         // load and sort
    261         Set<String> set = (Set<String>) yaml.load(output);
    262         assertEquals(3, set.size());
    263         assertTrue(set.contains("aaa"));
    264         assertTrue(set.contains("bbb"));
    265         assertTrue(set.contains("zzz"));
    266         Iterator<String> iter = set.iterator();
    267         assertEquals("aaa", iter.next());
    268         assertEquals("bbb", iter.next());
    269         assertEquals("zzz", iter.next());
    270     }
    271 }