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 org.yaml.snakeyaml.javabeans;
    18
    19  import junit.framework.TestCase;
    20
    21  import org.yaml.snakeyaml.Yaml;
    22
    23  public class TriangleBeanTest extends TestCase {
    24
    25      public void testGetTriangle() {
    26          Triangle triangle = new Triangle();
    27          triangle.setName("Triangle25");
    28          TriangleBean bean = new TriangleBean();
    29          bean.setShape(triangle);
    30          bean.setName("Bean25");
    31          Yaml beanDumper = new Yaml();
    32          String output = beanDumper.dumpAsMap(bean);
    33          assertEquals(
    34                  "name: Bean25\nshape: !!org.yaml.snakeyaml.javabeans.Triangle\n  name: Triangle25\n",
    35                  output);
    36          Yaml beanLoader = new Yaml();
    37          TriangleBean loadedBean = beanLoader.loadAs(output, TriangleBean.class);
    38          assertNotNull(loadedBean);
    39          assertEquals("Bean25", loadedBean.getName());
    40          assertEquals(7, loadedBean.getShape().process());
    41      }
    42
    43      public void testClassNotFound() {
    44          String output = "name: Bean25\nshape: !!org.yaml.snakeyaml.javabeans.Triangle777\n  name: Triangle25\n";
    45          Yaml beanLoader = new Yaml();
    46          try {
    47              beanLoader.loadAs(output, TriangleBean.class);
    48              fail("Class not found expected.");
    49          } catch (Exception e) {
    50              assertEquals(
    51                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.javabeans.TriangleBean; exception=Cannot create property=shape for JavaBean=TriangleBean name=Bean25; null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.javabeans.Triangle777; exception=Class not found: org.yaml.snakeyaml.javabeans.Triangle777",
    52                      e.getMessage());
    53          }
    54      }
    55
    56      /**
    57       * Runtime class has less priority then an explicit tag
    58       */
    59      public void testClassAndTag() {
    60          String output = "name: !!whatever Bean25\nshape: !!org.yaml.snakeyaml.javabeans.Triangle\n  name: Triangle25\n";
    61          Yaml beanLoader = new Yaml();
    62          try {
    63              beanLoader.loadAs(output, TriangleBean.class);
    64              fail("Runtime class has less priority then an explicit tag");
    65          } catch (Exception e) {
    66              assertEquals(
    67                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.javabeans.TriangleBean; exception=Cannot create property=name for JavaBean=TriangleBean name=null; null; Can't construct a java object for tag:yaml.org,2002:whatever; exception=Class not found: whatever",
    68                      e.getMessage());
    69          }
    70      }
    71  }