1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import org.apache.tools.ant.BuildFileTest; 21 import org.apache.tools.ant.util.FileUtils; 22 23 import java.io.File ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.io.Reader ; 27 28 33 public class ConcatTest 34 extends BuildFileTest { 35 36 39 private static final String tempFile = "concat.tmp"; 40 41 44 private static final String tempFile2 = "concat.tmp.2"; 45 46 49 public ConcatTest(String name) { 50 super(name); 51 } 52 53 57 public void setUp() { 58 configureProject("src/etc/testcases/taskdefs/concat.xml"); 59 } 60 61 65 public void tearDown() { 66 executeTarget("cleanup"); 67 } 68 69 72 public void test1() { 73 expectBuildException("test1", "Insufficient information."); 74 } 75 76 79 public void test2() { 80 expectBuildException("test2", "Invalid destination file."); 81 } 82 83 86 public void test3() { 87 88 File file = new File (getProjectDir(), tempFile); 89 if (file.exists()) { 90 file.delete(); 91 } 92 93 executeTarget("test3"); 94 95 assertTrue(file.exists()); 96 } 97 98 101 public void test4() { 102 test3(); 103 104 File file = new File (getProjectDir(), tempFile); 105 final long origSize = file.length(); 106 107 executeTarget("test4"); 108 109 File file2 = new File (getProjectDir(), tempFile2); 110 final long newSize = file2.length(); 111 112 assertEquals(origSize * 3, newSize); 113 } 114 115 118 public void test5() { 119 expectLog("test5", "Hello, World!"); 120 } 121 122 public void test6() { 123 String filename = "src/etc/testcases/taskdefs/thisfiledoesnotexist" 124 .replace('/', File.separatorChar); 125 expectLogContaining("test6", filename +" does not exist."); 126 } 127 128 public void testConcatNoNewline() { 129 expectLog("testConcatNoNewline", "ab"); 130 } 131 132 public void testConcatNoNewlineEncoding() { 133 expectLog("testConcatNoNewlineEncoding", "ab"); 134 } 135 136 public void testPath() { 137 test3(); 138 139 File file = new File (getProjectDir(), tempFile); 140 final long origSize = file.length(); 141 142 executeTarget("testPath"); 143 144 File file2 = new File (getProjectDir(), tempFile2); 145 final long newSize = file2.length(); 146 147 assertEquals(origSize, newSize); 148 149 } 150 public void testAppend() { 151 test3(); 152 153 File file = new File (getProjectDir(), tempFile); 154 final long origSize = file.length(); 155 156 executeTarget("testAppend"); 157 158 File file2 = new File (getProjectDir(), tempFile2); 159 final long newSize = file2.length(); 160 161 assertEquals(origSize*2, newSize); 162 163 } 164 165 public void testFilter() { 166 executeTarget("testfilter"); 167 assertTrue(getLog().indexOf("REPLACED") > -1); 168 } 169 170 public void testNoOverwrite() { 171 executeTarget("testnooverwrite"); 172 File file2 = new File (getProjectDir(), tempFile2); 173 long size = file2.length(); 174 assertEquals(size, 0); 175 } 176 177 public void testheaderfooter() { 178 test3(); 179 expectLog("testheaderfooter", "headerHello, World!footer"); 180 } 181 182 public void testfileheader() { 183 test3(); 184 expectLog("testfileheader", "Hello, World!Hello, World!"); 185 } 186 187 190 public void testsame() { 191 expectBuildException("samefile", "output file same as input"); 192 } 193 194 197 public void testfilterinline() { 198 executeTarget("testfilterinline"); 199 assertTrue(getLog().indexOf("REPLACED") > -1); 200 } 201 202 205 public void testmultireader() { 206 executeTarget("testmultireader"); 207 assertTrue(getLog().indexOf("Bye") > -1); 208 assertTrue(getLog().indexOf("Hello") == -1); 209 } 210 213 public void testfixlastline() 214 throws IOException 215 { 216 expectFileContains( 217 "testfixlastline", "concat.line4", 218 "end of line" + System.getProperty("line.separator") 219 + "This has"); 220 } 221 222 225 public void testfixlastlineeol() 226 throws IOException 227 { 228 expectFileContains( 229 "testfixlastlineeol", "concat.linecr", 230 "end of line\rThis has"); 231 } 232 233 237 private String getFileString(String filename) 238 throws IOException 239 { 240 Reader r = null; 241 try { 242 r = new FileReader (getProject().resolveFile(filename)); 243 return FileUtils.newFileUtils().readFully(r); 244 } 245 finally { 246 try {r.close();} catch (Throwable ignore) {} 247 } 248 249 } 250 251 private String getFileString(String target, String filename) 252 throws IOException 253 { 254 executeTarget(target); 255 return getFileString(filename); 256 } 257 258 private void expectFileContains( 259 String target, String filename, String contains) 260 throws IOException 261 { 262 String content = getFileString(target, filename); 263 assertTrue( 264 "expecting file " + filename + " to contain " + 265 contains + 266 " but got " + content, content.indexOf(contains) > -1); 267 } 268 269 public void testTranscoding() throws IOException { 270 executeTarget("testTranscoding"); 271 FileUtils fileUtils = FileUtils.newFileUtils(); 272 File f1 = getProject().resolveFile("copy/expected/utf-8"); 273 File f2 = getProject().resolveFile("concat.utf8"); 274 assertTrue(fileUtils.contentEquals(f1, f2)); 275 } 276 } 277 | Popular Tags |