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.javabeans;
18  
19  import java.io.IOException;
20  import java.io.Serializable;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.Yaml;
25  
26  public class ConstructEmptyBeanTest extends TestCase {
27      /**
28       * standard Yaml
29       */
30      public void testEmptyBean() throws IOException {
31          Yaml yaml = new Yaml();
32          EmptyBean bean = (EmptyBean) yaml
33                  .load("!!org.yaml.snakeyaml.javabeans.ConstructEmptyBeanTest$EmptyBean {}");
34          assertNotNull(bean);
35          assertNull(bean.getFirstName());
36          assertEquals(5, bean.getHatSize());
37      }
38  
39      /**
40       * global tag is correct (but ignored)
41       */
42      public void testEmptyBean1() throws IOException {
43          Yaml beanLoader = new Yaml();
44          EmptyBean bean = beanLoader.loadAs(
45                  "!!org.yaml.snakeyaml.javabeans.ConstructEmptyBeanTest$EmptyBean {}",
46                  EmptyBean.class);
47          assertNotNull(bean);
48          assertNull(bean.getFirstName());
49          assertEquals(5, bean.getHatSize());
50      }
51  
52      /**
53       * global tag is ignored
54       */
55      public void testEmptyBean2() throws IOException {
56          Yaml beanLoader = new Yaml();
57          EmptyBean bean = beanLoader.loadAs("!!Bla-bla-bla {}", EmptyBean.class);
58          assertNotNull(bean);
59          assertNull(bean.getFirstName());
60          assertEquals(5, bean.getHatSize());
61      }
62  
63      /**
64       * no tag
65       */
66      public void testEmptyBean3() throws IOException {
67          Yaml beanLoader = new Yaml();
68          EmptyBean bean = beanLoader.loadAs("{   }", EmptyBean.class);
69          assertNotNull(bean);
70          assertNull(bean.getFirstName());
71          assertEquals(5, bean.getHatSize());
72      }
73  
74      /**
75       * empty document
76       */
77      public void testEmptyBean4() throws IOException {
78          Yaml beanLoader = new Yaml();
79          EmptyBean bean = beanLoader.loadAs("", EmptyBean.class);
80          assertNull(bean);
81      }
82  
83      /**
84       * local tag is ignored
85       */
86      public void testEmptyBean5() throws IOException {
87          Yaml beanLoader = new Yaml();
88          EmptyBean bean = beanLoader.loadAs("!Bla-bla-bla {}", EmptyBean.class);
89          assertNotNull(bean);
90          assertNull(bean.getFirstName());
91          assertEquals(5, bean.getHatSize());
92      }
93  
94      /**
95       * invalid document
96       */
97      public void testEmptyBean6() throws IOException {
98          Yaml beanLoader = new Yaml();
99          try {
100             beanLoader.loadAs("{", EmptyBean.class);
101             fail("Invalid document provided.");
102         } catch (Exception e) {
103             assertEquals(
104                     "while parsing a flow node; expected the node content, but found StreamEnd",
105                     e.getMessage());
106         }
107     }
108 
109     public static class EmptyBean implements Serializable {
110         private static final long serialVersionUID = -8001155967276657180L;
111         private String firstName;
112         private int hatSize = 5;
113 
114         public EmptyBean() {
115         }
116 
117         public String getFirstName() {
118             return firstName;
119         }
120 
121         public void setFirstName(String firstName) {
122             this.firstName = firstName;
123         }
124 
125         public int getHatSize() {
126             return hatSize;
127         }
128 
129         public void setHatSize(int hatSize) {
130             this.hatSize = hatSize;
131         }
132     }
133 }