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.LinkedHashMap;
    20  import java.util.Map;
    21
    22  import junit.framework.TestCase;
    23
    24  import org.yaml.snakeyaml.Util;
    25  import org.yaml.snakeyaml.Yaml;
    26
    27  /**
    28   * Test MapBean->Map<String, Developer> developers <br/>
    29   * Developer class must be properly recognised
    30   */
    31  public class TypeSafeMapTest extends TestCase {
    32      public void testDumpMap() {
    33          MapBean bean = new MapBean();
    34          Map<String, Integer> data = new LinkedHashMap<String, Integer>();
    35          data.put("aaa", 1);
    36          data.put("bbb", 2);
    37          data.put("zzz", 3);
    38          bean.setData(data);
    39          Map<String, Developer2> developers = new LinkedHashMap<String, Developer2>();
    40          developers.put("team1", new Developer2("Fred", "creator"));
    41          developers.put("team2", new Developer2("John", "committer"));
    42          bean.setDevelopers(developers);
    43          Yaml yaml = new Yaml();
    44          String output = yaml.dumpAsMap(bean);
    45          // System.out.println(output);
    46          String etalon = Util.getLocalResource("examples/map-bean-10.yaml");
    47          assertEquals(etalon, output);
    48      }
    49
    50      public void testDumpMap2() {
    51          MapBean bean = new MapBean();
    52          Map<String, Integer> data = new LinkedHashMap<String, Integer>();
    53          data.put("aaa", 1);
    54          data.put("bbb", 2);
    55          bean.setData(data);
    56          Map<String, Developer2> developers = new LinkedHashMap<String, Developer2>();
    57          developers.put("team1", new Developer2("Fred", "creator"));
    58          developers.put("team2", new Developer2("John", "committer"));
    59          developers.put("team3", new Developer222("Bill", "head"));
    60          bean.setDevelopers(developers);
    61          Yaml yaml = new Yaml();
    62          String output = yaml.dumpAsMap(bean);
    63          // System.out.println(output);
    64          String etalon = Util.getLocalResource("examples/map-bean-11.yaml");
    65          assertEquals(etalon, output);
    66      }
    67
    68      public void testLoadMap() {
    69          String output = Util.getLocalResource("examples/map-bean-10.yaml");
    70          // System.out.println(output);
    71          Yaml beanLoader = new Yaml();
    72          MapBean parsed = beanLoader.loadAs(output, MapBean.class);
    73          assertNotNull(parsed);
    74          Map<String, Integer> data = parsed.getData();
    75          assertEquals(3, data.size());
    76          assertEquals(new Integer(1), data.get("aaa"));
    77          assertEquals(new Integer(2), data.get("bbb"));
    78          assertEquals(new Integer(3), data.get("zzz"));
    79          Map<String, Developer2> developers = parsed.getDevelopers();
    80          assertEquals(2, developers.size());
    81          assertEquals("Developer must be recognised.", Developer2.class, developers.get("team1")
    82                  .getClass());
    83          Developer2 fred = developers.get("team1");
    84          assertEquals("Fred", fred.getName());
    85          assertEquals("creator", fred.getRole());
    86      }
    87
    88      public static class MapBean {
    89          private Map<String, Integer> data;
    90          private String name;
    91          private Map<String, Developer2> developers;
    92
    93          public MapBean() {
    94              name = "Bean123";
    95          }
    96
    97          public Map<String, Integer> getData() {
    98              return data;
    99          }
    100
    101         public void setData(Map<String, Integer> data) {
    102             this.data = data;
    103         }
    104
    105         public String getName() {
    106             return name;
    107         }
    108
    109         public void setName(String name) {
    110             this.name = name;
    111         }
    112
    113         public Map<String, Developer2> getDevelopers() {
    114             return developers;
    115         }
    116
    117         public void setDevelopers(Map<String, Developer2> developers) {
    118             this.developers = developers;
    119         }
    120     }
    121
    122     public static class Developer2 {
    123         private String name;
    124         private String role;
    125
    126         public Developer2() {
    127         }
    128
    129         public Developer2(String name, String role) {
    130             this.name = name;
    131             this.role = role;
    132         }
    133
    134         public String getName() {
    135             return name;
    136         }
    137
    138         public void setName(String name) {
    139             this.name = name;
    140         }
    141
    142         public String getRole() {
    143             return role;
    144         }
    145
    146         public void setRole(String role) {
    147             this.role = role;
    148         }
    149     }
    150
    151     public static class Developer222 extends Developer2 {
    152         public Developer222() {
    153             super();
    154         }
    155
    156         public Developer222(String name, String role) {
    157             super(name, role);
    158         }
    159     }
    160
    161     /*
    162      * No generic collection
    163      */
    164     @SuppressWarnings({ "unchecked", "rawtypes" })
    165     public void testLoadMapWithObject() {
    166         String output = Util.getLocalResource("examples/map-bean-10.yaml");
    167         // System.out.println(output);
    168         Yaml beanLoader = new Yaml();
    169         MapBeanNoGenerics parsed = beanLoader.loadAs(output, MapBeanNoGenerics.class);
    170         assertNotNull(parsed);
    171         Map<String, Integer> data = parsed.getData();
    172         assertEquals(3, data.size());
    173         assertEquals(new Integer(1), data.get("aaa"));
    174         assertEquals(new Integer(2), data.get("bbb"));
    175         assertEquals(new Integer(3), data.get("zzz"));
    176         Map developers = parsed.getDevelopers();
    177         assertNotNull(developers);
    178         assertEquals(2, developers.size());
    179         Object o1 = developers.get("team1");
    180         // because of erasure we get simply Map
    181         Map<String, String> developer = (Map<String, String>) o1;
    182         assertEquals("Fred", developer.get("name"));
    183         assertEquals("creator", developer.get("role"));
    184     }
    185
    186     @SuppressWarnings("rawtypes")
    187     public static class MapBeanNoGenerics {
    188         private Map data;
    189         private String name;
    190         private Map developers;
    191
    192         public MapBeanNoGenerics() {
    193             name = "Bean123";
    194         }
    195
    196         public Map getData() {
    197             return data;
    198         }
    199
    200         public void setData(Map data) {
    201             this.data = data;
    202         }
    203
    204         public String getName() {
    205             return name;
    206         }
    207
    208         public void setName(String name) {
    209             this.name = name;
    210         }
    211
    212         public Map getDevelopers() {
    213             return developers;
    214         }
    215
    216         public void setDevelopers(Map developers) {
    217             this.developers = developers;
    218         }
    219     }
    220 }