2
* Copyright (C) 2009 by Eric Herman <eric@freesa.org>
3
* Use and distribution licensed under the
4
* GNU Lesser General Public License (LGPL) version 2.1.
5
* See the COPYING file in the parent directory for full text.
7
package org.gearman.util;
9
import static org.gearman.util.TestUtil.assertArraysEqual;
10
import static org.junit.Assert.assertEquals;
11
import static org.junit.Assert.assertNotNull;
12
import static org.junit.Assert.assertTrue;
14
import java.io.ByteArrayInputStream;
15
import java.io.ByteArrayOutputStream;
16
import java.io.EOFException;
17
import java.io.Flushable;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.io.OutputStream;
21
import java.net.ServerSocket;
22
import java.net.Socket;
23
import java.util.ArrayList;
24
import java.util.List;
26
import org.junit.Test;
28
public class IOUtilTest {
31
public void testReadFully() throws Exception {
32
byte[] source = new byte[] { 0, 1, 2 };
33
InputStream is = new ByteArrayInputStream(source);
34
byte[] buf = new byte[source.length];
35
IOUtil.readFully(is, buf);
37
assertArraysEqual(source, buf);
39
is = new ByteArrayInputStream(source);
40
EOFException expected = null;
42
buf = new byte[source.length + 1];
43
IOUtil.readFully(is, buf);
45
} catch (IORuntimeException e) {
46
expected = (EOFException) e.getCause();
48
assertNotNull(expected);
49
String find = ByteUtils.toHex(source);
50
String msg = expected.getMessage();
51
assertTrue(msg, msg.indexOf(find) >= 0);
55
public void testFlush() {
56
class MockFlushable implements Flushable {
57
public int flushCalls = 0;
58
public boolean boom = false;
60
public void flush() throws IOException {
63
throw new IOException("boom");
67
MockFlushable mf = new MockFlushable();
69
assertEquals(1, mf.flushCalls);
71
IOException expected = null;
75
} catch (IORuntimeException e) {
76
expected = e.getCause();
78
assertNotNull(expected);
79
String msg = expected.getMessage();
80
assertTrue(msg, msg.indexOf("boom") >= 0);
84
public void testRead() {
85
class MockInputStream extends InputStream {
90
public int read() throws IOException {
92
throw new IOException("bang");
102
MockInputStream mis = new MockInputStream();
104
byte[] buf = new byte[6];
105
int c = IOUtil.read(mis, buf);
107
assertArraysEqual(new byte[] { 1, 2, 3, 0, 0, 0 }, buf);
109
IOException expected = null;
112
IOUtil.read(mis, buf);
113
} catch (IORuntimeException e) {
114
expected = e.getCause();
116
assertNotNull(expected);
117
String msg = expected.getMessage();
118
assertTrue(msg, msg.indexOf("bang") >= 0);
122
public void testWrite() {
123
class MockOutputStream extends OutputStream {
124
boolean boom = false;
125
List<Integer> written = new ArrayList<Integer>();
127
public void write(int b) throws IOException {
129
throw new IOException("blam");
135
MockOutputStream mos = new MockOutputStream();
136
IOUtil.write(mos, new byte[] { 5, 7 });
137
assertEquals(2, mos.written.size());
138
assertEquals(5, mos.written.get(0).intValue());
139
assertEquals(7, mos.written.get(1).intValue());
141
IOException expected = null;
144
IOUtil.write(mos, new byte[] { 13 });
145
} catch (IORuntimeException e) {
146
expected = e.getCause();
148
assertNotNull(expected);
149
String msg = expected.getMessage();
150
assertTrue(msg, msg.indexOf("blam") >= 0);
154
public void testGetStreams() {
155
final InputStream in = new ByteArrayInputStream(new byte[1]);
156
final OutputStream out = new ByteArrayOutputStream();
157
class MockSocket extends Socket {
158
boolean boom = false;
160
public InputStream getInputStream() throws IOException {
162
throw new IOException();
167
public OutputStream getOutputStream() throws IOException {
169
throw new IOException();
175
MockSocket ms = new MockSocket();
176
assertEquals(in, IOUtil.getInputStream(ms));
177
assertEquals(out, IOUtil.getOutputStream(ms));
180
IOException expected = null;
182
IOUtil.getInputStream(ms);
183
} catch (IORuntimeException e) {
184
expected = e.getCause();
186
assertNotNull(expected);
190
IOUtil.getOutputStream(ms);
191
} catch (IORuntimeException e) {
192
expected = e.getCause();
194
assertNotNull(expected);
198
public void testNewSocket() throws Exception {
199
ServerSocket ss = new ServerSocket(0);
200
int port = ss.getLocalPort();
201
Socket s2 = IOUtil.newSocket("localhost", port);
202
assertEquals(port, s2.getPort());
205
IOException expected = null;
207
IOUtil.newSocket("bogus.example.org", 8000);
208
} catch (IORuntimeException e) {
209
expected = e.getCause();
211
String msg = "Does DNS redirects to someplace for failed DNS queries?";
212
assertNotNull(msg, expected);