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.issue47;
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 ReadOnlyPropertiesTest extends TestCase {
25      public void testBean1() {
26          IncompleteBean bean = new IncompleteBean();
27          bean.setName("lunch");
28          Yaml yaml = new Yaml();
29          String output = yaml.dumpAsMap(bean);
30          // System.out.println(output);
31          assertEquals("name: lunch\n", output);
32          //
33          Yaml loader = new Yaml();
34          IncompleteBean parsed = loader.loadAs(output, IncompleteBean.class);
35          assertEquals(bean.getName(), parsed.getName());
36      }
37  
38      public void testBean2() {
39          IncompleteBean bean = new IncompleteBean();
40          bean.setName("lunch");
41          DumperOptions options = new DumperOptions();
42          options.setAllowReadOnlyProperties(true);
43          Yaml yaml = new Yaml(options);
44          String output = yaml.dumpAsMap(bean);
45          // System.out.println(output);
46          assertEquals("id: 10\nname: lunch\n", output);
47          //
48          Yaml loader = new Yaml();
49          try {
50              loader.loadAs(output, IncompleteBean.class);
51              fail("Setter is missing.");
52          } catch (YAMLException e) {
53              String message = e.getMessage();
54              assertTrue(
55                      message,
56                      message.contains("Unable to find property 'id' on class: org.yaml.snakeyaml.issues.issue47.IncompleteBean"));
57          }
58      }
59  }