View Javadoc

    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
    17  package org.yaml.snakeyaml.extensions.compactnotation;
    18
    19  import java.util.List;
    20  import java.util.Map;
    21
    22  import junit.framework.TestCase;
    23
    24  import org.yaml.snakeyaml.Util;
    25  import org.yaml.snakeyaml.Yaml;
    26  import org.yaml.snakeyaml.constructor.BaseConstructor;
    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          try {
    59              failLoad(fileName, failure);
    60          } catch (Exception e) {
    61              assertEquals(message, e.getMessage());
    62          }
    63      }
    64
    65      public void test2() {
    66          check("error2.yaml",
    67                  "No single argument constructor provided.",
    68                  "java.lang.NoSuchMethodException: org.yaml.snakeyaml.extensions.compactnotation.Table.<init>(java.lang.String)");
    69      }
    70
    71      public void test3() {
    72          check("error3.yaml",
    73                  "In-line parameters can only be Strings.",
    74                  "org.yaml.snakeyaml.error.YAMLException: Cannot set property='size' with value='17' (class java.lang.String) in Row id=id111");
    75      }
    76
    77      /**
    78       * Created Map instead of Row
    79       */
    80      @SuppressWarnings("unchecked")
    81      public void test4() {
    82          Table table = (Table) load("error4.yaml");
    83          List<Row> rows = table.getRows();
    84          assertEquals(1, rows.size());
    85          assertFalse("Row should not be created.", rows.get(0) instanceof Row);
    86          Map<String, String> map = (Map<String, String>) rows.get(0);
    87          assertEquals(1, map.size());
    88          assertEquals("15}", map.get("Row(id111, description = text) {size"));
    89      }
    90
    91      /**
    92       * Wrong indent
    93       */
    94      @SuppressWarnings("unchecked")
    95      public void test5() {
    96          Table table = (Table) load("error5.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(4, map.size());
    102         // System.out.println(map);
    103         assertNull(map.get(new Row("id222")));
    104         assertTrue(map.containsKey(new Row("id222")));
    105         assertEquals(17, map.get("size"));
    106     }
    107
    108     public void test6() {
    109         check("error6.yaml",
    110                 "Invalid property.",
    111                 "org.yaml.snakeyaml.error.YAMLException: Unable to find property 'foo' on class: org.yaml.snakeyaml.extensions.compactnotation.Table");
    112     }
    113
    114     public void test7() {
    115         check("error7.yaml",
    116                 "Invalid property.",
    117                 "org.yaml.snakeyaml.error.YAMLException: Unable to find property 'foo' on class: org.yaml.snakeyaml.extensions.compactnotation.Table");
    118     }
    119
    120     public void test8() {
    121         check("error8.yaml",
    122                 "No list property",
    123                 "org.yaml.snakeyaml.error.YAMLException: No list property found in class org.yaml.snakeyaml.extensions.compactnotation.Row");
    124     }
    125
    126     public void test9() {
    127         check("error9.yaml",
    128                 "Many list properties found",
    129                 "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.");
    130     }
    131 }