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