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