DataOutputStreamTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.DataInputStream;
23import java.io.DataOutputStream;
24import java.io.IOException;
25
26public class DataOutputStreamTest extends junit.framework.TestCase {
27
28    private DataOutputStream os;
29
30    private DataInputStream dis;
31
32    private ByteArrayOutputStream bos;
33
34    String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
35
36    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
37
38    /**
39     * @tests java.io.DataOutputStream#DataOutputStream(java.io.OutputStream)
40     */
41    public void test_ConstructorLjava_io_OutputStream() {
42        assertTrue("Used in all tests", true);
43    }
44
45    /**
46     * @tests java.io.DataOutputStream#flush()
47     */
48    public void test_flush() throws IOException {
49        os.writeInt(9087589);
50        os.flush();
51        openDataInputStream();
52        int c = dis.readInt();
53        dis.close();
54        assertEquals("Failed to flush correctly", 9087589, c);
55    }
56
57    /**
58     * @tests java.io.DataOutputStream#size()
59     */
60    public void test_size() throws IOException {
61        os.write(fileString.getBytes(), 0, 150);
62        os.close();
63        openDataInputStream();
64        byte[] rbuf = new byte[150];
65        dis.read(rbuf, 0, 150);
66        dis.close();
67        assertEquals("Incorrect size returned", 150, os.size());
68    }
69
70    /**
71     * @tests java.io.DataOutputStream#write(byte[], int, int)
72     */
73    public void test_write$BII() throws IOException {
74        os.write(fileString.getBytes(), 0, 150);
75        os.close();
76        openDataInputStream();
77        byte[] rbuf = new byte[150];
78        dis.read(rbuf, 0, 150);
79        dis.close();
80        assertTrue("Incorrect bytes written", new String(rbuf, 0, 150)
81                .equals(fileString.substring(0, 150)));
82    }
83
84    /**
85     * @tests java.io.DataOutputStream#write(int)
86     */
87    public void test_writeI() throws IOException {
88        os.write((int) 't');
89        os.close();
90        openDataInputStream();
91        int c = dis.read();
92        dis.close();
93        assertTrue("Incorrect int written", (int) 't' == c);
94    }
95
96    /**
97     * @tests java.io.DataOutputStream#writeBoolean(boolean)
98     */
99    public void test_writeBooleanZ() throws IOException {
100        os.writeBoolean(true);
101        os.close();
102        openDataInputStream();
103        boolean c = dis.readBoolean();
104        dis.close();
105        assertTrue("Incorrect boolean written", c);
106    }
107
108    /**
109     * @tests java.io.DataOutputStream#writeByte(int)
110     */
111    public void test_writeByteI() throws IOException {
112        os.writeByte((byte) 127);
113        os.close();
114        openDataInputStream();
115        byte c = dis.readByte();
116        dis.close();
117        assertTrue("Incorrect byte written", c == (byte) 127);
118    }
119
120    /**
121     * @tests java.io.DataOutputStream#writeBytes(java.lang.String)
122     */
123    public void test_writeBytesLjava_lang_String() throws IOException {
124        os.write(fileString.getBytes());
125        os.close();
126        openDataInputStream();
127        byte[] rbuf = new byte[4000];
128        dis.read(rbuf, 0, fileString.length());
129        dis.close();
130        assertTrue("Incorrect bytes written", new String(rbuf, 0, fileString
131                .length()).equals(fileString));
132
133        // regression test for HARMONY-1101
134        new DataOutputStream(null).writeBytes("");
135    }
136
137    /**
138     * @tests java.io.DataOutputStream#writeChar(int)
139     */
140    public void test_writeCharI() throws IOException {
141        os.writeChar('T');
142        os.close();
143        openDataInputStream();
144        char c = dis.readChar();
145        dis.close();
146        assertEquals("Incorrect char written", 'T', c);
147    }
148
149    /**
150     * @tests java.io.DataOutputStream#writeChars(java.lang.String)
151     */
152    public void test_writeCharsLjava_lang_String() throws IOException {
153        os.writeChars("Test String");
154        os.close();
155        openDataInputStream();
156        char[] chars = new char[50];
157        int i, a = dis.available() / 2;
158        for (i = 0; i < a; i++)
159            chars[i] = dis.readChar();
160        assertEquals("Incorrect chars written", "Test String", new String(
161                chars, 0, i));
162    }
163
164    /**
165     * @tests java.io.DataOutputStream#writeDouble(double)
166     */
167    public void test_writeDoubleD() throws IOException {
168        os.writeDouble(908755555456.98);
169        os.close();
170        openDataInputStream();
171        double c = dis.readDouble();
172        dis.close();
173        assertEquals("Incorrect double written", 908755555456.98, c);
174    }
175
176    /**
177     * @tests java.io.DataOutputStream#writeFloat(float)
178     */
179    public void test_writeFloatF() throws IOException {
180        os.writeFloat(9087.456f);
181        os.close();
182        openDataInputStream();
183        float c = dis.readFloat();
184        dis.close();
185        assertTrue("Incorrect float written", c == 9087.456f);
186    }
187
188    /**
189     * @tests java.io.DataOutputStream#writeInt(int)
190     */
191    public void test_writeIntI() throws IOException {
192        os.writeInt(9087589);
193        os.close();
194        openDataInputStream();
195        int c = dis.readInt();
196        dis.close();
197        assertEquals("Incorrect int written", 9087589, c);
198    }
199
200    /**
201     * @tests java.io.DataOutputStream#writeLong(long)
202     */
203    public void test_writeLongJ() throws IOException {
204        os.writeLong(908755555456L);
205        os.close();
206        openDataInputStream();
207        long c = dis.readLong();
208        dis.close();
209        assertEquals("Incorrect long written", 908755555456L, c);
210    }
211
212    /**
213     * @tests java.io.DataOutputStream#writeShort(int)
214     */
215    public void test_writeShortI() throws IOException {
216        os.writeShort((short) 9087);
217        os.close();
218        openDataInputStream();
219        short c = dis.readShort();
220        dis.close();
221        assertEquals("Incorrect short written", 9087, c);
222    }
223
224    /**
225     * @tests java.io.DataOutputStream#writeUTF(java.lang.String)
226     */
227    public void test_writeUTFLjava_lang_String() throws IOException {
228        os.writeUTF(unihw);
229        os.close();
230        openDataInputStream();
231        assertTrue("Failed to write string in UTF format",
232                dis.available() == unihw.length() + 2);
233        assertTrue("Incorrect string returned", dis.readUTF().equals(unihw));
234    }
235
236    private void openDataInputStream() throws IOException {
237        dis = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
238    }
239
240    /**
241     * Sets up the fixture, for example, open a network connection. This method
242     * is called before a test is executed.
243     */
244    protected void setUp() {
245        bos = new ByteArrayOutputStream();
246        os = new DataOutputStream(bos);
247    }
248
249    /**
250     * Tears down the fixture, for example, close a network connection. This
251     * method is called after a test is executed.
252     */
253    protected void tearDown() {
254        try {
255            if (os != null)
256                os.close();
257            if (dis != null)
258                dis.close();
259        } catch (IOException e) {
260        }
261    }
262}
263