Commit | Line | Data |
---|---|---|
6ef54b36 NR |
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; | |
8e76f6ab | 7 | import be.nikiroo.utils.streams.ReplaceInputStream; |
6ef54b36 NR |
8 | import be.nikiroo.utils.test.TestCase; |
9 | import be.nikiroo.utils.test.TestLauncher; | |
10 | ||
876dbf8b | 11 | class 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 | ||
aaef3a51 | 51 | addTest(new TestCase("Longer 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), | |
aaef3a51 | 82 | "red", "blue"); |
876dbf8b NR |
83 | |
84 | checkArrays(this, "FIRST", in, "I like blue".getBytes("UTF-8")); | |
85 | ||
5a2af0e4 | 86 | data = "I like blue hammers".getBytes("UTF-8"); |
876dbf8b | 87 | in = new ReplaceInputStream(new ByteArrayInputStream(data), |
aaef3a51 | 88 | "blue", "red"); |
876dbf8b | 89 | |
5a2af0e4 | 90 | checkArrays(this, "SECOND", in, "I like red hammers".getBytes("UTF-8")); |
876dbf8b NR |
91 | } |
92 | }); | |
aaef3a51 NR |
93 | |
94 | addTest(new TestCase("Multiple replaces") { | |
95 | @Override | |
96 | public void test() throws Exception { | |
5a2af0e4 | 97 | byte[] data = "I like red cabage".getBytes("UTF-8"); |
aaef3a51 NR |
98 | ReplaceInputStream in = new ReplaceInputStream( |
99 | new ByteArrayInputStream(data), // | |
5a2af0e4 NR |
100 | new String[] { "red", "like" }, // |
101 | new String[] { "green", "very very much like" } // | |
102 | ); | |
103 | ||
104 | String result = new String(IOUtils.toByteArray(in), "UTF-8"); | |
105 | assertEquals("I very very much like green cabage", result); | |
106 | } | |
107 | }); | |
108 | ||
109 | addTest(new TestCase("Multiple replaces") { | |
110 | @Override | |
111 | public void test() throws Exception { | |
112 | String str= ("" // | |
113 | + "<!DOCTYPE html>\n" // | |
114 | + "<html>\n" // | |
115 | + "<head>\n" // | |
116 | + "<!--\n" // | |
117 | + "\tCopyright 2020 David ROULET\n" // | |
118 | + "\t\n" // | |
119 | + "\tThis file is part of fanfix.\n" // | |
120 | + "\t\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" // | |
125 | + "\t\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" // | |
130 | + "\t\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" // | |
134 | + "\n" // | |
135 | + " This website was coded by:\n" // | |
136 | + " \t\tA kangaroo.\n" // | |
137 | + " _ _\n" // | |
138 | + " (\\\\( \\\n" // | |
139 | + " `.\\-.)\n" // | |
140 | + " _...._ _,-' `-.\n" // | |
141 | + "\\ ,' `-._.- -.,-' . \\\n" // | |
142 | + " \\`. ,' `.\n" // | |
143 | + " \\ `-...__ / . .: y\n" // | |
144 | + " `._ ``-...__ / ,'```-._/\n" // | |
145 | + " `-._ ```-' | /_ //\n" // | |
146 | + " `.._ _ ; <_ \\ //\n" // | |
147 | + " ``-.___ `. `-._ \\ \\ //\n" // | |
148 | + " `- < `. (\\ _/)/ `.\\/ //\n" // | |
149 | + " \\ \\ ` ^^^^^^^^^\n" // | |
150 | + "\t___________________________________________________________________________\n" // | |
151 | + "\t\n" // | |
152 | + "-->\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" // | |
158 | + "</head>\n" // | |
159 | + "<body>\n" // | |
160 | + "\t<div class='main'>\n" // | |
161 | + "${banner}${content}\t</div>\n" // | |
162 | + "</body>\n" // | |
163 | + "" // | |
164 | ); | |
165 | byte[] data = str.getBytes("UTF-8"); | |
166 | ||
167 | String title = "Fanfix"; | |
168 | String banner = "<div class='banner'>Super banner v3</div>"; | |
169 | String content = ""; | |
170 | ||
171 | InputStream in = new ReplaceInputStream( | |
172 | new ByteArrayInputStream(data), // | |
173 | new String[] { "${title}", "${banner}", "${content}" }, // | |
174 | new String[] { title, banner, content } // | |
aaef3a51 NR |
175 | ); |
176 | ||
177 | String result = new String(IOUtils.toByteArray(in), "UTF-8"); | |
5a2af0e4 NR |
178 | assertEquals(str // |
179 | .replace("${title}", title) // | |
180 | .replace("${banner}", banner) // | |
181 | .replace("${content}", content) // | |
182 | , result); | |
aaef3a51 NR |
183 | } |
184 | }); | |
5a2af0e4 NR |
185 | |
186 | ||
6ef54b36 NR |
187 | } |
188 | ||
189 | static void checkArrays(TestCase test, String prefix, InputStream in, | |
190 | byte[] expected) throws Exception { | |
191 | byte[] actual = IOUtils.toByteArray(in); | |
5a2af0e4 NR |
192 | |
193 | // System.out.println("\nActual:"); | |
194 | // for(byte byt : actual) { | |
195 | // System.out.print(byt+" "); | |
196 | // } | |
197 | // System.out.println("\nExpected:"); | |
198 | // for(byte byt : expected) { | |
199 | // System.out.print(byt+" "); | |
200 | // } | |
201 | ||
6ef54b36 NR |
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]); | |
208 | } | |
209 | } | |
210 | } |