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