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.immutable;
    18
    19  import junit.framework.TestCase;
    20
    21  import org.yaml.snakeyaml.Util;
    22  import org.yaml.snakeyaml.Yaml;
    23
    24  public class ShapeImmutableTest extends TestCase {
    25
    26      public void testColor() {
    27          Yaml yaml = new Yaml();
    28          Color loaded = (Color) yaml.load("!!org.yaml.snakeyaml.immutable.Color BLACK");
    29          assertEquals("BLACK", loaded.getName());
    30      }
    31
    32      public void testCode() {
    33          Yaml yaml = new Yaml();
    34          Code loaded = (Code) yaml.load("!!org.yaml.snakeyaml.immutable.Code 123");
    35          assertEquals(new Integer(123), loaded.getCode());
    36      }
    37
    38      public void testSuperColor() {
    39          Yaml yaml = new Yaml();
    40          SuperColor superColor = (SuperColor) yaml
    41                  .load("!!org.yaml.snakeyaml.immutable.SuperColor [!!org.yaml.snakeyaml.immutable.Color BLACK]");
    42          assertEquals("BLACK", superColor.getColor().getName());
    43      }
    44
    45      public void testSuperColorFail() {
    46          Yaml yaml = new Yaml();
    47          try {
    48              yaml.load("!!org.yaml.snakeyaml.immutable.SuperColor BLACK");
    49              fail("SuperColor requires Color and not a String.");
    50          } catch (Exception e) {
    51              assertEquals(
    52                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.immutable.SuperColor; exception=Unsupported class: class org.yaml.snakeyaml.immutable.Color",
    53                      e.getMessage());
    54          }
    55      }
    56
    57      public void testCode2() {
    58          Yaml yaml = new Yaml();
    59          Code2 code2 = (Code2) yaml.load("!!org.yaml.snakeyaml.immutable.Code2 555");
    60          assertEquals(new Integer(555), code2.getCode());
    61      }
    62
    63      public void testCode3() {
    64          Yaml yaml = new Yaml();
    65          try {
    66              yaml.load("!!org.yaml.snakeyaml.immutable.Code3 777");
    67              fail("There must be 1 constructor with 1 argument for scalar.");
    68          } catch (Exception e) {
    69              assertEquals(
    70                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.immutable.Code3; exception=No single argument constructor found for class org.yaml.snakeyaml.immutable.Code3",
    71                      e.getMessage());
    72          }
    73      }
    74
    75      public void testCode4() {
    76          Yaml yaml = new Yaml();
    77          try {
    78              yaml.load("!!org.yaml.snakeyaml.immutable.Code4 777");
    79              fail("Constructor with String is required.");
    80          } catch (Exception e) {
    81              assertEquals(
    82                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.immutable.Code4; exception=null; Can't construct a java object for scalar tag:yaml.org,2002:org.yaml.snakeyaml.immutable.Code4; No String constructor found. Exception=org.yaml.snakeyaml.immutable.Code4.<init>(java.lang.String)",
    83                      e.getMessage());
    84          }
    85      }
    86
    87      public void testPoint() {
    88          Yaml yaml = new Yaml();
    89          Point loaded = (Point) yaml.load("!!org.yaml.snakeyaml.immutable.Point [1.17, 3.14]");
    90          assertEquals(1.17, loaded.getX());
    91          assertEquals(3.14, loaded.getY());
    92      }
    93
    94      public void testPointBlock() {
    95          Yaml yaml = new Yaml();
    96          Point loaded = (Point) yaml.load("!!org.yaml.snakeyaml.immutable.Point\n- 1.17\n- 3.14");
    97          assertEquals(1.17, loaded.getX());
    98          assertEquals(3.14, loaded.getY());
    99      }
    100
    101     public void testPointOnlyOneArgument() {
    102         Yaml yaml = new Yaml();
    103         try {
    104             yaml.load("!!org.yaml.snakeyaml.immutable.Point\n- 1.17");
    105             fail("Two arguments required.");
    106         } catch (Exception e) {
    107             assertEquals(
    108                     "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.immutable.Point; exception=No suitable constructor with 1 arguments found for class org.yaml.snakeyaml.immutable.Point",
    109                     e.getMessage());
    110         }
    111     }
    112
    113     public void testPoint2() {
    114         Yaml yaml = new Yaml();
    115         Point2 loaded = (Point2) yaml.load("!!org.yaml.snakeyaml.immutable.Point2\n- 1\n- 3");
    116         assertEquals(new Integer(1), loaded.getX());
    117         assertEquals(new Integer(3), loaded.getY());
    118     }
    119
    120     public void testPoint3d() {
    121         Yaml yaml = new Yaml();
    122         Point3d loaded = (Point3d) yaml
    123                 .load("!!org.yaml.snakeyaml.immutable.Point3d [!!org.yaml.snakeyaml.immutable.Point [1.17, 3.14], 345.1]");
    124         assertEquals(345.1, loaded.getZ());
    125     }
    126
    127     public void testShape() {
    128         Yaml yaml = new Yaml();
    129         String source = Util.getLocalResource("immutable/shape1.yaml");
    130         Shape loaded = (Shape) yaml.load(source);
    131         assertEquals(new Integer(123), loaded.getId());
    132     }
    133
    134     public void testShapeNoTags() {
    135         String source = Util.getLocalResource("immutable/shapeNoTags.yaml");
    136         Yaml beanLoader = new Yaml();
    137         Shape loaded = beanLoader.loadAs(source, Shape.class);
    138         assertEquals(new Integer(123), loaded.getId());
    139         assertEquals("BLACK", loaded.getColor().getName());
    140         assertEquals(1.17, loaded.getPoint().getX());
    141         assertEquals(3.14, loaded.getPoint().getY());
    142         assertEquals(345.1, loaded.getPoint3d().getZ());
    143         assertEquals(1.96, loaded.getPoint3d().getPoint().getX());
    144         assertEquals(1.78, loaded.getPoint3d().getPoint().getY());
    145     }
    146 }