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
.ReplaceInputStream
;
8 import be
.nikiroo
.utils
.test
.TestCase
;
9 import be
.nikiroo
.utils
.test
.TestLauncher
;
11 public class ReplaceInputStreamTest
extends TestLauncher
{
12 public ReplaceInputStreamTest(String
[] args
) {
13 super("ReplaceInputStream test", args
);
15 addTest(new TestCase("Simple InputStream, empty replace") {
17 public void test() throws Exception
{
18 byte[] expected
= new byte[] { 42, 12, 0, 127 };
19 ReplaceInputStream in
= new ReplaceInputStream(
20 new ByteArrayInputStream(expected
), new byte[0],
22 checkArrays(this, "FIRST", in
, expected
);
26 addTest(new TestCase("Simple InputStream, simple replace") {
28 public void test() throws Exception
{
29 byte[] expected
= new byte[] { 42, 12, 0, 127 };
30 ReplaceInputStream in
= new ReplaceInputStream(
31 new ByteArrayInputStream(expected
), new byte[] { 0 },
34 checkArrays(this, "FIRST", in
, new byte[] { 42, 12, 10, 127 });
38 addTest(new TestCase("Simple byte array reading, 3/4 replace") {
40 public void test() throws Exception
{
41 byte[] expected
= new byte[] { 42, 12, 0, 127 };
42 ReplaceInputStream in
= new ReplaceInputStream(
43 new ByteArrayInputStream(expected
), new byte[] { 12, 0,
44 127 }, new byte[] { 10, 10, 10 });
46 checkArrays(this, "FIRST", in
, new byte[] { 42, 10, 10, 10 });
50 addTest(new TestCase("Simple byte array reading, longer replace") {
52 public void test() throws Exception
{
53 byte[] expected
= new byte[] { 42, 12, 0, 127 };
54 ReplaceInputStream in
= new ReplaceInputStream(
55 new ByteArrayInputStream(expected
), new byte[] { 0 },
56 new byte[] { 10, 10, 10 });
60 for (int i
= 0; i
< expected
.length
; i
++) {
61 System
.out
.println("expected[" + i
+ "] = " + expected
[i
]);
63 byte[] actual
= IOUtils
.readSmallStream(in
).getBytes("UTF-8");
64 for (int i
= 0; i
< actual
.length
; i
++) {
65 System
.out
.println("actual[" + i
+ "] = " + actual
[i
]);
70 checkArrays(this, "FIRST", in
, new byte[] { 42, 12, 10, 10, 10,
75 addTest(new TestCase("Simple byte array reading, shorter replace") {
77 public void test() throws Exception
{
78 byte[] expected
= new byte[] { 42, 12, 0, 127 };
79 ReplaceInputStream in
= new ReplaceInputStream(
80 new ByteArrayInputStream(expected
), new byte[] { 42,
81 12, 0 }, new byte[] { 10 });
82 checkArrays(this, "FIRST", in
, new byte[] { 10, 127 });
87 static void checkArrays(TestCase test
, String prefix
, InputStream in
,
88 byte[] expected
) throws Exception
{
89 byte[] actual
= IOUtils
.toByteArray(in
);
90 test
.assertEquals("The " + prefix
91 + " resulting array has not the correct number of items",
92 expected
.length
, actual
.length
);
93 for (int i
= 0; i
< actual
.length
; i
++) {
94 test
.assertEquals("Item " + i
+ " (0-based) is not the same",
95 expected
[i
], actual
[i
]);