1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayOutputStream
;
5 import be
.nikiroo
.utils
.streams
.ReplaceOutputStream
;
6 import be
.nikiroo
.utils
.test
.TestCase
;
7 import be
.nikiroo
.utils
.test
.TestLauncher
;
9 class ReplaceOutputStreamTest
extends TestLauncher
{
10 public ReplaceOutputStreamTest(String
[] args
) {
11 super("ReplaceOutputStream test", args
);
13 addTest(new TestCase("Single write, empty bytes replaces") {
15 public void test() throws Exception
{
16 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
17 ReplaceOutputStream out
= new ReplaceOutputStream(bout
,
18 new byte[0], new byte[0]);
20 byte[] data
= new byte[] { 42, 12, 0, 127 };
25 checkArrays(this, "FIRST", bout
, data
);
29 addTest(new TestCase("Multiple writes, empty Strings replaces") {
31 public void test() throws Exception
{
32 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
33 ReplaceOutputStream out
= new ReplaceOutputStream(bout
, "", "");
35 byte[] data1
= new byte[] { 42, 12, 0, 127 };
36 byte[] data2
= new byte[] { 15, 55 };
37 byte[] data3
= new byte[] {};
39 byte[] dataAll
= new byte[] { 42, 12, 0, 127, 15, 55 };
46 checkArrays(this, "FIRST", bout
, dataAll
);
50 addTest(new TestCase("Single write, bytes replaces") {
52 public void test() throws Exception
{
53 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
54 ReplaceOutputStream out
= new ReplaceOutputStream(bout
,
55 new byte[] { 12 }, new byte[] { 55 });
57 byte[] data
= new byte[] { 42, 12, 0, 127 };
62 checkArrays(this, "FIRST", bout
, new byte[] { 42, 55, 0, 127 });
66 addTest(new TestCase("Multiple writes, Strings replaces") {
68 public void test() throws Exception
{
69 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
70 ReplaceOutputStream out
= new ReplaceOutputStream(bout
, "(-)",
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");
82 checkArrays(this, "FIRST", bout
,
83 "un mot (.) of twee (.) makes the difference"
88 addTest(new TestCase("Single write, longer bytes replaces") {
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 });
95 byte[] data
= new byte[] { 42, 12, 0, 127 };
100 checkArrays(this, "FIRST", bout
, new byte[] { 42, 55, 55, 66,
105 addTest(new TestCase("Single write, shorter bytes replaces") {
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 });
112 byte[] data
= new byte[] { 42, 12, 0, 127 };
117 checkArrays(this, "FIRST", bout
, new byte[] { 42, 55, 127 });
121 addTest(new TestCase("Single write, remove bytes replaces") {
123 public void test() throws Exception
{
124 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
125 ReplaceOutputStream out
= new ReplaceOutputStream(bout
,
126 new byte[] { 12 }, new byte[] {});
128 byte[] data
= new byte[] { 42, 12, 0, 127 };
133 checkArrays(this, "FIRST", bout
, new byte[] { 42, 0, 127 });
138 static void checkArrays(TestCase test
, String prefix
,
139 ByteArrayOutputStream bout
, byte[] expected
) throws Exception
{
140 byte[] actual
= bout
.toByteArray();
143 System
.out
.print("\nExpected data: [ ");
144 for (int i
= 0; i
< expected
.length
; i
++) {
146 System
.out
.print(", ");
147 System
.out
.print(expected
[i
]);
149 System
.out
.println(" ]");
151 System
.out
.print("Actual data : [ ");
152 for (int i
= 0; i
< actual
.length
; i
++) {
154 System
.out
.print(", ");
155 System
.out
.print(actual
[i
]);
157 System
.out
.println(" ]");
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
]);