View Javadoc

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