View Javadoc

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