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.BufferedOutputStream;
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.OutputStream;
025:
026: public class BufferedOutputStreamTest extends junit.framework.TestCase {
027:
028: private java.io.OutputStream os;
029:
030: java.io.ByteArrayOutputStream baos;
031:
032: java.io.ByteArrayInputStream bais;
033:
034: public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
035:
036: /**
037: * @tests java.io.BufferedOutputStream#BufferedOutputStream(java.io.OutputStream)
038: */
039: public void test_ConstructorLjava_io_OutputStream()
040: throws IOException {
041: baos = new java.io.ByteArrayOutputStream();
042: os = new java.io.BufferedOutputStream(baos);
043: os.write(fileString.getBytes(), 0, 500);
044: }
045:
046: /**
047: * @tests java.io.BufferedOutputStream#BufferedOutputStream(java.io.OutputStream,
048: * int)
049: */
050: public void test_ConstructorLjava_io_OutputStreamI()
051: throws IOException {
052: baos = new java.io.ByteArrayOutputStream();
053: os = new java.io.BufferedOutputStream(baos, 1024);
054: os.write(fileString.getBytes(), 0, 500);
055: }
056:
057: /**
058: * @tests java.io.BufferedOutputStream#flush()
059: */
060: public void test_flush() throws IOException {
061: baos = new ByteArrayOutputStream();
062: os = new java.io.BufferedOutputStream(baos, 600);
063: os.write(fileString.getBytes(), 0, 500);
064: os.flush();
065: assertEquals("Bytes not written after flush", 500,
066: ((ByteArrayOutputStream) baos).size());
067: }
068:
069: private static class MockOutputStream extends OutputStream {
070: byte[] written;
071: int count;
072:
073: public MockOutputStream(int size) {
074: written = new byte[size];
075: count = 0;
076: }
077:
078: public void write(int b) {
079: written[count++] = (byte) b;
080: }
081:
082: public String getWritten() {
083: return new String(written, 0, count);
084: }
085: }
086:
087: /**
088: * @tests java.io.BufferedOutputStream#write(byte[], int, int)
089: */
090: public void test_write$BII() throws IOException {
091: os = new BufferedOutputStream(
092: baos = new ByteArrayOutputStream(), 512);
093: os.write(fileString.getBytes(), 0, 500);
094: bais = new ByteArrayInputStream(baos.toByteArray());
095: assertEquals("Bytes written, not buffered", 0, bais.available());
096: os.flush();
097: bais = new ByteArrayInputStream(baos.toByteArray());
098: assertEquals("Bytes not written after flush", 500, bais
099: .available());
100: os.write(fileString.getBytes(), 500, 513);
101: bais = new ByteArrayInputStream(baos.toByteArray());
102: assertTrue("Bytes not written when buffer full", bais
103: .available() >= 1000);
104: byte[] wbytes = new byte[1013];
105: bais.read(wbytes, 0, 1013);
106: assertTrue("Incorrect bytes written", fileString.substring(0,
107: 1013).equals(new String(wbytes, 0, wbytes.length)));
108:
109: // regression test for HARMONY-4177
110: MockOutputStream mos = new MockOutputStream(5);
111: BufferedOutputStream bos = new BufferedOutputStream(mos, 3);
112: bos.write("a".getBytes());
113: bos.write("bcde".getBytes());
114: assertEquals("Large data should be written directly", "abcde",
115: mos.getWritten());
116: mos = new MockOutputStream(4);
117: bos = new BufferedOutputStream(mos, 3);
118: bos.write("ab".getBytes());
119: bos.write("cd".getBytes());
120: assertEquals("Should flush before write", "ab", mos
121: .getWritten());
122: }
123:
124: /**
125: * @tests java.io.BufferedOutputStream#write(byte[], int, int)
126: */
127: public void test_write_$BII_Exception() throws IOException {
128: OutputStream bos = new BufferedOutputStream(
129: new ByteArrayOutputStream());
130: byte[] nullByteArray = null;
131: byte[] byteArray = new byte[10];
132:
133: try {
134: bos.write(nullByteArray, -1, -1);
135: fail("should throw NullPointerException");
136: } catch (NullPointerException e) {
137: // expected
138: }
139:
140: try {
141: bos.write(nullByteArray, -1, 0);
142: fail("should throw NullPointerException");
143: } catch (NullPointerException e) {
144: // expected
145: }
146:
147: try {
148: bos.write(nullByteArray, -1, 1);
149: fail("should throw NullPointerException");
150: } catch (NullPointerException e) {
151: // expected
152: }
153:
154: try {
155: bos.write(nullByteArray, 0, -1);
156: fail("should throw NullPointerException");
157: } catch (NullPointerException e) {
158: // expected
159: }
160:
161: try {
162: bos.write(nullByteArray, 0, 0);
163: fail("should throw NullPointerException");
164: } catch (NullPointerException e) {
165: // expected
166: }
167:
168: try {
169: bos.write(nullByteArray, 0, 1);
170: fail("should throw NullPointerException");
171: } catch (NullPointerException e) {
172: // expected
173: }
174:
175: try {
176: bos.write(nullByteArray, 1, -1);
177: fail("should throw NullPointerException");
178: } catch (NullPointerException e) {
179: // expected
180: }
181:
182: try {
183: bos.write(nullByteArray, 1, 0);
184: fail("should throw NullPointerException");
185: } catch (NullPointerException e) {
186: // expected
187: }
188:
189: try {
190: bos.write(nullByteArray, 1, 1);
191: fail("should throw NullPointerException");
192: } catch (NullPointerException e) {
193: // expected
194: }
195:
196: try {
197: bos.write(byteArray, -1, -1);
198: fail("should throw ArrayIndexOutOfBoundsException");
199: } catch (ArrayIndexOutOfBoundsException e) {
200: // expected
201: }
202:
203: try {
204: bos.write(byteArray, -1, 0);
205: fail("should throw ArrayIndexOutOfBoundsException");
206: } catch (ArrayIndexOutOfBoundsException e) {
207: // expected
208: }
209:
210: try {
211: bos.write(byteArray, -1, 1);
212: fail("should throw ArrayIndexOutOfBoundsException");
213: } catch (ArrayIndexOutOfBoundsException e) {
214: // expected
215: }
216:
217: try {
218: bos.write(byteArray, 0, -1);
219: fail("should throw ArrayIndexOutOfBoundsException");
220: } catch (ArrayIndexOutOfBoundsException e) {
221: // expected
222: }
223:
224: bos.write(byteArray, 0, 0);
225: bos.write(byteArray, 0, 1);
226: bos.write(byteArray, 0, byteArray.length);
227:
228: try {
229: bos.write(byteArray, 1, -1);
230: fail("should throw ArrayIndexOutOfBoundsException");
231: } catch (ArrayIndexOutOfBoundsException e) {
232: // expected
233: }
234:
235: bos.write(byteArray, 1, 0);
236: bos.write(byteArray, 1, 1);
237:
238: bos.write(byteArray, byteArray.length, 0);
239:
240: try {
241: bos.write(byteArray, byteArray.length + 1, 0);
242: fail("should throw ArrayIndexOutOfBoundsException");
243: } catch (ArrayIndexOutOfBoundsException e) {
244: // expected
245: }
246:
247: try {
248: bos.write(byteArray, byteArray.length + 1, 1);
249: fail("should throw ArrayIndexOutOfBoundsException");
250: } catch (ArrayIndexOutOfBoundsException e) {
251: // expected
252: }
253:
254: bos.close();
255:
256: try {
257: bos.write(byteArray, -1, -1);
258: fail("should throw ArrayIndexOutOfBoundsException");
259: } catch (ArrayIndexOutOfBoundsException e) {
260: // expected
261: }
262: }
263:
264: /**
265: * @tests java.io.BufferedOutputStream#write(byte[], int, int)
266: */
267: public void test_write_$BII_NullStream_NullArray()
268: throws IOException {
269: OutputStream bos = new BufferedOutputStream(null);
270: byte[] nullByteArray = null;
271:
272: try {
273: bos.write(nullByteArray, -1, -1);
274: fail("should throw NullPointerException");
275: } catch (NullPointerException e) {
276: // expected
277: }
278:
279: try {
280: bos.write(nullByteArray, 0, -1);
281: fail("should throw NullPointerException");
282: } catch (NullPointerException e) {
283: // expected
284: }
285:
286: try {
287: bos.write(nullByteArray, 1, -1);
288: fail("should throw NullPointerException");
289: } catch (NullPointerException e) {
290: // expected
291: }
292:
293: try {
294: bos.write(nullByteArray, -1, 0);
295: fail("should throw NullPointerException");
296: } catch (NullPointerException e) {
297: // expected
298: }
299:
300: try {
301: bos.write(nullByteArray, 0, 0);
302: fail("should throw NullPointerException");
303: } catch (NullPointerException e) {
304: // expected
305: }
306:
307: try {
308: bos.write(nullByteArray, 1, 0);
309: fail("should throw NullPointerException");
310: } catch (NullPointerException e) {
311: // expected
312: }
313:
314: try {
315: bos.write(nullByteArray, -1, 1);
316: fail("should throw NullPointerException");
317: } catch (NullPointerException e) {
318: // expected
319: }
320:
321: try {
322: bos.write(nullByteArray, 0, 1);
323: fail("should throw NullPointerException");
324: } catch (NullPointerException e) {
325: // expected
326: }
327:
328: try {
329: bos.write(nullByteArray, 1, 1);
330: fail("should throw NullPointerException");
331: } catch (NullPointerException e) {
332: // expected
333: }
334: }
335:
336: /**
337: * @tests java.io.BufferedOutputStream#write(byte[], int, int)
338: */
339: public void test_write_$BII_NullStream_NullArray_Size()
340: throws IOException {
341: OutputStream bos = new BufferedOutputStream(null, 1);
342: byte[] nullByteArray = null;
343:
344: try {
345: bos.write(nullByteArray, -1, -1);
346: fail("should throw NullPointerException");
347: } catch (NullPointerException e) {
348: // expected
349: }
350:
351: try {
352: bos.write(nullByteArray, 0, -1);
353: fail("should throw NullPointerException");
354: } catch (NullPointerException e) {
355: // expected
356: }
357:
358: try {
359: bos.write(nullByteArray, 1, -1);
360: fail("should throw NullPointerException");
361: } catch (NullPointerException e) {
362: // expected
363: }
364:
365: try {
366: bos.write(nullByteArray, -1, 0);
367: fail("should throw NullPointerException");
368: } catch (NullPointerException e) {
369: // expected
370: }
371:
372: try {
373: bos.write(nullByteArray, 0, 0);
374: fail("should throw NullPointerException");
375: } catch (NullPointerException e) {
376: // expected
377: }
378:
379: try {
380: bos.write(nullByteArray, 1, 0);
381: fail("should throw NullPointerException");
382: } catch (NullPointerException e) {
383: // expected
384: }
385:
386: try {
387: bos.write(nullByteArray, -1, 1);
388: fail("should throw NullPointerException");
389: } catch (NullPointerException e) {
390: // expected
391: }
392:
393: try {
394: bos.write(nullByteArray, 0, 1);
395: fail("should throw NullPointerException");
396: } catch (NullPointerException e) {
397: // expected
398: }
399:
400: try {
401: bos.write(nullByteArray, 1, 1);
402: fail("should throw NullPointerException");
403: } catch (NullPointerException e) {
404: // expected
405: }
406: }
407:
408: /**
409: * @tests java.io.BufferedOutputStream#write(byte[], int, int)
410: */
411: public void test_write_$BII_NullStream() throws IOException {
412: BufferedOutputStream bos = new BufferedOutputStream(null);
413: byte[] byteArray = new byte[10];
414:
415: try {
416: bos.write(byteArray, -1, -1);
417: fail("should throw ArrayIndexOutOfBoundsException");
418: } catch (ArrayIndexOutOfBoundsException e) {
419: // expected
420: }
421:
422: try {
423: bos.write(byteArray, 0, -1);
424: fail("should throw ArrayIndexOutOfBoundsException");
425: } catch (ArrayIndexOutOfBoundsException e) {
426: // expected
427: }
428:
429: try {
430: bos.write(byteArray, 1, -1);
431: fail("should throw ArrayIndexOutOfBoundsException");
432: } catch (ArrayIndexOutOfBoundsException e) {
433: // expected
434: }
435:
436: try {
437: bos.write(byteArray, -1, 0);
438: fail("should throw ArrayIndexOutOfBoundsException");
439: } catch (ArrayIndexOutOfBoundsException e) {
440: // expected
441: }
442:
443: bos.write(byteArray, 0, 0);
444:
445: bos.write(byteArray, 1, 0);
446:
447: bos.write(byteArray, byteArray.length, 0);
448:
449: try {
450: bos.write(byteArray, byteArray.length + 1, 0);
451: fail("should throw ArrayIndexOutOfBoundsException");
452: } catch (ArrayIndexOutOfBoundsException e) {
453: // expected
454: }
455:
456: try {
457: bos.write(byteArray, -1, 1);
458: fail("should throw ArrayIndexOutOfBoundsException");
459: } catch (ArrayIndexOutOfBoundsException e) {
460: // expected
461: }
462:
463: bos.write(byteArray, 0, 1);
464: bos.write(byteArray, 1, 1);
465:
466: bos.write(byteArray, 0, byteArray.length);
467:
468: try {
469: bos.write(byteArray, byteArray.length + 1, 1);
470: fail("should throw ArrayIndexOutOfBoundsException");
471: } catch (ArrayIndexOutOfBoundsException e) {
472: // expected
473: }
474: }
475:
476: /**
477: * @tests java.io.BufferedOutputStream#write(byte[], int, int)
478: */
479: public void test_write_$BII_NullStream_Size() throws IOException {
480: BufferedOutputStream bos = new BufferedOutputStream(null, 1);
481: byte[] byteArray = new byte[10];
482:
483: try {
484: bos.write(byteArray, -1, -1);
485: fail("should throw ArrayIndexOutOfBoundsException");
486: } catch (ArrayIndexOutOfBoundsException e) {
487: // expected
488: }
489:
490: try {
491: bos.write(byteArray, 0, -1);
492: fail("should throw ArrayIndexOutOfBoundsException");
493: } catch (ArrayIndexOutOfBoundsException e) {
494: // expected
495: }
496:
497: try {
498: bos.write(byteArray, 1, -1);
499: fail("should throw ArrayIndexOutOfBoundsException");
500: } catch (ArrayIndexOutOfBoundsException e) {
501: // expected
502: }
503:
504: try {
505: bos.write(byteArray, -1, 0);
506: fail("should throw ArrayIndexOutOfBoundsException");
507: } catch (ArrayIndexOutOfBoundsException e) {
508: // expected
509: }
510:
511: bos.write(byteArray, 0, 0);
512:
513: bos.write(byteArray, 1, 0);
514:
515: bos.write(byteArray, byteArray.length, 0);
516:
517: try {
518: bos.write(byteArray, byteArray.length + 1, 0);
519: fail("should throw ArrayIndexOutOfBoundsException");
520: } catch (ArrayIndexOutOfBoundsException e) {
521: // expected
522: }
523:
524: try {
525: bos.write(byteArray, -1, 1);
526: fail("should throw NullPointerException");
527: } catch (NullPointerException e) {
528: // expected
529: }
530:
531: try {
532: bos.write(byteArray, 0, 1);
533: fail("should throw NullPointerException");
534: } catch (NullPointerException e) {
535: // expected
536: }
537:
538: try {
539: bos.write(byteArray, 0, byteArray.length);
540: fail("should throw NullPointerException");
541: } catch (NullPointerException e) {
542: // expected
543: }
544:
545: try {
546: bos.write(byteArray, 1, 1);
547: fail("should throw NullPointerException");
548: } catch (NullPointerException e) {
549: // expected
550: }
551:
552: try {
553: bos.write(byteArray, byteArray.length + 1, 1);
554: fail("should throw NullPointerException");
555: } catch (NullPointerException e) {
556: // expected
557: }
558: }
559:
560: /**
561: * @tests java.io.BufferedOutputStream#write(int)
562: */
563: public void test_writeI() throws IOException {
564: baos = new java.io.ByteArrayOutputStream();
565: os = new java.io.BufferedOutputStream(baos);
566: os.write('t');
567: bais = new java.io.ByteArrayInputStream(baos.toByteArray());
568: assertEquals("Byte written, not buffered", 0, bais.available());
569: os.flush();
570: bais = new java.io.ByteArrayInputStream(baos.toByteArray());
571: assertEquals("Byte not written after flush", 1, bais
572: .available());
573: byte[] wbytes = new byte[1];
574: bais.read(wbytes, 0, 1);
575: assertEquals("Incorrect byte written", 't', wbytes[0]);
576: }
577:
578: /**
579: * Tears down the fixture, for example, close a network connection. This
580: * method is called after a test is executed.
581: */
582: protected void tearDown() {
583: try {
584: if (bais != null)
585: bais.close();
586: if (os != null)
587: os.close();
588: if (baos != null)
589: baos.close();
590: } catch (Exception e) {
591: System.out.println("Exception during tearDown"
592: + e.toString());
593: }
594: }
595: }
|