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: package org.apache.harmony.luni.tests.java.io;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.DataInput;
022: import java.io.DataInputStream;
023: import java.io.DataOutputStream;
024: import java.io.EOFException;
025: import java.io.IOException;
026:
027: public class DataInputStreamTest extends junit.framework.TestCase {
028:
029: private DataOutputStream os;
030:
031: private DataInputStream dis;
032:
033: private ByteArrayOutputStream bos;
034:
035: String unihw = "\u0048\u0065\u006C\u006C\u006F\u0020\u0057\u006F\u0072\u006C\u0064";
036:
037: public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_DataInputStream\n";
038:
039: /**
040: * @tests java.io.DataInputStream#DataInputStream(java.io.InputStream)
041: */
042: public void test_ConstructorLjava_io_InputStream()
043: throws IOException {
044: try {
045: os.writeChar('t');
046: os.close();
047: openDataInputStream();
048: } finally {
049: dis.close();
050: }
051: }
052:
053: /**
054: * @tests java.io.DataInputStream#read(byte[])
055: */
056: public void test_read$B() throws IOException {
057: os.write(fileString.getBytes());
058: os.close();
059: openDataInputStream();
060: byte rbytes[] = new byte[fileString.length()];
061: dis.read(rbytes);
062: assertTrue("Incorrect data read", new String(rbytes, 0,
063: fileString.length()).equals(fileString));
064: }
065:
066: /**
067: * @tests java.io.DataInputStream#read(byte[], int, int)
068: */
069: public void test_read$BII() throws IOException {
070: os.write(fileString.getBytes());
071: os.close();
072: openDataInputStream();
073: byte rbytes[] = new byte[fileString.length()];
074: dis.read(rbytes, 0, rbytes.length);
075: assertTrue("Incorrect data read", new String(rbytes, 0,
076: fileString.length()).equals(fileString));
077: }
078:
079: /**
080: * @tests java.io.DataInputStream#readBoolean()
081: */
082: public void test_readBoolean() throws IOException {
083: os.writeBoolean(true);
084: os.close();
085: openDataInputStream();
086: assertTrue("Incorrect boolean written", dis.readBoolean());
087: }
088:
089: /**
090: * @tests java.io.DataInputStream#readByte()
091: */
092: public void test_readByte() throws IOException {
093: os.writeByte((byte) 127);
094: os.close();
095: openDataInputStream();
096: assertTrue("Incorrect byte read", dis.readByte() == (byte) 127);
097: }
098:
099: /**
100: * @tests java.io.DataInputStream#readChar()
101: */
102: public void test_readChar() throws IOException {
103: os.writeChar('t');
104: os.close();
105: openDataInputStream();
106: assertEquals("Incorrect char read", 't', dis.readChar());
107: }
108:
109: /**
110: * @tests java.io.DataInputStream#readDouble()
111: */
112: public void test_readDouble() throws IOException {
113: os.writeDouble(2345.76834720202);
114: os.close();
115: openDataInputStream();
116: assertEquals("Incorrect double read", 2345.76834720202, dis
117: .readDouble());
118: }
119:
120: /**
121: * @tests java.io.DataInputStream#readFloat()
122: */
123: public void test_readFloat() throws IOException {
124: os.writeFloat(29.08764f);
125: os.close();
126: openDataInputStream();
127: assertTrue("Incorrect float read", dis.readFloat() == 29.08764f);
128: }
129:
130: /**
131: * @tests java.io.DataInputStream#readFully(byte[])
132: */
133: public void test_readFully$B() throws IOException {
134: os.write(fileString.getBytes());
135: os.close();
136: openDataInputStream();
137: byte rbytes[] = new byte[fileString.length()];
138: dis.readFully(rbytes);
139: assertTrue("Incorrect data read", new String(rbytes, 0,
140: fileString.length()).equals(fileString));
141: }
142:
143: /**
144: * @tests java.io.DataInputStream#readFully(byte[], int, int)
145: */
146: public void test_readFully$BII() throws IOException {
147: os.write(fileString.getBytes());
148: os.close();
149: openDataInputStream();
150: byte rbytes[] = new byte[fileString.length()];
151: dis.readFully(rbytes, 0, fileString.length());
152: assertTrue("Incorrect data read", new String(rbytes, 0,
153: fileString.length()).equals(fileString));
154: }
155:
156: /**
157: * @tests java.io.DataInputStream#readFully(byte[], int, int)
158: */
159: public void test_readFully$BII_Exception() throws IOException {
160: DataInputStream is = new DataInputStream(
161: new ByteArrayInputStream(new byte[fileString.length()]));
162:
163: byte[] byteArray = new byte[fileString.length()];
164:
165: try {
166: is.readFully(byteArray, -1, -1);
167: fail("should throw IndexOutOfBoundsException");
168: } catch (IndexOutOfBoundsException e) {
169: // expected
170: }
171:
172: try {
173: is.readFully(byteArray, 0, -1);
174: fail("should throw IndexOutOfBoundsException");
175: } catch (IndexOutOfBoundsException e) {
176: // expected
177: }
178:
179: try {
180: is.readFully(byteArray, 1, -1);
181: fail("should throw IndexOutOfBoundsException");
182: } catch (IndexOutOfBoundsException e) {
183: // expected
184: }
185:
186: is.readFully(byteArray, -1, 0);
187: is.readFully(byteArray, 0, 0);
188: is.readFully(byteArray, 1, 0);
189:
190: try {
191: is.readFully(byteArray, -1, 1);
192: fail("should throw IndexOutOfBoundsException");
193: } catch (IndexOutOfBoundsException e) {
194: // expected
195: }
196:
197: is.readFully(byteArray, 0, 1);
198: is.readFully(byteArray, 1, 1);
199: try {
200: is.readFully(byteArray, 0, Integer.MAX_VALUE);
201: fail("should throw IndexOutOfBoundsException");
202: } catch (IndexOutOfBoundsException e) {
203: // expected
204: }
205: }
206:
207: /**
208: * @tests java.io.DataInputStream#readFully(byte[], int, int)
209: */
210: public void test_readFully$BII_NullArray() throws IOException {
211: DataInputStream is = new DataInputStream(
212: new ByteArrayInputStream(new byte[fileString.length()]));
213:
214: byte[] nullByteArray = null;
215:
216: try {
217: is.readFully(nullByteArray, -1, -1);
218: fail("should throw IndexOutOfBoundsException");
219: } catch (IndexOutOfBoundsException e) {
220: // expected
221: }
222:
223: try {
224: is.readFully(nullByteArray, 0, -1);
225: fail("should throw IndexOutOfBoundsException");
226: } catch (IndexOutOfBoundsException e) {
227: // expected
228: }
229:
230: try {
231: is.readFully(nullByteArray, 1, -1);
232: fail("should throw IndexOutOfBoundsException");
233: } catch (IndexOutOfBoundsException e) {
234: // expected
235: }
236:
237: is.readFully(nullByteArray, -1, 0);
238: is.readFully(nullByteArray, 0, 0);
239: is.readFully(nullByteArray, 1, 0);
240:
241: try {
242: is.readFully(nullByteArray, -1, 1);
243: fail("should throw NullPointerException");
244: } catch (NullPointerException e) {
245: // expected
246: }
247:
248: try {
249: is.readFully(nullByteArray, 0, 1);
250: fail("should throw NullPointerException");
251: } catch (NullPointerException e) {
252: // expected
253: }
254:
255: try {
256: is.readFully(nullByteArray, 1, 1);
257: fail("should throw NullPointerException");
258: } catch (NullPointerException e) {
259: // expected
260: }
261:
262: try {
263: is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
264: fail("should throw NullPointerException");
265: } catch (NullPointerException e) {
266: // expected
267: }
268: }
269:
270: /**
271: * @tests java.io.DataInputStream#readFully(byte[], int, int)
272: */
273: public void test_readFully$BII_NullStream() throws IOException {
274: DataInputStream is = new DataInputStream(null);
275: byte[] byteArray = new byte[fileString.length()];
276:
277: try {
278: is.readFully(byteArray, -1, -1);
279: fail("should throw IndexOutOfBoundsException");
280: } catch (IndexOutOfBoundsException e) {
281: // expected
282: }
283:
284: try {
285: is.readFully(byteArray, 0, -1);
286: fail("should throw IndexOutOfBoundsException");
287: } catch (IndexOutOfBoundsException e) {
288: // expected
289: }
290:
291: try {
292: is.readFully(byteArray, 1, -1);
293: fail("should throw IndexOutOfBoundsException");
294: } catch (IndexOutOfBoundsException e) {
295: // expected
296: }
297:
298: is.readFully(byteArray, -1, 0);
299: is.readFully(byteArray, 0, 0);
300: is.readFully(byteArray, 1, 0);
301:
302: try {
303: is.readFully(byteArray, -1, 1);
304: fail("should throw NullPointerException");
305: } catch (NullPointerException e) {
306: // expected
307: }
308:
309: try {
310: is.readFully(byteArray, 0, 1);
311: fail("should throw NullPointerException");
312: } catch (NullPointerException e) {
313: // expected
314: }
315:
316: try {
317: is.readFully(byteArray, 1, 1);
318: fail("should throw NullPointerException");
319: } catch (NullPointerException e) {
320: // expected
321: }
322:
323: try {
324: is.readFully(byteArray, 0, Integer.MAX_VALUE);
325: fail("should throw NullPointerException");
326: } catch (NullPointerException e) {
327: // expected
328: }
329: }
330:
331: /**
332: * @tests java.io.DataInputStream#readFully(byte[], int, int)
333: */
334: public void test_readFully$BII_NullStream_NullArray()
335: throws IOException {
336: DataInputStream is = new DataInputStream(null);
337: byte[] nullByteArray = null;
338:
339: try {
340: is.readFully(nullByteArray, -1, -1);
341: fail("should throw IndexOutOfBoundsException");
342: } catch (IndexOutOfBoundsException e) {
343: // expected
344: }
345:
346: try {
347: is.readFully(nullByteArray, 0, -1);
348: fail("should throw IndexOutOfBoundsException");
349: } catch (IndexOutOfBoundsException e) {
350: // expected
351: }
352:
353: try {
354: is.readFully(nullByteArray, 1, -1);
355: fail("should throw IndexOutOfBoundsException");
356: } catch (IndexOutOfBoundsException e) {
357: // expected
358: }
359:
360: is.readFully(nullByteArray, -1, 0);
361: is.readFully(nullByteArray, 0, 0);
362: is.readFully(nullByteArray, 1, 0);
363:
364: try {
365: is.readFully(nullByteArray, -1, 1);
366: fail("should throw NullPointerException");
367: } catch (NullPointerException e) {
368: // expected
369: }
370:
371: try {
372: is.readFully(nullByteArray, 0, 1);
373: fail("should throw NullPointerException");
374: } catch (NullPointerException e) {
375: // expected
376: }
377:
378: try {
379: is.readFully(nullByteArray, 1, 1);
380: fail("should throw NullPointerException");
381: } catch (NullPointerException e) {
382: // expected
383: }
384:
385: try {
386: is.readFully(nullByteArray, 0, Integer.MAX_VALUE);
387: fail("should throw NullPointerException");
388: } catch (NullPointerException e) {
389: // expected
390: }
391: }
392:
393: /**
394: * @tests java.io.DataInputStream#readInt()
395: */
396: public void test_readInt() throws IOException {
397: os.writeInt(768347202);
398: os.close();
399: openDataInputStream();
400: assertEquals("Incorrect int read", 768347202, dis.readInt());
401: }
402:
403: /**
404: * @tests java.io.DataInputStream#readLine()
405: */
406: @SuppressWarnings("deprecation")
407: public void test_readLine() throws IOException {
408: os.writeBytes("Hello");
409: os.close();
410: openDataInputStream();
411: String line = dis.readLine();
412: assertTrue("Incorrect line read: " + line, line.equals("Hello"));
413: }
414:
415: /**
416: * @tests java.io.DataInputStream#readLong()
417: */
418: public void test_readLong() throws IOException {
419: os.writeLong(9875645283333L);
420: os.close();
421: openDataInputStream();
422: assertEquals("Incorrect long read", 9875645283333L, dis
423: .readLong());
424: }
425:
426: /**
427: * @tests java.io.DataInputStream#readShort()
428: */
429: public void test_readShort() throws IOException {
430: os.writeShort(9875);
431: os.close();
432: openDataInputStream();
433: assertTrue("Incorrect short read",
434: dis.readShort() == (short) 9875);
435: }
436:
437: /**
438: * @tests java.io.DataInputStream#readUnsignedByte()
439: */
440: public void test_readUnsignedByte() throws IOException {
441: os.writeByte((byte) -127);
442: os.close();
443: openDataInputStream();
444: assertEquals("Incorrect byte read", 129, dis.readUnsignedByte());
445: }
446:
447: /**
448: * @tests java.io.DataInputStream#readUnsignedShort()
449: */
450: public void test_readUnsignedShort() throws IOException {
451: os.writeShort(9875);
452: os.close();
453: openDataInputStream();
454: assertEquals("Incorrect short read", 9875, dis
455: .readUnsignedShort());
456: }
457:
458: /**
459: * @tests java.io.DataInputStream#readUTF()
460: */
461: public void test_readUTF() throws IOException {
462: os.writeUTF(unihw);
463: os.close();
464: openDataInputStream();
465: assertTrue("Failed to write string in UTF format", dis
466: .available() == unihw.length() + 2);
467: assertTrue("Incorrect string read", dis.readUTF().equals(unihw));
468: }
469:
470: static class TestDataInputStream implements DataInput {
471: public boolean readBoolean() throws IOException {
472: return false;
473: }
474:
475: public byte readByte() throws IOException {
476: return (byte) 0;
477: }
478:
479: public char readChar() throws IOException {
480: return (char) 0;
481: }
482:
483: public double readDouble() throws IOException {
484: return 0.0;
485: }
486:
487: public float readFloat() throws IOException {
488: return (float) 0.0;
489: }
490:
491: public void readFully(byte[] buffer) throws IOException {
492: }
493:
494: public void readFully(byte[] buffer, int offset, int count)
495: throws IOException {
496: }
497:
498: public int readInt() throws IOException {
499: return 0;
500: }
501:
502: public String readLine() throws IOException {
503: return null;
504: }
505:
506: public long readLong() throws IOException {
507: return (long) 0;
508: }
509:
510: public short readShort() throws IOException {
511: return (short) 0;
512: }
513:
514: public int readUnsignedByte() throws IOException {
515: return 0;
516: }
517:
518: public int readUnsignedShort() throws IOException {
519: return 0;
520: }
521:
522: public String readUTF() throws IOException {
523: return DataInputStream.readUTF(this );
524: }
525:
526: public int skipBytes(int count) throws IOException {
527: return 0;
528: }
529: }
530:
531: /**
532: * @tests java.io.DataInputStream#readUTF(java.io.DataInput)
533: */
534: public void test_readUTFLjava_io_DataInput() throws IOException {
535: os.writeUTF(unihw);
536: os.close();
537: openDataInputStream();
538: assertTrue("Failed to write string in UTF format", dis
539: .available() == unihw.length() + 2);
540: assertTrue("Incorrect string read", DataInputStream
541: .readUTF(dis).equals(unihw));
542:
543: // Regression test for HARMONY-5336
544: new TestDataInputStream().readUTF();
545: }
546:
547: /**
548: * @tests java.io.DataInputStream#skipBytes(int)
549: */
550: public void test_skipBytesI() throws IOException {
551: byte fileBytes[] = fileString.getBytes();
552: os.write(fileBytes);
553: os.close();
554: openDataInputStream();
555: dis.skipBytes(100);
556: byte rbytes[] = new byte[fileString.length()];
557: dis.read(rbytes, 0, 50);
558: dis.close();
559: assertTrue("Incorrect data read", new String(rbytes, 0, 50)
560: .equals(fileString.substring(100, 150)));
561:
562: int skipped = 0;
563: openDataInputStream();
564: try {
565: skipped = dis.skipBytes(50000);
566: } catch (EOFException e) {
567: }
568: assertTrue("Skipped should report " + fileString.length()
569: + " not " + skipped, skipped == fileString.length());
570: }
571:
572: private void openDataInputStream() throws IOException {
573: dis = new DataInputStream(new ByteArrayInputStream(bos
574: .toByteArray()));
575: }
576:
577: /**
578: * Sets up the fixture, for example, open a network connection. This method
579: * is called before a test is executed.
580: */
581: protected void setUp() {
582: bos = new ByteArrayOutputStream();
583: os = new DataOutputStream(bos);
584: }
585:
586: /**
587: * Tears down the fixture, for example, close a network connection. This
588: * method is called after a test is executed.
589: */
590: protected void tearDown() {
591: try {
592: os.close();
593: } catch (Exception e) {
594: }
595: try {
596: dis.close();
597: } catch (Exception e) {
598: }
599: }
600: }
|