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 org.yaml.snakeyaml;
    18
    19  import java.io.ByteArrayInputStream;
    20  import java.io.InputStream;
    21  import java.io.Reader;
    22  import java.io.StringReader;
    23  import java.util.ArrayList;
    24  import java.util.List;
    25
    26  import junit.framework.TestCase;
    27
    28  import org.yaml.snakeyaml.constructor.Constructor;
    29  import org.yaml.snakeyaml.introspector.BeanAccess;
    30
    31  public class JavaBeanLoaderTest extends TestCase {
    32
    33      public void testLoadString() {
    34          Bean bean = new Bean();
    35          bean.setId(3);
    36          bean.setName("Test me.");
    37          Yaml yaml = new Yaml();
    38          String output = yaml.dump(bean);
    39          assertEquals("!!org.yaml.snakeyaml.JavaBeanLoaderTest$Bean {id: 3, name: Test me.}\n",
    40                  output);
    41          Yaml loader = new Yaml();
    42          Bean parsed = loader.loadAs(output, Bean.class);
    43          assertEquals(3, parsed.getId());
    44          assertEquals("Test me.", parsed.getName());
    45          // Runtime definition is more important
    46          Bean2 parsed2 = loader.loadAs(output, Bean2.class);
    47          assertEquals(3, parsed2.getId());
    48          assertEquals("Test me.", parsed2.getName());
    49          assertFalse(parsed2.isValid());
    50      }
    51
    52      public void testLoadInputStream() {
    53          String yaml = "!!org.yaml.snakeyaml.JavaBeanParserTest$Bean {id: 3, name: Test me.}\n";
    54          InputStream input = new ByteArrayInputStream(yaml.getBytes());
    55          Yaml loader = new Yaml();
    56          Bean parsed = loader.loadAs(input, Bean.class);
    57          assertEquals(3, parsed.getId());
    58          assertEquals("Test me.", parsed.getName());
    59      }
    60
    61      public void testLoadReader() {
    62          String yaml = "!!org.yaml.snakeyaml.JavaBeanParserTest$Bean {id: 3, name: Test me.}\n";
    63          Reader input = new StringReader(yaml);
    64          Yaml loader = new Yaml();
    65          Bean parsed = loader.loadAs(input, Bean.class);
    66          assertEquals(3, parsed.getId());
    67          assertEquals("Test me.", parsed.getName());
    68      }
    69
    70      @SuppressWarnings("deprecation")
    71      public void testLoaderNullClass() {
    72          try {
    73              new JavaBeanLoader<Bean>((Class<Bean>) null);
    74              fail();
    75          } catch (NullPointerException e) {
    76              assertEquals("Class for tag must be provided.", e.getMessage());
    77          }
    78      }
    79
    80      @SuppressWarnings("deprecation")
    81      public void testLoaderNullTypeDescription() {
    82          try {
    83              new JavaBeanLoader<Bean>((TypeDescription) null);
    84              fail();
    85          } catch (NullPointerException e) {
    86              assertEquals("TypeDescription must be provided.", e.getMessage());
    87          }
    88      }
    89
    90      public void testLoaderNullRootClass() {
    91          try {
    92              Yaml loader = new Yaml();
    93              loader.loadAs("123", null);
    94              fail();
    95          } catch (NullPointerException e) {
    96              assertEquals("Class for tag must be provided.", e.getMessage());
    97          }
    98      }
    99
    100     @SuppressWarnings("deprecation")
    101     public void testLoaderNullOptions() {
    102         try {
    103             new JavaBeanLoader<Bean>((LoaderOptions) null, BeanAccess.DEFAULT);
    104             fail();
    105         } catch (NullPointerException e) {
    106             assertEquals("LoaderOptions must be provided.", e.getMessage());
    107         }
    108     }
    109
    110     public static class Bean {
    111         private String name;
    112         private int id;
    113
    114         public String getName() {
    115             return name;
    116         }
    117
    118         public void setName(String name) {
    119             this.name = name;
    120         }
    121
    122         public int getId() {
    123             return id;
    124         }
    125
    126         public void setId(int id) {
    127             this.id = id;
    128         }
    129     }
    130
    131     public static class Bean2 {
    132         private String name;
    133         private int id;
    134         private boolean valid;
    135
    136         public String getName() {
    137             return name;
    138         }
    139
    140         public void setName(String name) {
    141             this.name = name;
    142         }
    143
    144         public int getId() {
    145             return id;
    146         }
    147
    148         public void setId(int id) {
    149             this.id = id;
    150         }
    151
    152         public boolean isValid() {
    153             return valid;
    154         }
    155
    156         public void setValid(boolean valid) {
    157             this.valid = valid;
    158         }
    159     }
    160
    161     public void testTypeDescription1() {
    162         Bean3 bean3 = new Bean3();
    163         bean3.setName("Name123");
    164         Bean bean = new Bean();
    165         bean.setId(3);
    166         bean.setName("Test me.");
    167         bean3.setBean(bean);
    168         Yaml yaml = new Yaml();
    169         String output = yaml.dumpAsMap(bean3);
    170         assertEquals("bean:\n  id: 3\n  name: Test me.\nlist: null\nname: Name123\n", output);
    171         TypeDescription td = new TypeDescription(Bean3.class);
    172         td.putListPropertyType("list", Integer.class);
    173         Yaml loader = new Yaml(new Constructor(td));
    174         Bean3 parsed = (Bean3) loader.load(output);
    175         assertEquals("Name123", parsed.getName());
    176     }
    177
    178     public void testTypeDescription2() {
    179         Bean3 bean3 = new Bean3();
    180         bean3.setName("Name123");
    181         Bean bean = new Bean();
    182         bean.setId(3);
    183         bean.setName("Test me.");
    184         bean3.setBean(bean);
    185         List<Integer> list = new ArrayList<Integer>();
    186         list.add(13);
    187         list.add(17);
    188         bean3.setList(list);
    189         Yaml yaml = new Yaml();
    190         String output = yaml.dumpAsMap(bean3);
    191         assertEquals("bean:\n  id: 3\n  name: Test me.\nlist:\n- 13\n- 17\nname: Name123\n", output);
    192         TypeDescription td = new TypeDescription(Bean3.class);
    193         td.putListPropertyType("list", Integer.class);
    194         Yaml loader = new Yaml(new Constructor(td));
    195         Bean3 parsed = (Bean3) loader.load(output);
    196         assertEquals("Name123", parsed.getName());
    197         List<Integer> parsedList = parsed.getList();
    198         assertEquals(2, parsedList.size());
    199     }
    200
    201     public void testTypeDescription3() {
    202         Bean3 bean3 = new Bean3();
    203         bean3.setName("Name123");
    204         Bean bean = new Bean();
    205         bean.setId(3);
    206         bean.setName("Test me.");
    207         bean3.setBean(bean);
    208         Yaml yaml = new Yaml();
    209         String output = yaml.dumpAsMap(bean3);
    210         assertEquals("bean:\n  id: 3\n  name: Test me.\nlist: null\nname: Name123\n", output);
    211         TypeDescription td = new TypeDescription(Bean2.class);
    212         Yaml loader = new Yaml(new Constructor(td));
    213         Bean3 parsed = loader.loadAs(output, Bean3.class);// Bean3 must be taken
    214         assertEquals("Name123", parsed.getName());
    215     }
    216
    217     public static class Bean3 {
    218         private String name;
    219         private Bean bean;
    220         private List<Integer> list;
    221
    222         public String getName() {
    223             return name;
    224         }
    225
    226         public void setName(String name) {
    227             this.name = name;
    228         }
    229
    230         public Bean getBean() {
    231             return bean;
    232         }
    233
    234         public void setBean(Bean bean) {
    235             this.bean = bean;
    236         }
    237
    238         public List<Integer> getList() {
    239             return list;
    240         }
    241
    242         public void setList(List<Integer> list) {
    243             this.list = list;
    244         }
    245     }
    246 }