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