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.pyyaml;
    18
    19  import java.io.File;
    20  import java.io.FileInputStream;
    21  import java.io.FileNotFoundException;
    22  import java.io.InputStream;
    23  import java.io.StringWriter;
    24  import java.io.Writer;
    25  import java.util.ArrayList;
    26  import java.util.List;
    27
    28  import org.yaml.snakeyaml.DumperOptions;
    29  import org.yaml.snakeyaml.emitter.Emitter;
    30  import org.yaml.snakeyaml.emitter.EventConstructor;
    31  import org.yaml.snakeyaml.error.YAMLException;
    32  import org.yaml.snakeyaml.events.Event;
    33
    34  /**
    35   * @see imported from PyYAML
    36   */
    37  public class PyErrorsTest extends PyImportTest {
    38      private boolean skip(String filename) {
    39          List<String> failures = new ArrayList<String>();
    40          // in python list cannot be a key in a dictionary.
    41          failures.add("unacceptable-key.loader-error");
    42          for (String name : failures) {
    43              if (name.equals(filename)) {
    44                  return true;
    45              }
    46          }
    47          return false;
    48      }
    49
    50      public void testLoaderErrors() throws FileNotFoundException {
    51          File[] files = getStreamsByExtension(".loader-error");
    52          assertTrue("No test files found.", files.length > 0);
    53          for (int i = 0; i < files.length; i++) {
    54              if (skip(files[i].getName())) {
    55                  continue;
    56              }
    57              try {
    58                  InputStream input = new FileInputStream(files[i]);
    59                  for (Object document : loadAll(input)) {
    60                      assertNotNull("File " + files[i], document);
    61                  }
    62                  input.close();
    63                  fail("Loading must fail for " + files[i].getAbsolutePath());
    64                  // System.err.println("Loading must fail for " +
    65                  // files[i].getAbsolutePath());
    66              } catch (Exception e) {
    67                  assertTrue(true);
    68              }
    69          }
    70      }
    71
    72      public void testLoaderStringErrors() throws FileNotFoundException {
    73          File[] files = getStreamsByExtension(".loader-error");
    74          assertTrue("No test files found.", files.length > 0);
    75          for (int i = 0; i < files.length; i++) {
    76              if (skip(files[i].getName())) {
    77                  continue;
    78              }
    79              try {
    80                  String content = getResource(files[i].getName());
    81                  for (Object document : loadAll(content.trim())) {
    82                      assertNotNull(document);
    83                  }
    84                  fail("Loading must fail for " + files[i].getAbsolutePath());
    85                  // System.err.println("Loading must fail for " +
    86                  // files[i].getAbsolutePath());
    87              } catch (Exception e) {
    88                  assertTrue(true);
    89              }
    90          }
    91      }
    92
    93      public void testLoaderSingleErrors() throws FileNotFoundException {
    94          File[] files = getStreamsByExtension(".single-loader-error");
    95          assertTrue("No test files found.", files.length > 0);
    96          for (int i = 0; i < files.length; i++) {
    97              try {
    98                  String content = getResource(files[i].getName());
    99                  load(content.trim());
    100                 fail("Loading must fail for " + files[i].getAbsolutePath());
    101                 // multiple documents must not be accepted
    102                 System.err.println("Loading must fail for " + files[i].getAbsolutePath());
    103             } catch (YAMLException e) {
    104                 assertTrue(true);
    105             }
    106         }
    107     }
    108
    109     @SuppressWarnings("unchecked")
    110     public void testEmitterErrors() {
    111         File[] files = getStreamsByExtension(".emitter-error");
    112         assertTrue("No test files found.", files.length > 0);
    113         for (int i = 0; i < files.length; i++) {
    114             String content = getResource(files[i].getName());
    115             List<Event> document = (List<Event>) load(new EventConstructor(), content.trim());
    116             Writer writer = new StringWriter();
    117             Emitter emitter = new Emitter(writer, new DumperOptions());
    118             try {
    119                 for (Event event : document) {
    120                     emitter.emit(event);
    121                 }
    122                 fail("Loading must fail for " + files[i].getAbsolutePath());
    123                 // System.err.println("Loading must fail for " +
    124                 // files[i].getAbsolutePath());
    125             } catch (Exception e) {
    126                 assertTrue(true);
    127             }
    128         }
    129     }
    130
    131     // testDumperErrors() is implemented in SerializerTest.java
    132 }