• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
17 
18 import java.math.BigDecimal;
19 import java.util.Map;
20 import java.util.regex.Pattern;
21 
22 import junit.framework.TestCase;
23 
24 import org.yaml.snakeyaml.Yaml;
25 import org.yaml.snakeyaml.constructor.AbstractConstruct;
26 import org.yaml.snakeyaml.constructor.SafeConstructor;
27 import org.yaml.snakeyaml.nodes.Node;
28 import org.yaml.snakeyaml.nodes.ScalarNode;
29 import org.yaml.snakeyaml.nodes.Tag;
30 
31 /**
32  * Use custom implicit resolver when the runtime class is not defined.
33  * http://code.google.com/p/snakeyaml/issues/detail?id=75
34  */
35 public class CustomImplicitResolverTest extends TestCase {
36     private final Tag CUSTOM_TAG = new Tag("!BigDecimalDividedBy100");
37     private final Pattern CUSTOM_PATTERN = Pattern.compile("\\d+%");
38 
39     @SuppressWarnings("unchecked")
testImplicit()40     public void testImplicit() {
41         Yaml yaml = new Yaml(new BigConstructor());
42         yaml.addImplicitResolver(CUSTOM_TAG, CUSTOM_PATTERN, "-0123456789");
43         Map<String, Object> obj = (Map<String, Object>) yaml.load("bar: 50%");
44         assertEquals("0.5", obj.get("bar").toString());
45         assertEquals(BigDecimal.class, obj.get("bar").getClass());
46     }
47 
testImplicitFailure()48     public void testImplicitFailure() {
49         Yaml yaml = new Yaml(new BigConstructor());
50         yaml.addImplicitResolver(CUSTOM_TAG, Pattern.compile("\\d+%"), "-0123456789");
51         try {
52             yaml.load("bar: !!float 50%");
53             fail("Both implicit and explicit are present.");
54         } catch (NumberFormatException e) {
55             assertEquals("For input string: \"50%\"", e.getMessage());
56         }
57     }
58 
59     class BigConstructor extends SafeConstructor {
BigConstructor()60         public BigConstructor() {
61             this.yamlConstructors.put(CUSTOM_TAG, new ConstructBig());
62         }
63 
64         private class ConstructBig extends AbstractConstruct {
construct(Node node)65             public Object construct(Node node) {
66                 String val = (String) constructScalar((ScalarNode) node);
67                 return new BigDecimal(val.substring(0, val.length() - 1))
68                         .divide(new BigDecimal(100));
69             }
70         }
71     }
72 }
73