• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.pyyaml;
15 
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.InputStream;
20 import java.io.StringWriter;
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.yaml.snakeyaml.DumperOptions;
25 import org.yaml.snakeyaml.emitter.Emitter;
26 import org.yaml.snakeyaml.emitter.EventConstructor;
27 import org.yaml.snakeyaml.error.YAMLException;
28 import org.yaml.snakeyaml.events.Event;
29 
30 /**
31  * imported from PyYAML
32  */
33 public class PyErrorsTest extends PyImportTest {
34 
skip(String filename)35   private boolean skip(String filename) {
36     List<String> failures = new ArrayList<String>();
37     // in python list cannot be a key in a dictionary.
38     failures.add("unacceptable-key.loader-error");
39     for (String name : failures) {
40       if (name.equals(filename)) {
41         return true;
42       }
43     }
44     return false;
45   }
46 
testLoaderErrors()47   public void testLoaderErrors() throws FileNotFoundException {
48     File[] files = getStreamsByExtension(".loader-error");
49     assertTrue("No test files found.", files.length > 0);
50     for (int i = 0; i < files.length; i++) {
51       if (skip(files[i].getName())) {
52         continue;
53       }
54       try {
55         InputStream input = new FileInputStream(files[i]);
56         for (Object document : loadAll(input)) {
57           assertNotNull("File " + files[i], document);
58         }
59         input.close();
60         fail("Loading must fail for " + files[i].getAbsolutePath());
61         // System.err.println("Loading must fail for " +
62         // files[i].getAbsolutePath());
63       } catch (Exception e) {
64         assertTrue(true);
65       }
66     }
67   }
68 
testLoaderStringErrors()69   public void testLoaderStringErrors() throws FileNotFoundException {
70     File[] files = getStreamsByExtension(".loader-error");
71     assertTrue("No test files found.", files.length > 0);
72     for (int i = 0; i < files.length; i++) {
73       if (skip(files[i].getName())) {
74         continue;
75       }
76       try {
77         String content = getResource(files[i].getName());
78         for (Object document : loadAll(content.trim())) {
79           assertNotNull(document);
80         }
81         fail("Loading must fail for " + files[i].getAbsolutePath());
82         // System.err.println("Loading must fail for " +
83         // files[i].getAbsolutePath());
84       } catch (Exception e) {
85         assertTrue(true);
86       }
87     }
88   }
89 
testLoaderSingleErrors()90   public void testLoaderSingleErrors() throws FileNotFoundException {
91     File[] files = getStreamsByExtension(".single-loader-error");
92     assertTrue("No test files found.", files.length > 0);
93     for (int i = 0; i < files.length; i++) {
94       try {
95         String content = getResource(files[i].getName());
96         load(content.trim());
97         fail("Loading must fail for " + files[i].getAbsolutePath());
98         // multiple documents must not be accepted
99         System.err.println("Loading must fail for " + files[i].getAbsolutePath());
100       } catch (YAMLException e) {
101         assertTrue(true);
102       }
103     }
104   }
105 
106   @SuppressWarnings("unchecked")
testEmitterErrors()107   public void testEmitterErrors() {
108     File[] files = getStreamsByExtension(".emitter-error");
109     assertTrue("No test files found.", files.length > 0);
110     for (int i = 0; i < files.length; i++) {
111       String content = getResource(files[i].getName());
112       List<Event> document = (List<Event>) load(new EventConstructor(), content.trim());
113       Writer writer = new StringWriter();
114       Emitter emitter = new Emitter(writer, new DumperOptions());
115       try {
116         for (Event event : document) {
117           emitter.emit(event);
118         }
119         fail("Loading must fail for " + files[i].getAbsolutePath());
120         // System.err.println("Loading must fail for " +
121         // files[i].getAbsolutePath());
122       } catch (Exception e) {
123         assertTrue(true);
124       }
125     }
126   }
127 
128   // testDumperErrors() is implemented in SerializerTest.java
129 }
130