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 examples.resolver;
17 
18 import java.util.HashMap;
19 import java.util.Map;
20 
21 import junit.framework.TestCase;
22 
23 import org.yaml.snakeyaml.DumperOptions;
24 import org.yaml.snakeyaml.Yaml;
25 import org.yaml.snakeyaml.constructor.Constructor;
26 import org.yaml.snakeyaml.representer.Representer;
27 
28 public class CustomResolverTest extends TestCase {
29 
testResolverToDump()30     public void testResolverToDump() {
31         Map<Object, Object> map = new HashMap<Object, Object>();
32         map.put("1.0", "2009-01-01");
33         Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(),
34                 new CustomResolver());
35         String output = yaml.dump(map);
36         assertEquals("{1.0: 2009-01-01}\n", output);
37         assertEquals("Float and Date must be escaped.", "{'1.0': '2009-01-01'}\n",
38                 new Yaml().dump(map));
39     }
40 
41     @SuppressWarnings("unchecked")
testResolverToLoad()42     public void testResolverToLoad() {
43         Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(),
44                 new CustomResolver());
45         Map<Object, Object> map = (Map<Object, Object>) yaml.load("1.0: 2009-01-01");
46         assertEquals(1, map.size());
47         assertEquals("2009-01-01", map.get("1.0"));
48         // the default Resolver shall create Date and Double from the same YAML
49         // document
50         Yaml yaml2 = new Yaml();
51         Map<Object, Object> map2 = (Map<Object, Object>) yaml2.load("1.0: 2009-01-01");
52         assertEquals(1, map2.size());
53         assertFalse(map2.containsKey("1.0"));
54         assertTrue(map2.toString(), map2.containsKey(new Double(1.0)));
55     }
56 }
57