1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayOutputStream
;
5 import be
.nikiroo
.utils
.streams
.BufferedOutputStream
;
6 import be
.nikiroo
.utils
.test
.TestCase
;
7 import be
.nikiroo
.utils
.test
.TestLauncher
;
9 class BufferedOutputStreamTest
extends TestLauncher
{
10 public BufferedOutputStreamTest(String
[] args
) {
11 super("BufferedOutputStream test", args
);
13 addTest(new TestCase("Single write") {
15 public void test() throws Exception
{
16 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
17 BufferedOutputStream out
= new BufferedOutputStream(bout
);
19 byte[] data
= new byte[] { 42, 12, 0, 127 };
24 checkArrays(this, "FIRST", bout
, data
);
28 addTest(new TestCase("Multiple writes") {
30 public void test() throws Exception
{
31 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
32 BufferedOutputStream out
= new BufferedOutputStream(bout
);
34 byte[] data1
= new byte[] { 42, 12, 0, 127 };
35 byte[] data2
= new byte[] { 15, 55 };
36 byte[] data3
= new byte[] {};
38 byte[] dataAll
= new byte[] { 42, 12, 0, 127, 15, 55 };
45 checkArrays(this, "FIRST", bout
, dataAll
);
50 static void checkArrays(TestCase test
, String prefix
,
51 ByteArrayOutputStream bout
, byte[] expected
) throws Exception
{
52 byte[] actual
= bout
.toByteArray();
55 System
.out
.print("\nExpected data: [ ");
56 for (int i
= 0; i
< actual
.length
; i
++) {
58 System
.out
.print(", ");
59 System
.out
.print(expected
[i
]);
61 System
.out
.println(" ]");
63 System
.out
.print("Actual data : [ ");
64 for (int i
= 0; i
< actual
.length
; i
++) {
66 System
.out
.print(", ");
67 System
.out
.print(actual
[i
]);
69 System
.out
.println(" ]");
72 test
.assertEquals("The " + prefix
73 + " resulting array has not the correct number of items",
74 expected
.length
, actual
.length
);
75 for (int i
= 0; i
< actual
.length
; i
++) {
76 test
.assertEquals(prefix
+ ": item " + i
77 + " (0-based) is not the same", expected
[i
], actual
[i
]);