• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.pyyaml;
15 
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.TreeMap;
23 import junit.framework.TestCase;
24 import org.yaml.snakeyaml.Yaml;
25 import org.yaml.snakeyaml.constructor.SafeConstructor;
26 
27 public class PyRecursiveTest extends TestCase {
28 
29   @SuppressWarnings("unchecked")
testDict()30   public void testDict() {
31     Map<AnInstance, AnInstance> value = new HashMap<AnInstance, AnInstance>();
32     AnInstance instance = new AnInstance(value, value);
33     value.put(instance, instance);
34     Yaml yaml = new Yaml();
35     String output1 = yaml.dump(value);
36     assertTrue(output1.contains("!!org.pyyaml.AnInstance"));
37     assertTrue(output1.contains("&id001"));
38     assertTrue(output1.contains("&id002"));
39     assertTrue(output1.contains("*id001"));
40     assertTrue(output1.contains("*id002"));
41     assertTrue(output1.contains("foo"));
42     assertTrue(output1.contains("bar"));
43     Map<AnInstance, AnInstance> value2 = yaml.load(output1);
44     assertEquals(value.size(), value2.size());
45     for (AnInstance tmpInstance : value2.values()) {
46       assertSame(tmpInstance.getBar(), tmpInstance.getFoo());
47       assertSame(tmpInstance.getBar(), value2);
48       assertSame(tmpInstance, value2.get(tmpInstance));
49     }
50   }
51 
52   @SuppressWarnings({"unchecked", "rawtypes"})
testDictSafeConstructor()53   public void testDictSafeConstructor() {
54     Map value = new TreeMap();
55     value.put("abc", "www");
56     value.put("qwerty", value);
57     Yaml yaml = new Yaml(new SafeConstructor());
58     String output1 = yaml.dump(value);
59     assertEquals("&id001\nabc: www\nqwerty: *id001\n", output1);
60     Map value2 = yaml.load(output1);
61     assertEquals(2, value2.size());
62     assertEquals("www", value2.get("abc"));
63     assertTrue(value2.get("qwerty") instanceof Map);
64     Map value3 = (Map) value2.get("qwerty");
65     assertTrue(value3.get("qwerty") instanceof Map);
66   }
67 
68   @SuppressWarnings({"unchecked", "rawtypes"})
testList()69   public void testList() {
70     List value = new ArrayList();
71     value.add(value);
72     value.add("test");
73     value.add(Integer.valueOf(1));
74 
75     Yaml yaml = new Yaml();
76     String output1 = yaml.dump(value);
77     assertEquals("&id001\n- *id001\n- test\n- 1\n", output1);
78     List value2 = yaml.load(output1);
79     assertEquals(3, value2.size());
80     assertEquals(value.size(), value2.size());
81     assertSame(value2, value2.get(0));
82     // we expect self-reference as 1st element of the list
83     // let's remove self-reference and check other "simple" members of the
84     // list. otherwise assertEquals will lead us to StackOverflow
85     value.remove(0);
86     value2.remove(0);
87     assertEquals(value, value2);
88   }
89 
90   @SuppressWarnings({"unchecked", "rawtypes"})
testListSafeConstructor()91   public void testListSafeConstructor() {
92     List value = new ArrayList();
93     value.add(value);
94     value.add("test");
95     value.add(Integer.valueOf(1));
96 
97     Yaml yaml = new Yaml(new SafeConstructor());
98     String output1 = yaml.dump(value);
99     assertEquals("&id001\n- *id001\n- test\n- 1\n", output1);
100     List value2 = yaml.load(output1);
101     assertEquals(3, value2.size());
102     assertEquals(value.size(), value2.size());
103     assertSame(value2, value2.get(0));
104     // we expect self-reference as 1st element of the list
105     // let's remove self-reference and check other "simple" members of the
106     // list. otherwise assertEquals will lead us to StackOverflow
107     value.remove(0);
108     value2.remove(0);
109     assertEquals(value, value2);
110   }
111 
112   @SuppressWarnings({"unchecked", "rawtypes"})
testSet()113   public void testSet() {
114     Set value = new HashSet();
115     value.add(new AnInstance(value, value));
116     Yaml yaml = new Yaml();
117     String output1 = yaml.dump(value);
118     Set<AnInstance> value2 = yaml.load(output1);
119 
120     assertEquals(value.size(), value2.size());
121     for (AnInstance tmpInstance : value2) {
122       assertSame(tmpInstance.getBar(), tmpInstance.getFoo());
123       assertSame(tmpInstance.getBar(), value2);
124     }
125   }
126 
testSet2()127   public void testSet2() {
128     Set<Object> set = new HashSet<Object>(3);
129     set.add("aaa");
130     set.add(111);
131     set.add(set);
132     Yaml yaml = new Yaml();
133     try {
134       yaml.dump(set);
135       fail("Java does not allow a recursive set to be a key for a map.");
136     } catch (StackOverflowError e) {
137       // ignore
138     }
139   }
140 }
141