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.types;
    18
    19  import java.io.IOException;
    20  import java.util.List;
    21  import java.util.Map;
    22
    23  import org.yaml.snakeyaml.YamlDocument;
    24
    25  /**
    26   * @see http://yaml.org/type/merge.html
    27   */
    28  public class MergeTagTest extends AbstractTest {
    29
    30      @SuppressWarnings("unchecked")
    31      public void testMerge() throws IOException {
    32          YamlDocument document = new YamlDocument("types/merge.yaml");
    33          List<Object> list = (List<Object>) document.getNativeData();
    34          assertEquals(8, list.size());
    35          Map<Object, Object> center = (Map<Object, Object>) list.get(0);
    36          assertEquals(2, center.size());
    37          assertEquals(new Integer(1), center.get("x"));
    38          assertEquals(new Integer(2), center.get("y"));
    39          //
    40          Map<Object, Object> left = (Map<Object, Object>) list.get(1);
    41          assertEquals(2, left.size());
    42          assertEquals(left.get("x").getClass().toString(), new Integer(0), left.get("x"));
    43          assertEquals(new Integer(2), left.get("y"));
    44          //
    45          Map<Object, Object> big = (Map<Object, Object>) list.get(2);
    46          assertEquals(1, big.size());
    47          assertEquals(new Integer(10), big.get("r"));
    48          //
    49          Map<Object, Object> small = (Map<Object, Object>) list.get(3);
    50          assertEquals(1, small.size());
    51          assertEquals(new Integer(1), small.get("r"));
    52          // Explicit keys
    53          Map<Object, Object> explicit = (Map<Object, Object>) list.get(4);
    54          assertEquals(4, explicit.size());
    55          assertEquals(new Integer(1), explicit.get("x"));
    56          assertEquals(new Integer(2), explicit.get("y"));
    57          assertEquals(new Integer(10), explicit.get("r"));
    58          assertEquals("center/big", explicit.get("label"));
    59          // Merge one map
    60          Map<Object, Object> merged1 = (Map<Object, Object>) list.get(5);
    61          assertEquals(explicit, merged1);
    62          assertNotSame(explicit, merged1);
    63          // Merge multiple maps
    64          Map<Object, Object> merged2 = (Map<Object, Object>) list.get(6);
    65          assertEquals(explicit, merged2);
    66          assertNotSame(explicit, merged2);
    67          // Override
    68          Map<Object, Object> merged3 = (Map<Object, Object>) list.get(7);
    69          assertEquals(explicit, merged3);
    70          assertNotSame(explicit, merged3);
    71      }
    72  }