1 17 18 package org.apache.tools.ant.filters; 19 20 import java.io.File ; 21 import java.io.Reader ; 22 import java.io.FileReader ; 23 import java.io.IOException ; 24 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.BuildFileTest; 27 import org.apache.tools.ant.util.FileUtils; 28 29 31 public class DynamicFilterTest extends BuildFileTest { 32 33 public DynamicFilterTest(String name) { 34 super(name); 35 } 36 37 public void setUp() { 38 configureProject("src/etc/testcases/filters/dynamicfilter.xml"); 39 executeTarget("init"); 40 } 41 42 public void tearDown() { 43 executeTarget("cleanup"); 44 } 45 public void testCustomFilter() throws IOException { 46 expectFileContains("dynamicfilter", "result/dynamicfilter", 47 "hellO wOrld"); 48 } 49 50 54 55 private void assertStringContains(String string, String contains) { 56 assertTrue("[" + string + "] does not contain [" + contains +"]", 57 string.indexOf(contains) > -1); 58 } 59 60 private void assertStringNotContains(String string, String contains) { 61 assertTrue("[" + string + "] does contain [" + contains +"]", 62 string.indexOf(contains) == -1); 63 } 64 65 private String getFileString(String filename) 66 throws IOException 67 { 68 Reader r = null; 69 try { 70 r = new FileReader (getProject().resolveFile(filename)); 71 return FileUtils.newFileUtils().readFully(r); 72 } 73 finally { 74 try {r.close();} catch (Throwable ignore) {} 75 } 76 77 } 78 79 private String getFileString(String target, String filename) 80 throws IOException 81 { 82 executeTarget(target); 83 return getFileString(filename); 84 } 85 86 private void expectFileContains(String name, String contains) 87 throws IOException 88 { 89 String content = getFileString(name); 90 assertTrue( 91 "expecting file " + name + " to contain " + contains + 92 " but got " + content, content.indexOf(contains) > -1); 93 } 94 95 private void expectFileContains( 96 String target, String name, String contains) 97 throws IOException 98 { 99 executeTarget(target); 100 expectFileContains(name, contains); 101 } 102 103 public static class CustomFilter implements ChainableReader { 104 char replace = 'x'; 105 char with = 'y'; 106 107 public void setReplace(char replace) { 108 this.replace = replace; 109 } 110 111 public void setWith(char with) { 112 this.with = with; 113 } 114 115 public Reader chain(final Reader rdr) { 116 return new BaseFilterReader(rdr) { 117 public int read() 118 throws IOException 119 { 120 int c = in.read(); 121 if (c == replace) 122 return with; 123 else 124 return c; 125 } 126 }; 127 } 128 } 129 } 130 | Popular Tags |