1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }