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.util.ArrayList;
    20  import java.util.Collection;
    21  import java.util.LinkedList;
    22  import java.util.List;
    23  import java.util.Map;
    24
    25  import org.yaml.snakeyaml.YamlDocument;
    26
    27  /**
    28   * @see http://yaml.org/type/seq.html
    29   */
    30  public class SeqTagTest extends AbstractTest {
    31
    32      @SuppressWarnings("unchecked")
    33      public void testSeq() {
    34          YamlDocument document = new YamlDocument("types/seq.yaml");
    35          Map<String, List<String>> map = (Map<String, List<String>>) document.getNativeData();
    36          assertEquals(2, map.size());
    37          List<String> list1 = (List<String>) map.get("Block style");
    38          assertEquals(9, list1.size());
    39          assertEquals("Mercury", list1.get(0));
    40          assertEquals("Venus", list1.get(1));
    41          assertEquals("Earth", list1.get(2));
    42          assertEquals("Mars", list1.get(3));
    43          assertEquals("Jupiter", list1.get(4));
    44          assertEquals("Saturn", list1.get(5));
    45          assertEquals("Uranus", list1.get(6));
    46          assertEquals("Neptune", list1.get(7));
    47          assertEquals("Pluto", list1.get(8));
    48          //
    49          List<String> list2 = (List<String>) map.get("Flow style");
    50          assertEquals(9, list2.size());
    51          assertEquals("Mercury", list2.get(0));
    52          assertEquals("Venus", list2.get(1));
    53          assertEquals("Earth", list2.get(2));
    54          assertEquals("Mars", list2.get(3));
    55          assertEquals("Jupiter", list2.get(4));
    56          assertEquals("Saturn", list2.get(5));
    57          assertEquals("Uranus", list2.get(6));
    58          assertEquals("Neptune", list2.get(7));
    59          assertEquals("Pluto", list2.get(8));
    60          //
    61          assertEquals(list1, list2);
    62          assertNotSame(list1, list2);
    63      }
    64
    65      @SuppressWarnings("unchecked")
    66      public void testCollection() {
    67          Collection<Integer> collection = new LinkedList<Integer>();
    68          collection.add(1);
    69          collection.add(2);
    70          collection.add(3);
    71          String output = dump(collection);
    72          assertEquals("[1, 2, 3]\n", output);
    73          Collection<Integer> obj = (Collection<Integer>) load(output);
    74          // System.out.println(obj);
    75          assertEquals(3, obj.size());
    76          assertTrue("Create ArrayList by default: " + obj.getClass().toString(),
    77                  obj instanceof ArrayList);
    78      }
    79
    80      public void testArray() {
    81          Integer[] array = new Integer[3];
    82          array[0] = new Integer(1);
    83          array[1] = new Integer(1);
    84          array[2] = new Integer(2);
    85          String output = dump(array);
    86          assertEquals("[1, 1, 2]\n", output);
    87      }
    88
    89      public void testStringArray() {
    90          String[] array = new String[] { "aaa", "bbb", "ccc" };
    91          String output = dump(array);
    92          assertEquals("[aaa, bbb, ccc]\n", output);
    93      }
    94
    95      public void testArrayPrimitives() {
    96          int[] array = new int[3];
    97          array[0] = 1;
    98          array[1] = 1;
    99          array[2] = 2;
    100         try {
    101             dump(array);
    102             fail("Arrays of primitives are not supported.");
    103         } catch (RuntimeException e) {
    104             assertEquals("Arrays of primitives are not fully supported.", e.getMessage());
    105         }
    106     }
    107 }