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