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.HashSet;
    20  import java.util.LinkedHashSet;
    21  import java.util.Set;
    22  import java.util.TreeSet;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.yaml.snakeyaml.DumperOptions;
    27  import org.yaml.snakeyaml.Util;
    28  import org.yaml.snakeyaml.Yaml;
    29  import org.yaml.snakeyaml.introspector.BeanAccess;
    30
    31  public class SetAsSequenceTest extends TestCase {
    32
    33      public void testDump() {
    34          Blog blog = new Blog("Test Me!");
    35          blog.addPost(new Post("Title1", "text 1"));
    36          blog.addPost(new Post("Title2", "text text 2"));
    37          blog.numbers.add(19);
    38          blog.numbers.add(17);
    39          TreeSet<String> labels = new TreeSet<String>();
    40          labels.add("Java");
    41          labels.add("YAML");
    42          labels.add("SnakeYAML");
    43          blog.setLabels(labels);
    44          DumperOptions options = new DumperOptions();
    45          options.setAllowReadOnlyProperties(true);
    46          Yaml yaml = new Yaml(options);
    47          String output = yaml.dump(blog);
    48          // System.out.println(output);
    49          assertEquals(Util.getLocalResource("issues/issue73-1.txt"), output);
    50      }
    51
    52      public void testLoad() {
    53          Yaml yaml = new Yaml();
    54          yaml.setBeanAccess(BeanAccess.FIELD);
    55          String doc = Util.getLocalResource("issues/issue73-1.txt");
    56          Blog blog = (Blog) yaml.load(doc);
    57          // System.out.println(blog);
    58          assertEquals("Test Me!", blog.getName());
    59          assertEquals(2, blog.numbers.size());
    60          assertEquals(2, blog.getPosts().size());
    61          for (Post post : blog.getPosts()) {
    62              assertEquals(Post.class, post.getClass());
    63          }
    64      }
    65
    66      public void testYaml() {
    67          String serialized = Util.getLocalResource("issues/issue73-2.txt");
    68          // System.out.println(serialized);
    69          Yaml beanLoader = new Yaml();
    70          beanLoader.setBeanAccess(BeanAccess.FIELD);
    71          Blog rehydrated = beanLoader.loadAs(serialized, Blog.class);
    72          checkTestBlog(rehydrated);
    73      }
    74
    75      protected void checkTestBlog(Blog blog) {
    76          Set<Post> posts = blog.getPosts();
    77          assertEquals("Blog contains 2 posts", 2, posts.size());
    78          assertTrue(posts.contains(new Post("Test", "Dummy")));
    79          assertTrue(posts.contains(new Post("Highly", "Creative")));
    80          assertEquals("No tags!", blog.getName());
    81          assertEquals(0, blog.numbers.size());
    82      }
    83
    84      @SuppressWarnings("unchecked")
    85      public void testLoadRootSet() {
    86          Yaml yaml = new Yaml();
    87          String doc = Util.getLocalResource("issues/issue73-3.txt");
    88          Set<String> strings = (Set<String>) yaml.load(doc);
    89          // System.out.println(strings);
    90          assertEquals(3, strings.size());
    91          assertEquals(HashSet.class, strings.getClass());
    92          assertTrue(strings.contains("aaa"));
    93          assertTrue(strings.contains("bbb"));
    94          assertTrue(strings.contains("ccc"));
    95      }
    96
    97      @SuppressWarnings("unchecked")
    98      public void testLoadRootSet2() {
    99          Yaml yaml = new Yaml();
    100         String doc = "!!java.util.HashSet {aaa: null, bbb: null, ccc: null}";
    101         Set<String> strings = (Set<String>) yaml.load(doc);
    102         // System.out.println(strings);
    103         assertEquals(3, strings.size());
    104         assertEquals(LinkedHashSet.class, strings.getClass());
    105         assertTrue(strings.contains("aaa"));
    106         assertTrue(strings.contains("bbb"));
    107         assertTrue(strings.contains("ccc"));
    108     }
    109
    110     @SuppressWarnings("unchecked")
    111     public void testLoadRootSet3() {
    112         Yaml yaml = new Yaml();
    113         String doc = "!!java.util.TreeSet {aaa: null, bbb: null, ccc: null}";
    114         Set<String> strings = (Set<String>) yaml.load(doc);
    115         // System.out.println(strings);
    116         assertEquals(3, strings.size());
    117         assertEquals(TreeSet.class, strings.getClass());
    118         assertTrue(strings.contains("aaa"));
    119         assertTrue(strings.contains("bbb"));
    120         assertTrue(strings.contains("ccc"));
    121     }
    122
    123     @SuppressWarnings("unchecked")
    124     public void testLoadRootSet6() {
    125         Yaml yaml = new Yaml();
    126         String doc = Util.getLocalResource("issues/issue73-6.txt");
    127         Set<String> strings = (Set<String>) yaml.load(doc);
    128         // System.out.println(strings);
    129         assertEquals(3, strings.size());
    130         assertEquals(TreeSet.class, strings.getClass());
    131         assertTrue(strings.contains("aaa"));
    132         assertTrue(strings.contains("bbb"));
    133         assertTrue(strings.contains("ccc"));
    134     }
    135 }