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