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