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.issue73;
17  
18  import java.util.HashSet;
19  import java.util.LinkedHashSet;
20  import java.util.Set;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.Util;
25  import org.yaml.snakeyaml.Yaml;
26  
27  public class RecursiveSetTest extends TestCase {
28      public void testDumpException() {
29          Set<Object> set1 = new HashSet<Object>();
30          Set<Object> set2 = new HashSet<Object>();
31          set1.add(set2);
32          set2.add(set1);
33          Yaml yaml = new Yaml();
34          try {
35              yaml.dump(set1);
36              fail("Recursive sets are not supported.");
37          } catch (StackOverflowError e) {
38              assertEquals(null, e.getMessage());
39          }
40      }
41  
42      public void testLoadException() {
43          String doc = Util.getLocalResource("issues/issue73-recursive4.txt");
44          // System.out.println(doc);
45          Yaml yaml = new Yaml();
46          try {
47              yaml.load(doc);
48              fail("Recursive sets are not supported.");
49          } catch (Exception e) {
50              assertTrue(e.getMessage(), e.getMessage().contains("Set cannot be recursive."));
51          }
52      }
53  
54      /**
55       * XXX: sets can be recursive
56       */
57      @SuppressWarnings("unchecked")
58      public void testLoadRecursiveTest() {
59          String doc = Util.getLocalResource("issues/issue73-recursive5.txt");
60          // System.out.println(doc);
61          Yaml yaml = new Yaml();
62          Bean1 obj = (Bean1) yaml.load(doc);
63          Set<Object> set = obj.getSet();
64          // System.out.println(set);
65          assertEquals(LinkedHashSet.class, set.getClass());
66          assertEquals("ID123", obj.getId());
67          assertEquals(3, set.size());
68          assertTrue(set.remove("zzz"));
69          assertTrue(set.remove("ccc"));
70          assertFalse(set.contains("111"));
71          try {
72              set.contains(set);
73              fail("Recursive set fails to provide a hashcode.");
74          } catch (StackOverflowError e) {
75              // ignore
76          }
77          //
78          Set<Object> self = (Set<Object>) set.iterator().next();
79          assertEquals(LinkedHashSet.class, self.getClass());
80          assertEquals(set, self);
81          assertSame(set, self);
82          assertEquals(1, set.size());
83          assertEquals(1, self.size());
84          set.add("111");
85          assertEquals(2, set.size());
86          assertEquals(2, self.size());
87          //
88          self.clear();
89          assertTrue(self.isEmpty());
90          assertTrue(set.isEmpty());
91          assertFalse("Now it should not be recursive any longer (no StackOverflowError).",
92                  set.contains(set));
93          //
94          set.add("jjj");
95          assertEquals(1, set.size());
96          assertEquals(1, self.size());
97      }
98  
99      public static class Bean1 {
100         private Set<Object> set;
101         private String id;
102 
103         public Set<Object> getSet() {
104             return set;
105         }
106 
107         public void setSet(Set<Object> set) {
108             this.set = set;
109         }
110 
111         public String getId() {
112             return id;
113         }
114 
115         public void setId(String id) {
116             this.id = id;
117         }
118     }
119 }