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.IOException;
     21 import java.io.InputStream;
     22 
     23 import org.yaml.snakeyaml.error.YAMLException;
     24 import org.yaml.snakeyaml.reader.ReaderException;
     25 import org.yaml.snakeyaml.reader.StreamReader;
     26 import org.yaml.snakeyaml.reader.UnicodeReader;
     27 
     28 /**
     29  * imported from PyYAML
     30  */
     31 public class PyReaderTest extends PyImportTest {
     32 
     33     public void testReaderUnicodeErrors() throws IOException {
     34         File[] inputs = getStreamsByExtension(".stream-error");
     35         for (int i = 0; i < inputs.length; i++) {
     36             InputStream input = new FileInputStream(inputs[i]);
     37             StreamReader stream = new StreamReader(new UnicodeReader(input));
     38             try {
     39                 while (stream.peek() != '\u0000') {
     40                     stream.forward();
     41                 }
     42                 fail("Invalid stream must not be accepted: " + inputs[i].getAbsolutePath()
     43                         + "; encoding=" + stream.getEncoding());
     44             } catch (ReaderException e) {
     45                 assertTrue(e.toString(),
     46                         e.toString().contains(" special characters are not allowed"));
     47             } catch (YAMLException e) {
     48                 assertTrue(e.toString(), e.toString().contains("MalformedInputException"));
     49             } finally {
     50                 input.close();
     51             }
     52         }
     53     }
     54 }
     55