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;
18  
19  import java.sql.Timestamp;
20  import java.util.Date;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.nodes.Node;
25  import org.yaml.snakeyaml.nodes.Tag;
26  import org.yaml.snakeyaml.representer.Represent;
27  import org.yaml.snakeyaml.representer.Representer;
28  
29  public class JavaBeanWithNullValuesTest extends TestCase {
30      private Yaml loader;
31  
32      @Override
33      protected void setUp() throws Exception {
34          loader = new Yaml();
35      }
36  
37      public void testNotNull() throws Exception {
38          String dumpStr = dumpJavaBeanWithNullValues(false);
39          // System.out.println(dumpStr);
40          Yaml yaml = new Yaml();
41          JavaBeanWithNullValues parsed = (JavaBeanWithNullValues) yaml.load(dumpStr);
42          assertNotNull(parsed.getString());
43          assertNotNull(parsed.getBoolean1());
44          assertNotNull(parsed.getDate());
45          assertNotNull(parsed.getDouble1());
46          assertNotNull(parsed.getFloat1());
47          assertNotNull(parsed.getInteger());
48          assertNotNull(parsed.getLong1());
49          assertNotNull(parsed.getSqlDate());
50          assertNotNull(parsed.getTimestamp());
51          //
52          parsed = loader.loadAs(dumpStr, JavaBeanWithNullValues.class);
53          assertNotNull(parsed.getString());
54          assertNotNull(parsed.getBoolean1());
55          assertNotNull(parsed.getDate());
56          assertNotNull(parsed.getDouble1());
57          assertNotNull(parsed.getFloat1());
58          assertNotNull(parsed.getInteger());
59          assertNotNull(parsed.getLong1());
60          assertNotNull(parsed.getSqlDate());
61          assertNotNull(parsed.getTimestamp());
62      }
63  
64      public void testNull() throws Exception {
65          String dumpStr = dumpJavaBeanWithNullValues(true);
66          Yaml yaml = new Yaml();
67          JavaBeanWithNullValues parsed = (JavaBeanWithNullValues) yaml.load(dumpStr);
68          assertNull(parsed.getString());
69          //
70          parsed = loader.loadAs(dumpStr, JavaBeanWithNullValues.class);
71          assertNull(parsed.getString());
72      }
73  
74      public void testNullStringAndBoolean() throws Exception {
75          JavaBeanWithNullValues javaBeanWithNullValues = new JavaBeanWithNullValues();
76          DumperOptions options = new DumperOptions();
77          options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
78          options.setExplicitStart(true);
79          options.setExplicitEnd(true);
80          Yaml yaml = new Yaml(new CustomRepresenter(), options);
81          javaBeanWithNullValues.setBoolean1(null);
82          javaBeanWithNullValues.setDate(new Date(System.currentTimeMillis()));
83          javaBeanWithNullValues.setDouble1(1d);
84          javaBeanWithNullValues.setFloat1(1f);
85          javaBeanWithNullValues.setInteger(1);
86          javaBeanWithNullValues.setLong1(1l);
87          javaBeanWithNullValues.setSqlDate(new java.sql.Date(System.currentTimeMillis()));
88          javaBeanWithNullValues.setString(null); // ok
89          javaBeanWithNullValues.setTimestamp(new Timestamp(System.currentTimeMillis()));
90  
91          String dumpStr = yaml.dump(javaBeanWithNullValues);
92          // System.out.println(dumpStr);
93          yaml = new Yaml();
94          JavaBeanWithNullValues parsed = (JavaBeanWithNullValues) yaml.load(dumpStr);
95          assertNull(" expect null, got " + parsed.getBoolean1(), parsed.getBoolean1());
96          assertNull(" expect null, got " + parsed.getString(), parsed.getString());
97      }
98  
99      public void testNoRootTag() throws Exception {
100         JavaBeanWithNullValues javaBeanWithNullValues = new JavaBeanWithNullValues();
101         DumperOptions options = new DumperOptions();
102         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
103         options.setExplicitStart(true);
104         options.setExplicitEnd(true);
105         Yaml yaml = new Yaml(new CustomRepresenter(), options);
106         javaBeanWithNullValues.setBoolean1(null);
107         javaBeanWithNullValues.setDate(new Date(System.currentTimeMillis()));
108         javaBeanWithNullValues.setDouble1(1d);
109         javaBeanWithNullValues.setFloat1(1f);
110         javaBeanWithNullValues.setInteger(1);
111         javaBeanWithNullValues.setLong1(1l);
112         javaBeanWithNullValues.setSqlDate(new java.sql.Date(System.currentTimeMillis()));
113         javaBeanWithNullValues.setString(null); // ok
114         javaBeanWithNullValues.setTimestamp(new Timestamp(System.currentTimeMillis()));
115 
116         String dumpStr = yaml.dumpAsMap(javaBeanWithNullValues);
117         // System.out.println(dumpStr);
118         assertFalse("No explicit root tag must be used.",
119                 dumpStr.contains("JavaBeanWithNullValues"));
120         yaml = new Yaml(new CustomRepresenter(), options);
121         JavaBeanWithNullValues parsed = loader.loadAs(dumpStr, JavaBeanWithNullValues.class);
122         assertNull(" expect null, got " + parsed.getBoolean1(), parsed.getBoolean1());
123         assertNull(" expect null, got " + parsed.getString(), parsed.getString());
124         assertEquals(1d, parsed.getDouble1());
125         assertEquals(1f, parsed.getFloat1());
126         assertEquals(new Integer(1), parsed.getInteger());
127         assertEquals(new Long(1l), parsed.getLong1());
128     }
129 
130     private String dumpJavaBeanWithNullValues(boolean nullValues) {
131         JavaBeanWithNullValues javaBeanWithNullValues = new JavaBeanWithNullValues();
132         DumperOptions options = new DumperOptions();
133         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
134         options.setExplicitStart(true);
135         options.setExplicitEnd(true);
136         Yaml yaml = new Yaml(new CustomRepresenter(), options);
137         if (nullValues) {
138             return yaml.dump(javaBeanWithNullValues);
139         }
140         javaBeanWithNullValues.setBoolean1(false);
141         javaBeanWithNullValues.setDate(new Date(System.currentTimeMillis()));
142         javaBeanWithNullValues.setDouble1(1d);
143         javaBeanWithNullValues.setFloat1(1f);
144         javaBeanWithNullValues.setInteger(1);
145         javaBeanWithNullValues.setLong1(1l);
146         javaBeanWithNullValues.setSqlDate(new java.sql.Date(System.currentTimeMillis()));
147         javaBeanWithNullValues.setString(""); // ok
148         javaBeanWithNullValues.setTimestamp(new Timestamp(System.currentTimeMillis()));
149         return yaml.dump(javaBeanWithNullValues);
150     }
151 
152     public class CustomRepresenter extends Representer {
153         public CustomRepresenter() {
154             this.representers.put(Float.class, new RepresentFloat());
155             this.representers.put(Long.class, new RepresentLong());
156             this.representers.put(java.sql.Date.class, new RepresentDate());
157             this.representers.put(java.sql.Timestamp.class, new RepresentTime());
158         }
159 
160         private class RepresentFloat implements Represent {
161             public Node representData(Object data) {
162                 return representScalar(new Tag(Tag.PREFIX + "java.lang.Float"),
163                         ((Float) data).toString());
164             }
165         }
166 
167         private class RepresentLong implements Represent {
168             public Node representData(Object data) {
169                 return representScalar(new Tag(Tag.PREFIX + "java.lang.Long"),
170                         ((Long) data).toString());
171             }
172         }
173 
174         private class RepresentDate implements Represent {
175             public Node representData(Object data) {
176                 return representScalar(new Tag(Tag.PREFIX + "java.sql.Date"),
177                         ((java.sql.Date) data).toString());
178             }
179         }
180 
181         private class RepresentTime implements Represent {
182             public Node representData(Object data) {
183                 return representScalar(new Tag(Tag.PREFIX + "java.sql.Timestamp"),
184                         ((java.sql.Timestamp) data).toString());
185             }
186         }
187     }
188 }