|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AntTestCase.java | 50% | 74.5% | 66.7% | 67.5% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ====================================================================
|
|
| 3 |
*
|
|
| 4 |
* The Apache Software License, Version 1.1
|
|
| 5 |
*
|
|
| 6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
| 7 |
* reserved.
|
|
| 8 |
*
|
|
| 9 |
* Redistribution and use in source and binary forms, with or without
|
|
| 10 |
* modification, are permitted provided that the following conditions
|
|
| 11 |
* are met:
|
|
| 12 |
*
|
|
| 13 |
* 1. Redistributions of source code must retain the above copyright
|
|
| 14 |
* notice, this list of conditions and the following disclaimer.
|
|
| 15 |
*
|
|
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
| 17 |
* notice, this list of conditions and the following disclaimer in
|
|
| 18 |
* the documentation and/or other materials provided with the
|
|
| 19 |
* distribution.
|
|
| 20 |
*
|
|
| 21 |
* 3. The end-user documentation included with the redistribution, if
|
|
| 22 |
* any, must include the following acknowlegement:
|
|
| 23 |
* "This product includes software developed by the
|
|
| 24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
| 25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
| 26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
| 27 |
*
|
|
| 28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
| 29 |
* Foundation" must not be used to endorse or promote products
|
|
| 30 |
* derived from this software without prior written permission. For
|
|
| 31 |
* written permission, please contact apache@apache.org.
|
|
| 32 |
*
|
|
| 33 |
* 5. Products derived from this software may not be called "Apache"
|
|
| 34 |
* nor may "Apache" appear in their names without prior written
|
|
| 35 |
* permission of the Apache Group.
|
|
| 36 |
*
|
|
| 37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
| 38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
| 39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
| 40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
| 41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
| 42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
| 43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
| 44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
| 45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
| 46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
| 47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
| 48 |
* SUCH DAMAGE.
|
|
| 49 |
* ====================================================================
|
|
| 50 |
*
|
|
| 51 |
* This software consists of voluntary contributions made by many
|
|
| 52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
| 53 |
* information on the Apache Software Foundation, please see
|
|
| 54 |
* <http://www.apache.org/>.
|
|
| 55 |
*
|
|
| 56 |
*/
|
|
| 57 |
package org.apache.cactus.integration.ant;
|
|
| 58 |
|
|
| 59 |
import java.io.BufferedReader;
|
|
| 60 |
import java.io.File;
|
|
| 61 |
import java.io.IOException;
|
|
| 62 |
import java.io.StringReader;
|
|
| 63 |
import java.util.HashMap;
|
|
| 64 |
import java.util.HashSet;
|
|
| 65 |
import java.util.Map;
|
|
| 66 |
import java.util.Set;
|
|
| 67 |
|
|
| 68 |
import junit.framework.AssertionFailedError;
|
|
| 69 |
import junit.framework.TestCase;
|
|
| 70 |
|
|
| 71 |
import org.apache.tools.ant.BuildEvent;
|
|
| 72 |
import org.apache.tools.ant.BuildException;
|
|
| 73 |
import org.apache.tools.ant.BuildListener;
|
|
| 74 |
import org.apache.tools.ant.Project;
|
|
| 75 |
import org.apache.tools.ant.ProjectHelper;
|
|
| 76 |
import org.apache.tools.ant.Target;
|
|
| 77 |
|
|
| 78 |
/**
|
|
| 79 |
* An AntTestCase is a TestCase specialization for unit testing Ant tasks.
|
|
| 80 |
*
|
|
| 81 |
* @author <a href="mailto:cmlenz@gmx.de">Christopher Lenz</a>
|
|
| 82 |
*
|
|
| 83 |
* @version $Id: AntTestCase.java,v 1.9.2.1 2003/10/23 18:20:45 vmassol Exp $
|
|
| 84 |
*/
|
|
| 85 |
public abstract class AntTestCase extends TestCase implements BuildListener |
|
| 86 |
{
|
|
| 87 |
|
|
| 88 |
// Instance Variables ------------------------------------------------------
|
|
| 89 |
|
|
| 90 |
/**
|
|
| 91 |
* The Ant project.
|
|
| 92 |
*/
|
|
| 93 |
private Project project;
|
|
| 94 |
|
|
| 95 |
/**
|
|
| 96 |
* The name of the test build file.
|
|
| 97 |
*/
|
|
| 98 |
private String buildFile;
|
|
| 99 |
|
|
| 100 |
/**
|
|
| 101 |
* Buffer containing all messages logged by Ant. Keys correspond to the
|
|
| 102 |
* message priority as <code>java.lang.Integer</code>, the values are are
|
|
| 103 |
* <code>java.lang.StringBuffer</code>s containing the actual log messages.
|
|
| 104 |
*/
|
|
| 105 |
private Map log = new HashMap(); |
|
| 106 |
|
|
| 107 |
/**
|
|
| 108 |
* The targets the have been executed.
|
|
| 109 |
*/
|
|
| 110 |
private Set executedTargets = new HashSet(); |
|
| 111 |
|
|
| 112 |
// Constructors ------------------------------------------------------------
|
|
| 113 |
|
|
| 114 |
/**
|
|
| 115 |
* Constructor
|
|
| 116 |
*
|
|
| 117 |
* @param theBuildFile The Ant build file corresponding to the test fixture
|
|
| 118 |
*/
|
|
| 119 | 31 |
public AntTestCase(String theBuildFile)
|
| 120 |
{
|
|
| 121 | 31 |
this.buildFile = theBuildFile;
|
| 122 |
} |
|
| 123 |
|
|
| 124 |
// BuildListener Implementation --------------------------------------------
|
|
| 125 |
|
|
| 126 |
/**
|
|
| 127 |
* @see BuildListener#buildStarted
|
|
| 128 |
*/
|
|
| 129 | 0 |
public final void buildStarted(BuildEvent theEvent) |
| 130 |
{
|
|
| 131 |
} |
|
| 132 |
|
|
| 133 |
/**
|
|
| 134 |
* @see BuildListener#buildFinished
|
|
| 135 |
*/
|
|
| 136 | 0 |
public final void buildFinished(BuildEvent theEvent) |
| 137 |
{
|
|
| 138 |
} |
|
| 139 |
|
|
| 140 |
/**
|
|
| 141 |
* @see BuildListener#targetStarted
|
|
| 142 |
*/
|
|
| 143 | 67 |
public final void targetStarted(BuildEvent theEvent) |
| 144 |
{
|
|
| 145 |
} |
|
| 146 |
|
|
| 147 |
/**
|
|
| 148 |
* @see BuildListener#targetFinished
|
|
| 149 |
*/
|
|
| 150 | 67 |
public final void targetFinished(BuildEvent theEvent) |
| 151 |
{
|
|
| 152 | 67 |
this.executedTargets.add(theEvent.getTarget().getName());
|
| 153 |
} |
|
| 154 |
|
|
| 155 |
/**
|
|
| 156 |
* @see BuildListener#taskStarted
|
|
| 157 |
*/
|
|
| 158 | 69 |
public final void taskStarted(BuildEvent theEvent) |
| 159 |
{
|
|
| 160 |
} |
|
| 161 |
|
|
| 162 |
/**
|
|
| 163 |
* @see BuildListener#taskFinished
|
|
| 164 |
*/
|
|
| 165 | 69 |
public final void taskFinished(BuildEvent theEvent) |
| 166 |
{
|
|
| 167 |
} |
|
| 168 |
|
|
| 169 |
/**
|
|
| 170 |
* @see BuildListener#messageLogged
|
|
| 171 |
*/
|
|
| 172 | 7304 |
public final void messageLogged(BuildEvent theEvent) |
| 173 |
{
|
|
| 174 | 7304 |
StringBuffer buffer = (StringBuffer) |
| 175 |
log.get(new Integer(theEvent.getPriority()));
|
|
| 176 | 7304 |
if (buffer == null) |
| 177 |
{
|
|
| 178 | 96 |
buffer = new StringBuffer();
|
| 179 | 96 |
log.put(new Integer(theEvent.getPriority()), buffer);
|
| 180 |
} |
|
| 181 | 7304 |
buffer.append(theEvent.getMessage()).append("\n");
|
| 182 |
} |
|
| 183 |
|
|
| 184 |
// TestCase Implementation -------------------------------------------------
|
|
| 185 |
|
|
| 186 |
/**
|
|
| 187 |
* Initializes a fresh Ant project with a target named after the name of the
|
|
| 188 |
* test case.
|
|
| 189 |
*
|
|
| 190 |
* @see junit.framework.TestCase#setUp()
|
|
| 191 |
*/
|
|
| 192 | 31 |
protected void setUp() throws Exception |
| 193 |
{
|
|
| 194 | 31 |
this.project = new Project(); |
| 195 | 31 |
this.project.addBuildListener(this); |
| 196 | 31 |
this.project.init();
|
| 197 | 31 |
File buildFile = getBuildFile(this.buildFile);
|
| 198 | 31 |
this.project.setUserProperty("ant.file", buildFile.getAbsolutePath()); |
| 199 | 31 |
ProjectHelper helper = ProjectHelper.getProjectHelper(); |
| 200 | 31 |
helper.parse(this.project, buildFile);
|
| 201 | 31 |
if (getProject().getTargets().get("setUp") != null) |
| 202 |
{
|
|
| 203 | 18 |
getProject().executeTarget("setUp");
|
| 204 |
} |
|
| 205 |
} |
|
| 206 |
|
|
| 207 |
/**
|
|
| 208 |
* @see junit.framework.TestCase#tearDown()
|
|
| 209 |
*/
|
|
| 210 | 31 |
protected void tearDown() throws Exception |
| 211 |
{
|
|
| 212 | 31 |
if (getProject().getTargets().get("tearDown") != null) |
| 213 |
{
|
|
| 214 | 18 |
try
|
| 215 |
{
|
|
| 216 | 18 |
getProject().executeTarget("tearDown");
|
| 217 |
} |
|
| 218 |
catch (BuildException be)
|
|
| 219 |
{
|
|
| 220 |
// exception has been logged
|
|
| 221 |
} |
|
| 222 |
} |
|
| 223 |
} |
|
| 224 |
|
|
| 225 |
// Protected Methods -------------------------------------------------------
|
|
| 226 |
|
|
| 227 |
/**
|
|
| 228 |
* Asserts that a specific message has been logged at a specific log level.
|
|
| 229 |
*
|
|
| 230 |
* @param theMessage The message to check for
|
|
| 231 |
* @param theLogLevel The log level of the message
|
|
| 232 |
* @throws IOException If an error occurred reading the log buffer
|
|
| 233 |
*/
|
|
| 234 | 5 |
protected final void assertMessageLogged(String theMessage, int theLogLevel) |
| 235 |
throws IOException
|
|
| 236 |
{
|
|
| 237 | 5 |
StringBuffer buffer = (StringBuffer) log.get(new Integer(theLogLevel));
|
| 238 | 5 |
if (buffer != null) |
| 239 |
{
|
|
| 240 | 5 |
BufferedReader reader = |
| 241 |
new BufferedReader(new StringReader(buffer.toString())); |
|
| 242 | 5 |
String line = null;
|
| 243 | ? |
while ((line = reader.readLine()) != null) |
| 244 |
{
|
|
| 245 | 55 |
if (line.equals(theMessage))
|
| 246 |
{
|
|
| 247 | 5 |
return;
|
| 248 |
} |
|
| 249 |
} |
|
| 250 |
} |
|
| 251 | 0 |
throw new AssertionFailedError( |
| 252 |
"Expected log message '" + theMessage + "'"); |
|
| 253 |
} |
|
| 254 |
|
|
| 255 |
/**
|
|
| 256 |
* Asserts that a message containing the specified substring has been logged
|
|
| 257 |
* at a specific log level.
|
|
| 258 |
*
|
|
| 259 |
* @param theSubstring The substring to check for
|
|
| 260 |
* @param theLogLevel The log level of the message
|
|
| 261 |
* @throws IOException If an error occurred reading the log buffer
|
|
| 262 |
*/
|
|
| 263 | 0 |
protected final void assertMessageLoggedContaining(String theSubstring, |
| 264 |
int theLogLevel)
|
|
| 265 |
throws IOException
|
|
| 266 |
{
|
|
| 267 | 0 |
StringBuffer buffer = (StringBuffer) log.get(new Integer(theLogLevel));
|
| 268 | 0 |
if (buffer != null) |
| 269 |
{
|
|
| 270 | 0 |
BufferedReader reader = |
| 271 |
new BufferedReader(new StringReader(buffer.toString())); |
|
| 272 | 0 |
String line = null;
|
| 273 | 0 |
while ((line = reader.readLine()) != null) |
| 274 |
{
|
|
| 275 | 0 |
if (line.indexOf(theSubstring) >= 0)
|
| 276 |
{
|
|
| 277 | 0 |
return;
|
| 278 |
} |
|
| 279 |
} |
|
| 280 |
} |
|
| 281 | 0 |
throw new AssertionFailedError( |
| 282 |
"Expected log message containing '" + theSubstring + "'"); |
|
| 283 |
} |
|
| 284 |
|
|
| 285 |
/**
|
|
| 286 |
* Asserts that a named target has been executed.
|
|
| 287 |
*
|
|
| 288 |
* @param theName The name of the target
|
|
| 289 |
*/
|
|
| 290 | 0 |
protected final void assertTargetExecuted(String theName) |
| 291 |
{
|
|
| 292 | 0 |
assertTrue("Target '" + theName + "' should have been executed", |
| 293 |
this.executedTargets.contains(theName));
|
|
| 294 |
} |
|
| 295 |
|
|
| 296 |
/**
|
|
| 297 |
* Executes the target in the project that corresponds to the current test
|
|
| 298 |
* case.
|
|
| 299 |
*/
|
|
| 300 | 31 |
protected final void executeTestTarget() |
| 301 |
{
|
|
| 302 | 31 |
this.project.executeTarget(getName());
|
| 303 |
} |
|
| 304 |
|
|
| 305 |
/**
|
|
| 306 |
* Returns the Ant project.
|
|
| 307 |
*
|
|
| 308 |
* @return The project
|
|
| 309 |
*/
|
|
| 310 | 153 |
protected final Project getProject()
|
| 311 |
{
|
|
| 312 | 153 |
return this.project; |
| 313 |
} |
|
| 314 |
|
|
| 315 |
/**
|
|
| 316 |
* Returns the base directory of the Ant project.
|
|
| 317 |
*
|
|
| 318 |
* @return The base directory
|
|
| 319 |
*/
|
|
| 320 | 0 |
protected final File getProjectDir()
|
| 321 |
{
|
|
| 322 | 0 |
return this.project.getBaseDir(); |
| 323 |
} |
|
| 324 |
|
|
| 325 |
/**
|
|
| 326 |
* Returns the target in the project that corresponds to the current test
|
|
| 327 |
* case.
|
|
| 328 |
*
|
|
| 329 |
* @return The test target
|
|
| 330 |
*/
|
|
| 331 | 0 |
protected final Target getTestTarget()
|
| 332 |
{
|
|
| 333 | 0 |
return (Target) getProject().getTargets().get(getName());
|
| 334 |
} |
|
| 335 |
|
|
| 336 |
// Private Methods ---------------------------------------------------------
|
|
| 337 |
|
|
| 338 |
/**
|
|
| 339 |
* Returns a file from the test inputs directory, which is determined by the
|
|
| 340 |
* system property <code>testinput.dir</code>.
|
|
| 341 |
*
|
|
| 342 |
* @param theFileName The name of the file relative to the test input
|
|
| 343 |
* directory
|
|
| 344 |
* @return The file from the test input directory
|
|
| 345 |
*/
|
|
| 346 | 31 |
private File getBuildFile(String theFileName)
|
| 347 |
{
|
|
| 348 | 31 |
String testInputDirProperty = System.getProperty("testinput.dir");
|
| 349 | 31 |
assertTrue("The system property 'testinput.dir' must be set",
|
| 350 |
testInputDirProperty != null);
|
|
| 351 | 31 |
File testInputDir = new File(testInputDirProperty);
|
| 352 | 31 |
assertTrue("The system property 'testinput.dir' must point to an "
|
| 353 |
+ "existing directory", testInputDir.isDirectory());
|
|
| 354 | 31 |
File buildFile = new File(testInputDir, theFileName);
|
| 355 | 31 |
assertTrue("The test input " + theFileName + " does not exist", |
| 356 |
buildFile.exists()); |
|
| 357 | 31 |
return buildFile;
|
| 358 |
} |
|
| 359 |
|
|
| 360 |
} |
|
| 361 |
|
|
||||||||||