001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.DataInputStream;
023: import java.io.DataOutputStream;
024: import java.io.IOException;
025:
026: public class DataOutputStreamTest extends junit.framework.TestCase {
027:
028: private DataOutputStream os;
029:
030: private DataInputStream dis;
031:
032: private ByteArrayOutputStream bos;
033:
034: String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
035:
036: 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";
037:
038: /**
039: * @tests java.io.DataOutputStream#DataOutputStream(java.io.OutputStream)
040: */
041: public void test_ConstructorLjava_io_OutputStream() {
042: assertTrue("Used in all tests", true);
043: }
044:
045: /**
046: * @tests java.io.DataOutputStream#flush()
047: */
048: public void test_flush() throws IOException {
049: os.writeInt(9087589);
050: os.flush();
051: openDataInputStream();
052: int c = dis.readInt();
053: dis.close();
054: assertEquals("Failed to flush correctly", 9087589, c);
055: }
056:
057: /**
058: * @tests java.io.DataOutputStream#size()
059: */
060: public void test_size() throws IOException {
061: os.write(fileString.getBytes(), 0, 150);
062: os.close();
063: openDataInputStream();
064: byte[] rbuf = new byte[150];
065: dis.read(rbuf, 0, 150);
066: dis.close();
067: assertEquals("Incorrect size returned", 150, os.size());
068: }
069:
070: /**
071: * @tests java.io.DataOutputStream#write(byte[], int, int)
072: */
073: public void test_write$BII() throws IOException {
074: os.write(fileString.getBytes(), 0, 150);
075: os.close();
076: openDataInputStream();
077: byte[] rbuf = new byte[150];
078: dis.read(rbuf, 0, 150);
079: dis.close();
080: assertTrue("Incorrect bytes written", new String(rbuf, 0, 150)
081: .equals(fileString.substring(0, 150)));
082: }
083:
084: /**
085: * @tests java.io.DataOutputStream#write(int)
086: */
087: public void test_writeI() throws IOException {
088: os.write((int) 't');
089: os.close();
090: openDataInputStream();
091: int c = dis.read();
092: dis.close();
093: assertTrue("Incorrect int written", (int) 't' == c);
094: }
095:
096: /**
097: * @tests java.io.DataOutputStream#writeBoolean(boolean)
098: */
099: 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,
131: fileString.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",
161: new String(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", dis
232: .available() == unihw.length() + 2);
233: assertTrue("Incorrect string returned", dis.readUTF().equals(
234: unihw));
235: }
236:
237: private void openDataInputStream() throws IOException {
238: dis = new DataInputStream(new ByteArrayInputStream(bos
239: .toByteArray()));
240: }
241:
242: /**
243: * Sets up the fixture, for example, open a network connection. This method
244: * is called before a test is executed.
245: */
246: protected void setUp() {
247: bos = new ByteArrayOutputStream();
248: os = new DataOutputStream(bos);
249: }
250:
251: /**
252: * Tears down the fixture, for example, close a network connection. This
253: * method is called after a test is executed.
254: */
255: protected void tearDown() {
256: try {
257: if (os != null)
258: os.close();
259: if (dis != null)
260: dis.close();
261: } catch (IOException e) {
262: }
263: }
264: }
|