• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, 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.javabeans;
17 
18 import junit.framework.TestCase;
19 
20 import org.yaml.snakeyaml.Yaml;
21 
22 public class TriangleBeanTest extends TestCase {
23 
testGetTriangle()24     public void testGetTriangle() {
25         Triangle triangle = new Triangle();
26         triangle.setName("Triangle25");
27         TriangleBean bean = new TriangleBean();
28         bean.setShape(triangle);
29         bean.setName("Bean25");
30         Yaml beanDumper = new Yaml();
31         String output = beanDumper.dumpAsMap(bean);
32         assertEquals(
33                 "name: Bean25\nshape: !!org.yaml.snakeyaml.javabeans.Triangle\n  name: Triangle25\n",
34                 output);
35         Yaml beanLoader = new Yaml();
36         TriangleBean loadedBean = beanLoader.loadAs(output, TriangleBean.class);
37         assertNotNull(loadedBean);
38         assertEquals("Bean25", loadedBean.getName());
39         assertEquals(7, loadedBean.getShape().process());
40     }
41 
testClassNotFound()42     public void testClassNotFound() {
43         String output = "name: Bean25\nshape: !!org.yaml.snakeyaml.javabeans.Triangle777\n  name: Triangle25\n";
44         Yaml beanLoader = new Yaml();
45         try {
46             beanLoader.loadAs(output, TriangleBean.class);
47             fail("Class not found expected.");
48         } catch (Exception e) {
49             assertTrue(
50                     e.getMessage(),
51                     e.getMessage().contains(
52                             "Class not found: org.yaml.snakeyaml.javabeans.Triangle777"));
53         }
54     }
55 
56     /**
57      * Runtime class has less priority then an explicit tag
58      */
testClassAndTag()59     public void testClassAndTag() {
60         String output = "name: !!whatever Bean25\nshape: !!org.yaml.snakeyaml.javabeans.Triangle\n  name: Triangle25\n";
61         Yaml beanLoader = new Yaml();
62         try {
63             beanLoader.loadAs(output, TriangleBean.class);
64             fail("Runtime class has less priority then an explicit tag");
65         } catch (Exception e) {
66             assertTrue(e.getMessage(), e.getMessage().contains("Class not found: whatever"));
67         }
68     }
69 }
70