1 17 18 package org.apache.tools.ant.filters; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 23 import org.apache.tools.ant.BuildFileTest; 24 import org.apache.tools.ant.taskdefs.condition.Os; 25 import org.apache.tools.ant.util.FileUtils; 26 27 30 public class ConcatFilterTest extends BuildFileTest { 31 32 private static FileUtils fu = FileUtils.newFileUtils(); 33 private static final String lSep = 34 Os.isFamily("mac") ? "\r" : System.getProperty("line.separator"); 35 36 private static final String FILE_PREPEND_WITH = 37 "this-should-be-the-first-line" + lSep 38 + "Line 1" + lSep 39 + "Line 2" + lSep 40 + "Line 3" + lSep 41 + "Line 4" + lSep 42 ; 43 44 private static final String FILE_PREPEND = 45 "Line 1" + lSep 46 + "Line 2" + lSep 47 + "Line 3" + lSep 48 + "Line 4" + lSep 49 + "Line 5" + lSep 50 ; 51 52 private static final String FILE_APPEND_WITH = 53 "Line 57" + lSep 54 + "Line 58" + lSep 55 + "Line 59" + lSep 56 + "Line 60" + lSep 57 + "this-should-be-the-last-line" + lSep 58 ; 59 60 private static final String FILE_APPEND = 61 "Line 56" + lSep 62 + "Line 57" + lSep 63 + "Line 58" + lSep 64 + "Line 59" + lSep 65 + "Line 60" + lSep 66 ; 67 68 69 public ConcatFilterTest(String name) { 70 super(name); 71 } 72 73 public void setUp() { 74 configureProject("src/etc/testcases/filters/concat.xml"); 75 } 76 77 public void tearDown() { 78 executeTarget("cleanup"); 79 } 80 81 public void testFilterReaderNoArgs() throws IOException { 82 executeTarget("testFilterReaderNoArgs"); 83 File expected = getProject().resolveFile("input/concatfilter.test"); 84 File result = getProject().resolveFile("result/concat.FilterReaderNoArgs.test"); 85 assertTrue("testFilterReaderNoArgs: Result not like expected", fu.contentEquals(expected, result)); 86 } 87 88 public void testFilterReaderBefore() { 89 doTest("testFilterReaderPrepend", FILE_PREPEND_WITH, FILE_APPEND); 90 } 91 92 public void testFilterReaderAfter() { 93 doTest("testFilterReaderAppend", FILE_PREPEND, FILE_APPEND_WITH); 94 } 95 96 public void testFilterReaderBeforeAfter() { 97 doTest("testFilterReaderPrependAppend", FILE_PREPEND_WITH, FILE_APPEND_WITH); 98 } 99 100 public void testConcatFilter() { 101 doTest("testConcatFilter", FILE_PREPEND, FILE_APPEND); 102 } 103 104 public void testConcatFilterBefore() { 105 doTest("testConcatFilterPrepend", FILE_PREPEND_WITH, FILE_APPEND); 106 } 107 108 public void testConcatFilterAfter() { 109 doTest("testConcatFilterAppend", FILE_PREPEND, FILE_APPEND_WITH); 110 } 111 112 public void testConcatFilterBeforeAfter() { 113 doTest("testConcatFilterPrependAppend", FILE_PREPEND_WITH, FILE_APPEND_WITH); 114 } 115 116 117 125 protected void doTest(String target, String expectedStart, String expectedEnd) { 126 executeTarget(target); 127 String resultContent = read("result/concat." + target.substring(4) + ".test"); 128 assertTrue("First 5 lines differs.", resultContent.startsWith(expectedStart)); 129 assertTrue("Last 5 lines differs.", resultContent.endsWith(expectedEnd)); 130 } 131 132 133 140 protected String read(String filename) { 141 String content = null; 142 try { 143 File file = getProject().resolveFile(filename); 144 java.io.FileReader rdr = new java.io.FileReader (file); 145 content = fu.readFully(rdr); 146 rdr.close(); 147 rdr = null; 148 } catch (Exception e) { 149 e.printStackTrace(); 150 } 151 return content; 152 } 153 154 } 155 | Popular Tags |