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