• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.util.ArrayList;
17 import java.util.List;
18 import java.util.Set;
19 import junit.framework.TestCase;
20 import org.yaml.snakeyaml.DumperOptions;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.constructor.Constructor;
23 import org.yaml.snakeyaml.introspector.Property;
24 import org.yaml.snakeyaml.nodes.MappingNode;
25 import org.yaml.snakeyaml.nodes.Node;
26 import org.yaml.snakeyaml.nodes.NodeTuple;
27 import org.yaml.snakeyaml.nodes.ScalarNode;
28 import org.yaml.snakeyaml.nodes.Tag;
29 import org.yaml.snakeyaml.representer.Representer;
30 
31 /**
32  * Example with static fields
33  */
34 public class StaticFieldsTest extends TestCase {
35 
testAsJavaBean()36   public void testAsJavaBean() {
37     JavaBeanWithStaticState bean = new JavaBeanWithStaticState();
38     bean.setName("Bahrack");
39     bean.setAge(-47);
40     JavaBeanWithStaticState.setType("Represent");
41     JavaBeanWithStaticState.color = "Black";
42     Yaml yaml = new Yaml();
43     String output = yaml.dump(bean);
44     // System.out.println(output);
45     assertEquals("!!examples.staticstate.JavaBeanWithStaticState {age: -47, name: Bahrack}\n",
46         output);
47     // parse back to instance
48     JavaBeanWithStaticState bean2 = yaml.load(output);
49     assertEquals(-47, bean2.getAge());
50     assertEquals("Bahrack", bean2.getName());
51   }
52 
testCustomDump()53   public void testCustomDump() {
54     JavaBeanWithStaticState bean = new JavaBeanWithStaticState();
55     bean.setName("Lui");
56     bean.setAge(25);
57     JavaBeanWithStaticState.setType("Represent");
58     JavaBeanWithStaticState.color = "Black";
59     Yaml yaml = new Yaml(new MyRepresenter(), new DumperOptions());
60     String output = yaml.dump(bean);
61     // System.out.println(output);
62     assertEquals(
63         "!!examples.staticstate.JavaBeanWithStaticState {age: 25, name: Lui, color: Black,\n  type: Represent}\n",
64         output);
65   }
66 
testCustomLoad()67   public void testCustomLoad() {
68     Yaml yaml = new Yaml(new MyConstructor());
69     String output =
70         "!!examples.staticstate.JavaBeanWithStaticState {age: 25, name: Lui, color: Oranje,\n  type: King}\n";
71     JavaBeanWithStaticState bean2 = yaml.load(output);
72     assertEquals(25, bean2.getAge());
73     assertEquals("Lui", bean2.getName());
74     assertEquals("Oranje", JavaBeanWithStaticState.color);
75     assertEquals("King", JavaBeanWithStaticState.getType());
76   }
77 
78   private class MyRepresenter extends Representer {
79 
80     @Override
representJavaBean(Set<Property> properties, Object javaBean)81     protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
82       MappingNode node = super.representJavaBean(properties, javaBean);
83       if (javaBean instanceof JavaBeanWithStaticState) {
84         List<NodeTuple> value = node.getValue();
85         value.add(
86             new NodeTuple(representData("color"), representData(JavaBeanWithStaticState.color)));
87         value.add(
88             new NodeTuple(representData("type"), representData(JavaBeanWithStaticState.getType())));
89       }
90       return node;
91     }
92   }
93 
94   private class MyConstructor extends Constructor {
95 
96     private final Tag JBWSS = new Tag(JavaBeanWithStaticState.class);
97 
constructObject(Node node)98     protected Object constructObject(Node node) {
99       if (JavaBeanWithStaticState.class.isAssignableFrom(node.getType())
100           || JBWSS.equals(node.getTag())) {
101         MappingNode beanNode = (MappingNode) node;
102         List<NodeTuple> value = beanNode.getValue();
103         List<NodeTuple> removed = new ArrayList<NodeTuple>();
104         for (NodeTuple tuple : value) {
105           ScalarNode keyNode = (ScalarNode) tuple.getKeyNode();
106           if (keyNode.getValue().equals("color")) {
107             ScalarNode valueNode = (ScalarNode) tuple.getValueNode();
108             JavaBeanWithStaticState.color = valueNode.getValue();
109           } else if (keyNode.getValue().equals("type")) {
110             ScalarNode valueNode = (ScalarNode) tuple.getValueNode();
111             JavaBeanWithStaticState.setType(valueNode.getValue());
112           } else {
113             removed.add(tuple);
114           }
115         }
116         beanNode.setValue(removed);
117         JavaBeanWithStaticState bean = (JavaBeanWithStaticState) super.constructObject(beanNode);
118 
119         return bean;
120       } else {
121         return super.constructObject(node);
122       }
123     }
124   }
125 }
126