View Javadoc

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