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 */
16package examples.staticstate;
17
18import junit.framework.TestCase;
19
20import org.yaml.snakeyaml.TypeDescription;
21import org.yaml.snakeyaml.Yaml;
22import org.yaml.snakeyaml.constructor.Constructor;
23import org.yaml.snakeyaml.nodes.Tag;
24import org.yaml.snakeyaml.representer.Representer;
25
26/**
27 * Example: using wrapper object for static fields
28 */
29public class StaticFieldsWrapperTest extends TestCase {
30
31    /**
32     * use wrapper with global tag
33     */
34    public void testWrapper() {
35        JavaBeanWithStaticState bean = new JavaBeanWithStaticState();
36        bean.setName("Bahrack");
37        bean.setAge(-47);
38        JavaBeanWithStaticState.setType("Type3");
39        JavaBeanWithStaticState.color = "Violet";
40        Yaml yaml = new Yaml();
41        String output = yaml.dump(new Wrapper(bean));
42        // System.out.println(output);
43        assertEquals(
44                "!!examples.staticstate.Wrapper {age: -47, color: Violet, name: Bahrack, type: Type3}\n",
45                output);
46        // parse back to instance
47        Wrapper wrapper = (Wrapper) yaml.load(output);
48        JavaBeanWithStaticState bean2 = wrapper.createBean();
49        assertEquals(-47, bean2.getAge());
50        assertEquals("Bahrack", bean2.getName());
51    }
52
53    /**
54     * use wrapper with local tag
55     */
56    public void testLocalTag() {
57        JavaBeanWithStaticState bean = new JavaBeanWithStaticState();
58        bean.setName("Bahrack");
59        bean.setAge(-47);
60        JavaBeanWithStaticState.setType("Type3");
61        JavaBeanWithStaticState.color = "Violet";
62        Representer repr = new Representer();
63        repr.addClassTag(Wrapper.class, new Tag("!mybean"));
64        Yaml yaml = new Yaml(repr);
65        String output = yaml.dump(new Wrapper(bean));
66        // System.out.println(output);
67        assertEquals("!mybean {age: -47, color: Violet, name: Bahrack, type: Type3}\n", output);
68        // parse back to instance
69        Constructor constr = new Constructor();
70        TypeDescription description = new TypeDescription(Wrapper.class, new Tag("!mybean"));
71        constr.addTypeDescription(description);
72        yaml = new Yaml(constr);
73        Wrapper wrapper = (Wrapper) yaml.load(output);
74        JavaBeanWithStaticState bean2 = wrapper.createBean();
75        assertEquals(-47, bean2.getAge());
76        assertEquals("Bahrack", bean2.getName());
77    }
78
79    /**
80     * use wrapper with no tag
81     */
82    public void testRootBean() {
83        JavaBeanWithStaticState bean = new JavaBeanWithStaticState();
84        bean.setName("Bahrack");
85        bean.setAge(-47);
86        JavaBeanWithStaticState.setType("Type3");
87        JavaBeanWithStaticState.color = "Violet";
88        Yaml yaml = new Yaml();
89        String output = yaml.dumpAsMap(new Wrapper(bean));
90        // System.out.println(output);
91        assertEquals("age: -47\ncolor: Violet\nname: Bahrack\ntype: Type3\n", output);
92        // parse back to instance
93        Yaml loader = new Yaml();
94        Wrapper wrapper = loader.loadAs(output, Wrapper.class);
95        JavaBeanWithStaticState bean2 = wrapper.createBean();
96        assertEquals(-47, bean2.getAge());
97        assertEquals("Bahrack", bean2.getName());
98    }
99
100}
101