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.SortedSet;
21  import java.util.TreeSet;
22  
23  import junit.framework.TestCase;
24  
25  import org.yaml.snakeyaml.Util;
26  import org.yaml.snakeyaml.Yaml;
27  
28  public class RecursiveSortedSetTest extends TestCase {
29      public void testDumpException() {
30          SortedSet<Object> set = new TreeSet<Object>();
31          Bean11 bean = new Bean11();
32          bean.setId("ID555");
33          bean.setSet(set);
34          set.add("ggg");
35          set.add("hhh");
36          set.add(bean);
37          Yaml yaml = new Yaml();
38          String doc = yaml.dump(bean);
39          // System.out.println(doc);
40          assertEquals(Util.getLocalResource("issues/issue73-recursive9.txt"), doc);
41      }
42  
43      public void testLoadException() {
44          String doc = Util.getLocalResource("issues/issue73-recursive10.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      public void testLoadRecursiveTest() {
59          String doc = Util.getLocalResource("issues/issue73-recursive9.txt");
60          // System.out.println(doc);
61          Yaml yaml = new Yaml();
62          Bean11 obj = (Bean11) yaml.load(doc);
63          Set<Object> set = obj.getSet();
64          // System.out.println(set);
65          assertEquals(TreeSet.class, set.getClass());
66          assertEquals("ID555", obj.getId());
67          assertEquals(3, set.size());
68          assertTrue(set.remove("ggg"));
69          assertTrue(set.remove("hhh"));
70          //
71          Bean11 beanRef = (Bean11) set.iterator().next();
72          assertEquals(obj, beanRef);
73          assertSame(obj, beanRef);
74          //
75          try {
76              set.add(obj);
77              fail("Recursive set fails to provide a hashcode.");
78          } catch (StackOverflowError e) {
79              // ignore
80          }
81          set.clear();
82          assertTrue("Empty set is not recursive.", set.add(obj));
83      }
84  
85      public static class Bean11 implements Comparable<Object> {
86          private SortedSet<Object> set;
87          private String id;
88  
89          public SortedSet<Object> getSet() {
90              return set;
91          }
92  
93          public void setSet(SortedSet<Object> set) {
94              this.set = set;
95          }
96  
97          public String getId() {
98              return id;
99          }
100 
101         public void setId(String id) {
102             this.id = id;
103         }
104 
105         public int compareTo(Object o) {
106             return id.compareTo(o.toString());
107         }
108 
109         @Override
110         public boolean equals(Object obj) {
111             if (obj instanceof Bean11) {
112                 Bean11 b = (Bean11) obj;
113                 return id.equals(b.id);
114             } else {
115                 return false;
116             }
117         }
118 
119         @Override
120         public int hashCode() {
121             return id.hashCode();
122         }
123 
124         @Override
125         public String toString() {
126             return "Bean id=" + id + "set=" + set.toString();
127         }
128     }
129 }