xref: /aosp_15_r20/external/snakeyaml/src/test/java/examples/CustomBeanResolverTest.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.math.BigDecimal;
17 import java.util.regex.Pattern;
18 import junit.framework.TestCase;
19 import org.yaml.snakeyaml.Yaml;
20 import org.yaml.snakeyaml.constructor.Constructor;
21 import org.yaml.snakeyaml.nodes.Node;
22 import org.yaml.snakeyaml.nodes.NodeId;
23 import org.yaml.snakeyaml.nodes.ScalarNode;
24 
25 /**
26  * http://code.google.com/p/snakeyaml/issues/detail?id=75
27  */
28 public class CustomBeanResolverTest extends TestCase {
29 
30   private final Pattern CUSTOM_PATTERN = Pattern.compile("\\d+%");
31 
testOnlyBigDecimal()32   public void testOnlyBigDecimal() {
33     Yaml yaml = new Yaml(new BigBeanConstructor());
34     Foo foo = yaml.load("bar: 50\nbaz: 35%\nbas: 1250");
35     assertEquals(50.0, foo.bar);
36     assertEquals("0.35", foo.baz.toString());
37     assertEquals("1250", foo.bas);
38   }
39 
testPrimitive()40   public void testPrimitive() {
41     Yaml yaml = new Yaml(new BigBeanConstructor());
42     Foo foo = yaml.load("bar: 50%\nbaz: 35%\nbas: 1250%\nbaw: 35");
43     assertEquals(0.5, foo.bar);
44     assertEquals("0.35", foo.baz.toString());
45     assertEquals("1250%", foo.bas);
46     assertEquals("35", foo.baw.toString());
47   }
48 
49   class BigBeanConstructor extends Constructor {
50 
BigBeanConstructor()51     public BigBeanConstructor() {
52       super(Foo.class);
53       yamlClassConstructors.put(NodeId.scalar, new ConstructBig());
54     }
55 
56     private class ConstructBig extends ConstructScalar {
57 
construct(Node node)58       public Object construct(Node node) {
59         if (node.getType().equals(BigDecimal.class)) {
60           String val = constructScalar((ScalarNode) node);
61           if (CUSTOM_PATTERN.matcher(val).matches()) {
62             return new BigDecimal(val.substring(0, val.length() - 1)).divide(new BigDecimal(100));
63           }
64         } else if (node.getType().isAssignableFrom(double.class)) {
65           String val = constructScalar((ScalarNode) node);
66           if (CUSTOM_PATTERN.matcher(val).matches()) {
67             return Double.valueOf(val.substring(0, val.length() - 1)) / 100;
68           }
69         }
70         return super.construct(node);
71       }
72     }
73   }
74 
75   public static class Foo {
76 
77     public double bar = 0;
78     public BigDecimal baz;
79     public BigDecimal baw;
80     public String bas;
81   }
82 }
83