Commit | Line | Data |
---|---|---|
c8ce09c4 NR |
1 | package be.nikiroo.utils.test_code; |
2 | ||
3 | import java.io.ByteArrayOutputStream; | |
4 | ||
5 | import be.nikiroo.utils.streams.ReplaceOutputStream; | |
6 | import be.nikiroo.utils.test.TestCase; | |
7 | import be.nikiroo.utils.test.TestLauncher; | |
8 | ||
9 | class ReplaceOutputStreamTest extends TestLauncher { | |
10 | public ReplaceOutputStreamTest(String[] args) { | |
11 | super("ReplaceOutputStream test", args); | |
12 | ||
13 | addTest(new TestCase("Single write, empty bytes replaces") { | |
14 | @Override | |
15 | public void test() throws Exception { | |
16 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
17 | ReplaceOutputStream out = new ReplaceOutputStream(bout, | |
18 | new byte[0], new byte[0]); | |
19 | ||
20 | byte[] data = new byte[] { 42, 12, 0, 127 }; | |
21 | ||
22 | out.write(data); | |
23 | out.close(); | |
24 | ||
25 | checkArrays(this, "FIRST", bout, data); | |
26 | } | |
27 | }); | |
28 | ||
29 | addTest(new TestCase("Multiple writes, empty Strings replaces") { | |
30 | @Override | |
31 | public void test() throws Exception { | |
32 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
33 | ReplaceOutputStream out = new ReplaceOutputStream(bout, "", ""); | |
34 | ||
35 | byte[] data1 = new byte[] { 42, 12, 0, 127 }; | |
36 | byte[] data2 = new byte[] { 15, 55 }; | |
37 | byte[] data3 = new byte[] {}; | |
38 | ||
39 | byte[] dataAll = new byte[] { 42, 12, 0, 127, 15, 55 }; | |
40 | ||
41 | out.write(data1); | |
42 | out.write(data2); | |
43 | out.write(data3); | |
44 | out.close(); | |
45 | ||
46 | checkArrays(this, "FIRST", bout, dataAll); | |
47 | } | |
48 | }); | |
49 | ||
50 | addTest(new TestCase("Single write, bytes replaces") { | |
51 | @Override | |
52 | public void test() throws Exception { | |
53 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
54 | ReplaceOutputStream out = new ReplaceOutputStream(bout, | |
55 | new byte[] { 12 }, new byte[] { 55 }); | |
56 | ||
57 | byte[] data = new byte[] { 42, 12, 0, 127 }; | |
58 | ||
59 | out.write(data); | |
60 | out.close(); | |
61 | ||
62 | checkArrays(this, "FIRST", bout, new byte[] { 42, 55, 0, 127 }); | |
63 | } | |
64 | }); | |
65 | ||
66 | addTest(new TestCase("Multiple writes, Strings replaces") { | |
67 | @Override | |
68 | public void test() throws Exception { | |
69 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
70 | ReplaceOutputStream out = new ReplaceOutputStream(bout, "(-)", | |
71 | "(.)"); | |
72 | ||
73 | byte[] data1 = "un mot ".getBytes("UTF-8"); | |
74 | byte[] data2 = "(-) of twee ".getBytes("UTF-8"); | |
75 | byte[] data3 = "(-) makes the difference".getBytes("UTF-8"); | |
76 | ||
77 | out.write(data1); | |
78 | out.write(data2); | |
79 | out.write(data3); | |
80 | out.close(); | |
81 | ||
82 | checkArrays(this, "FIRST", bout, | |
83 | "un mot (.) of twee (.) makes the difference" | |
84 | .getBytes("UTF-8")); | |
85 | } | |
86 | }); | |
87 | ||
88 | addTest(new TestCase("Single write, longer bytes replaces") { | |
89 | @Override | |
90 | public void test() throws Exception { | |
91 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
92 | ReplaceOutputStream out = new ReplaceOutputStream(bout, | |
93 | new byte[] { 12 }, new byte[] { 55, 55, 66 }); | |
94 | ||
95 | byte[] data = new byte[] { 42, 12, 0, 127 }; | |
96 | ||
97 | out.write(data); | |
98 | out.close(); | |
99 | ||
100 | checkArrays(this, "FIRST", bout, new byte[] { 42, 55, 55, 66, | |
101 | 0, 127 }); | |
102 | } | |
103 | }); | |
104 | ||
105 | addTest(new TestCase("Single write, shorter bytes replaces") { | |
106 | @Override | |
107 | public void test() throws Exception { | |
108 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
109 | ReplaceOutputStream out = new ReplaceOutputStream(bout, | |
110 | new byte[] { 12, 0 }, new byte[] { 55 }); | |
111 | ||
112 | byte[] data = new byte[] { 42, 12, 0, 127 }; | |
113 | ||
114 | out.write(data); | |
115 | out.close(); | |
116 | ||
117 | checkArrays(this, "FIRST", bout, new byte[] { 42, 55, 127 }); | |
118 | } | |
119 | }); | |
120 | ||
121 | addTest(new TestCase("Single write, remove bytes replaces") { | |
122 | @Override | |
123 | public void test() throws Exception { | |
124 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
125 | ReplaceOutputStream out = new ReplaceOutputStream(bout, | |
126 | new byte[] { 12 }, new byte[] {}); | |
127 | ||
128 | byte[] data = new byte[] { 42, 12, 0, 127 }; | |
129 | ||
130 | out.write(data); | |
131 | out.close(); | |
132 | ||
133 | checkArrays(this, "FIRST", bout, new byte[] { 42, 0, 127 }); | |
134 | } | |
135 | }); | |
136 | } | |
137 | ||
138 | static void checkArrays(TestCase test, String prefix, | |
139 | ByteArrayOutputStream bout, byte[] expected) throws Exception { | |
140 | byte[] actual = bout.toByteArray(); | |
141 | ||
142 | if (false) { | |
143 | System.out.print("\nExpected data: [ "); | |
144 | for (int i = 0; i < expected.length; i++) { | |
145 | if (i > 0) | |
146 | System.out.print(", "); | |
147 | System.out.print(expected[i]); | |
148 | } | |
149 | System.out.println(" ]"); | |
150 | ||
151 | System.out.print("Actual data : [ "); | |
152 | for (int i = 0; i < actual.length; i++) { | |
153 | if (i > 0) | |
154 | System.out.print(", "); | |
155 | System.out.print(actual[i]); | |
156 | } | |
157 | System.out.println(" ]"); | |
158 | } | |
159 | ||
160 | test.assertEquals("The " + prefix | |
161 | + " resulting array has not the correct number of items", | |
162 | expected.length, actual.length); | |
163 | for (int i = 0; i < actual.length; i++) { | |
164 | test.assertEquals(prefix + ": item " + i | |
165 | + " (0-based) is not the same", expected[i], actual[i]); | |
166 | } | |
167 | } | |
168 | } |