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.issues.issue154;
17  
18  import junit.framework.TestCase;
19  
20  import org.yaml.snakeyaml.Yaml;
21  import org.yaml.snakeyaml.constructor.Constructor;
22  import org.yaml.snakeyaml.error.YAMLException;
23  import org.yaml.snakeyaml.representer.Representer;
24  
25  public class MissingPropertyTest extends TestCase {
26  
27      private Yaml yaml;
28  
29      public void setUp() {
30          yaml = new Yaml();
31      }
32  
33      /**
34       * A normal scalar property should work fine.
35       */
36      public void testPublicField() throws Exception {
37          String doc = "hello: 5";
38          TestBean bean = yaml.loadAs(doc, TestBean.class);
39          assertNotNull(bean);
40          assertEquals(5, bean.hello);
41      }
42  
43      /**
44       * By default, unknown fields should throw a YAMLException.
45       */
46      public void testUnknownField() throws Exception {
47          try {
48              String doc = "goodbye: 10";
49              yaml.loadAs(doc, TestBean.class);
50          } catch (YAMLException e) {
51              assertTrue(e.getMessage().contains("Cannot create property=goodbye"));
52          }
53      }
54  
55      /**
56       * A new method setSkipMissingProperties(boolean) was added to configure
57       * whether missing properties should throw a YAMLException (the default) or
58       * simply show a warning. The default is false.
59       */
60      public void testSkipMissingProperties() throws Exception {
61          Representer representer = new Representer();
62          representer.getPropertyUtils().setSkipMissingProperties(true);
63          yaml = new Yaml(new Constructor(), representer);
64          String doc = "goodbye: 10\nhello: 5\nfizz: [1]";
65          TestBean bean = yaml.loadAs(doc, TestBean.class);
66          assertNotNull(bean);
67          assertEquals(5, bean.hello);
68      }
69  
70      /**
71       * The default for setSkipMissingProperties(boolean) is false; this just
72       * ensures it works if set manually.
73       */
74      public void testNoSkipMissingProperties() throws Exception {
75          try {
76              Representer representer = new Representer();
77              representer.getPropertyUtils().setSkipMissingProperties(false);
78              yaml = new Yaml(new Constructor(), representer);
79              String doc = "goodbye: 10";
80              yaml.loadAs(doc, TestBean.class);
81          } catch (YAMLException e) {
82              assertTrue(e.getMessage().contains("Cannot create property=goodbye"));
83          }
84      }
85  }