1 17 18 package org.apache.tools.ant; 19 20 import junit.framework.TestCase; 21 import org.apache.tools.ant.Project; 22 import java.io.File ; 23 import java.io.PrintStream ; 24 import java.net.URL ; 25 26 34 public abstract class BuildFileTest extends TestCase { 35 36 protected Project project; 37 38 private StringBuffer logBuffer; 39 private StringBuffer fullLogBuffer; 40 private StringBuffer outBuffer; 41 private StringBuffer errBuffer; 42 private BuildException buildException; 43 44 49 public BuildFileTest(String name) { 50 super(name); 51 } 52 53 59 protected void expectBuildException(String target, String cause) { 60 expectSpecificBuildException(target, cause, null); 61 } 62 63 67 protected void expectLog(String target, String log) { 68 executeTarget(target); 69 String realLog = getLog(); 70 assertEquals(log, realLog); 71 } 72 73 76 77 protected void assertLogContaining(String substring) { 78 String realLog = getLog(); 79 assertTrue("expecting log to contain \"" + substring + "\" log was \"" 80 + realLog + "\"", 81 realLog.indexOf(substring) >= 0); 82 } 83 84 88 protected void expectLogContaining(String target, String log) { 89 executeTarget(target); 90 assertLogContaining(log); 91 } 92 93 100 protected String getLog() { 101 return logBuffer.toString(); 102 } 103 104 108 protected void expectDebuglog(String target, String log) { 109 executeTarget(target); 110 String realLog = getFullLog(); 111 assertEquals(log, realLog); 112 } 113 114 121 protected String getFullLog() { 122 return fullLogBuffer.toString(); 123 } 124 125 131 132 protected void expectOutput(String target, String output) { 133 executeTarget(target); 134 String realOutput = getOutput(); 135 assertEquals(output, realOutput.trim()); 136 } 137 138 145 146 protected void expectOutputAndError(String target, String output, String error) { 147 executeTarget(target); 148 String realOutput = getOutput(); 149 assertEquals(output, realOutput); 150 String realError = getError(); 151 assertEquals(error, realError); 152 } 153 154 protected String getOutput() { 155 return cleanBuffer(outBuffer); 156 } 157 158 protected String getError() { 159 return cleanBuffer(errBuffer); 160 } 161 162 protected BuildException getBuildException() { 163 return buildException; 164 } 165 166 private String cleanBuffer(StringBuffer buffer) { 167 StringBuffer cleanedBuffer = new StringBuffer (); 168 boolean cr = false; 169 for (int i = 0; i < buffer.length(); i++) { 170 char ch = buffer.charAt(i); 171 if (ch == '\r') { 172 cr = true; 173 continue; 174 } 175 176 if (!cr) { 177 cleanedBuffer.append(ch); 178 } else { 179 cleanedBuffer.append(ch); 180 } 181 } 182 return cleanedBuffer.toString(); 183 } 184 185 190 protected void configureProject(String filename) throws BuildException { 191 configureProject(filename, Project.MSG_DEBUG); 192 } 193 194 199 protected void configureProject(String filename, int logLevel) 200 throws BuildException { 201 logBuffer = new StringBuffer (); 202 fullLogBuffer = new StringBuffer (); 203 project = new Project(); 204 project.init(); 205 project.setUserProperty( "ant.file" , new File (filename).getAbsolutePath() ); 206 project.addBuildListener(new AntTestListener(logLevel)); 207 ProjectHelper.configureProject(project, new File (filename)); 208 } 209 210 215 protected void executeTarget(String targetName) { 216 PrintStream sysOut = System.out; 217 PrintStream sysErr = System.err; 218 try { 219 sysOut.flush(); 220 sysErr.flush(); 221 outBuffer = new StringBuffer (); 222 PrintStream out = new PrintStream (new AntOutputStream(outBuffer)); 223 System.setOut(out); 224 errBuffer = new StringBuffer (); 225 PrintStream err = new PrintStream (new AntOutputStream(errBuffer)); 226 System.setErr(err); 227 logBuffer = new StringBuffer (); 228 fullLogBuffer = new StringBuffer (); 229 buildException = null; 230 project.executeTarget(targetName); 231 } finally { 232 System.setOut(sysOut); 233 System.setErr(sysErr); 234 } 235 236 } 237 238 243 protected Project getProject() { 244 return project; 245 } 246 247 251 protected File getProjectDir() { 252 return project.getBaseDir(); 253 } 254 255 263 protected void expectSpecificBuildException(String target, String cause, String msg) { 264 try { 265 executeTarget(target); 266 } catch (org.apache.tools.ant.BuildException ex) { 267 buildException = ex; 268 if ((null != msg) && (!ex.getMessage().equals(msg))) { 269 fail("Should throw BuildException because '" + cause 270 + "' with message '" + msg 271 + "' (actual message '" + ex.getMessage() + "' instead)"); 272 } 273 return; 274 } 275 fail("Should throw BuildException because: " + cause); 276 } 277 278 286 protected void expectBuildExceptionContaining(String target, String cause, String contains) { 287 try { 288 executeTarget(target); 289 } catch (org.apache.tools.ant.BuildException ex) { 290 buildException = ex; 291 if ((null != contains) && (ex.getMessage().indexOf(contains) == -1)) { 292 fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)"); 293 } 294 return; 295 } 296 fail("Should throw BuildException because: " + cause); 297 } 298 299 300 307 308 protected void expectPropertySet(String target, String property, String value) { 309 executeTarget(target); 310 assertPropertyEquals(property, value); 311 } 312 313 318 protected void assertPropertyEquals(String property, String value) { 319 String result = project.getProperty(property); 320 assertEquals("property " + property,value,result); 321 } 322 323 327 protected void assertPropertySet(String property) { 328 assertPropertyEquals(property, "true"); 329 } 330 331 335 protected void assertPropertyUnset(String property) { 336 assertPropertyEquals(property, null); 337 } 338 339 340 346 protected void expectPropertySet(String target, String property) { 347 expectPropertySet(target, property, "true"); 348 } 349 350 351 356 protected void expectPropertyUnset(String target, String property) { 357 expectPropertySet(target, property, null); 358 } 359 360 367 protected URL getResource(String resource){ 368 URL url = getClass().getResource(resource); 369 assertNotNull("Could not find resource :" + resource, url); 370 return url; 371 } 372 373 376 private static class AntOutputStream extends java.io.OutputStream { 377 private StringBuffer buffer; 378 379 public AntOutputStream( StringBuffer buffer ) { 380 this.buffer = buffer; 381 } 382 383 public void write(int b) { 384 buffer.append((char)b); 385 } 386 } 387 388 391 private class AntTestListener implements BuildListener { 392 private int logLevel; 393 394 398 public AntTestListener(int logLevel) { 399 this.logLevel = logLevel; 400 } 401 402 405 public void buildStarted(BuildEvent event) { 406 } 407 408 414 public void buildFinished(BuildEvent event) { 415 } 416 417 422 public void targetStarted(BuildEvent event) { 423 } 425 426 432 public void targetFinished(BuildEvent event) { 433 } 435 436 441 public void taskStarted(BuildEvent event) { 442 } 444 445 451 public void taskFinished(BuildEvent event) { 452 } 454 455 461 public void messageLogged(BuildEvent event) { 462 if (event.getPriority() > logLevel) { 463 return; 465 } 466 467 if (event.getPriority() == Project.MSG_INFO || 468 event.getPriority() == Project.MSG_WARN || 469 event.getPriority() == Project.MSG_ERR) { 470 logBuffer.append(event.getMessage()); 471 } 472 fullLogBuffer.append(event.getMessage()); 473 474 } 475 } 476 477 478 } 479 | Popular Tags |