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