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.issue73;
18  
19  import java.util.Set;
20  import java.util.TreeSet;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.DumperOptions;
25  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
26  import org.yaml.snakeyaml.Util;
27  import org.yaml.snakeyaml.Yaml;
28  import org.yaml.snakeyaml.introspector.BeanAccess;
29  import org.yaml.snakeyaml.nodes.Node;
30  import org.yaml.snakeyaml.nodes.Tag;
31  import org.yaml.snakeyaml.representer.Represent;
32  import org.yaml.snakeyaml.representer.Representer;
33  
34  public class DumpSetAsSequenceExampleTest extends TestCase {
35  
36      public void testDumpFlow() {
37          DumperOptions options = new DumperOptions();
38          options.setAllowReadOnlyProperties(true);
39          Yaml yaml = new Yaml(new SetRepresenter(), options);
40          String output = yaml.dump(createBlog());
41          // System.out.println(output);
42          assertEquals(Util.getLocalResource("issues/issue73-dump7.txt"), output);
43          //
44          check(output);
45      }
46  
47      public void testDumpBlock() {
48          DumperOptions options = new DumperOptions();
49          options.setAllowReadOnlyProperties(true);
50          options.setDefaultFlowStyle(FlowStyle.BLOCK);
51          Yaml yaml = new Yaml(new SetRepresenter(), options);
52          String output = yaml.dump(createBlog());
53          // System.out.println(output);
54          assertEquals(Util.getLocalResource("issues/issue73-dump8.txt"), output);
55          //
56          check(output);
57      }
58  
59      private class SetRepresenter extends Representer {
60          public SetRepresenter() {
61              this.multiRepresenters.put(Set.class, new RepresentIterable());
62          }
63  
64          private class RepresentIterable implements Represent {
65              @SuppressWarnings("unchecked")
66              public Node representData(Object data) {
67                  return representSequence(getTag(data.getClass(), Tag.SEQ), (Iterable<Object>) data,
68                          null);
69  
70              }
71          }
72      }
73  
74      private Blog createBlog() {
75          Blog blog = new Blog("Test Me!");
76          blog.addPost(new Post("Title1", "text 1"));
77          blog.addPost(new Post("Title2", "text text 2"));
78          blog.numbers.add(19);
79          blog.numbers.add(17);
80          TreeSet<String> labels = new TreeSet<String>();
81          labels.add("Java");
82          labels.add("YAML");
83          labels.add("SnakeYAML");
84          blog.setLabels(labels);
85          return blog;
86      }
87  
88      private void check(String doc) {
89          Yaml yamlLoader = new Yaml();
90          yamlLoader.setBeanAccess(BeanAccess.FIELD);
91          Blog blog = (Blog) yamlLoader.load(doc);
92          assertEquals("Test Me!", blog.getName());
93          assertEquals(2, blog.numbers.size());
94          assertEquals(2, blog.getPosts().size());
95          for (Post post : blog.getPosts()) {
96              assertEquals(Post.class, post.getClass());
97          }
98      }
99  }