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.generics;
18  
19  import java.beans.IntrospectionException;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Util;
24  import org.yaml.snakeyaml.Yaml;
25  
26  public class GenericArrayTypeTest extends TestCase {
27  
28      public void testPrimitives() {
29          GenericPrimitivesArray ga = new GenericPrimitivesArray(3);
30          Yaml yaml = new Yaml();
31          try {
32              yaml.dump(ga);
33              fail("Arrays of primitives are not yet fully supported.");
34          } catch (Exception e) {
35              assertEquals("Arrays of primitives are not fully supported.", e.getMessage());
36          }
37      }
38  
39      public static class GenericPrimitivesArray extends AbstractAnimal<int[]> {
40          private int[] home;
41  
42          public GenericPrimitivesArray(int count) {
43              home = new int[count];
44              for (int i = 0; i < home.length; i++) {
45                  home[i] = i + 1;
46              }
47          }
48  
49          @Override
50          public int[] getHome() {
51              return home;
52          }
53  
54          @Override
55          public void setHome(int[] home) {
56              this.home = home;
57          }
58      }
59  
60      public void testClasses() throws IntrospectionException {
61          GenericArray ga = new GenericArray();
62          Yaml yaml = new Yaml();
63          String doc = yaml.dump(ga);
64          // System.out.println(doc);
65          String etalon = "!!org.yaml.snakeyaml.generics.GenericArrayTypeTest$GenericArray\n"
66                  + "home: [1, 2, 3]\n" + "name: Array3\n";
67          assertEquals(etalon, doc);
68          if (GenericsBugDetector.isProperIntrospection()) {
69              GenericArray parsed = (GenericArray) yaml.load(doc);
70              assertEquals("Array3", parsed.getName());
71              assertEquals(3, parsed.getHome().length);
72          } else {
73              try {
74                  yaml.load(doc);
75              } catch (Exception e) {
76                  // TODO Check GenericArrayType
77                  String message = "Cannot create property=home for JavaBean=org.yaml.snakeyaml.generics.GenericArrayTypeTest$GenericArray";
78                  assertTrue(e.getMessage(), e.getMessage().contains(message));
79              }
80          }
81      }
82  
83      public static class GenericArray extends AbstractAnimal<Integer[]> {
84          private Integer[] home;
85  
86          public GenericArray() {
87              home = new Integer[3];
88              for (int i = 0; i < home.length; i++) {
89                  home[i] = i + 1;
90              }
91              setName("Array" + String.valueOf(3));
92          }
93  
94          @Override
95          public Integer[] getHome() {
96              return home;
97          }
98  
99          @Override
100         public void setHome(Integer[] home) {
101             this.home = home;
102         }
103     }
104 
105     public void testJavaBean() throws IntrospectionException {
106         GenericArray ga = new GenericArray();
107         ArrayBean bean = new ArrayBean();
108         bean.setId("ID556677");
109         bean.setGa(ga);
110         Yaml dumper = new Yaml();
111         String doc = dumper.dumpAsMap(bean);
112         // System.out.println(doc);
113         assertEquals(Util.getLocalResource("javabeans/genericArray-1.yaml"), doc);
114         //
115         Yaml beanLoader = new Yaml();
116         if (GenericsBugDetector.isProperIntrospection()) {
117             ArrayBean loaded = beanLoader.loadAs(doc, ArrayBean.class);
118             assertEquals("ID556677", loaded.getId());
119             assertEquals("Array3", loaded.getGa().getName());
120             assertEquals(3, loaded.getGa().getHome().length);
121         } else {
122             try {
123                 beanLoader.load(doc);
124             } catch (Exception e) {
125                 // TODO Check GenericArrayType
126                 String message = "Cannot create property=home for JavaBean=org.yaml.snakeyaml.generics.GenericArrayTypeTest$GenericArray";
127                 assertTrue(e.getMessage(), e.getMessage().contains(message));
128             }
129         }
130     }
131 
132     public static class ArrayBean {
133         private String id;
134         private GenericArray ga;
135 
136         public String getId() {
137             return id;
138         }
139 
140         public void setId(String id) {
141             this.id = id;
142         }
143 
144         public GenericArray getGa() {
145             return ga;
146         }
147 
148         public void setGa(GenericArray ga) {
149             this.ga = ga;
150         }
151     }
152 }