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.issues.issue145;
17  
18  import junit.framework.TestCase;
19  
20  import org.yaml.snakeyaml.Yaml;
21  
22  public class LineNumberInExceptionTest extends TestCase {
23  
24      public void testLineReport() {
25          Yaml yaml = new Yaml();
26          try {
27              yaml.load("---\n!!org.yaml.snakeyaml.issues.issue145.AbstractThing { id: QQQ }");
28              fail("Instances for abstract classes cannot be created");
29          } catch (Exception e) {
30              assertTrue(e.toString().contains("line 2, column 1"));
31              assertEquals(
32                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.issues.issue145.AbstractThing; exception=java.lang.InstantiationException;  in 'string', line 2, column 1:\n    !!org.yaml.snakeyaml.issues.issu ... \n    ^",
33                      e.getMessage());
34          }
35      }
36  
37      public void testCompleteThing() {
38          Yaml yaml = new Yaml();
39          CompleteThing thing = (CompleteThing) yaml
40                  .load("---\n!!org.yaml.snakeyaml.issues.issue145.CompleteThing { id: QQQ }");
41          assertEquals("QQQ", thing.getId());
42      }
43  
44      public void testWrongParameter() {
45          Yaml yaml = new Yaml();
46          try {
47              yaml.load("---\n!!org.yaml.snakeyaml.issues.issue145.CompleteThing { id2: QQQ }");
48              fail("Invalid parameter");
49          } catch (Exception e) {
50              assertTrue(e.toString().contains("line 2, column 1"));
51              assertEquals(
52                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.issues.issue145.CompleteThing; exception=Cannot create property=id2 for JavaBean=CompleteThing-null; Unable to find property 'id2' on class: org.yaml.snakeyaml.issues.issue145.CompleteThing;  in 'string', line 2, column 1:\n    !!org.yaml.snakeyaml.issues.issu ... \n    ^",
53                      e.getMessage());
54          }
55      }
56  }