View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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 org.yaml.snakeyaml.resolver;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.regex.Pattern;
21  
22  import junit.framework.TestCase;
23  
24  import org.yaml.snakeyaml.TypeDescription;
25  import org.yaml.snakeyaml.Yaml;
26  import org.yaml.snakeyaml.constructor.AbstractConstruct;
27  import org.yaml.snakeyaml.constructor.Construct;
28  import org.yaml.snakeyaml.constructor.Constructor;
29  import org.yaml.snakeyaml.nodes.Node;
30  import org.yaml.snakeyaml.nodes.ScalarNode;
31  import org.yaml.snakeyaml.nodes.Tag;
32  
33  /**
34   * Custom implicit resolver does not apply inside JavaBean declaration <a href=
35   * "http://groups.google.com/group/snakeyaml-core/browse_frm/thread/c75c35a3d9cfcaba"
36   * >mailing list</a> for more information
37   */
38  public class ImplicitResolverTest extends TestCase {
39      private static final Tag CFG = new Tag("!cfg");
40  
41      public static class ConfigurationConstructor extends Constructor {
42          protected Map<String, String> config = null;
43  
44          public ConfigurationConstructor(Map<String, String> config) {
45              this.config = config;
46              this.yamlConstructors.put(CFG, new ConfigObjectConstruct());
47          }
48  
49          private class ConfigObjectConstruct extends AbstractConstruct {
50              public Object construct(Node node) {
51                  String val = (String) constructScalar((ScalarNode) node);
52                  val = val.substring(2, val.length() - 1);
53                  return config.get(val);
54              }
55          }
56  
57          protected Construct getConstructor(Node node) {
58              if (CFG.equals(node.getTag())) {
59                  node.setUseClassConstructor(false);
60              }
61              return super.getConstructor(node);
62          }
63      }
64  
65      public static class TestBean {
66          String myval;
67  
68          public String getMyval() {
69              return myval;
70          }
71  
72          public void setMyval(String myval) {
73              this.myval = myval;
74          }
75  
76          public String toString() {
77              return "MyVal: " + myval;
78          }
79      }
80  
81      public void testMain() {
82          Map<String, String> config = new HashMap<String, String>();
83          config.put("user.home", "HOME");
84          Constructor constructor = new ConfigurationConstructor(config);
85          constructor.addTypeDescription(new TypeDescription(TestBean.class, "!testbean"));
86          Yaml yaml = new Yaml(constructor);
87          yaml.addImplicitResolver(CFG, Pattern.compile("\\$\\([a-zA-Z\\d\\u002E\\u005F]+\\)"), "$");
88          TestBean bean = (TestBean) yaml.load("!testbean {myval: !cfg $(user.home)}");
89          // System.out.println(bean.toString());
90          assertEquals("Explicit tag must be respected", "HOME", bean.getMyval());
91          bean = (TestBean) yaml.load("!testbean {myval: $(user.home)}");
92          // System.out.println(bean.toString());
93          assertEquals("Implicit tag must be respected", "HOME", bean.getMyval());
94      }
95  }