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