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