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.constructor;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Yaml;
24  import org.yaml.snakeyaml.composer.Composer;
25  import org.yaml.snakeyaml.parser.Parser;
26  import org.yaml.snakeyaml.parser.ParserImpl;
27  import org.yaml.snakeyaml.reader.StreamReader;
28  import org.yaml.snakeyaml.resolver.Resolver;
29  
30  public class ConstructorSequenceTest extends TestCase {
31  
32      public void testGetList() {
33          String data = "[ 1, 2, 3 ]";
34          List<Object> list = construct(new CustomConstructor(), data);
35          assertNotNull(list);
36          assertTrue(list.getClass().toString(), list instanceof ArrayList<?>);
37      }
38  
39      public void testGetArrayList() {
40          String data = "[ 1, 2, 3 ]";
41          List<Object> list = construct(data);
42          assertNotNull(list);
43          assertTrue(list.getClass().toString(), list instanceof ArrayList<?>);
44      }
45  
46      public void testDumpList() {
47          List<Integer> l = new ArrayList<Integer>(2);
48          l.add(1);
49          l.add(2);
50          Yaml yaml = new Yaml();
51          String result = yaml.dump(l);
52          assertEquals("[1, 2]\n", result);
53      }
54  
55      public void testDumpListSameIntegers() {
56          List<Integer> l = new ArrayList<Integer>(2);
57          l.add(1);
58          l.add(1);
59          Yaml yaml = new Yaml();
60          String result = yaml.dump(l);
61          assertEquals("[1, 1]\n", result);
62      }
63  
64      private List<Object> construct(String data) {
65          return construct(new Constructor(), data);
66      }
67  
68      @SuppressWarnings("unchecked")
69      private List<Object> construct(Constructor constructor, String data) {
70          StreamReader reader = new StreamReader(data);
71          Parser parser = new ParserImpl(reader);
72          Resolver resolver = new Resolver();
73          Composer composer = new Composer(parser, resolver);
74          constructor.setComposer(composer);
75          List<Object> result = (List<Object>) constructor.getSingleData(Object.class);
76          return result;
77      }
78  
79      class CustomConstructor extends Constructor {
80          @Override
81          protected List<Object> createDefaultList(int initSize) {
82              return new ArrayList<Object>(initSize);
83          }
84      }
85  }