Home | History | Annotate | Download | only in collections
      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 examples.collections;
     17 
     18 import java.util.ArrayList;
     19 import java.util.List;
     20 import java.util.Map;
     21 import java.util.Properties;
     22 import java.util.SortedMap;
     23 import java.util.TreeMap;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import org.yaml.snakeyaml.Util;
     28 import org.yaml.snakeyaml.Yaml;
     29 
     30 /**
     31  * Test different Map implementations as JavaBean properties
     32  */
     33 public class TypeSafeMapImplementationsTest extends TestCase {
     34     public void testDumpMap() {
     35         MapBean bean = new MapBean();
     36         SortedMap<String, String> sortedMap = new TreeMap<String, String>();
     37         sortedMap.put("2", "two");
     38         sortedMap.put("1", "one");
     39         bean.setSorted(sortedMap);
     40         Properties props = new Properties();
     41         props.setProperty("key1", "value1");
     42         props.setProperty("key2", "value2");
     43         bean.setProperties(props);
     44         Yaml yaml = new Yaml();
     45         String output = yaml.dumpAsMap(bean);
     46         // System.out.println(output);
     47         String etalon = Util.getLocalResource("examples/map-bean-1.yaml");
     48         assertEquals(etalon, output);
     49     }
     50 
     51     public void testLoadMap() {
     52         String output = Util.getLocalResource("examples/map-bean-1.yaml");
     53         // System.out.println(output);
     54         Yaml beanLoader = new Yaml();
     55         MapBean parsed = beanLoader.loadAs(output, MapBean.class);
     56         assertNotNull(parsed);
     57         SortedMap<String, String> sortedMap = parsed.getSorted();
     58         assertEquals(2, sortedMap.size());
     59         assertEquals("one", sortedMap.get("1"));
     60         assertEquals("two", sortedMap.get("2"));
     61         String first = sortedMap.keySet().iterator().next();
     62         assertEquals("1", first);
     63         //
     64         Properties props = parsed.getProperties();
     65         assertEquals(2, props.size());
     66         assertEquals("value1", props.getProperty("key1"));
     67         assertEquals("value2", props.getProperty("key2"));
     68     }
     69 
     70     public static class MapBean {
     71         private SortedMap<String, String> sorted;
     72         private Properties properties;
     73         private String name;
     74 
     75         public MapBean() {
     76             name = "Bean123";
     77         }
     78 
     79         public SortedMap<String, String> getSorted() {
     80             return sorted;
     81         }
     82 
     83         public void setSorted(SortedMap<String, String> sorted) {
     84             this.sorted = sorted;
     85         }
     86 
     87         public Properties getProperties() {
     88             return properties;
     89         }
     90 
     91         public void setProperties(Properties properties) {
     92             this.properties = properties;
     93         }
     94 
     95         public String getName() {
     96             return name;
     97         }
     98 
     99         public void setName(String name) {
    100             this.name = name;
    101         }
    102     }
    103 
    104     @SuppressWarnings("unchecked")
    105     public void testNoJavaBeanMap() {
    106         List<Object> list = new ArrayList<Object>(3);
    107         SortedMap<String, String> sortedMap = new TreeMap<String, String>();
    108         sortedMap.put("2", "two");
    109         sortedMap.put("1", "one");
    110         list.add(sortedMap);
    111         Properties props = new Properties();
    112         props.setProperty("key1", "value1");
    113         props.setProperty("key2", "value2");
    114         list.add(props);
    115         list.add("aaa");
    116         Yaml yaml = new Yaml();
    117         String output = yaml.dump(list);
    118         // System.out.println(output);
    119         String etalon = Util.getLocalResource("examples/map-bean-2.yaml");
    120         assertEquals(etalon, output);
    121         // load
    122         List<Object> list2 = (List<Object>) yaml.load(output);
    123         assertEquals(3, list2.size());
    124         Map<Object, Object> map1 = (Map<Object, Object>) list.get(0);// it was
    125                                                                      // SortedMap
    126         assertEquals(2, map1.size());
    127         assertEquals("one", map1.get("1"));
    128         assertEquals("two", map1.get("2"));
    129         Map<Object, Object> map2 = (Map<Object, Object>) list.get(1);// it was
    130                                                                      // Properties
    131         assertEquals(2, map2.size());
    132         assertEquals("value1", map2.get("key1"));
    133         assertEquals("value2", map2.get("key2"));
    134         assertEquals("aaa", list.get(2));
    135     }
    136 
    137     public void testRecursiveNoJavaBeanMap1() {
    138         SortedMap<String, Object> sortedMap = new TreeMap<String, Object>();
    139         sortedMap.put("2", "two");
    140         sortedMap.put("1", "one");
    141         sortedMap.put("3", sortedMap);
    142         Yaml yaml = new Yaml();
    143         String output = yaml.dump(sortedMap);
    144         // System.out.println(output);
    145         String etalon = Util.getLocalResource("examples/map-recursive-1.yaml");
    146         assertEquals(etalon, output);
    147         // load with different order
    148         @SuppressWarnings("unchecked")
    149         Map<Object, Object> map1 = (Map<Object, Object>) yaml.load(Util
    150                 .getLocalResource("examples/map-recursive-1_1.yaml"));
    151         assertEquals(3, map1.size());
    152         assertEquals("one", map1.get("1"));
    153         assertEquals("two", map1.get("2"));
    154         // test that the order is taken from YAML instead of sorting
    155         String first = (String) map1.keySet().iterator().next();
    156         assertEquals("2", first);
    157     }
    158 
    159     @SuppressWarnings("unchecked")
    160     public void testRecursiveNoJavaBeanProperties2() {
    161         Properties props = new Properties();
    162         props.setProperty("key1", "value1");
    163         props.setProperty("key2", "value2");
    164         Map<Object, Object> map = props;
    165         map.put("key3", props);
    166         Yaml yaml = new Yaml();
    167         String output = yaml.dump(props);
    168         // System.out.println(output);
    169         String etalon = Util.getLocalResource("examples/map-recursive-2.yaml");
    170         assertEquals(etalon, output);
    171         // load
    172         Map<Object, Object> map2 = (Map<Object, Object>) yaml.load(output);
    173         assertEquals(3, map2.size());
    174         assertEquals("value1", map2.get("key1"));
    175         assertEquals("value2", map2.get("key2"));
    176     }
    177 
    178     public void testRecursiveNoJavaBeanMap3() {
    179         Yaml yaml = new Yaml();
    180         String output = Util.getLocalResource("examples/map-recursive-3.yaml");
    181         // System.out.println(output);
    182         @SuppressWarnings("unchecked")
    183         SortedMap<Object, Object> map1 = (SortedMap<Object, Object>) yaml.load(output);
    184         assertEquals(3, map1.size());
    185         assertEquals("one", map1.get("1"));
    186         assertEquals("two", map1.get("2"));
    187         // test that the order is NOT taken from YAML but sorted
    188         String first = (String) map1.keySet().iterator().next();
    189         assertEquals("1", first);
    190     }
    191 
    192     public void testRecursiveNoJavaBeanProperties4() {
    193         Yaml yaml = new Yaml();
    194         String output = Util.getLocalResource("examples/map-recursive-4.yaml");
    195         // System.out.println(output);
    196         try {
    197             yaml.load(output);
    198             fail("Recursive Properties are not supported.");
    199         } catch (Exception e) {
    200             assertTrue(e.getMessage(), e.getMessage().contains("Properties must not be recursive."));
    201         }
    202     }
    203 }
    204