~elambert/gearmanij/gearman_java_library

« back to all changes in this revision

Viewing changes to test/org/gearman/util/IOUtilTest.java

  • Committer: Eric Lambert
  • Date: 2009-07-10 03:56:00 UTC
  • Revision ID: eric.d.lambert@gmail.com-20090710035600-fy5ghxevm51b5lvm
started refactoring unused classes and tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
 */
7
 
package org.gearman.util;
8
 
 
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;
13
 
 
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;
25
 
 
26
 
import org.junit.Test;
27
 
 
28
 
public class IOUtilTest {
29
 
 
30
 
    @Test
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);
36
 
        is.close();
37
 
        assertArraysEqual(source, buf);
38
 
 
39
 
        is = new ByteArrayInputStream(source);
40
 
        EOFException expected = null;
41
 
        try {
42
 
            buf = new byte[source.length + 1];
43
 
            IOUtil.readFully(is, buf);
44
 
            is.close();
45
 
        } catch (IORuntimeException e) {
46
 
            expected = (EOFException) e.getCause();
47
 
        }
48
 
        assertNotNull(expected);
49
 
        String find = ByteUtils.toHex(source);
50
 
        String msg = expected.getMessage();
51
 
        assertTrue(msg, msg.indexOf(find) >= 0);
52
 
    }
53
 
 
54
 
    @Test
55
 
    public void testFlush() {
56
 
        class MockFlushable implements Flushable {
57
 
            public int flushCalls = 0;
58
 
            public boolean boom = false;
59
 
 
60
 
            public void flush() throws IOException {
61
 
                flushCalls++;
62
 
                if (boom) {
63
 
                    throw new IOException("boom");
64
 
                }
65
 
            }
66
 
        }
67
 
        MockFlushable mf = new MockFlushable();
68
 
        IOUtil.flush(mf);
69
 
        assertEquals(1, mf.flushCalls);
70
 
 
71
 
        IOException expected = null;
72
 
        try {
73
 
            mf.boom = true;
74
 
            IOUtil.flush(mf);
75
 
        } catch (IORuntimeException e) {
76
 
            expected = e.getCause();
77
 
        }
78
 
        assertNotNull(expected);
79
 
        String msg = expected.getMessage();
80
 
        assertTrue(msg, msg.indexOf("boom") >= 0);
81
 
    }
82
 
 
83
 
    @Test
84
 
    public void testRead() {
85
 
        class MockInputStream extends InputStream {
86
 
            int chars = 0;
87
 
            int reads = 0;
88
 
            boolean boom = false;
89
 
 
90
 
            public int read() throws IOException {
91
 
                if (boom) {
92
 
                    throw new IOException("bang");
93
 
                }
94
 
                if (chars > reads) {
95
 
                    reads++;
96
 
                    return reads;
97
 
                } else {
98
 
                    return -1;
99
 
                }
100
 
            }
101
 
        }
102
 
        MockInputStream mis = new MockInputStream();
103
 
        mis.chars = 3;
104
 
        byte[] buf = new byte[6];
105
 
        int c = IOUtil.read(mis, buf);
106
 
        assertEquals(3, c);
107
 
        assertArraysEqual(new byte[] { 1, 2, 3, 0, 0, 0 }, buf);
108
 
 
109
 
        IOException expected = null;
110
 
        try {
111
 
            mis.boom = true;
112
 
            IOUtil.read(mis, buf);
113
 
        } catch (IORuntimeException e) {
114
 
            expected = e.getCause();
115
 
        }
116
 
        assertNotNull(expected);
117
 
        String msg = expected.getMessage();
118
 
        assertTrue(msg, msg.indexOf("bang") >= 0);
119
 
    }
120
 
 
121
 
    @Test
122
 
    public void testWrite() {
123
 
        class MockOutputStream extends OutputStream {
124
 
            boolean boom = false;
125
 
            List<Integer> written = new ArrayList<Integer>();
126
 
 
127
 
            public void write(int b) throws IOException {
128
 
                if (boom) {
129
 
                    throw new IOException("blam");
130
 
                }
131
 
                written.add(b);
132
 
            }
133
 
        }
134
 
 
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());
140
 
 
141
 
        IOException expected = null;
142
 
        try {
143
 
            mos.boom = true;
144
 
            IOUtil.write(mos, new byte[] { 13 });
145
 
        } catch (IORuntimeException e) {
146
 
            expected = e.getCause();
147
 
        }
148
 
        assertNotNull(expected);
149
 
        String msg = expected.getMessage();
150
 
        assertTrue(msg, msg.indexOf("blam") >= 0);
151
 
    }
152
 
 
153
 
    @Test
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;
159
 
 
160
 
            public InputStream getInputStream() throws IOException {
161
 
                if (boom) {
162
 
                    throw new IOException();
163
 
                }
164
 
                return in;
165
 
            }
166
 
 
167
 
            public OutputStream getOutputStream() throws IOException {
168
 
                if (boom) {
169
 
                    throw new IOException();
170
 
                }
171
 
                return out;
172
 
            }
173
 
        }
174
 
 
175
 
        MockSocket ms = new MockSocket();
176
 
        assertEquals(in, IOUtil.getInputStream(ms));
177
 
        assertEquals(out, IOUtil.getOutputStream(ms));
178
 
 
179
 
        ms.boom = true;
180
 
        IOException expected = null;
181
 
        try {
182
 
            IOUtil.getInputStream(ms);
183
 
        } catch (IORuntimeException e) {
184
 
            expected = e.getCause();
185
 
        }
186
 
        assertNotNull(expected);
187
 
 
188
 
        expected = null;
189
 
        try {
190
 
            IOUtil.getOutputStream(ms);
191
 
        } catch (IORuntimeException e) {
192
 
            expected = e.getCause();
193
 
        }
194
 
        assertNotNull(expected);
195
 
    }
196
 
 
197
 
    @Test
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());
203
 
        ss.close();
204
 
 
205
 
        IOException expected = null;
206
 
        try {
207
 
            IOUtil.newSocket("bogus.example.org", 8000);
208
 
        } catch (IORuntimeException e) {
209
 
            expected = e.getCause();
210
 
        }
211
 
        String msg = "Does DNS redirects to someplace for failed DNS queries?";
212
 
        assertNotNull(msg, expected);
213
 
    }
214
 
 
215
 
}