|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AntTestCase.java | 50% | 80% | 68.4% | 70.9% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2003 The Apache Software Foundation.
|
|
| 5 |
*
|
|
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 7 |
* you may not use this file except in compliance with the License.
|
|
| 8 |
* You may obtain a copy of the License at
|
|
| 9 |
*
|
|
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 11 |
*
|
|
| 12 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 15 |
* See the License for the specific language governing permissions and
|
|
| 16 |
* limitations under the License.
|
|
| 17 |
*
|
|
| 18 |
* ========================================================================
|
|
| 19 |
*/
|
|
| 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 |
/**
|
|
| 44 |
* An AntTestCase is a TestCase specialization for unit testing Ant tasks.
|
|
| 45 |
*
|
|
| 46 |
* @version $Id: AntTestCase.java 239003 2004-05-31 20:05:27Z vmassol $
|
|
| 47 |
*/
|
|
| 48 |
public abstract class AntTestCase extends TestCase implements BuildListener |
|
| 49 |
{
|
|
| 50 |
// Instance Variables ------------------------------------------------------
|
|
| 51 |
|
|
| 52 |
/**
|
|
| 53 |
* The Ant project.
|
|
| 54 |
*/
|
|
| 55 |
private Project project;
|
|
| 56 |
|
|
| 57 |
/**
|
|
| 58 |
* The name of the test build file.
|
|
| 59 |
*/
|
|
| 60 |
private String buildFile;
|
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Buffer containing all messages logged by Ant. Keys correspond to the
|
|
| 64 |
* message priority as <code>java.lang.Integer</code>, the values are are
|
|
| 65 |
* <code>java.lang.StringBuffer</code>s containing the actual log messages.
|
|
| 66 |
*/
|
|
| 67 |
private Map log = new HashMap(); |
|
| 68 |
|
|
| 69 |
/**
|
|
| 70 |
* File where to log Ant outputs for debugging. If no file location has
|
|
| 71 |
* been passed, there will be no file logging.
|
|
| 72 |
*/
|
|
| 73 |
private FileOutputStream outputStream;
|
|
| 74 |
|
|
| 75 |
/**
|
|
| 76 |
* The targets the have been executed.
|
|
| 77 |
*/
|
|
| 78 |
private Set executedTargets = new HashSet(); |
|
| 79 |
|
|
| 80 |
// Constructors ------------------------------------------------------------
|
|
| 81 |
|
|
| 82 |
/**
|
|
| 83 |
* @param theBuildFile The Ant build file corresponding to the test fixture
|
|
| 84 |
*/
|
|
| 85 | 43 |
public AntTestCase(String theBuildFile)
|
| 86 |
{
|
|
| 87 | 43 |
this.buildFile = theBuildFile;
|
| 88 |
|
|
| 89 | 43 |
String outputString = System.getProperty("logfile");
|
| 90 | 43 |
if (outputString != null) |
| 91 |
{
|
|
| 92 | 43 |
try
|
| 93 |
{
|
|
| 94 | 43 |
this.outputStream = new FileOutputStream(outputString); |
| 95 |
} |
|
| 96 |
catch (FileNotFoundException e)
|
|
| 97 |
{
|
|
| 98 |
// Silently ignore error when creating output stream
|
|
| 99 |
} |
|
| 100 |
} |
|
| 101 |
} |
|
| 102 |
|
|
| 103 |
// BuildListener Implementation --------------------------------------------
|
|
| 104 |
|
|
| 105 |
/**
|
|
| 106 |
* @see BuildListener#buildStarted
|
|
| 107 |
*/
|
|
| 108 | 0 |
public final void buildStarted(BuildEvent theEvent) |
| 109 |
{
|
|
| 110 |
} |
|
| 111 |
|
|
| 112 |
/**
|
|
| 113 |
* @see BuildListener#buildFinished
|
|
| 114 |
*/
|
|
| 115 | 0 |
public final void buildFinished(BuildEvent theEvent) |
| 116 |
{
|
|
| 117 |
} |
|
| 118 |
|
|
| 119 |
/**
|
|
| 120 |
* @see BuildListener#targetStarted
|
|
| 121 |
*/
|
|
| 122 | 86 |
public final void targetStarted(BuildEvent theEvent) |
| 123 |
{
|
|
| 124 |
} |
|
| 125 |
|
|
| 126 |
/**
|
|
| 127 |
* @see BuildListener#targetFinished
|
|
| 128 |
*/
|
|
| 129 | 86 |
public final void targetFinished(BuildEvent theEvent) |
| 130 |
{
|
|
| 131 | 86 |
this.executedTargets.add(theEvent.getTarget().getName());
|
| 132 |
} |
|
| 133 |
|
|
| 134 |
/**
|
|
| 135 |
* @see BuildListener#taskStarted
|
|
| 136 |
*/
|
|
| 137 | 112 |
public final void taskStarted(BuildEvent theEvent) |
| 138 |
{
|
|
| 139 |
} |
|
| 140 |
|
|
| 141 |
/**
|
|
| 142 |
* @see BuildListener#taskFinished
|
|
| 143 |
*/
|
|
| 144 | 112 |
public final void taskFinished(BuildEvent theEvent) |
| 145 |
{
|
|
| 146 |
} |
|
| 147 |
|
|
| 148 |
/**
|
|
| 149 |
* @see BuildListener#messageLogged
|
|
| 150 |
*/
|
|
| 151 | 2313 |
public final void messageLogged(BuildEvent theEvent) |
| 152 |
{
|
|
| 153 | 2313 |
StringBuffer buffer = (StringBuffer) |
| 154 |
log.get(new Integer(theEvent.getPriority()));
|
|
| 155 | 2313 |
if (buffer == null) |
| 156 |
{
|
|
| 157 | 139 |
buffer = new StringBuffer();
|
| 158 | 139 |
log.put(new Integer(theEvent.getPriority()), buffer);
|
| 159 |
} |
|
| 160 | 2313 |
buffer.append(theEvent.getMessage()).append("\n");
|
| 161 | 2313 |
if (this.outputStream != null) |
| 162 |
{
|
|
| 163 | 2313 |
try
|
| 164 |
{
|
|
| 165 | 2313 |
this.outputStream.write(theEvent.getMessage().getBytes());
|
| 166 | 2313 |
this.outputStream.write('\n');
|
| 167 |
} |
|
| 168 |
catch (IOException e)
|
|
| 169 |
{
|
|
| 170 |
// Silently ignore log error
|
|
| 171 |
} |
|
| 172 |
} |
|
| 173 |
} |
|
| 174 |
|
|
| 175 |
// TestCase Implementation -------------------------------------------------
|
|
| 176 |
|
|
| 177 |
/**
|
|
| 178 |
* Initializes a fresh Ant project with a target named after the name of the
|
|
| 179 |
* test case.
|
|
| 180 |
*
|
|
| 181 |
* @see junit.framework.TestCase#setUp()
|
|
| 182 |
*/
|
|
| 183 | 43 |
protected void setUp() throws Exception |
| 184 |
{
|
|
| 185 | 43 |
this.project = new Project(); |
| 186 | 43 |
this.project.addBuildListener(this); |
| 187 | 43 |
this.project.init();
|
| 188 | 43 |
File buildFile = getBuildFile(this.buildFile);
|
| 189 | 43 |
this.project.setUserProperty("ant.file", buildFile.getAbsolutePath()); |
| 190 | 43 |
ProjectHelper helper = ProjectHelper.getProjectHelper(); |
| 191 | 43 |
helper.parse(this.project, buildFile);
|
| 192 | 43 |
if (getProject().getTargets().get("setUp") != null) |
| 193 |
{
|
|
| 194 | 24 |
getProject().executeTarget("setUp");
|
| 195 |
} |
|
| 196 |
} |
|
| 197 |
|
|
| 198 |
/**
|
|
| 199 |
* @see junit.framework.TestCase#tearDown()
|
|
| 200 |
*/
|
|
| 201 | 43 |
protected void tearDown() throws Exception |
| 202 |
{
|
|
| 203 | 43 |
if (getProject().getTargets().get("tearDown") != null) |
| 204 |
{
|
|
| 205 | 24 |
try
|
| 206 |
{
|
|
| 207 | 24 |
getProject().executeTarget("tearDown");
|
| 208 |
} |
|
| 209 |
catch (BuildException be)
|
|
| 210 |
{
|
|
| 211 |
// exception has been logged
|
|
| 212 |
} |
|
| 213 |
} |
|
| 214 |
} |
|
| 215 |
|
|
| 216 |
// Protected Methods -------------------------------------------------------
|
|
| 217 |
|
|
| 218 |
/**
|
|
| 219 |
* @return the buffer containing all Ant logs for the specified
|
|
| 220 |
* log level
|
|
| 221 |
* @param theLogLevel The log level of the message
|
|
| 222 |
*/
|
|
| 223 | 5 |
protected String getLog(int theLogLevel) |
| 224 |
{
|
|
| 225 | 5 |
String result = null;
|
| 226 | 5 |
StringBuffer buffer = (StringBuffer) this.log.get(
|
| 227 |
new Integer(theLogLevel));
|
|
| 228 | 5 |
if (buffer != null) |
| 229 |
{
|
|
| 230 | 5 |
result = buffer.toString(); |
| 231 |
} |
|
| 232 | 5 |
return result;
|
| 233 |
} |
|
| 234 |
|
|
| 235 |
/**
|
|
| 236 |
* Asserts that a specific message has been logged at a specific log level.
|
|
| 237 |
*
|
|
| 238 |
* @param theMessage The message to check for
|
|
| 239 |
* @param theLogLevel The log level of the message
|
|
| 240 |
* @throws IOException If an error occurred reading the log buffer
|
|
| 241 |
*/
|
|
| 242 | 5 |
protected final void assertMessageLogged(String theMessage, int theLogLevel) |
| 243 |
throws IOException
|
|
| 244 |
{
|
|
| 245 | 5 |
String buffer = getLog(theLogLevel); |
| 246 | 5 |
if (buffer != null) |
| 247 |
{
|
|
| 248 | 5 |
BufferedReader reader = |
| 249 |
new BufferedReader(new StringReader(buffer)); |
|
| 250 | 5 |
String line = null;
|
| 251 | ? |
while ((line = reader.readLine()) != null) |
| 252 |
{
|
|
| 253 | 55 |
if (line.equals(theMessage))
|
| 254 |
{
|
|
| 255 | 5 |
return;
|
| 256 |
} |
|
| 257 |
} |
|
| 258 |
} |
|
| 259 | 0 |
throw new AssertionFailedError( |
| 260 |
"Expected log message '" + theMessage + "'"); |
|
| 261 |
} |
|
| 262 |
|
|
| 263 |
/**
|
|
| 264 |
* Asserts that a message containing the specified substring has been logged
|
|
| 265 |
* at a specific log level.
|
|
| 266 |
*
|
|
| 267 |
* @param theSubstring The substring to check for
|
|
| 268 |
* @param theLogLevel The log level of the message
|
|
| 269 |
* @throws IOException If an error occurred reading the log buffer
|
|
| 270 |
*/
|
|
| 271 | 0 |
protected final void assertMessageLoggedContaining(String theSubstring, |
| 272 |
int theLogLevel)
|
|
| 273 |
throws IOException
|
|
| 274 |
{
|
|
| 275 | 0 |
StringBuffer buffer = (StringBuffer) log.get(new Integer(theLogLevel));
|
| 276 | 0 |
if (buffer != null) |
| 277 |
{
|
|
| 278 | 0 |
BufferedReader reader = |
| 279 |
new BufferedReader(new StringReader(buffer.toString())); |
|
| 280 | 0 |
String line = null;
|
| 281 | 0 |
while ((line = reader.readLine()) != null) |
| 282 |
{
|
|
| 283 | 0 |
if (line.indexOf(theSubstring) >= 0)
|
| 284 |
{
|
|
| 285 | 0 |
return;
|
| 286 |
} |
|
| 287 |
} |
|
| 288 |
} |
|
| 289 | 0 |
throw new AssertionFailedError( |
| 290 |
"Expected log message containing '" + theSubstring + "'"); |
|
| 291 |
} |
|
| 292 |
|
|
| 293 |
/**
|
|
| 294 |
* Asserts that a named target has been executed.
|
|
| 295 |
*
|
|
| 296 |
* @param theName The name of the target
|
|
| 297 |
*/
|
|
| 298 | 0 |
protected final void assertTargetExecuted(String theName) |
| 299 |
{
|
|
| 300 | 0 |
assertTrue("Target '" + theName + "' should have been executed", |
| 301 |
this.executedTargets.contains(theName));
|
|
| 302 |
} |
|
| 303 |
|
|
| 304 |
/**
|
|
| 305 |
* Executes the target in the project that corresponds to the current test
|
|
| 306 |
* case.
|
|
| 307 |
*/
|
|
| 308 | 38 |
protected final void executeTestTarget() |
| 309 |
{
|
|
| 310 | 38 |
this.project.executeTarget(getName());
|
| 311 |
} |
|
| 312 |
|
|
| 313 |
/**
|
|
| 314 |
* Returns the Ant project.
|
|
| 315 |
*
|
|
| 316 |
* @return The project
|
|
| 317 |
*/
|
|
| 318 | 223 |
protected final Project getProject()
|
| 319 |
{
|
|
| 320 | 223 |
return this.project; |
| 321 |
} |
|
| 322 |
|
|
| 323 |
/**
|
|
| 324 |
* Returns the base directory of the Ant project.
|
|
| 325 |
*
|
|
| 326 |
* @return The base directory
|
|
| 327 |
*/
|
|
| 328 | 0 |
protected final File getProjectDir()
|
| 329 |
{
|
|
| 330 | 0 |
return this.project.getBaseDir(); |
| 331 |
} |
|
| 332 |
|
|
| 333 |
/**
|
|
| 334 |
* Returns the target in the project that corresponds to the current test
|
|
| 335 |
* case.
|
|
| 336 |
*
|
|
| 337 |
* @return The test target
|
|
| 338 |
*/
|
|
| 339 | 0 |
protected final Target getTestTarget()
|
| 340 |
{
|
|
| 341 | 0 |
return (Target) getProject().getTargets().get(getName());
|
| 342 |
} |
|
| 343 |
|
|
| 344 |
// Private Methods ---------------------------------------------------------
|
|
| 345 |
|
|
| 346 |
/**
|
|
| 347 |
* Returns a file from the test inputs directory, which is determined by the
|
|
| 348 |
* system property <code>testinput.dir</code>.
|
|
| 349 |
*
|
|
| 350 |
* @param theFileName The name of the file relative to the test input
|
|
| 351 |
* directory
|
|
| 352 |
* @return The file from the test input directory
|
|
| 353 |
*/
|
|
| 354 | 43 |
private File getBuildFile(String theFileName)
|
| 355 |
{
|
|
| 356 | 43 |
String testInputDirProperty = System.getProperty("testinput.dir");
|
| 357 | 43 |
assertTrue("The system property 'testinput.dir' must be set",
|
| 358 |
testInputDirProperty != null);
|
|
| 359 | 43 |
File testInputDir = new File(testInputDirProperty);
|
| 360 | 43 |
assertTrue("The system property 'testinput.dir' must point to an "
|
| 361 |
+ "existing directory", testInputDir.isDirectory());
|
|
| 362 | 43 |
File buildFile = new File(testInputDir, theFileName);
|
| 363 | 43 |
assertTrue("The test input " + theFileName + " does not exist", |
| 364 |
buildFile.exists()); |
|
| 365 | 43 |
return buildFile;
|
| 366 |
} |
|
| 367 |
|
|
| 368 |
} |
|
| 369 |
|
|
||||||||||