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.resolver; 15 16 import java.util.HashMap; 17 import java.util.Map; 18 import junit.framework.TestCase; 19 import org.yaml.snakeyaml.DumperOptions; 20 import org.yaml.snakeyaml.Yaml; 21 import org.yaml.snakeyaml.constructor.Constructor; 22 import org.yaml.snakeyaml.representer.Representer; 23 24 public class CustomResolverTest extends TestCase { 25 testResolverToDump()26 public void testResolverToDump() { 27 Map<Object, Object> map = new HashMap<Object, Object>(); 28 map.put("1.0", "2009-01-01"); 29 Yaml yaml = 30 new Yaml(new Constructor(), new Representer(), new DumperOptions(), new CustomResolver()); 31 String output = yaml.dump(map); 32 assertEquals("{1.0: 2009-01-01}\n", output); 33 assertEquals("Float and Date must be escaped.", "{'1.0': '2009-01-01'}\n", 34 new Yaml().dump(map)); 35 } 36 37 @SuppressWarnings("unchecked") testResolverToLoad()38 public void testResolverToLoad() { 39 Yaml yaml = 40 new Yaml(new Constructor(), new Representer(), new DumperOptions(), new CustomResolver()); 41 Map<Object, Object> map = yaml.load("1.0: 2009-01-01"); 42 assertEquals(1, map.size()); 43 assertEquals("2009-01-01", map.get("1.0")); 44 // the default Resolver shall create Date and Double from the same YAML 45 // document 46 Yaml yaml2 = new Yaml(); 47 Map<Object, Object> map2 = yaml2.load("1.0: 2009-01-01"); 48 assertEquals(1, map2.size()); 49 assertFalse(map2.containsKey("1.0")); 50 assertTrue(map2.toString(), map2.containsKey(Double.valueOf(1.0))); 51 } 52 53 /** 54 * https://bitbucket.org/snakeyaml/snakeyaml/issues/454/snakeyaml-implicitly-converts-time-into 55 */ testResolverToLoadNoTime()56 public void testResolverToLoadNoTime() { 57 Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(), 58 new NoTimeIntResolver()); 59 Map<Object, Object> map = yaml.load("a: 17:00:00\nb: 17"); 60 assertEquals(2, map.size()); 61 assertEquals("17:00:00", map.get("a")); 62 assertEquals(17, map.get("b")); 63 } 64 testJsonBooleanResolverToLoad()65 public void testJsonBooleanResolverToLoad() { 66 Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(), 67 new JsonBooleanResolver()); 68 Map<Object, Object> map = yaml.load("no: true"); 69 assertEquals(1, map.size()); 70 assertEquals(true, map.get("no")); 71 } 72 } 73