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.issue55;
18  
19  import java.util.LinkedList;
20  import java.util.List;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.Util;
25  import org.yaml.snakeyaml.Yaml;
26  import org.yaml.snakeyaml.introspector.BeanAccess;
27  
28  public class JavaBeanListTest extends TestCase {
29  
30      public void testYaml() {
31          Yaml beanLoader = new Yaml();
32          beanLoader.setBeanAccess(BeanAccess.FIELD);
33          BlogBean rehydrated = (BlogBean) beanLoader.loadAs(
34                  Util.getLocalResource("issues/issue55_2.txt"), BlogBean.class);
35          assertEquals(4, rehydrated.getPosts().size());
36      }
37  
38      public void testFailureWithoutFieldAccess() {
39          Yaml beanLoader = new Yaml();
40          try {
41              beanLoader.loadAs(Util.getLocalResource("issues/issue55_2.txt"), BlogBean.class);
42              fail("Private field must not be available");
43          } catch (Exception e) {
44              assertTrue(e.getMessage().contains("Unable to find property 'posts'"));
45          }
46      }
47  
48      public static class BlogBean {
49          private List<Integer> posts;
50  
51          public BlogBean() {
52              posts = new LinkedList<Integer>();
53          }
54  
55          public List<Integer> getPosts() {
56              return posts;
57          }
58      }
59  }