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.issue64;
17  
18  import java.util.LinkedList;
19  import java.util.List;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Yaml;
24  import org.yaml.snakeyaml.constructor.AbstractConstruct;
25  import org.yaml.snakeyaml.constructor.Constructor;
26  import org.yaml.snakeyaml.nodes.Node;
27  import org.yaml.snakeyaml.nodes.ScalarNode;
28  import org.yaml.snakeyaml.nodes.Tag;
29  import org.yaml.snakeyaml.representer.Represent;
30  import org.yaml.snakeyaml.representer.Representer;
31  
32  public class ParameterizedTypeTest extends TestCase {
33  
34      public void testRepresenter() {
35          Yaml yaml = new Yaml(new ClassConstructor(), new ClassRepresenter());
36  
37          String methodName = "testMethod";
38          List<Class<?>> argTypes = new LinkedList<Class<?>>();
39          argTypes.add(String.class);
40          argTypes.add(Integer.class);
41          argTypes.add(Boolean.class);
42          MethodDesc methodDesc = new MethodDesc(methodName, argTypes);
43  
44          String out = yaml.dump(methodDesc);
45          // System.out.println(out);
46          assertEquals(
47                  "!!org.yaml.snakeyaml.issues.issue64.MethodDesc\nargTypes: [!clazz 'String', !clazz 'Integer', !clazz 'Boolean']\nname: testMethod\n",
48                  out);
49          MethodDesc parsed = (MethodDesc) yaml.load(out);
50          assertEquals(methodName, parsed.getName());
51          List<Class<?>> argTypes2 = parsed.getArgTypes();
52          assertEquals(3, argTypes2.size());
53          assertEquals(argTypes, argTypes2);
54      }
55  
56      static class ClassRepresenter extends Representer {
57          public ClassRepresenter() {
58              this.representers.put(Class.class, new RepresentClass());
59          }
60  
61          private class RepresentClass implements Represent {
62              public Node representData(Object data) {
63                  Class<?> clazz = (Class<?>) data;
64                  return representScalar(new Tag("!clazz"), clazz.getSimpleName());
65              }
66          }
67      }
68  
69      static class ClassConstructor extends Constructor {
70          public ClassConstructor() {
71              this.yamlConstructors.put(new Tag("!clazz"), new ConstructClass());
72          }
73  
74          private class ConstructClass extends AbstractConstruct {
75  
76              public Object construct(Node node) {
77                  String clazz = (String) constructScalar((ScalarNode) node);
78                  try {
79                      return Class.forName("java.lang." + clazz);
80                  } catch (ClassNotFoundException e) {
81                      throw new RuntimeException(e);
82                  }
83              }
84          }
85      }
86  }