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.extensions.compactnotation;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.Util;
24  import org.yaml.snakeyaml.Yaml;
25  import org.yaml.snakeyaml.constructor.BaseConstructor;
26  import org.yaml.snakeyaml.error.YAMLException;
27  
28  public class CompactConstructorErrorsTest extends TestCase {
29  
30      public void test1() {
31          BaseConstructor compact = new CompactConstructor();
32          Yaml yaml = new Yaml(compact);
33          String doc = Util.getLocalResource("compactnotation/error1.yaml");
34          try {
35              yaml.load(doc);
36              fail("Package is not specified.");
37          } catch (Exception e) {
38              assertEquals("java.lang.ClassNotFoundException: Table", e.getMessage());
39          }
40      }
41  
42      private Object load(String fileName) {
43          CompactConstructor compact = new PackageCompactConstructor(
44                  "org.yaml.snakeyaml.extensions.compactnotation");
45          Yaml yaml = new Yaml(compact);
46          String doc = Util.getLocalResource("compactnotation/" + fileName);
47          Object obj = yaml.load(doc);
48          assertNotNull(obj);
49          return obj;
50      }
51  
52      private void failLoad(String fileName, String failure) {
53          load(fileName);
54          fail(failure);
55      }
56  
57      private void check(String fileName, String failure, String message) {
58          check(fileName, failure, message, true);
59      }
60  
61      private void check(String fileName, String failure, String message, boolean exactMatch) {
62          try {
63              failLoad(fileName, failure);
64          } catch (YAMLException e) {
65              String eMessage = e.getMessage();
66              if (exactMatch) {
67                  assertEquals(message, eMessage);
68              } else {
69                  assertNotNull("Exception message is NULL", eMessage);
70                  assertTrue(String.format(
71                          "\nException message\n%s\ndoes not contain expected value\n%s",
72                          e.getMessage(), message), eMessage.contains(message));
73              }
74          } catch (Exception e) {
75              fail("Exception must be YAMLException");
76          }
77      }
78  
79      public void test2() {
80          check("error2.yaml",
81                  "No single argument constructor provided.",
82                  "java.lang.NoSuchMethodException: org.yaml.snakeyaml.extensions.compactnotation.Table.<init>(java.lang.String)");
83      }
84  
85      public void test3() {
86          check("error3.yaml",
87                  "In-line parameters can only be Strings.",
88                  "org.yaml.snakeyaml.error.YAMLException: Cannot set property='size' with value='17' (class java.lang.String) in Row id=id111");
89      }
90  
91      /**
92       * Created Map instead of Row
93       */
94      @SuppressWarnings("unchecked")
95      public void test4() {
96          Table table = (Table) load("error4.yaml");
97          List<Row> rows = table.getRows();
98          assertEquals(1, rows.size());
99          assertFalse("Row should not be created.", rows.get(0) instanceof Row);
100         Map<String, String> map = (Map<String, String>) rows.get(0);
101         assertEquals(1, map.size());
102         assertEquals("15}", map.get("Row(id111, description = text) {size"));
103     }
104 
105     /**
106      * Wrong indent
107      */
108     @SuppressWarnings("unchecked")
109     public void test5() {
110         Table table = (Table) load("error5.yaml");
111         List<Row> rows = table.getRows();
112         assertEquals(1, rows.size());
113         assertFalse("Row should not be created.", rows.get(0) instanceof Row);
114         Map<String, String> map = (Map<String, String>) rows.get(0);
115         assertEquals(4, map.size());
116         // System.out.println(map);
117         assertNull(map.get(new Row("id222")));
118         assertTrue(map.containsKey(new Row("id222")));
119         assertEquals(17, map.get("size"));
120     }
121 
122     public void test6() {
123         check("error6.yaml",
124                 "Invalid property.",
125                 "org.yaml.snakeyaml.error.YAMLException: Unable to find property 'foo' on class: org.yaml.snakeyaml.extensions.compactnotation.Table");
126     }
127 
128     public void test7() {
129         check("error7.yaml",
130                 "Invalid property.",
131                 "Unable to find property 'foo' on class: org.yaml.snakeyaml.extensions.compactnotation.Table",
132                 false);
133     }
134 
135     public void test8() {
136         check("error8.yaml",
137                 "No list property",
138                 "org.yaml.snakeyaml.error.YAMLException: No list property found in class org.yaml.snakeyaml.extensions.compactnotation.Row");
139     }
140 
141     public void test9() {
142         check("error9.yaml",
143                 "Many list properties found",
144                 "org.yaml.snakeyaml.error.YAMLException: Many list properties found in class org.yaml.snakeyaml.extensions.compactnotation.ManyListsTable; Please override getSequencePropertyName() to specify which property to use.");
145     }
146 }