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.issues.issue100;
    18
    19  import java.util.HashSet;
    20  import java.util.List;
    21  import java.util.Map;
    22  import java.util.Set;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.yaml.snakeyaml.Util;
    27  import org.yaml.snakeyaml.Yaml;
    28  import org.yaml.snakeyaml.constructor.Constructor;
    29
    30  public class MergeJavaBeanTest extends TestCase {
    31
    32      @SuppressWarnings("unchecked")
    33      public void testNoMerge() {
    34          String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- *id001";
    35          // System.out.println(input);
    36          Yaml yaml = new Yaml();
    37          List<Data> list = (List<Data>) yaml.load(input);
    38          for (Data data : list) {
    39              // System.out.println(data);
    40              assertEquals("id123", data.getId());
    41              assertEquals(11, data.getAge());
    42          }
    43      }
    44
    45      public void testMergeWithTags() {
    46          String input = Util.getLocalResource("issues/issue100-1.yaml");
    47          // System.out.println(input);
    48          Yaml yaml = new Yaml();
    49
    50          List<?> list = (List<?>) yaml.load(input);
    51          // First object: Data ( 11, "id123" )
    52          assertEquals(list.get(0).getClass(), Data.class);
    53          assertEquals(11, ((Data) list.get(0)).getAge());
    54          assertEquals("id123", ((Data) list.get(0)).getId());
    55
    56          // Second object: Data ( 13, "id456" )
    57          assertEquals(list.get(1).getClass(), Data.class);
    58          assertEquals(13, ((Data) list.get(1)).getAge());
    59          assertEquals("id456", ((Data) list.get(1)).getId());
    60
    61          // Third object: Data ( 11, "id789" )
    62          assertEquals(list.get(2).getClass(), Data.class);
    63          assertEquals(11, ((Data) list.get(2)).getAge());
    64          assertEquals("id789", ((Data) list.get(2)).getId());
    65
    66          // 4th object: DataMore ( 30, "id123" )
    67          assertEquals(list.get(3).getClass(), DataMore.class);
    68          assertEquals(30, ((DataMore) list.get(3)).getAge());
    69          assertEquals("id123", ((DataMore) list.get(3)).getId());
    70          assertTrue(((DataMore) list.get(3)).isComplete());
    71
    72          // 5th object: Map ( age: 100 )
    73          assertTrue(list.get(4) instanceof Map);
    74          assertEquals(1, ((Map<?, ?>) list.get(4)).size());
    75          assertTrue(((Map<?, ?>) list.get(4)).containsKey("age"));
    76          assertEquals(100, ((Map<?, ?>) list.get(4)).get("age"));
    77
    78          // 6th object: DataMore ( 100, "id789" )
    79          assertEquals(list.get(5).getClass(), DataMore.class);
    80          assertEquals(100, ((DataMore) list.get(5)).getAge());
    81          assertEquals("id789", ((DataMore) list.get(5)).getId());
    82          assertFalse(((DataMore) list.get(5)).isComplete());
    83          // All instances except the last Map must be different Data instances
    84          Set<Data> dataSet = new HashSet<Data>();
    85          for (Object data : list) {
    86              if (data instanceof Data) {
    87                  dataSet.add((Data) data);
    88              }
    89          }
    90          assertEquals("Must be all but one Data instances.", list.size() - 1, dataSet.size());
    91      }
    92
    93      /**
    94       * Since to explicit tag is provided JavaBean properties are used to create
    95       * a map
    96       */
    97      @SuppressWarnings("unchecked")
    98      public void testMergeBeanToMap() {
    99          String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- << : *id001";
    100         // System.out.println(input);
    101         Yaml yaml = new Yaml();
    102         List<Object> list = (List<Object>) yaml.load(input);
    103         // First object: Data ( 11, "id123" )
    104         Data first = (Data) list.get(0);
    105         assertEquals(11, first.getAge());
    106         assertEquals("id123", first.getId());
    107         // Second object is map
    108         Map<?, ?> second = (Map<?, ?>) list.get(1);
    109         assertEquals(11, second.get("age"));
    110         assertEquals("id123", second.get("id"));
    111     }
    112
    113     @SuppressWarnings("unchecked")
    114     public void testMergeAndDeviate() {
    115         String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- <<: *id001\n  id: id456";
    116         // System.out.println(input);
    117         Yaml yaml = new Yaml();
    118         List<Object> list = (List<Object>) yaml.load(input);
    119         // First object: Data ( 11, "id123" )
    120         Data first = (Data) list.get(0);
    121         assertEquals(11, first.getAge());
    122         assertEquals("id123", first.getId());
    123         // Second object is map with a diffrent id
    124         Map<?, ?> second = (Map<?, ?>) list.get(1);
    125         assertEquals(11, second.get("age"));
    126         assertEquals("id456", second.get("id"));
    127     }
    128
    129     /**
    130      * <pre>
    131      * Test that the merge-override works correctly in the case of custom classes / data types.
    132      * Two base objects are specified:
    133      *    id001 refers to Data ( 11, "id123" )
    134      *    id002 refers to Data ( 37, null )  with a literal null value
    135      * The third object is specified as having the properties of id001, with the properties of
    136      * id002 overriding where they conflict.
    137      *    Data ( 37, "id123" )
    138      * </pre>
    139      */
    140     @SuppressWarnings("unchecked")
    141     public void testMergeAndDeviateOverride() {
    142         String input = "- &id001 !!org.yaml.snakeyaml.issues.issue100.Data {age: 11, id: id123}\n- &id002 !!org.yaml.snakeyaml.issues.issue100.Data {age: 37}\n- <<: [ *id002, *id001 ]";
    143         // System.out.println(input);
    144         Yaml yaml = new Yaml();
    145         List<Data> list = (List<Data>) yaml.load(input);
    146
    147         // First object: Data ( 11, "id123" )
    148         assertEquals(11, list.get(0).getAge());
    149         assertEquals("id123", list.get(0).getId());
    150
    151         // Second object: Data ( 37, null )
    152         assertEquals(37, list.get(1).getAge());
    153         assertEquals(null, list.get(1).getId());
    154
    155         // Third object: map
    156         Map<?, ?> third = (Map<?, ?>) list.get(2);
    157         assertEquals(37, third.get("age"));
    158         assertEquals("id123", third.get("id"));
    159     }
    160
    161     /**
    162      * When the merged JavaBean is itself a JavaBean property then explicit tag
    163      * is not required
    164      */
    165     public void testMergeBeanProperty() {
    166         String input = Util.getLocalResource("issues/issue100-3.yaml");
    167         // System.out.println(input);
    168         Yaml yaml = new Yaml(new Constructor(DataBean.class));
    169         DataBean bean = (DataBean) yaml.load(input);
    170         assertEquals("id001", bean.getId());
    171         assertEquals("id002", bean.getData().getId());
    172         assertEquals(17, bean.getData().getAge());
    173         assertEquals(17, bean.getMore().getAge());
    174         assertEquals("more003", bean.getMore().getId());
    175         assertTrue(bean.getMore().isComplete());
    176     }
    177
    178     /**
    179      * Merge map to JavaBean
    180      */
    181     @SuppressWarnings("unchecked")
    182     public void testMergeMapToJavaBean() {
    183         String input = "- &id001 { age: 11, id: id123 }\n- !!org.yaml.snakeyaml.issues.issue100.Data\n  <<: *id001\n  id: id456";
    184         // System.out.println(input);
    185         Yaml yaml = new Yaml(new Constructor());
    186         List<Object> objects = (List<Object>) yaml.load(input);
    187         assertEquals(2, objects.size());
    188         // Check first type
    189         Object first = objects.get(0);
    190         Map<Object, Object> firstMap = (Map<Object, Object>) first;
    191         // Check first contents
    192         assertEquals(11, firstMap.get("age"));
    193         assertEquals("id123", firstMap.get("id"));
    194         // Check second contents
    195         Data secondData = (Data) objects.get(1);
    196         assertEquals(11, secondData.getAge());
    197         assertEquals("id456", secondData.getId());
    198     }
    199 }