1 20 package org.apache.cactus.integration.ant; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import junit.framework.AssertionFailedError; 34 import junit.framework.TestCase; 35 36 import org.apache.tools.ant.BuildEvent; 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.BuildListener; 39 import org.apache.tools.ant.Project; 40 import org.apache.tools.ant.ProjectHelper; 41 import org.apache.tools.ant.Target; 42 43 48 public abstract class AntTestCase extends TestCase implements BuildListener 49 { 50 52 55 private Project project; 56 57 60 private String buildFile; 61 62 67 private Map log = new HashMap (); 68 69 73 private FileOutputStream outputStream; 74 75 78 private Set executedTargets = new HashSet (); 79 80 82 85 public AntTestCase(String theBuildFile) 86 { 87 this.buildFile = theBuildFile; 88 89 String outputString = System.getProperty("logfile"); 90 if (outputString != null) 91 { 92 try 93 { 94 this.outputStream = new FileOutputStream (outputString); 95 } 96 catch (FileNotFoundException e) 97 { 98 } 100 } 101 } 102 103 105 108 public final void buildStarted(BuildEvent theEvent) 109 { 110 } 111 112 115 public final void buildFinished(BuildEvent theEvent) 116 { 117 } 118 119 122 public final void targetStarted(BuildEvent theEvent) 123 { 124 } 125 126 129 public final void targetFinished(BuildEvent theEvent) 130 { 131 this.executedTargets.add(theEvent.getTarget().getName()); 132 } 133 134 137 public final void taskStarted(BuildEvent theEvent) 138 { 139 } 140 141 144 public final void taskFinished(BuildEvent theEvent) 145 { 146 } 147 148 151 public final void messageLogged(BuildEvent theEvent) 152 { 153 StringBuffer buffer = (StringBuffer ) 154 log.get(new Integer (theEvent.getPriority())); 155 if (buffer == null) 156 { 157 buffer = new StringBuffer (); 158 log.put(new Integer (theEvent.getPriority()), buffer); 159 } 160 buffer.append(theEvent.getMessage()).append("\n"); 161 if (this.outputStream != null) 162 { 163 try 164 { 165 this.outputStream.write(theEvent.getMessage().getBytes()); 166 this.outputStream.write('\n'); 167 } 168 catch (IOException e) 169 { 170 } 172 } 173 } 174 175 177 183 protected void setUp() throws Exception 184 { 185 this.project = new Project(); 186 this.project.addBuildListener(this); 187 this.project.init(); 188 File buildFile = getBuildFile(this.buildFile); 189 this.project.setUserProperty("ant.file", buildFile.getAbsolutePath()); 190 ProjectHelper helper = ProjectHelper.getProjectHelper(); 191 helper.parse(this.project, buildFile); 192 if (getProject().getTargets().get("setUp") != null) 193 { 194 getProject().executeTarget("setUp"); 195 } 196 } 197 198 201 protected void tearDown() throws Exception 202 { 203 if (getProject().getTargets().get("tearDown") != null) 204 { 205 try 206 { 207 getProject().executeTarget("tearDown"); 208 } 209 catch (BuildException be) 210 { 211 } 213 } 214 } 215 216 218 223 protected String getLog(int theLogLevel) 224 { 225 String result = null; 226 StringBuffer buffer = (StringBuffer ) this.log.get( 227 new Integer (theLogLevel)); 228 if (buffer != null) 229 { 230 result = buffer.toString(); 231 } 232 return result; 233 } 234 235 242 protected final void assertMessageLogged(String theMessage, int theLogLevel) 243 throws IOException 244 { 245 String buffer = getLog(theLogLevel); 246 if (buffer != null) 247 { 248 BufferedReader reader = 249 new BufferedReader (new StringReader (buffer)); 250 String line = null; 251 while ((line = reader.readLine()) != null) 252 { 253 if (line.equals(theMessage)) 254 { 255 return; 256 } 257 } 258 } 259 throw new AssertionFailedError( 260 "Expected log message '" + theMessage + "'"); 261 } 262 263 271 protected final void assertMessageLoggedContaining(String theSubstring, 272 int theLogLevel) 273 throws IOException 274 { 275 StringBuffer buffer = (StringBuffer ) log.get(new Integer (theLogLevel)); 276 if (buffer != null) 277 { 278 BufferedReader reader = 279 new BufferedReader (new StringReader (buffer.toString())); 280 String line = null; 281 while ((line = reader.readLine()) != null) 282 { 283 if (line.indexOf(theSubstring) >= 0) 284 { 285 return; 286 } 287 } 288 } 289 throw new AssertionFailedError( 290 "Expected log message containing '" + theSubstring + "'"); 291 } 292 293 298 protected final void assertTargetExecuted(String theName) 299 { 300 assertTrue("Target '" + theName + "' should have been executed", 301 this.executedTargets.contains(theName)); 302 } 303 304 308 protected final void executeTestTarget() 309 { 310 this.project.executeTarget(getName()); 311 } 312 313 318 protected final Project getProject() 319 { 320 return this.project; 321 } 322 323 328 protected final File getProjectDir() 329 { 330 return this.project.getBaseDir(); 331 } 332 333 339 protected final Target getTestTarget() 340 { 341 return (Target) getProject().getTargets().get(getName()); 342 } 343 344 346 354 private File getBuildFile(String theFileName) 355 { 356 String testInputDirProperty = System.getProperty("testinput.dir"); 357 assertTrue("The system property 'testinput.dir' must be set", 358 testInputDirProperty != null); 359 File testInputDir = new File (testInputDirProperty); 360 assertTrue("The system property 'testinput.dir' must point to an " 361 + "existing directory", testInputDir.isDirectory()); 362 File buildFile = new File (testInputDir, theFileName); 363 assertTrue("The test input " + theFileName + " does not exist", 364 buildFile.exists()); 365 return buildFile; 366 } 367 368 } 369
| Popular Tags
|