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