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