ReplaceInputStream (wip)
[fanfix.git] / src / be / nikiroo / utils / test_code / ReplaceInputStreamTest.java
CommitLineData
6ef54b36
NR
1package be.nikiroo.utils.test_code;
2
3import java.io.ByteArrayInputStream;
4import java.io.InputStream;
5
6import be.nikiroo.utils.IOUtils;
7import be.nikiroo.utils.ReplaceInputStream;
8import be.nikiroo.utils.test.TestCase;
9import be.nikiroo.utils.test.TestLauncher;
10
11public class ReplaceInputStreamTest extends TestLauncher {
12 public ReplaceInputStreamTest(String[] args) {
13 super("ReplaceInputStream test", args);
14
15 addTest(new TestCase("Simple InputStream, empty replace") {
16 @Override
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],
21 new byte[0]);
22 checkArrays(this, "FIRST", in, expected);
23 }
24 });
25
26 addTest(new TestCase("Simple InputStream, simple replace") {
27 @Override
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 },
32 new byte[] { 10 });
33
34 checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 127 });
35 }
36 });
37
38 addTest(new TestCase("Simple byte array reading, 3/4 replace") {
39 @Override
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 });
45
46 checkArrays(this, "FIRST", in, new byte[] { 42, 10, 10, 10 });
47 }
48 });
49
50 addTest(new TestCase("Simple byte array reading, longer replace") {
51 @Override
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 });
57
58 // TODO NOT OK!!
59 System.out.println();
60 for (int i = 0; i < expected.length; i++) {
61 System.out.println("expected[" + i + "] = " + expected[i]);
62 }
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]);
66 }
67 System.exit(1);
68 //
69
70 checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 10, 10,
71 127 });
72 }
73 });
74
75 addTest(new TestCase("Simple byte array reading, shorter replace") {
76 @Override
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 });
83 }
84 });
85 }
86
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]);
96 }
97 }
98}