| 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("Longer 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", "blue"); |
| 83 | |
| 84 | checkArrays(this, "FIRST", in, "I like blue".getBytes("UTF-8")); |
| 85 | |
| 86 | data = "I like blue hammers".getBytes("UTF-8"); |
| 87 | in = new ReplaceInputStream(new ByteArrayInputStream(data), |
| 88 | "blue", "red"); |
| 89 | |
| 90 | checkArrays(this, "SECOND", in, "I like red hammers".getBytes("UTF-8")); |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | addTest(new TestCase("Multiple replaces") { |
| 95 | @Override |
| 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" } // |
| 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 } // |
| 175 | ); |
| 176 | |
| 177 | String result = new String(IOUtils.toByteArray(in), "UTF-8"); |
| 178 | assertEquals(str // |
| 179 | .replace("${title}", title) // |
| 180 | .replace("${banner}", banner) // |
| 181 | .replace("${content}", content) // |
| 182 | , result); |
| 183 | } |
| 184 | }); |
| 185 | |
| 186 | |
| 187 | } |
| 188 | |
| 189 | static void checkArrays(TestCase test, String prefix, InputStream in, |
| 190 | byte[] expected) throws Exception { |
| 191 | byte[] actual = IOUtils.toByteArray(in); |
| 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 | |
| 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 | } |