Merge commit '9bd801bfc6a10e5298cdec6e48ca0506ecdfde53'
[fanfix.git] / test_code / ReplaceInputStreamTest.java
1 package be.nikiroo.utils.test_code;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5
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;
10
11 class ReplaceInputStreamTest extends TestLauncher {
12 public ReplaceInputStreamTest(String[] args) {
13 super("ReplaceInputStream test", args);
14
15 addTest(new TestCase("Empty replace") {
16 @Override
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],
21 new byte[0]);
22
23 checkArrays(this, "FIRST", in, data);
24 }
25 });
26
27 addTest(new TestCase("Simple replace") {
28 @Override
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 },
33 new byte[] { 10 });
34
35 checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 127 });
36 }
37 });
38
39 addTest(new TestCase("3/4 replace") {
40 @Override
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 });
46
47 checkArrays(this, "FIRST", in, new byte[] { 42, 10, 10, 10 });
48 }
49 });
50
51 addTest(new TestCase("Lnger replace") {
52 @Override
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 });
58
59 checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 10, 10,
60 127 });
61 }
62 });
63
64 addTest(new TestCase("Shorter replace") {
65 @Override
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 });
71
72 checkArrays(this, "FIRST", in, new byte[] { 10, 127 });
73 }
74 });
75
76 addTest(new TestCase("String replace") {
77 @Override
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"));
83
84 checkArrays(this, "FIRST", in, "I like blue".getBytes("UTF-8"));
85
86 data = "I like blue".getBytes("UTF-8");
87 in = new ReplaceInputStream(new ByteArrayInputStream(data),
88 "blue".getBytes("UTF-8"), "red".getBytes("UTF-8"));
89
90 checkArrays(this, "FIRST", in, "I like red".getBytes("UTF-8"));
91 }
92 });
93 }
94
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]);
104 }
105 }
106 }