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.issue103;
    18
    19  import java.util.List;
    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  import org.yaml.snakeyaml.constructor.Constructor;
    27
    28  public class MergingTest extends TestCase {
    29
    30      @SuppressWarnings({ "rawtypes", "unchecked" })
    31      public void testMergeWithDefaultMap() {
    32          String input = Util.getLocalResource("issues/issue103.yaml");
    33          // System.out.println(input);
    34          Yaml yaml = new Yaml();
    35
    36          check((Map) yaml.load(input));
    37      }
    38
    39      @SuppressWarnings({ "unchecked", "rawtypes" })
    40      public void testMergeWithFakeMap() {
    41          String input = Util.getLocalResource("issues/issue103.yaml");
    42          // System.out.println(input);
    43          Constructor c = new Constructor() {
    44              protected java.util.Map<Object, Object> createDefaultMap() {
    45                  return new FakeMap<Object, Object>();
    46              }
    47          };
    48
    49          Yaml yaml = new Yaml(c);
    50
    51          check((FakeMap) yaml.load(input));
    52      }
    53
    54      private void check(Map<String, List<Map<Object, Object>>> map) {
    55
    56          assertEquals(2, map.size());
    57          assertTrue(map.containsKey("input"));
    58          assertTrue(map.containsKey("result"));
    59
    60          // input: ...
    61          List<Map<Object, Object>> inputList = map.get("input");
    62          assertEquals(4, inputList.size());
    63
    64          Map<Object, Object> center = inputList.get(0);
    65          assertEquals(2, center.size());
    66          assertEquals(Integer.valueOf(1), center.get("x"));
    67          assertEquals(Integer.valueOf(2), center.get("y"));
    68
    69          Map<Object, Object> left = inputList.get(1);
    70          assertEquals(2, left.size());
    71          assertEquals(Integer.valueOf(0), left.get("x"));
    72          assertEquals(Integer.valueOf(2), left.get("y"));
    73
    74          Map<Object, Object> big = inputList.get(2);
    75          assertEquals(1, big.size());
    76          assertEquals(Integer.valueOf(10), big.get("r"));
    77
    78          Map<Object, Object> small = inputList.get(3);
    79          assertEquals(1, small.size());
    80          assertEquals(Integer.valueOf(1), small.get("r"));
    81
    82          // result : ...
    83          List<Map<Object, Object>> resultList = map.get("result");
    84          assertEquals(5, resultList.size());
    85
    86          Map<Object, Object> explicitKeys = resultList.get(0);
    87          assertEquals(4, explicitKeys.size());
    88          assertEquals(Integer.valueOf(1), explicitKeys.get("x"));
    89          assertEquals(Integer.valueOf(2), explicitKeys.get("y"));
    90          assertEquals(Integer.valueOf(10), explicitKeys.get("r"));
    91          assertEquals("center/big", explicitKeys.get("label"));
    92
    93          Map<?, ?> merge_center = resultList.get(1);
    94          assertEquals(4, merge_center.size());
    95          assertEquals(Integer.valueOf(1), merge_center.get("x"));
    96          assertEquals(Integer.valueOf(2), merge_center.get("y"));
    97          assertEquals(Integer.valueOf(10), merge_center.get("r"));
    98          assertEquals("center/big", merge_center.get("label"));
    99
    100         Map<?, ?> merge_left_override = resultList.get(2);
    101         assertEquals(4, merge_left_override.size());
    102         assertEquals(Integer.valueOf(0), merge_left_override.get("x"));
    103         assertEquals(Integer.valueOf(5), merge_left_override.get("y"));
    104         assertEquals(Integer.valueOf(10), merge_left_override.get("r"));
    105         assertEquals("center/big", merge_left_override.get("label"));
    106
    107         Map<?, ?> merge_center_big = resultList.get(3);
    108         assertEquals(4, merge_center_big.size());
    109         assertEquals(Integer.valueOf(1), merge_center_big.get("x"));
    110         assertEquals(Integer.valueOf(2), merge_center_big.get("y"));
    111         assertEquals(Integer.valueOf(10), merge_center_big.get("r"));
    112         assertEquals("center/big", merge_center_big.get("label"));
    113
    114         Map<?, ?> merge_big_left_small_override = resultList.get(4);
    115         assertEquals(4, merge_big_left_small_override.size());
    116         assertEquals(Integer.valueOf(1), merge_big_left_small_override.get("x"));
    117         assertEquals(Integer.valueOf(2), merge_big_left_small_override.get("y"));
    118         assertEquals(Integer.valueOf(10), merge_big_left_small_override.get("r"));
    119         assertEquals("center/big", merge_big_left_small_override.get("label"));
    120     }
    121 }