PageRenderTime 59ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/snakeyaml/src/test/java/org/yaml/snakeyaml/immutable/ShapeImmutableTest.java

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