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.issue61;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Util;
24  import org.yaml.snakeyaml.Yaml;
25  
26  public class GenericListBeanTest extends TestCase {
27      @SuppressWarnings("unchecked")
28      public void testGenericList() {
29          Yaml yaml = new Yaml();
30          ListProvider<String> listProvider = new ListProvider<String>();
31          listProvider.getList().add("foo");
32          listProvider.getList().add("bar");
33          String s = yaml.dumpAsMap(listProvider);
34          // System.out.println(s);
35          assertEquals("list:\n- foo\n- bar\n", s);
36          // parse
37          Yaml loader = new Yaml();
38          ListProvider<String> listProvider2 = loader.loadAs(s, ListProvider.class);
39          assertEquals("foo", listProvider2.getList().get(0));
40          assertEquals("bar", listProvider2.getList().get(1));
41          assertEquals(listProvider, listProvider2);
42      }
43  
44      @SuppressWarnings("rawtypes")
45      public void testGenericBean() {
46          Yaml yaml = new Yaml();
47          ListProvider<Bean> listProvider = new ListProvider<Bean>();
48          Bean foo = new Bean();
49          foo.setName("foo");
50          listProvider.getList().add(foo);
51          Bean bar = new Bean();
52          bar.setName("bar");
53          bar.setNumber(3);
54          listProvider.getList().add(bar);
55          String s = yaml.dumpAsMap(listProvider);
56          // System.out.println(s);
57          String etalon = Util.getLocalResource("issues/issue61-1.yaml");
58          assertEquals(etalon, s);
59          // parse
60          Yaml loader = new Yaml();
61          ListProvider listProvider2 = loader.loadAs(s, ListProvider.class);
62          Bean foo2 = (Bean) listProvider2.getList().get(0);
63          assertEquals("foo", foo2.getName());
64          assertEquals(0, foo2.getNumber());
65          Bean bar2 = (Bean) listProvider2.getList().get(1);
66          assertEquals("bar", bar2.getName());
67          assertEquals(3, bar2.getNumber());
68      }
69  
70      public static class ListProvider<T> {
71          private List<T> list = new ArrayList<T>();
72  
73          public List<T> getList() {
74              return list;
75          }
76  
77          public void setList(List<T> list) {
78              this.list = list;
79          }
80  
81          @SuppressWarnings("rawtypes")
82          @Override
83          public boolean equals(Object obj) {
84              if (obj instanceof ListProvider) {
85                  return list.equals(((ListProvider) obj).getList());
86              } else {
87                  return false;
88              }
89          }
90  
91          @Override
92          public int hashCode() {
93              return list.hashCode();
94          }
95      }
96  
97      public static class Bean {
98          private String name;
99          private int number;
100 
101         public String getName() {
102             return name;
103         }
104 
105         public void setName(String name) {
106             this.name = name;
107         }
108 
109         public int getNumber() {
110             return number;
111         }
112 
113         public void setNumber(int number) {
114             this.number = number;
115         }
116     }
117 }