1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.InputStream
;
6 import be
.nikiroo
.utils
.IOUtils
;
7 import be
.nikiroo
.utils
.streams
.ReplaceInputStream
;
8 import be
.nikiroo
.utils
.test
.TestCase
;
9 import be
.nikiroo
.utils
.test
.TestLauncher
;
11 class ReplaceInputStreamTest
extends TestLauncher
{
12 public ReplaceInputStreamTest(String
[] args
) {
13 super("ReplaceInputStream test", args
);
15 addTest(new TestCase("Empty replace") {
17 public void test() throws Exception
{
18 byte[] data
= new byte[] { 42, 12, 0, 127 };
19 ReplaceInputStream in
= new ReplaceInputStream(
20 new ByteArrayInputStream(data
), new byte[0],
23 checkArrays(this, "FIRST", in
, data
);
27 addTest(new TestCase("Simple replace") {
29 public void test() throws Exception
{
30 byte[] data
= new byte[] { 42, 12, 0, 127 };
31 ReplaceInputStream in
= new ReplaceInputStream(
32 new ByteArrayInputStream(data
), new byte[] { 0 },
35 checkArrays(this, "FIRST", in
, new byte[] { 42, 12, 10, 127 });
39 addTest(new TestCase("3/4 replace") {
41 public void test() throws Exception
{
42 byte[] data
= new byte[] { 42, 12, 0, 127 };
43 ReplaceInputStream in
= new ReplaceInputStream(
44 new ByteArrayInputStream(data
),
45 new byte[] { 12, 0, 127 }, new byte[] { 10, 10, 10 });
47 checkArrays(this, "FIRST", in
, new byte[] { 42, 10, 10, 10 });
51 addTest(new TestCase("Lnger replace") {
53 public void test() throws Exception
{
54 byte[] data
= new byte[] { 42, 12, 0, 127 };
55 ReplaceInputStream in
= new ReplaceInputStream(
56 new ByteArrayInputStream(data
), new byte[] { 0 },
57 new byte[] { 10, 10, 10 });
59 checkArrays(this, "FIRST", in
, new byte[] { 42, 12, 10, 10, 10,
64 addTest(new TestCase("Shorter replace") {
66 public void test() throws Exception
{
67 byte[] data
= new byte[] { 42, 12, 0, 127 };
68 ReplaceInputStream in
= new ReplaceInputStream(
69 new ByteArrayInputStream(data
),
70 new byte[] { 42, 12, 0 }, new byte[] { 10 });
72 checkArrays(this, "FIRST", in
, new byte[] { 10, 127 });
76 addTest(new TestCase("String replace") {
78 public void test() throws Exception
{
79 byte[] data
= "I like red".getBytes("UTF-8");
80 ReplaceInputStream in
= new ReplaceInputStream(
81 new ByteArrayInputStream(data
),
82 "red".getBytes("UTF-8"), "blue".getBytes("UTF-8"));
84 checkArrays(this, "FIRST", in
, "I like blue".getBytes("UTF-8"));
86 data
= "I like blue".getBytes("UTF-8");
87 in
= new ReplaceInputStream(new ByteArrayInputStream(data
),
88 "blue".getBytes("UTF-8"), "red".getBytes("UTF-8"));
90 checkArrays(this, "FIRST", in
, "I like red".getBytes("UTF-8"));
95 static void checkArrays(TestCase test
, String prefix
, InputStream in
,
96 byte[] expected
) throws Exception
{
97 byte[] actual
= IOUtils
.toByteArray(in
);
98 test
.assertEquals("The " + prefix
99 + " resulting array has not the correct number of items",
100 expected
.length
, actual
.length
);
101 for (int i
= 0; i
< actual
.length
; i
++) {
102 test
.assertEquals("Item " + i
+ " (0-based) is not the same",
103 expected
[i
], actual
[i
]);