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