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.constructor;
17  
18  import junit.framework.TestCase;
19  
20  import org.yaml.snakeyaml.DumperOptions;
21  import org.yaml.snakeyaml.Yaml;
22  import org.yaml.snakeyaml.error.YAMLException;
23  
24  public class IncompleteBeanConstructorTest extends TestCase {
25  
26      public void testRepresentor() {
27          IncompleteJavaBean bean = new IncompleteJavaBean();
28          DumperOptions options = new DumperOptions();
29          options.setAllowReadOnlyProperties(true);
30          Yaml yaml = new Yaml(options);
31          String output = yaml.dump(bean);
32          String className = this.getClass().getPackage().getName();
33          assertEquals("!!" + className + ".IncompleteJavaBean {name: No name}\n", output);
34      }
35  
36      public void testConstructor() {
37          String className = "!!" + this.getClass().getPackage().getName()
38                  + ".IncompleteJavaBean {number: 2}";
39          Yaml yaml = new Yaml();
40          IncompleteJavaBean bean = (IncompleteJavaBean) yaml.load(className);
41          assertNotNull(bean);
42          assertEquals("No name", bean.getName());
43          assertEquals(2, bean.obtainNumber());
44      }
45  
46      public void testConstructor2() {
47          String className = "!!" + this.getClass().getPackage().getName()
48                  + ".IncompleteJavaBean {number: 2, name: Bill}";
49          Yaml yaml = new Yaml();
50          try {
51              yaml.load(className);
52              fail("'name' property does not have setter.");
53          } catch (YAMLException e) {
54              assertEquals(
55                      "Cannot create property=name for JavaBean=<IncompleteJavaBean name=No name>; Unable to find property 'name' on class: org.yaml.snakeyaml.constructor.IncompleteJavaBean",
56                      e.getCause().getMessage());
57          }
58      }
59  }