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.issue72;
    18
    19  import java.util.ArrayList;
    20  import java.util.Collection;
    21  import java.util.HashSet;
    22
    23  import junit.framework.TestCase;
    24
    25  import org.yaml.snakeyaml.Yaml;
    26
    27  public class CollectionTest extends TestCase {
    28
    29      public void testCollectionList() {
    30          CollectionList bean = new CollectionList();
    31          Yaml yaml = new Yaml();
    32          String doc = yaml.dumpAsMap(bean);
    33          // System.out.println(doc);
    34          Yaml beanLoader = new Yaml();
    35          CollectionList parsed = beanLoader.loadAs(doc, CollectionList.class);
    36          assertTrue(parsed.getNames().contains("aaa"));
    37          assertTrue(parsed.getNames().contains("bbb"));
    38          assertEquals(2, parsed.getNames().size());
    39      }
    40
    41      public static class CollectionList {
    42          private Collection<String> names;
    43
    44          public CollectionList() {
    45              names = new ArrayList<String>();
    46              names.add("aaa");
    47              names.add("bbb");
    48          }
    49
    50          public Collection<String> getNames() {
    51              return names;
    52          }
    53
    54          public void setNames(Collection<String> names) {
    55              this.names = names;
    56          }
    57      }
    58
    59      public void testCollectionSet() {
    60          CollectionSet bean = new CollectionSet();
    61          Yaml yaml = new Yaml();
    62          String doc = yaml.dumpAsMap(bean);
    63          // System.out.println(doc);
    64          Yaml beanLoader = new Yaml();
    65          CollectionSet parsed = beanLoader.loadAs(doc, CollectionSet.class);
    66          assertTrue(parsed.getRoles().contains(11));
    67          assertTrue(parsed.getRoles().contains(13));
    68          assertEquals(2, parsed.getRoles().size());
    69      }
    70
    71      public static class CollectionSet {
    72          private Collection<Integer> roles;
    73
    74          public CollectionSet() {
    75              roles = new HashSet<Integer>();
    76              roles.add(11);
    77              roles.add(13);
    78          }
    79
    80          public Collection<Integer> getRoles() {
    81              return roles;
    82          }
    83
    84          public void setRoles(Collection<Integer> roles) {
    85              this.roles = roles;
    86          }
    87      }
    88  }