• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.representer;
17 
18 import junit.framework.TestCase;
19 
20 import org.yaml.snakeyaml.DumperOptions;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.nodes.Tag;
23 
24 public class RepresenterTest extends TestCase {
25 
testRepresenter()26     public void testRepresenter() {
27         MyBean bean = new MyBean();
28         bean.setName("Gnome");
29         bean.setValid(true);
30         bean.setPrimitive(true);
31         Yaml yaml = new Yaml();
32         assertEquals(
33                 "!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean {name: Gnome, primitive: true}\n",
34                 yaml.dump(bean));
35     }
36 
37     public static class MyBean {
38         private String name;
39         private Boolean valid;
40         private boolean primitive;
41 
getName()42         public String getName() {
43             return name;
44         }
45 
setName(String name)46         public void setName(String name) {
47             this.name = name;
48         }
49 
isValid()50         public Boolean isValid() {
51             return valid;
52         }
53 
setValid(Boolean valid)54         public void setValid(Boolean valid) {
55             this.valid = valid;
56         }
57 
isPrimitive()58         public boolean isPrimitive() {
59             return primitive;
60         }
61 
setPrimitive(boolean primitive)62         public void setPrimitive(boolean primitive) {
63             this.primitive = primitive;
64         }
65     }
66 
testRepresenterNoConstructorAvailable()67     public void testRepresenterNoConstructorAvailable() {
68         MyBean2 bean = new MyBean2("Gnome", true);
69         DumperOptions options = new DumperOptions();
70         options.setAllowReadOnlyProperties(true);
71         Yaml yaml = new Yaml(options);
72         assertEquals("!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean2 {valid: true}\n",
73                 yaml.dump(bean));
74     }
75 
76     public static class MyBean2 {
77         private String name;
78         private Boolean valid;
79 
MyBean2(String name, Boolean valid)80         public MyBean2(String name, Boolean valid) {
81             this();
82             this.name = name;
83             this.valid = valid;
84         }
85 
MyBean2()86         private MyBean2() {
87             super();
88         }
89 
getName()90         private String getName() {
91             return name;
92         }
93 
getValid()94         public Boolean getValid() {
95             return valid;
96         }
97 
98         @Override
toString()99         public String toString() {
100             return getName() + " " + getValid();
101         }
102     }
103 
testRepresenterGetterWithException()104     public void testRepresenterGetterWithException() {
105         MyBean3 bean = new MyBean3("Gnome", false);
106         DumperOptions options = new DumperOptions();
107         options.setAllowReadOnlyProperties(true);
108         Yaml yaml = new Yaml(options);
109         try {
110             String str = yaml.dump(bean);
111             fail("Exception must be reported: " + str);
112         } catch (Exception e) {
113             assertTrue(true);
114         }
115         // no exception
116         MyBean3 bean2 = new MyBean3("Gnome", true);
117         String str = yaml.dump(bean2);
118         // isValid is no JavaBean property (it must be a primitive then)
119         assertEquals(
120                 "isValid property must not be dumped.",
121                 "!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean3 {boolProperty: true, name: Gnome}\n",
122                 str);
123     }
124 
125     public static class MyBean3 {
126         private String name;
127         private Boolean valid;
128         private boolean boolProperty;
129 
MyBean3(String name, Boolean valid)130         public MyBean3(String name, Boolean valid) {
131             this.name = name;
132             this.valid = valid;
133             boolProperty = true;
134         }
135 
getName()136         public String getName() {
137             if (valid) {
138                 return name;
139             } else {
140                 throw new UnsupportedOperationException("Test.");
141             }
142         }
143 
isValid()144         public Boolean isValid() {
145             return valid;
146         }
147 
isBoolProperty()148         public boolean isBoolProperty() {
149             return boolProperty;
150         }
151 
152         @Override
toString()153         public String toString() {
154             return "MyBean3<" + name + ", " + isValid() + ">";
155         }
156     }
157 
testRepresenterAddNull()158     public void testRepresenterAddNull() {
159         Representer representer = new Representer();
160         try {
161             representer.addClassTag(EmptyBean.class, (Tag) null);
162             fail("Tag must be provided.");
163         } catch (Exception e) {
164             assertEquals("Tag must be provided.", e.getMessage());
165         }
166     }
167 
testRepresenterEmptyBean()168     public void testRepresenterEmptyBean() {
169         EmptyBean bean = new EmptyBean();
170         Yaml yaml = new Yaml();
171         try {
172             yaml.dump(bean);
173             fail("EmptyBean has empty representation.");
174         } catch (Exception e) {
175             assertEquals(
176                     "No JavaBean properties found in org.yaml.snakeyaml.representer.RepresenterTest$EmptyBean",
177                     e.getMessage());
178         }
179     }
180 
181     public static class EmptyBean {
182         private int number;
183 
process()184         public void process() {
185             number += 1;
186         }
187 
obtain()188         public int obtain() {
189             return number;
190         }
191     }
192 }
193