X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=test_code%2FReplaceInputStreamTest.java;fp=test_code%2FReplaceInputStreamTest.java;h=efab8c73d62e5f79779a41b6f8c4c889ad8a8f74;hp=e6e211215430485cfd0938b5fcb89f6fc5ee4a5e;hb=7665ac5903eff6055e601dc0ad7540032d2f8a14;hpb=90eaf7c6209fc6ad21a9f9a37bc301e6078c930e diff --git a/test_code/ReplaceInputStreamTest.java b/test_code/ReplaceInputStreamTest.java index e6e2112..efab8c7 100644 --- a/test_code/ReplaceInputStreamTest.java +++ b/test_code/ReplaceInputStreamTest.java @@ -48,7 +48,7 @@ class ReplaceInputStreamTest extends TestLauncher { } }); - addTest(new TestCase("Lnger replace") { + addTest(new TestCase("Longer replace") { @Override public void test() throws Exception { byte[] data = new byte[] { 42, 12, 0, 127 }; @@ -79,22 +79,126 @@ class ReplaceInputStreamTest extends TestLauncher { byte[] data = "I like red".getBytes("UTF-8"); ReplaceInputStream in = new ReplaceInputStream( new ByteArrayInputStream(data), - "red".getBytes("UTF-8"), "blue".getBytes("UTF-8")); + "red", "blue"); checkArrays(this, "FIRST", in, "I like blue".getBytes("UTF-8")); - data = "I like blue".getBytes("UTF-8"); + data = "I like blue hammers".getBytes("UTF-8"); in = new ReplaceInputStream(new ByteArrayInputStream(data), - "blue".getBytes("UTF-8"), "red".getBytes("UTF-8")); + "blue", "red"); - checkArrays(this, "FIRST", in, "I like red".getBytes("UTF-8")); + checkArrays(this, "SECOND", in, "I like red hammers".getBytes("UTF-8")); } }); + + addTest(new TestCase("Multiple replaces") { + @Override + public void test() throws Exception { + byte[] data = "I like red cabage".getBytes("UTF-8"); + ReplaceInputStream in = new ReplaceInputStream( + new ByteArrayInputStream(data), // + new String[] { "red", "like" }, // + new String[] { "green", "very very much like" } // + ); + + String result = new String(IOUtils.toByteArray(in), "UTF-8"); + assertEquals("I very very much like green cabage", result); + } + }); + + addTest(new TestCase("Multiple replaces") { + @Override + public void test() throws Exception { + String str= ("" // + + "\n" // + + "\n" // + + "\n" // + + "\n" // + + "\t\n" // + + "\t\n" // + + "\t${title}\n" // + + "\t\n" // + + "\t\n" // + + "\n" // + + "\n" // + + "\t
\n" // + + "${banner}${content}\t
\n" // + + "\n" // + + "" // + ); + byte[] data = str.getBytes("UTF-8"); + + String title = "Fanfix"; + String banner = ""; + String content = ""; + + InputStream in = new ReplaceInputStream( + new ByteArrayInputStream(data), // + new String[] { "${title}", "${banner}", "${content}" }, // + new String[] { title, banner, content } // + ); + + String result = new String(IOUtils.toByteArray(in), "UTF-8"); + assertEquals(str // + .replace("${title}", title) // + .replace("${banner}", banner) // + .replace("${content}", content) // + , result); + } + }); + + } static void checkArrays(TestCase test, String prefix, InputStream in, byte[] expected) throws Exception { byte[] actual = IOUtils.toByteArray(in); + +// System.out.println("\nActual:"); +// for(byte byt : actual) { +// System.out.print(byt+" "); +// } +// System.out.println("\nExpected:"); +// for(byte byt : expected) { +// System.out.print(byt+" "); +// } + test.assertEquals("The " + prefix + " resulting array has not the correct number of items", expected.length, actual.length);