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.1 from the YAML specification
    26   *
    27   * @author py4fun
    28   * @see http://yaml.org/spec/1.1/
    29   */
    30  public class Chapter2_1Test extends TestCase {
    31
    32      @SuppressWarnings("unchecked")
    33      public void testExample_2_1() {
    34          YamlDocument document = new YamlDocument("example2_1.yaml");
    35          List<String> list = (List<String>) document.getNativeData();
    36          assertEquals(3, list.size());
    37          assertEquals("Mark McGwire", list.get(0));
    38          assertEquals("Sammy Sosa", list.get(1));
    39          assertEquals("Ken Griffey", list.get(2));
    40          assertEquals("[Mark McGwire, Sammy Sosa, Ken Griffey]\n", document.getPresentation());
    41      }
    42
    43      @SuppressWarnings("unchecked")
    44      public void testExample_2_2() {
    45          YamlDocument document = new YamlDocument("example2_2.yaml");
    46          Map<String, Object> map = (Map<String, Object>) document.getNativeData();
    47          assertEquals(3, map.size());
    48          assertEquals("Expect 65 to be a Integer", Integer.class, map.get("hr").getClass());
    49          assertEquals(new Integer(65), map.get("hr"));
    50          assertEquals(new Float(0.278), new Float("0.278"));
    51          assertEquals("Expect 0.278 to be a Float", Double.class, map.get("avg").getClass());
    52          assertEquals(new Double(0.278), map.get("avg"));
    53          assertEquals("Expect 147 to be an Integer", Integer.class, map.get("rbi").getClass());
    54          assertEquals(new Integer(147), map.get("rbi"));
    55      }
    56
    57      @SuppressWarnings("unchecked")
    58      public void testExample_2_3() {
    59          YamlDocument document = new YamlDocument("example2_3.yaml");
    60          Map<String, List<String>> map = (Map<String, List<String>>) document.getNativeData();
    61          assertEquals(2, map.size());
    62          List<String> list1 = map.get("american");
    63          assertEquals(3, list1.size());
    64          assertEquals("Boston Red Sox", list1.get(0));
    65          assertEquals("Detroit Tigers", list1.get(1));
    66          assertEquals("New York Yankees", list1.get(2));
    67          List<String> list2 = map.get("national");
    68          assertEquals(3, list2.size());
    69          assertEquals("New York Mets", list2.get(0));
    70          assertEquals("Chicago Cubs", list2.get(1));
    71          assertEquals("Atlanta Braves", list2.get(2));
    72      }
    73
    74      @SuppressWarnings("unchecked")
    75      public void testExample_2_4() {
    76          YamlDocument document = new YamlDocument("example2_4.yaml");
    77          List<Map<String, Object>> list = (List<Map<String, Object>>) document.getNativeData();
    78          assertEquals(2, list.size());
    79          Map<String, Object> map1 = list.get(0);
    80          assertEquals(3, map1.size());
    81          assertEquals("Mark McGwire", map1.get("name"));
    82      }
    83
    84      @SuppressWarnings("unchecked")
    85      public void testExample_2_5() {
    86          YamlDocument document = new YamlDocument("example2_5.yaml");
    87          List<List<Object>> list = (List<List<Object>>) document.getNativeData();
    88          assertEquals(3, list.size());
    89          List<Object> list1 = list.get(0);
    90          assertEquals(3, list1.size());
    91          assertEquals("name", list1.get(0));
    92          assertEquals("hr", list1.get(1));
    93          assertEquals("avg", list1.get(2));
    94          assertEquals(3, list.get(1).size());
    95          assertEquals(3, list.get(2).size());
    96      }
    97
    98      @SuppressWarnings("unchecked")
    99      public void testExample_2_6() {
    100         YamlDocument document = new YamlDocument("example2_6.yaml");
    101         Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) document
    102                 .getNativeData();
    103         assertEquals(2, map.size());
    104         Map<String, Object> map1 = map.get("Mark McGwire");
    105         assertEquals(2, map1.size());
    106         Map<String, Object> map2 = map.get("Sammy Sosa");
    107         assertEquals(2, map2.size());
    108     }
    109 }