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.Collection;
20  import java.util.Map;
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  import org.yaml.snakeyaml.representer.Representer;
28  
29  public class YamlFieldAccessCollectionTest extends TestCase {
30  
31      public void testYaml() {
32          Blog original = createTestBlog();
33          Yaml yamlDumper = constructYamlDumper();
34          String serialized = yamlDumper.dumpAsMap(original);
35          // System.out.println(serialized);
36          assertEquals(Util.getLocalResource("issues/issue55_1.txt"), serialized);
37          Yaml blogLoader = new Yaml();
38          blogLoader.setBeanAccess(BeanAccess.FIELD);
39          Blog rehydrated = blogLoader.loadAs(serialized, Blog.class);
40          checkTestBlog(rehydrated);
41      }
42  
43      @SuppressWarnings("unchecked")
44      public void testYamlWithoutConfiguration() {
45          Yaml yaml = new Yaml();
46          Map<String, Object> map = (Map<String, Object>) yaml.load(Util
47                  .getLocalResource("issues/issue55_1.txt"));
48          assertEquals(1, map.size());
49      }
50  
51      public void testYamlFailure() {
52          Yaml beanLoader = new Yaml();
53          try {
54              beanLoader.loadAs(Util.getLocalResource("issues/issue55_1.txt"), Blog.class);
55              fail("BeanAccess.FIELD is required.");
56          } catch (Exception e) {
57              assertTrue(e.getMessage(), e.getMessage().contains("Unable to find property 'posts'"));
58          }
59      }
60  
61      public void testYamlDefaultWithFeildAccess() {
62          Yaml yaml = new Yaml();
63          yaml.setBeanAccess(BeanAccess.FIELD);
64          Blog original = createTestBlog();
65          String serialized = yaml.dump(original);
66          assertEquals(Util.getLocalResource("issues/issue55_1_rootTag.txt"), serialized);
67          Blog rehydrated = (Blog) yaml.load(serialized);
68          checkTestBlog(rehydrated);
69      }
70  
71      protected Yaml constructYamlDumper() {
72          Representer representer = new Representer();
73          representer.getPropertyUtils().setBeanAccess(BeanAccess.FIELD);
74          Yaml yaml = new Yaml(representer);
75          return yaml;
76      }
77  
78      protected Yaml constructYamlParser() {
79          Yaml yaml = new Yaml();
80          yaml.setBeanAccess(BeanAccess.FIELD);
81          return yaml;
82      }
83  
84      protected Blog createTestBlog() {
85          Post post1 = new Post("Test", "Dummy");
86          Post post2 = new Post("Highly", "Creative");
87          Blog blog = new Blog();
88          blog.addPost(post1);
89          blog.addPost(post2);
90          return blog;
91      }
92  
93      protected void checkTestBlog(Blog blog) {
94          Collection<Post> posts = blog.getPosts();
95          assertEquals("Blog contains 2 posts", 2, posts.size());
96      }
97  }