allow getMetas without synchronized
[nikiroo-utils.git] / 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;
8e76f6ab 7import be.nikiroo.utils.streams.ReplaceInputStream;
6ef54b36
NR
8import be.nikiroo.utils.test.TestCase;
9import be.nikiroo.utils.test.TestLauncher;
10
876dbf8b 11class ReplaceInputStreamTest extends TestLauncher {
6ef54b36
NR
12 public ReplaceInputStreamTest(String[] args) {
13 super("ReplaceInputStream test", args);
14
876dbf8b 15 addTest(new TestCase("Empty replace") {
6ef54b36
NR
16 @Override
17 public void test() throws Exception {
876dbf8b 18 byte[] data = new byte[] { 42, 12, 0, 127 };
6ef54b36 19 ReplaceInputStream in = new ReplaceInputStream(
876dbf8b 20 new ByteArrayInputStream(data), new byte[0],
6ef54b36 21 new byte[0]);
876dbf8b
NR
22
23 checkArrays(this, "FIRST", in, data);
6ef54b36
NR
24 }
25 });
26
876dbf8b 27 addTest(new TestCase("Simple replace") {
6ef54b36
NR
28 @Override
29 public void test() throws Exception {
876dbf8b 30 byte[] data = new byte[] { 42, 12, 0, 127 };
6ef54b36 31 ReplaceInputStream in = new ReplaceInputStream(
876dbf8b 32 new ByteArrayInputStream(data), new byte[] { 0 },
6ef54b36
NR
33 new byte[] { 10 });
34
35 checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 127 });
36 }
37 });
38
876dbf8b 39 addTest(new TestCase("3/4 replace") {
6ef54b36
NR
40 @Override
41 public void test() throws Exception {
876dbf8b 42 byte[] data = new byte[] { 42, 12, 0, 127 };
6ef54b36 43 ReplaceInputStream in = new ReplaceInputStream(
876dbf8b
NR
44 new ByteArrayInputStream(data),
45 new byte[] { 12, 0, 127 }, new byte[] { 10, 10, 10 });
6ef54b36
NR
46
47 checkArrays(this, "FIRST", in, new byte[] { 42, 10, 10, 10 });
48 }
49 });
50
876dbf8b 51 addTest(new TestCase("Lnger replace") {
6ef54b36
NR
52 @Override
53 public void test() throws Exception {
876dbf8b 54 byte[] data = new byte[] { 42, 12, 0, 127 };
6ef54b36 55 ReplaceInputStream in = new ReplaceInputStream(
876dbf8b 56 new ByteArrayInputStream(data), new byte[] { 0 },
6ef54b36
NR
57 new byte[] { 10, 10, 10 });
58
6ef54b36
NR
59 checkArrays(this, "FIRST", in, new byte[] { 42, 12, 10, 10, 10,
60 127 });
61 }
62 });
63
876dbf8b 64 addTest(new TestCase("Shorter replace") {
6ef54b36
NR
65 @Override
66 public void test() throws Exception {
876dbf8b 67 byte[] data = new byte[] { 42, 12, 0, 127 };
6ef54b36 68 ReplaceInputStream in = new ReplaceInputStream(
876dbf8b
NR
69 new ByteArrayInputStream(data),
70 new byte[] { 42, 12, 0 }, new byte[] { 10 });
71
6ef54b36
NR
72 checkArrays(this, "FIRST", in, new byte[] { 10, 127 });
73 }
74 });
876dbf8b
NR
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 });
6ef54b36
NR
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}