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.util.List;
    20  import java.util.Map;
    21
    22  import junit.framework.TestCase;
    23
    24  /**
    25   * Test Chapter 2.2 from the YAML specification
    26   *
    27   * @author py4fun
    28   * @see http://yaml.org/spec/1.1/
    29   */
    30  public class Chapter2_2Test extends TestCase {
    31
    32      @SuppressWarnings("unchecked")
    33      public void testExample_2_7() {
    34          YamlStream resource = new YamlStream("example2_7.yaml");
    35          List<Object> list = (List<Object>) resource.getNativeData();
    36          assertEquals(2, list.size());
    37          List<String> list1 = (List<String>) list.get(0);
    38          assertEquals(3, list1.size());
    39          assertEquals("Mark McGwire", list1.get(0));
    40          assertEquals("Sammy Sosa", list1.get(1));
    41          assertEquals("Ken Griffey", list1.get(2));
    42          List<String> list2 = (List<String>) list.get(1);
    43          assertEquals(2, list2.size());
    44          assertEquals("Chicago Cubs", list2.get(0));
    45          assertEquals("St Louis Cardinals", list2.get(1));
    46      }
    47
    48      @SuppressWarnings("unchecked")
    49      public void testExample_2_8() {
    50          YamlStream resource = new YamlStream("example2_8.yaml");
    51          List<Object> list = (List<Object>) resource.getNativeData();
    52          assertEquals(2, list.size());
    53          Map<String, String> map1 = (Map<String, String>) list.get(0);
    54          assertEquals(3, map1.size());
    55          assertEquals(new Integer(72200), map1.get("time"));
    56          assertEquals("Sammy Sosa", map1.get("player"));
    57          assertEquals("strike (miss)", map1.get("action"));
    58          Map<String, String> map2 = (Map<String, String>) list.get(1);
    59          assertEquals(3, map2.size());
    60          assertEquals(new Integer(72227), map2.get("time"));
    61          assertEquals("Sammy Sosa", map2.get("player"));
    62          assertEquals("grand slam", map2.get("action"));
    63      }
    64
    65      @SuppressWarnings("unchecked")
    66      public void testExample_2_9() {
    67          YamlDocument document = new YamlDocument("example2_9.yaml");
    68          Map<String, Object> map = (Map<String, Object>) document.getNativeData();
    69          assertEquals(map.toString(), 2, map.size());
    70          List<String> list1 = (List<String>) map.get("hr");
    71          assertEquals(2, list1.size());
    72          assertEquals("Mark McGwire", list1.get(0));
    73          assertEquals("Sammy Sosa", list1.get(1));
    74          List<String> list2 = (List<String>) map.get("rbi");
    75          assertEquals(2, list2.size());
    76          assertEquals("Sammy Sosa", list2.get(0));
    77          assertEquals("Ken Griffey", list2.get(1));
    78      }
    79
    80      @SuppressWarnings("unchecked")
    81      public void testExample_2_10() {
    82          YamlDocument document = new YamlDocument("example2_10.yaml");
    83          Map<String, Object> map = (Map<String, Object>) document.getNativeData();
    84          assertEquals("Examples 2.9 and 2.10 must be identical.",
    85                  new YamlDocument("example2_9.yaml").getNativeData(), map);
    86      }
    87
    88      @SuppressWarnings("unchecked")
    89      public void testExample_2_11() {
    90          YamlDocument document = new YamlDocument("example2_11.yaml");
    91          Map<Object, Object> map = (Map<Object, Object>) document.getNativeData();
    92          assertEquals(2, map.size());
    93          for (Object key : map.keySet()) {
    94              List<String> list = (List<String>) key;
    95              assertEquals(2, list.size());
    96          }
    97      }
    98
    99      public void testExample_2_12() {
    100         YamlDocument document = new YamlDocument("example2_12.yaml");
    101         @SuppressWarnings("unchecked")
    102         List<Map<Object, Object>> list = (List<Map<Object, Object>>) document.getNativeData();
    103         assertEquals(3, list.size());
    104         Map<Object, Object> map1 = (Map<Object, Object>) list.get(0);
    105         assertEquals(2, map1.size());
    106         assertEquals("Super Hoop", map1.get("item"));
    107         Map<Object, Object> map2 = (Map<Object, Object>) list.get(1);
    108         assertEquals(2, map2.size());
    109         assertEquals("Basketball", map2.get("item"));
    110         Map<Object, Object> map3 = (Map<Object, Object>) list.get(2);
    111         assertEquals(2, map3.size());
    112         assertEquals("Big Shoes", map3.get("item"));
    113     }
    114 }