1 /** 2 * Copyright (c) 2008, 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 testColor()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 testCode()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 testSuperColor()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 testSuperColorFail()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 "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 testCode2()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 testCode3()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 "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 testCode4()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 "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)\n" 84 + " in 'string', line 1, column 1:\n" 85 + " !!org.yaml.snakeyaml.immutable.C ... \n" + " ^\n", 86 e.getMessage()); 87 } 88 } 89 testPoint()90 public void testPoint() { 91 Yaml yaml = new Yaml(); 92 Point loaded = (Point) yaml.load("!!org.yaml.snakeyaml.immutable.Point [1.17, 3.14]"); 93 assertEquals(1.17, loaded.getX()); 94 assertEquals(3.14, loaded.getY()); 95 } 96 testPointBlock()97 public void testPointBlock() { 98 Yaml yaml = new Yaml(); 99 Point loaded = (Point) yaml.load("!!org.yaml.snakeyaml.immutable.Point\n- 1.17\n- 3.14"); 100 assertEquals(1.17, loaded.getX()); 101 assertEquals(3.14, loaded.getY()); 102 } 103 testPointOnlyOneArgument()104 public void testPointOnlyOneArgument() { 105 Yaml yaml = new Yaml(); 106 try { 107 yaml.load("!!org.yaml.snakeyaml.immutable.Point\n- 1.17"); 108 fail("Two arguments required."); 109 } catch (Exception e) { 110 assertEquals( 111 "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\n" 112 + " in 'string', line 1, column 1:\n" 113 + " !!org.yaml.snakeyaml.immutable.Point\n" + " ^\n", 114 e.getMessage()); 115 } 116 } 117 testPoint2()118 public void testPoint2() { 119 Yaml yaml = new Yaml(); 120 Point2 loaded = (Point2) yaml.load("!!org.yaml.snakeyaml.immutable.Point2\n- 1\n- 3"); 121 assertEquals(new Integer(1), loaded.getX()); 122 assertEquals(new Integer(3), loaded.getY()); 123 } 124 testPoint3d()125 public void testPoint3d() { 126 Yaml yaml = new Yaml(); 127 Point3d loaded = (Point3d) yaml 128 .load("!!org.yaml.snakeyaml.immutable.Point3d [!!org.yaml.snakeyaml.immutable.Point [1.17, 3.14], 345.1]"); 129 assertEquals(345.1, loaded.getZ()); 130 } 131 testShape()132 public void testShape() { 133 Yaml yaml = new Yaml(); 134 String source = Util.getLocalResource("immutable/shape1.yaml"); 135 Shape loaded = (Shape) yaml.load(source); 136 assertEquals(new Integer(123), loaded.getId()); 137 } 138 testShapeNoTags()139 public void testShapeNoTags() { 140 String source = Util.getLocalResource("immutable/shapeNoTags.yaml"); 141 Yaml beanLoader = new Yaml(); 142 Shape loaded = beanLoader.loadAs(source, Shape.class); 143 assertEquals(new Integer(123), loaded.getId()); 144 assertEquals("BLACK", loaded.getColor().getName()); 145 assertEquals(1.17, loaded.getPoint().getX()); 146 assertEquals(3.14, loaded.getPoint().getY()); 147 assertEquals(345.1, loaded.getPoint3d().getZ()); 148 assertEquals(1.96, loaded.getPoint3d().getPoint().getX()); 149 assertEquals(1.78, loaded.getPoint3d().getPoint().getY()); 150 } 151 } 152