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.issue150;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.io.Reader;
24  import java.io.StringReader;
25  import java.util.List;
26  import java.util.regex.Pattern;
27  
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.yaml.snakeyaml.Yaml;
31  import org.yaml.snakeyaml.constructor.AbstractConstruct;
32  import org.yaml.snakeyaml.constructor.Constructor;
33  import org.yaml.snakeyaml.nodes.MappingNode;
34  import org.yaml.snakeyaml.nodes.Node;
35  import org.yaml.snakeyaml.nodes.NodeTuple;
36  import org.yaml.snakeyaml.nodes.ScalarNode;
37  import org.yaml.snakeyaml.nodes.SequenceNode;
38  import org.yaml.snakeyaml.nodes.Tag;
39  
40  public class YamlLoadAsIssueTest {
41  
42      private StringBuilder doc;
43  
44      @Before
45      public void setUp() {
46          doc = new StringBuilder();
47          line("!car");
48          line("plate: 12-XP-F4");
49          line("wheels:");
50          line("- w#1");
51          line("- w#2");
52          line("- w#3");
53          line("- w#4");
54      }
55  
56      private StringBuilder line(String text) {
57          return doc.append(text).append('\n');
58      }
59  
60      @Test
61      public void loadConstructsDocumentCorrectly() {
62          Yaml yaml = yaml();
63          Object result = yaml.load(reader());
64          assertNotNull(result);
65          assertEquals(Car.class, result.getClass());
66          assertEquals("12-XP-F4", ((Car) result).getPlate());
67          assertEquals(4, ((Car) result).getWheels().size());
68      }
69  
70      private Yaml yaml() {
71          Yaml yaml = new Yaml(new MyConstructor());
72          yaml.addImplicitResolver(new Tag("!wheel"), Pattern.compile("w#\\d+"), "w");
73          return yaml;
74      }
75  
76      @Test
77      public void ignoreImplicitTag() {
78          Yaml yaml = yaml();
79          try {
80              yaml.loadAs(reader(), Car.class);
81              fail();
82          } catch (Exception e) {
83              assertTrue(e
84                      .getMessage()
85                      .startsWith(
86                              "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.issues.issue150.Car; exception=Cannot create property=wheels for JavaBean=org.yaml.snakeyaml.issues.issue150.Car"));
87          }
88      }
89  
90      private Reader reader() {
91          return new StringReader(doc.toString());
92      }
93  
94      private class MyConstructor extends Constructor {
95          public MyConstructor() {
96              yamlConstructors.put(new Tag("!car"), new ConstructCar());
97              yamlConstructors.put(new Tag("!wheel"), new ConstructWheel());
98          }
99  
100         private String toScalarString(Node node) {
101             return (String) constructScalar((ScalarNode) node);
102         }
103 
104         private class ConstructCar extends AbstractConstruct {
105 
106             @SuppressWarnings("unchecked")
107             public Car construct(Node node) {
108                 Car car = new Car();
109                 MappingNode mapping = (MappingNode) node;
110                 List<NodeTuple> list = mapping.getValue();
111                 for (NodeTuple tuple : list) {
112                     String field = toScalarString(tuple.getKeyNode());
113                     if ("plate".equals(field)) {
114                         car.setPlate(toScalarString(tuple.getValueNode()));
115                     }
116                     if ("wheels".equals(field)) {
117                         SequenceNode snode = (SequenceNode) tuple.getValueNode();
118                         List<Wheel> wheels = (List<Wheel>) constructSequence(snode);
119                         car.setWheels(wheels);
120                     }
121                 }
122                 return car;
123             }
124         }
125 
126         private class ConstructWheel extends AbstractConstruct {
127 
128             public Wheel construct(Node node) {
129                 Wheel w = null;
130                 String strValue = toScalarString(node);
131                 if (strValue != null && strValue.length() > 2) {
132                     strValue = strValue.trim().substring(2);
133                     w = new Wheel();
134                     w.setId(Integer.valueOf(strValue));
135                 }
136                 return w;
137             }
138         }
139     }
140 }