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