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