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