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