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