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