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.pyyaml;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.TreeMap;
26  
27  import junit.framework.TestCase;
28  
29  import org.yaml.snakeyaml.Yaml;
30  import org.yaml.snakeyaml.constructor.SafeConstructor;
31  
32  public class PyRecursiveTest extends TestCase {
33  
34      @SuppressWarnings("unchecked")
35      public void testDict() {
36          Map<AnInstance, AnInstance> value = new HashMap<AnInstance, AnInstance>();
37          AnInstance instance = new AnInstance(value, value);
38          value.put(instance, instance);
39          Yaml yaml = new Yaml();
40          String output1 = yaml.dump(value);
41          assertTrue(output1.contains("!!org.pyyaml.AnInstance"));
42          assertTrue(output1.contains("&id001"));
43          assertTrue(output1.contains("&id002"));
44          assertTrue(output1.contains("*id001"));
45          assertTrue(output1.contains("*id002"));
46          assertTrue(output1.contains("foo"));
47          assertTrue(output1.contains("bar"));
48          Map<AnInstance, AnInstance> value2 = (Map<AnInstance, AnInstance>) yaml.load(output1);
49          assertEquals(value.size(), value2.size());
50          for (AnInstance tmpInstance : value2.values()) {
51              assertSame(tmpInstance.getBar(), tmpInstance.getFoo());
52              assertSame(tmpInstance.getBar(), value2);
53              assertSame(tmpInstance, value2.get(tmpInstance));
54          }
55      }
56  
57      @SuppressWarnings({ "unchecked", "rawtypes" })
58      public void testDictSafeConstructor() {
59          Map value = new TreeMap();
60          value.put("abc", "www");
61          value.put("qwerty", value);
62          Yaml yaml = new Yaml(new SafeConstructor());
63          String output1 = yaml.dump(value);
64          assertEquals("&id001\nabc: www\nqwerty: *id001\n", output1);
65          Map value2 = (Map) yaml.load(output1);
66          assertEquals(2, value2.size());
67          assertEquals("www", value2.get("abc"));
68          assertTrue(value2.get("qwerty") instanceof Map);
69          Map value3 = (Map) value2.get("qwerty");
70          assertTrue(value3.get("qwerty") instanceof Map);
71      }
72  
73      @SuppressWarnings({ "unchecked", "rawtypes" })
74      public void testList() {
75          List value = new ArrayList();
76          value.add(value);
77          value.add("test");
78          value.add(new Integer(1));
79  
80          Yaml yaml = new Yaml();
81          String output1 = yaml.dump(value);
82          assertEquals("&id001\n- *id001\n- test\n- 1\n", output1);
83          List value2 = (List) yaml.load(output1);
84          assertEquals(3, value2.size());
85          assertEquals(value.size(), value2.size());
86          assertSame(value2, value2.get(0));
87          // we expect self-reference as 1st element of the list
88          // let's remove self-reference and check other "simple" members of the
89          // list. otherwise assertEquals will lead us to StackOverflow
90          value.remove(0);
91          value2.remove(0);
92          assertEquals(value, value2);
93      }
94  
95      @SuppressWarnings({ "unchecked", "rawtypes" })
96      public void testListSafeConstructor() {
97          List value = new ArrayList();
98          value.add(value);
99          value.add("test");
100         value.add(new Integer(1));
101 
102         Yaml yaml = new Yaml(new SafeConstructor());
103         String output1 = yaml.dump(value);
104         assertEquals("&id001\n- *id001\n- test\n- 1\n", output1);
105         List value2 = (List) yaml.load(output1);
106         assertEquals(3, value2.size());
107         assertEquals(value.size(), value2.size());
108         assertSame(value2, value2.get(0));
109         // we expect self-reference as 1st element of the list
110         // let's remove self-reference and check other "simple" members of the
111         // list. otherwise assertEquals will lead us to StackOverflow
112         value.remove(0);
113         value2.remove(0);
114         assertEquals(value, value2);
115     }
116 
117     @SuppressWarnings({ "unchecked", "rawtypes" })
118     public void testSet() {
119         Set value = new HashSet();
120         value.add(new AnInstance(value, value));
121         Yaml yaml = new Yaml();
122         String output1 = yaml.dump(value);
123         Set<AnInstance> value2 = (Set<AnInstance>) yaml.load(output1);
124 
125         assertEquals(value.size(), value2.size());
126         for (AnInstance tmpInstance : value2) {
127             assertSame(tmpInstance.getBar(), tmpInstance.getFoo());
128             assertSame(tmpInstance.getBar(), value2);
129         }
130     }
131 
132     public void testSet2() {
133         Set<Object> set = new HashSet<Object>(3);
134         set.add("aaa");
135         set.add(111);
136         set.add(set);
137         Yaml yaml = new Yaml();
138         try {
139             yaml.dump(set);
140             fail("Java does not allow a recursive set to be a key for a map.");
141         } catch (StackOverflowError e) {
142             // ignore
143         }
144     }
145 }