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.issues.issue56;
18  
19  import java.util.Map;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.TypeDescription;
24  import org.yaml.snakeyaml.Util;
25  import org.yaml.snakeyaml.Yaml;
26  import org.yaml.snakeyaml.constructor.Construct;
27  import org.yaml.snakeyaml.constructor.Constructor;
28  import org.yaml.snakeyaml.constructor.SafeConstructor;
29  import org.yaml.snakeyaml.nodes.Node;
30  import org.yaml.snakeyaml.nodes.Tag;
31  
32  public class PerlTest extends TestCase {
33  
34      @SuppressWarnings("unchecked")
35      public void testMaps() {
36          Yaml yaml = new Yaml(new CustomConstructor());
37          String input = Util.getLocalResource("issues/issue56-1.yaml");
38          int counter = 0;
39          for (Object obj : yaml.loadAll(input)) {
40              // System.out.println(obj);
41              Map<String, Object> map = (Map<String, Object>) obj;
42              Integer oid = (Integer) map.get("oid");
43              assertTrue(oid > 10000);
44              counter++;
45          }
46          assertEquals(4, counter);
47          assertEquals(0, CodeBean.counter);
48      }
49  
50      private class CustomConstructor extends SafeConstructor {
51          public CustomConstructor() {
52              // define tags which begin with !org.yaml.
53              String prefix = "!de.oddb.org,2007/ODDB";
54              this.yamlMultiConstructors.put(prefix, new ConstructYamlMap());
55          }
56      }
57  
58      @SuppressWarnings("unchecked")
59      public void testJavaBeanWithTypeDescription() {
60          Constructor c = new CustomBeanConstructor();
61          TypeDescription descr = new TypeDescription(CodeBean.class, new Tag(
62                  "!de.oddb.org,2007/ODDB::Util::Code"));
63          c.addTypeDescription(descr);
64          Yaml yaml = new Yaml(c);
65          String input = Util.getLocalResource("issues/issue56-1.yaml");
66          int counter = 0;
67          for (Object obj : yaml.loadAll(input)) {
68              // System.out.println(obj);
69              Map<String, Object> map = (Map<String, Object>) obj;
70              Integer oid = (Integer) map.get("oid");
71              assertTrue(oid > 10000);
72              counter++;
73          }
74          assertEquals(4, counter);
75          assertEquals(55, CodeBean.counter);
76      }
77  
78      @SuppressWarnings("unchecked")
79      public void testJavaBean() {
80          Constructor c = new CustomBeanConstructor();
81          Yaml yaml = new Yaml(c);
82          String input = Util.getLocalResource("issues/issue56-1.yaml");
83          int counter = 0;
84          for (Object obj : yaml.loadAll(input)) {
85              // System.out.println(obj);
86              Map<String, Object> map = (Map<String, Object>) obj;
87              Integer oid = (Integer) map.get("oid");
88              assertTrue(oid > 10000);
89              counter++;
90          }
91          assertEquals(4, counter);
92          assertEquals(55, CodeBean.counter);
93      }
94  
95      private class CustomBeanConstructor extends Constructor {
96          public CustomBeanConstructor() {
97              // define tags which begin with !org.yaml.
98              String prefix = "!de.oddb.org,2007/ODDB";
99              this.yamlMultiConstructors.put(prefix, new ConstructYamlMap());
100         }
101 
102         protected Construct getConstructor(Node node) {
103             if (node.getTag().equals(new Tag("!de.oddb.org,2007/ODDB::Util::Code"))) {
104                 node.setUseClassConstructor(true);
105                 node.setType(CodeBean.class);
106             }
107             return super.getConstructor(node);
108         }
109     }
110 
111     @Override
112     protected void setUp() throws Exception {
113         CodeBean.counter = 0;
114     }
115 }