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