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.issue116;
    18
    19  import junit.framework.TestCase;
    20
    21  import org.yaml.snakeyaml.Yaml;
    22  import org.yaml.snakeyaml.error.YAMLException;
    23
    24  public class NoFieldsTest extends TestCase {
    25
    26      public void testEmptyClass() {
    27          Empty empty = new Empty();
    28          Yaml yaml = new Yaml();
    29          String result = yaml.dump(empty);
    30          assertEquals("!!org.yaml.snakeyaml.issues.issue116.Empty {}\n", result);
    31          Object emptyParsed = yaml.load(result);
    32          assertTrue(emptyParsed instanceof Empty);
    33      }
    34
    35      public void testHiddenParameter() {
    36          Hidden hidden = new Hidden();
    37          Yaml yaml = new Yaml();
    38          try {
    39              yaml.dump(hidden);
    40              fail("an exception should have been thrown");
    41          } catch (YAMLException e) {
    42              assertEquals(e.getMessage(),
    43                      "No JavaBean properties found in org.yaml.snakeyaml.issues.issue116.Hidden");
    44          }
    45          Object hiddenParsed = yaml.load("!!org.yaml.snakeyaml.issues.issue116.Hidden {}\n");
    46          assertTrue(hiddenParsed instanceof Hidden);
    47      }
    48
    49      public void testSpecialHiddenParameter() {
    50          HiddenSpecial hidden = new HiddenSpecial("qwerty");
    51          Yaml yaml = new Yaml();
    52          try {
    53              yaml.dump(hidden);
    54              fail("an exception should have been thrown");
    55          } catch (YAMLException e) {
    56              assertEquals(e.getMessage(),
    57                      "No JavaBean properties found in org.yaml.snakeyaml.issues.issue116.HiddenSpecial");
    58          }
    59          HiddenSpecial hs = (HiddenSpecial) yaml
    60                  .load("!!org.yaml.snakeyaml.issues.issue116.HiddenSpecial foo\n");
    61          assertEquals("foo".hashCode(), hs.retrieveMyVerySpecialField());
    62      }
    63  }
    64
    65  class Empty {
    66  }
    67
    68  class Hidden {
    69      @SuppressWarnings("unused")
    70      private int inaccessableField;
    71  }