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