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.LinkedHashMap;
    20  import java.util.Map;
    21  import java.util.TreeMap;
    22
    23  import junit.framework.TestCase;
    24
    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 ConstructorMappingTest extends TestCase {
    32
    33      @SuppressWarnings("unchecked")
    34      public void testGetDefaultMap() {
    35          String data = "{ one: 1, two: 2, three: 3 }";
    36          Map<Object, Object> map = (Map<Object, Object>) construct(new CustomConstructor(), data);
    37          assertNotNull(map);
    38          assertTrue(map.getClass().toString(), map instanceof TreeMap);
    39      }
    40
    41      @SuppressWarnings("unchecked")
    42      public void testGetArrayList() {
    43          String data = "{ one: 1, two: 2, three: 3 }";
    44          Map<Object, Object> map = (Map<Object, Object>) construct(data);
    45          assertNotNull(map);
    46          assertTrue(map.getClass().toString(), map instanceof LinkedHashMap);
    47      }
    48
    49      private Object construct(String data) {
    50          return construct(new Constructor(), data);
    51      }
    52
    53      private Object construct(Constructor constructor, String data) {
    54          StreamReader reader = new StreamReader(data);
    55          Parser parser = new ParserImpl(reader);
    56          Resolver resolver = new Resolver();
    57          Composer composer = new Composer(parser, resolver);
    58          constructor.setComposer(composer);
    59          return constructor.getSingleData(Object.class);
    60      }
    61
    62      class CustomConstructor extends Constructor {
    63          @Override
    64          protected Map<Object, Object> createDefaultMap() {
    65              return new TreeMap<Object, Object>();
    66          }
    67      }
    68  }