
patterntesting.tool.aspectj.AjcTestCase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-tools
Show all versions of patterntesting-tools
PatternTesting Tools (patterntesting-tools) is the container for
tools around PatternTesting like the Ant extensions and Maven plugin.
The newest version!
/*
*========================================================================
*
* Copyright 2001-2004 Vincent Massol & Matt Smith.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*========================================================================
*/
package patterntesting.tool.aspectj;
import static org.junit.Assert.fail;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
/**
* Helper test cases to write JUnit test case to assert result of Ajc
* compilation.
*
* @author Vincent Massol
* @author Matt Smith
*
* @version $Id: AjcTestCase.java,v 1.5 2009/12/30 13:33:04 oboehm Exp $
*/
public class AjcTestCase
{
/** for normal logging */
private static final Log log = LogFactoryImpl.getLog(AjcTestCase.class);
/**
* the AjcXml compiler
*/
private AjcXml compiler = new AjcXml();
/**
* Property to specify the project base directory (from where
* all source paths are computed).
*/
private static final String BASEDIR_PROPERTY = "basedir";
/**
* Property to specify the output dir where compiled classes will
* be compiled.
*/
private static final String OUTPUTDIR_PROPERTY = "patterntesting.outputdir";
/**
* Default constructor.
*/
public AjcTestCase() {
//this.setCompilerLog();
}
// private void setCompilerLog() {
// File logfile = new File(System.getProperty("java.io.tmpdir"), "ajc.log");
// log.debug("setting compiler logfile to " + logfile);
// this.compiler.setLog(logfile);
// }
/**
* If property BASEDIR_PROPERTY is not set the actual working directroy
* is now used as default (20-May-07, [email protected])
*
* @return the project base directory as set by the
* BASEDIR_PROPERTY system property
*/
protected String getBaseDir() {
String basedir = System.getProperty(BASEDIR_PROPERTY);
if (basedir == null) {
basedir = System.getProperty("user.dir");
if (basedir == null) {
throw new RuntimeException("The \"" + BASEDIR_PROPERTY
+ "\" property has not been set");
} else {
log.info("The \"" + BASEDIR_PROPERTY
+ "\" property has not been set - using " + basedir
+ " as default");
System.setProperty(BASEDIR_PROPERTY, basedir);
}
}
return basedir;
}
/**
* If property OUTPUTDIR_PROPERTY is not set the temp directroy
* is now used as default (20-May-07, [email protected])
*
* @return the output directory as set by the OUTPUTDIR_PROPERTY system
* property
*/
protected String getOutputDir() {
String outputdir = System.getProperty(OUTPUTDIR_PROPERTY);
if (outputdir == null) {
outputdir = System.getProperty("java.io.tmpdir");
if (outputdir == null) {
throw new RuntimeException("The \"" + OUTPUTDIR_PROPERTY
+ "\" property has not been set");
} else {
log.info("The \"" + OUTPUTDIR_PROPERTY
+ "\" property has not been set - using " + outputdir
+ " as default");
System.setProperty(OUTPUTDIR_PROPERTY, outputdir);
}
}
return outputdir;
}
protected AjcXml getCompiler()
{
return this.compiler;
}
protected boolean assertErrorEquals(String testFileName, int lineNumber,
String errorMessage) {
AjcFileResult fileResult = getFileResult(testFileName);
for (Enumeration results = fileResult.getErrors(); results
.hasMoreElements();) {
AjcResult result = results.nextElement();
if (result.getLine() == lineNumber
&& result.getErrorMessage().equals(errorMessage)) {
return true;
}
}
fail("no match for `line " + lineNumber + ": " + errorMessage);
return false;
}
protected boolean assertWarningEquals(String testFileName, int lineNumber,
String warnMessage) {
AjcFileResult fileResult = getFileResult(testFileName);
for (Enumeration results = fileResult.getWarnings(); results
.hasMoreElements();) {
AjcResult result = results.nextElement();
if (result.getLine() == lineNumber
&& result.getErrorMessage().equals(warnMessage)) {
return true;
}
}
fail("no match for `line " + lineNumber + ": " + warnMessage);
return false;
}
/**
* @param testFileName
* @return
*/
private AjcFileResult getFileResult(String testFileName) {
Map errors = this.compiler.getErrors();
if (errors == null) {
fail(this.compiler + " found no errors/warnings");
}
if (errors.get(testFileName) == null) {
fail("no compiler errors/warnings found for " + testFileName);
}
AjcFileResult fileResult = errors.get(testFileName);
return fileResult;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy