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("Longer 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
),
84 checkArrays(this, "FIRST", in
, "I like blue".getBytes("UTF-8"));
86 data
= "I like blue hammers".getBytes("UTF-8");
87 in
= new ReplaceInputStream(new ByteArrayInputStream(data
),
90 checkArrays(this, "SECOND", in
, "I like red hammers".getBytes("UTF-8"));
94 addTest(new TestCase("Multiple replaces") {
96 public void test() throws Exception
{
97 byte[] data
= "I like red cabage".getBytes("UTF-8");
98 ReplaceInputStream in
= new ReplaceInputStream(
99 new ByteArrayInputStream(data
), //
100 new String
[] { "red", "like" }, //
101 new String
[] { "green", "very very much like" } //
104 String result
= new String(IOUtils
.toByteArray(in
), "UTF-8");
105 assertEquals("I very very much like green cabage", result
);
109 addTest(new TestCase("Multiple replaces") {
111 public void test() throws Exception
{
113 + "<!DOCTYPE html>\n" //
117 + "\tCopyright 2020 David ROULET\n" //
119 + "\tThis file is part of fanfix.\n" //
121 + "\tfanfix is free software: you can redistribute it and/or modify\n" //
122 + "\tit under the terms of the GNU Affero General Public License as published by\n" //
123 + "\tthe Free Software Foundation, either version 3 of the License, or\n" //
124 + "\t(at your option) any later version.\n" //
126 + "\tfanfix is distributed in the hope that it will be useful,\n" //
127 + "\tbut WITHOUT ANY WARRANTY; without even the implied warranty of\n" //
128 + "\tMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" //
129 + "\tGNU Affero General Public License for more details.\n" //
131 + "\tYou should have received a copy of the GNU Affero General Public License\n" //
132 + "\talong with fanfix. If not, see <https://www.gnu.org/licenses/>.\n" //
133 + "\t___________________________________________________________________________\n" //
135 + " This website was coded by:\n" //
136 + " \t\tA kangaroo.\n" //
140 + " _...._ _,-' `-.\n" //
141 + "\\ ,' `-._.- -.,-' . \\\n" //
143 + " \\ `-...__ / . .: y\n" //
144 + " `._ ``-...__ / ,'```-._/\n" //
145 + " `-._ ```-' | /_ //\n" //
146 + " `.._ _ ; <_ \\ //\n" //
147 + " ``-.___ `. `-._ \\ \\ //\n" //
148 + " `- < `. (\\ _/)/ `.\\/ //\n" //
149 + " \\ \\ ` ^^^^^^^^^\n" //
150 + "\t___________________________________________________________________________\n" //
153 + "\t<meta http-equiv='content-type' content='text/html; charset=UTF-8'>\n" //
154 + "\t<meta name='viewport' content='width=device-width, initial-scale=1.0'>\n" //
155 + "\t<title>${title}</title>\n" //
156 + "\t<link rel='stylesheet' type='text/css' href='/style.css' />\n" //
157 + "\t<link rel='icon' type='image/x-icon' href='/${favicon}' />\n" //
160 + "\t<div class='main'>\n" //
161 + "${banner}${content}\t</div>\n" //
165 byte[] data
= str
.getBytes("UTF-8");
167 String title
= "Fanfix";
168 String banner
= "<div class='banner'>Super banner v3</div>";
171 InputStream in
= new ReplaceInputStream(
172 new ByteArrayInputStream(data
), //
173 new String
[] { "${title}", "${banner}", "${content}" }, //
174 new String
[] { title
, banner
, content
} //
177 String result
= new String(IOUtils
.toByteArray(in
), "UTF-8");
179 .replace("${title}", title
) //
180 .replace("${banner}", banner
) //
181 .replace("${content}", content
) //
189 static void checkArrays(TestCase test
, String prefix
, InputStream in
,
190 byte[] expected
) throws Exception
{
191 byte[] actual
= IOUtils
.toByteArray(in
);
193 // System.out.println("\nActual:");
194 // for(byte byt : actual) {
195 // System.out.print(byt+" ");
197 // System.out.println("\nExpected:");
198 // for(byte byt : expected) {
199 // System.out.print(byt+" ");
202 test
.assertEquals("The " + prefix
203 + " resulting array has not the correct number of items",
204 expected
.length
, actual
.length
);
205 for (int i
= 0; i
< actual
.length
; i
++) {
206 test
.assertEquals("Item " + i
+ " (0-based) is not the same",
207 expected
[i
], actual
[i
]);