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.constructor;
    18
    19  import junit.framework.TestCase;
    20
    21  import org.yaml.snakeyaml.Yaml;
    22
    23  public class CustomClassLoaderConstructorTest extends TestCase {
    24
    25      public void testGetClassForNameNull() {
    26          try {
    27              new CustomClassLoaderConstructor(null);
    28              fail();
    29          } catch (Exception e) {
    30              assertEquals("Loader must be provided.", e.getMessage());
    31          }
    32      }
    33
    34      public void testGetClassForName() {
    35          CustomClassLoaderConstructor constr = new CustomClassLoaderConstructor(
    36                  CustomClassLoaderConstructorTest.class.getClassLoader());
    37          Yaml yaml = new Yaml(constr);
    38          String s = (String) yaml.load("abc");
    39          assertEquals("abc", s);
    40      }
    41
    42      public void testGetClassForNameWithRoot() throws ClassNotFoundException {
    43          Class<?> clazz = Class.forName(
    44                  "org.yaml.snakeyaml.constructor.CustomClassLoaderConstructorTest$LoaderBean", true,
    45                  CustomClassLoaderConstructorTest.class.getClassLoader());
    46          CustomClassLoaderConstructor constr = new CustomClassLoaderConstructor(clazz,
    47                  CustomClassLoaderConstructorTest.class.getClassLoader());
    48          Yaml yaml = new Yaml(constr);
    49          LoaderBean bean = (LoaderBean) yaml.load("{name: Andrey, number: 555}");
    50          assertEquals("Andrey", bean.getName());
    51          assertEquals(555, bean.getNumber());
    52      }
    53
    54      public void testGetClassForNameBean() {
    55          CustomClassLoaderConstructor constr = new CustomClassLoaderConstructor(
    56                  CustomClassLoaderConstructorTest.class.getClassLoader());
    57          Yaml yaml = new Yaml(constr);
    58          LoaderBean bean = (LoaderBean) yaml
    59                  .load("!!org.yaml.snakeyaml.constructor.CustomClassLoaderConstructorTest$LoaderBean {name: Andrey, number: 555}");
    60          assertEquals("Andrey", bean.getName());
    61          assertEquals(555, bean.getNumber());
    62      }
    63
    64      public static class LoaderBean {
    65          private String name;
    66          private int number;
    67
    68          public String getName() {
    69              return name;
    70          }
    71
    72          public void setName(String name) {
    73              this.name = name;
    74          }
    75
    76          public int getNumber() {
    77              return number;
    78          }
    79
    80          public void setNumber(int number) {
    81              this.number = number;
    82          }
    83      }
    84  }