Commit | Line | Data |
---|---|---|
a26188d3 NR |
1 | package be.nikiroo.utils.test_code; |
2 | ||
3 | import java.io.ByteArrayOutputStream; | |
4 | ||
8e76f6ab | 5 | import be.nikiroo.utils.streams.BufferedOutputStream; |
a26188d3 NR |
6 | import be.nikiroo.utils.test.TestCase; |
7 | import be.nikiroo.utils.test.TestLauncher; | |
8 | ||
9 | class BufferedOutputStreamTest extends TestLauncher { | |
10 | public BufferedOutputStreamTest(String[] args) { | |
11 | super("BufferedOutputStream test", args); | |
12 | ||
13 | addTest(new TestCase("Single write") { | |
14 | @Override | |
15 | public void test() throws Exception { | |
16 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
17 | BufferedOutputStream out = new BufferedOutputStream(bout); | |
18 | ||
19 | byte[] data = new byte[] { 42, 12, 0, 127 }; | |
20 | ||
21 | out.write(data); | |
22 | out.close(); | |
23 | ||
24 | checkArrays(this, "FIRST", bout, data); | |
25 | } | |
26 | }); | |
27 | ||
28 | addTest(new TestCase("Multiple writes") { | |
29 | @Override | |
30 | public void test() throws Exception { | |
31 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
32 | BufferedOutputStream out = new BufferedOutputStream(bout); | |
33 | ||
34 | byte[] data1 = new byte[] { 42, 12, 0, 127 }; | |
35 | byte[] data2 = new byte[] { 15, 55 }; | |
36 | byte[] data3 = new byte[] {}; | |
37 | ||
38 | byte[] dataAll = new byte[] { 42, 12, 0, 127, 15, 55 }; | |
39 | ||
40 | out.write(data1); | |
41 | out.write(data2); | |
42 | out.write(data3); | |
43 | out.close(); | |
44 | ||
45 | checkArrays(this, "FIRST", bout, dataAll); | |
46 | } | |
47 | }); | |
48 | } | |
49 | ||
50 | static void checkArrays(TestCase test, String prefix, | |
51 | ByteArrayOutputStream bout, byte[] expected) throws Exception { | |
52 | byte[] actual = bout.toByteArray(); | |
53 | ||
54 | if (false) { | |
55 | System.out.print("\nExpected data: [ "); | |
c8ce09c4 | 56 | for (int i = 0; i < expected.length; i++) { |
a26188d3 NR |
57 | if (i > 0) |
58 | System.out.print(", "); | |
59 | System.out.print(expected[i]); | |
60 | } | |
61 | System.out.println(" ]"); | |
62 | ||
63 | System.out.print("Actual data : [ "); | |
64 | for (int i = 0; i < actual.length; i++) { | |
65 | if (i > 0) | |
66 | System.out.print(", "); | |
67 | System.out.print(actual[i]); | |
68 | } | |
69 | System.out.println(" ]"); | |
70 | } | |
71 | ||
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]); | |
78 | } | |
79 | } | |
80 | } |