1 17 18 package org.apache.tools.ant; 19 20 import java.io.File ; 21 import java.io.PrintStream ; 22 import java.net.URL ; 23 24 import junit.framework.TestCase; 25 26 39 public abstract class BuildFileTestA extends TestCase { 40 41 protected Project project; 42 43 private StringBuffer logBuffer; 44 private StringBuffer fullLogBuffer; 45 private StringBuffer outBuffer; 46 private StringBuffer errBuffer; 47 private BuildException buildException; 48 49 54 public BuildFileTestA(String name) { 55 super(name); 56 } 57 58 64 protected void expectBuildException(String target, String cause) { 65 expectSpecificBuildException(target, cause, null); 66 } 67 68 72 protected void expectLog(String target, String log) { 73 executeTarget(target); 74 String realLog = getLog(); 75 assertEquals(log, realLog); 76 } 77 78 81 82 protected void assertLogContaining(String substring) { 83 String realLog = getLog(); 84 assertTrue("expecting log to contain \"" + substring + "\" log was \"" 85 + realLog + "\"", 86 realLog.indexOf(substring) >= 0); 87 } 88 89 93 protected void expectLogContaining(String target, String log) { 94 executeTarget(target); 95 assertLogContaining(log); 96 } 97 98 105 protected String getLog() { 106 return logBuffer.toString(); 107 } 108 109 113 protected void expectDebuglog(String target, String log) { 114 executeTarget(target); 115 String realLog = getFullLog(); 116 assertEquals(log, realLog); 117 } 118 119 126 protected String getFullLog() { 127 return fullLogBuffer.toString(); 128 } 129 130 136 137 protected void expectOutput(String target, String output) { 138 executeTarget(target); 139 String realOutput = getOutput(); 140 assertEquals(output, realOutput.trim()); 141 } 142 143 150 151 protected void expectOutputAndError(String target, String output, String error) { 152 executeTarget(target); 153 String realOutput = getOutput(); 154 assertEquals(output, realOutput); 155 String realError = getError(); 156 assertEquals(error, realError); 157 } 158 159 protected String getOutput() { 160 return cleanBuffer(outBuffer); 161 } 162 163 protected String getError() { 164 return cleanBuffer(errBuffer); 165 } 166 167 protected BuildException getBuildException() { 168 return buildException; 169 } 170 171 private String cleanBuffer(StringBuffer buffer) { 172 StringBuffer cleanedBuffer = new StringBuffer (); 173 boolean cr = false; 174 for (int i = 0; i < buffer.length(); i++) { 175 char ch = buffer.charAt(i); 176 if (ch == '\r') { 177 cr = true; 178 continue; 179 } 180 181 if (!cr) { 182 cleanedBuffer.append(ch); 183 } else { 184 cleanedBuffer.append(ch); 185 } 186 } 187 return cleanedBuffer.toString(); 188 } 189 190 195 protected void configureProject(String filename) throws BuildException { 196 configureProject(filename, Project.MSG_DEBUG); 197 } 198 199 204 protected void configureProject(String filename, int logLevel) 205 throws BuildException { 206 logBuffer = new StringBuffer (); 207 fullLogBuffer = new StringBuffer (); 208 project = new Project(); 209 project.init(); 210 project.setUserProperty( "ant.file" , new File (filename).getAbsolutePath() ); 211 project.addBuildListener(new AntTestListener(logLevel)); 212 ProjectHelper.configureProject(project, new File (filename)); 213 } 214 215 220 protected void executeTarget(String targetName) { 221 PrintStream sysOut = System.out; 222 PrintStream sysErr = System.err; 223 try { 224 sysOut.flush(); 225 sysErr.flush(); 226 outBuffer = new StringBuffer (); 227 PrintStream out = new PrintStream (new AntOutputStream(outBuffer)); 228 System.setOut(out); 229 errBuffer = new StringBuffer (); 230 PrintStream err = new PrintStream (new AntOutputStream(errBuffer)); 231 System.setErr(err); 232 logBuffer = new StringBuffer (); 233 fullLogBuffer = new StringBuffer (); 234 buildException = null; 235 project.executeTarget(targetName); 236 } finally { 237 System.setOut(sysOut); 238 System.setErr(sysErr); 239 } 240 241 } 242 243 248 protected Project getProject() { 249 return project; 250 } 251 252 256 protected File getProjectDir() { 257 return project.getBaseDir(); 258 } 259 260 268 protected void expectSpecificBuildException(String target, String cause, String msg) { 269 try { 270 executeTarget(target); 271 } catch (org.apache.tools.ant.BuildException ex) { 272 buildException = ex; 273 if ((null != msg) && (!ex.getMessage().equals(msg))) { 274 fail("Should throw BuildException because '" + cause 275 + "' with message '" + msg 276 + "' (actual message '" + ex.getMessage() + "' instead)"); 277 } 278 return; 279 } 280 fail("Should throw BuildException because: " + cause); 281 } 282 283 291 protected void expectBuildExceptionContaining(String target, String cause, String contains) { 292 try { 293 executeTarget(target); 294 } catch (org.apache.tools.ant.BuildException ex) { 295 buildException = ex; 296 if ((null != contains) && (ex.getMessage().indexOf(contains) == -1)) { 297 fail("Should throw BuildException because '" + cause + "' with message containing '" + contains + "' (actual message '" + ex.getMessage() + "' instead)"); 298 } 299 return; 300 } 301 fail("Should throw BuildException because: " + cause); 302 } 303 304 305 312 313 protected void expectPropertySet(String target, String property, String value) { 314 executeTarget(target); 315 assertPropertyEquals(property, value); 316 } 317 318 323 protected void assertPropertyEquals(String property, String value) { 324 String result = project.getProperty(property); 325 assertEquals("property " + property,value,result); 326 } 327 328 332 protected void assertPropertySet(String property) { 333 assertPropertyEquals(property, "true"); 334 } 335 336 340 protected void assertPropertyUnset(String property) { 341 assertPropertyEquals(property, null); 342 } 343 344 345 351 protected void expectPropertySet(String target, String property) { 352 expectPropertySet(target, property, "true"); 353 } 354 355 356 361 protected void expectPropertyUnset(String target, String property) { 362 expectPropertySet(target, property, null); 363 } 364 365 372 protected URL getResource(String resource){ 373 URL url = getClass().getResource(resource); 374 assertNotNull("Could not find resource :" + resource, url); 375 return url; 376 } 377 378 381 private static class AntOutputStream extends java.io.OutputStream { 382 private StringBuffer buffer; 383 384 public AntOutputStream( StringBuffer buffer ) { 385 this.buffer = buffer; 386 } 387 388 public void write(int b) { 389 buffer.append((char)b); 390 } 391 } 392 393 396 private class AntTestListener implements BuildListener { 397 private int logLevel; 398 399 403 public AntTestListener(int logLevel) { 404 this.logLevel = logLevel; 405 } 406 407 410 public void buildStarted(BuildEvent event) { 411 } 412 413 419 public void buildFinished(BuildEvent event) { 420 } 421 422 427 public void targetStarted(BuildEvent event) { 428 } 430 431 437 public void targetFinished(BuildEvent event) { 438 } 440 441 446 public void taskStarted(BuildEvent event) { 447 } 449 450 456 public void taskFinished(BuildEvent event) { 457 } 459 460 466 public void messageLogged(BuildEvent event) { 467 if (event.getPriority() > logLevel) { 468 return; 470 } 471 472 if (event.getPriority() == Project.MSG_INFO || 473 event.getPriority() == Project.MSG_WARN || 474 event.getPriority() == Project.MSG_ERR) { 475 logBuffer.append(event.getMessage()); 476 logBuffer.append( "\n" ); 477 } 478 fullLogBuffer.append(event.getMessage()); 479 fullLogBuffer.append( "\n" ); 480 } 481 } 482 483 484 } 485 | Popular Tags |