1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.common;
23
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InterruptedIOException;
30 import java.io.OutputStream;
31 import java.io.Reader;
32 import java.io.Writer;
33 import java.net.Socket;
34
35 import org.junit.Test;
36
37
38
39
40
41
42
43 public class TestCloser {
44 private final IOException[] m_ioexception = new IOException[1];
45
46 @Test public void testCloseReader() throws Exception {
47 Closer.close((Reader)null);
48
49 final Reader reader = new Reader() {
50 public void close() throws IOException {
51 TestCloser.this.close();
52 }
53
54 public int read(char[] cbuf, int off, int len) {
55 return 0;
56 }
57 };
58
59 Closer.close(reader);
60
61 m_ioexception[0] = new IOException();
62 Closer.close(reader);
63
64 m_ioexception[0] = new InterruptedIOException();
65
66 try {
67 Closer.close(reader);
68 fail("Expected UncheckedInterruptedException");
69 }
70 catch (UncheckedInterruptedException e) {
71 assertSame(m_ioexception[0], e.getCause());
72 }
73 }
74
75 @Test public void testCloseWriter() throws Exception {
76 Closer.close((Writer)null);
77
78 final Writer writer = new Writer() {
79 public void close() throws IOException {
80 TestCloser.this.close();
81 }
82
83 public void flush() throws IOException {
84 }
85
86 public void write(char[] cbuf, int off, int len) throws IOException {
87 }
88 };
89
90 Closer.close(writer);
91
92 m_ioexception[0] = new IOException();
93 Closer.close(writer);
94
95 m_ioexception[0] = new InterruptedIOException();
96
97 try {
98 Closer.close(writer);
99 fail("Expected UncheckedInterruptedException");
100 }
101 catch (UncheckedInterruptedException e) {
102 assertSame(m_ioexception[0], e.getCause());
103 }
104 }
105
106 @Test public void testCloseInputStream() throws Exception {
107 Closer.close((InputStream)null);
108
109 final InputStream in = new InputStream() {
110 public void close() throws IOException {
111 TestCloser.this.close();
112 }
113
114 public int read() throws IOException {
115 return 0;
116 }
117 };
118
119 Closer.close(in);
120
121 m_ioexception[0] = new IOException();
122 Closer.close(in);
123
124 m_ioexception[0] = new InterruptedIOException();
125
126 try {
127 Closer.close(in);
128 fail("Expected UncheckedInterruptedException");
129 }
130 catch (UncheckedInterruptedException e) {
131 assertSame(m_ioexception[0], e.getCause());
132 }
133 }
134
135 @Test public void testCloseOutputStream() throws Exception {
136 Closer.close((OutputStream)null);
137
138 final OutputStream outputStream = new OutputStream() {
139 public void close() throws IOException {
140 TestCloser.this.close();
141 }
142
143 public void flush() throws IOException {
144 }
145
146 public void write(int b) throws IOException {
147 }
148 };
149
150 Closer.close(outputStream);
151
152 m_ioexception[0] = new IOException();
153 Closer.close(outputStream);
154
155 m_ioexception[0] = new InterruptedIOException();
156
157 try {
158 Closer.close(outputStream);
159 fail("Expected UncheckedInterruptedException");
160 }
161 catch (UncheckedInterruptedException e) {
162 assertSame(m_ioexception[0], e.getCause());
163 }
164 }
165
166 @Test public void testCloser() throws Exception {
167 Closer.close((Socket)null);
168
169 final Socket socket = new Socket() {
170 public synchronized void close() throws IOException {
171 TestCloser.this.close();
172 }
173 };
174
175 Closer.close(socket);
176
177 m_ioexception[0] = new IOException();
178 Closer.close(socket);
179
180 m_ioexception[0] = new InterruptedIOException();
181
182 try {
183 Closer.close(socket);
184 fail("Expected UncheckedInterruptedException");
185 }
186 catch (UncheckedInterruptedException e) {
187 assertSame(m_ioexception[0], e.getCause());
188 }
189 }
190
191 private void close() throws IOException {
192 if (m_ioexception[0] != null) {
193 throw m_ioexception[0];
194 }
195 }
196
197 @Test(expected=UnsupportedOperationException.class)
198 public void coverConstructor() throws Exception {
199 new Closer();
200 }
201 }