Home | History | Annotate | Download | only in snakeyaml
      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.yaml.snakeyaml;
     17 
     18 import java.io.IOException;
     19 import java.io.InputStream;
     20 import java.io.Writer;
     21 
     22 import junit.framework.TestCase;
     23 
     24 import org.yaml.snakeyaml.error.YAMLException;
     25 
     26 public class InputOutputExceptionTest extends TestCase {
     27     public void testIOExceptionOnLoad() {
     28         try {
     29             new Yaml().load(new BrokenInputStream());
     30             fail("Input must be broken.");
     31         } catch (YAMLException e) {
     32             assertTrue(e.getCause() instanceof IOException);
     33             assertEquals("java.io.IOException: Broken 2", e.getMessage());
     34         }
     35     }
     36 
     37     public void testIOExceptionOnDump() {
     38         try {
     39             new Yaml().dump("something", new BrokenWriter());
     40             fail("Output must be broken.");
     41         } catch (YAMLException e) {
     42             assertTrue(e.getCause() instanceof IOException);
     43             assertEquals("java.io.IOException: Broken 12", e.getMessage());
     44         }
     45     }
     46 
     47     private static class BrokenInputStream extends InputStream {
     48         @Override
     49         public int read() throws IOException {
     50             throw new IOException("Broken 1");
     51         }
     52 
     53         @Override
     54         public int read(byte[] bytes, int i, int i1) throws IOException {
     55             throw new IOException("Broken 2");
     56         }
     57 
     58         @Override
     59         public void close() throws IOException {
     60             throw new IOException("Broken 3");
     61         }
     62     }
     63 
     64     private static class BrokenWriter extends Writer {
     65         @Override
     66         public void close() throws IOException {
     67             throw new IOException("Broken 10");
     68         }
     69 
     70         @Override
     71         public void flush() throws IOException {
     72             throw new IOException("Broken 11");
     73         }
     74 
     75         @Override
     76         public void write(char[] cbuf, int off, int len) throws IOException {
     77             throw new IOException("Broken 12");
     78         }
     79     }
     80 }
     81