1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package examples.staticstate; 15 16 import junit.framework.TestCase; 17 import org.yaml.snakeyaml.TypeDescription; 18 import org.yaml.snakeyaml.Yaml; 19 import org.yaml.snakeyaml.constructor.Constructor; 20 import org.yaml.snakeyaml.nodes.Tag; 21 import org.yaml.snakeyaml.representer.Representer; 22 23 /** 24 * Example: using wrapper object for static fields 25 */ 26 public class StaticFieldsWrapperTest extends TestCase { 27 28 /** 29 * use wrapper with global tag 30 */ testWrapper()31 public void testWrapper() { 32 JavaBeanWithStaticState bean = new JavaBeanWithStaticState(); 33 bean.setName("Bahrack"); 34 bean.setAge(-47); 35 JavaBeanWithStaticState.setType("Type3"); 36 JavaBeanWithStaticState.color = "Violet"; 37 Yaml yaml = new Yaml(); 38 String output = yaml.dump(new Wrapper(bean)); 39 // System.out.println(output); 40 assertEquals( 41 "!!examples.staticstate.Wrapper {age: -47, color: Violet, name: Bahrack, type: Type3}\n", 42 output); 43 // parse back to instance 44 Wrapper wrapper = yaml.load(output); 45 JavaBeanWithStaticState bean2 = wrapper.createBean(); 46 assertEquals(-47, bean2.getAge()); 47 assertEquals("Bahrack", bean2.getName()); 48 } 49 50 /** 51 * use wrapper with local tag 52 */ testLocalTag()53 public void testLocalTag() { 54 JavaBeanWithStaticState bean = new JavaBeanWithStaticState(); 55 bean.setName("Bahrack"); 56 bean.setAge(-47); 57 JavaBeanWithStaticState.setType("Type3"); 58 JavaBeanWithStaticState.color = "Violet"; 59 Representer repr = new Representer(); 60 repr.addClassTag(Wrapper.class, new Tag("!mybean")); 61 Yaml yaml = new Yaml(repr); 62 String output = yaml.dump(new Wrapper(bean)); 63 // System.out.println(output); 64 assertEquals("!mybean {age: -47, color: Violet, name: Bahrack, type: Type3}\n", output); 65 // parse back to instance 66 Constructor constr = new Constructor(); 67 TypeDescription description = new TypeDescription(Wrapper.class, new Tag("!mybean")); 68 constr.addTypeDescription(description); 69 yaml = new Yaml(constr); 70 Wrapper wrapper = yaml.load(output); 71 JavaBeanWithStaticState bean2 = wrapper.createBean(); 72 assertEquals(-47, bean2.getAge()); 73 assertEquals("Bahrack", bean2.getName()); 74 } 75 76 /** 77 * use wrapper with no tag 78 */ testRootBean()79 public void testRootBean() { 80 JavaBeanWithStaticState bean = new JavaBeanWithStaticState(); 81 bean.setName("Bahrack"); 82 bean.setAge(-47); 83 JavaBeanWithStaticState.setType("Type3"); 84 JavaBeanWithStaticState.color = "Violet"; 85 Yaml yaml = new Yaml(); 86 String output = yaml.dumpAsMap(new Wrapper(bean)); 87 // System.out.println(output); 88 assertEquals("age: -47\ncolor: Violet\nname: Bahrack\ntype: Type3\n", output); 89 // parse back to instance 90 Yaml loader = new Yaml(); 91 Wrapper wrapper = loader.loadAs(output, Wrapper.class); 92 JavaBeanWithStaticState bean2 = wrapper.createBean(); 93 assertEquals(-47, bean2.getAge()); 94 assertEquals("Bahrack", bean2.getName()); 95 } 96 97 } 98