xref: /aosp_15_r20/external/snakeyaml/src/test/java/examples/CustomMapExampleTest.java (revision ac2a7c1bf4e14d82f3bd566dcc2d76d5b42faf34)
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package examples;
15 
16 import java.util.Map;
17 import java.util.TreeMap;
18 import junit.framework.TestCase;
19 import org.yaml.snakeyaml.Yaml;
20 import org.yaml.snakeyaml.constructor.Constructor;
21 
22 public class CustomMapExampleTest extends TestCase {
23 
testMap()24   public void testMap() {
25     Yaml yaml = new Yaml(new CustomConstructor());
26     @SuppressWarnings("unchecked")
27     Map<Integer, String> data = yaml.load("{2: '222', 1: '111', 3: '333'}");
28     assertTrue(data instanceof TreeMap);
29     Object[] keys = data.keySet().toArray();
30     // must be sorted
31     assertEquals(Integer.valueOf(1), keys[0]);
32     assertEquals(Integer.valueOf(2), keys[1]);
33     assertEquals(Integer.valueOf(3), keys[2]);
34   }
35 
36   class CustomConstructor extends Constructor {
37 
38     @Override
createDefaultMap(int initSize)39     protected Map<Object, Object> createDefaultMap(int initSize) {
40       return new TreeMap<Object, Object>();
41     }
42   }
43 }
44