functionalconditioncoveragetest.java
来自「java覆盖率测试工具」· Java 代码 · 共 448 行 · 第 1/2 页
JAVA
448 行
{
if (methodsFound.contains(methodName))
{
fail("Test " + testName
+ ": Found more than one instance of the method " + methodName + " in the class "
+ classElement.getAttributeValue("name"));
}
methodsFound.add(methodName);
verifyMethod(info, testName, classElement, methodElement);
}
else if (methodName.equals("<clinit>") ||
methodName.equals("<init>") ||
methodName.startsWith("util") ||
methodName.equals("class$"))
{
// These methods are ok--ignore them.
}
else
{
fail("Test " + testName + ": Found method " + methodName + " in the class "
+ classElement.getAttributeValue("name")
+ ", but was only expecting either 'call' or 'dontCall'.");
}
}
/*
* now make sure all methods in testInfoMap were found and verified
*/
for (Iterator iter = testInfoMap.keySet().iterator(); iter.hasNext();) {
String methodName = (String) iter.next();
assertTrue("Test " + testName + ": Did not find method " + methodName + " in the class "
+ classElement.getAttributeValue("name"), methodsFound.contains(methodName));
}
}
private static void verifyMethod(TestInfo info, String testName, Element classElement, Element methodElement) {
Element linesElement = methodElement.getChild("lines");
List lineList = linesElement.getChildren("line");
String methodName = methodElement.getAttributeValue("name");
assertTrue("Test " + testName + ", class " + classElement.getAttributeValue("name")
+ ": Did not find any lines in the method "
+ methodName, lineList.size() > 0);
boolean foundCondition = false;
for (Iterator iter = lineList.iterator(); iter.hasNext();)
{
Element lineElement = (Element)iter.next();
int number;
try {
number = lineElement.getAttribute("number").getIntValue();
if ((info.ignoreLineNumber != null) && (info.ignoreLineNumber.intValue() == number))
{
fail("Expected line " + info.ignoreLineNumber + " to be ignored.");
}
} catch (DataConversionException e) {
throw new RuntimeException(e.toString());
}
if (number == info.conditionNumber) {
foundCondition = true;
verifyLineConditionInfo(lineElement, info.conditionNumber,
info.expectedLineConditionCoverage, info.expectedConditions);
}
}
assertTrue("Expected condition element for line " + info.conditionNumber + " of " + methodName, foundCondition);
}
private static void verifyLineConditionInfo(Element lineElement, int conditionLineNumber,
String expectedLineConditionCoverage, ConditionTestInfo[] expectedConditions)
{
String errorMessage = "Line " + conditionLineNumber;
boolean branch = false;
try {
branch = lineElement.getAttribute("branch").getBooleanValue();
} catch (DataConversionException e) {
fail(errorMessage + " has missing or wrong branch attribute");
}
assertTrue(errorMessage + "Branch attribute should be true", branch);
String lineCoverageStr = getRequiredAttribute(lineElement, "condition-coverage", errorMessage).getValue();
assertEquals(errorMessage + " has incorrect condition-coverage", expectedLineConditionCoverage, lineCoverageStr);
List conditionList = lineElement.getChildren("conditions");
assertTrue(errorMessage + " should have one and only one conditions element.", conditionList.size() == 1);
conditionList = ((Element) conditionList.get(0)).getChildren("condition");
assertEquals(errorMessage + " has incorrect number of condition elements.", expectedConditions.length, conditionList.size());
errorMessage = "Condition for " + conditionLineNumber;
int i = 0;
for (Iterator iter = conditionList.iterator(); iter.hasNext(); i++) {
Element element = (Element) iter.next();
verifyCondition(element, errorMessage, expectedConditions[i]);
}
}
private static void verifyCondition(Element conditionElement, String errorMessage, ConditionTestInfo info)
{
String numberStr = getRequiredAttribute(conditionElement, "number", errorMessage).getValue();
assertEquals(errorMessage + " has incorrect number", info.number, numberStr);
String typeStr = getRequiredAttribute(conditionElement, "type", errorMessage).getValue();
assertEquals(errorMessage + " has incorrect type", info.type, typeStr);
String coverageStr = getRequiredAttribute(conditionElement, "coverage", errorMessage).getValue();
assertEquals(errorMessage + " has incorrect coverage", info.coverage, coverageStr);
}
private static Attribute getRequiredAttribute(Element element, String attribute, String errorMessage) {
Attribute attr = element.getAttribute(attribute);
assertNotNull(errorMessage + " has missing " + attribute + " attribute.", attr);
return attr;
}
private static void verifyHtml(String testName) throws Exception
{
File htmlReportDir = new File(BASEDIR, "reports/cobertura-html");
// Get all files from report directory
String htmlFiles[] = htmlReportDir.list(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".html");
}
});
Arrays.sort(htmlFiles);
assertTrue(htmlFiles.length >= 5);
// Assert that all required files are there
String[] requiredFiles = { "index.html", "help.html", "frame-packages.html",
"frame-summary.html", "frame-sourcefiles.html" };
for (int i = 0; i < requiredFiles.length; i++)
{
if (!containsFile(htmlFiles, requiredFiles[i]))
{
fail("Test " + testName + ": File " + requiredFiles[i]
+ " not found among report files");
}
}
// Validate selected files
String previousPrefix = "NONE";
for (int i = 0; i < htmlFiles.length; i++)
{
// Validate file if has prefix different than previous one, or is required file
if (containsFile(requiredFiles, htmlFiles[i])
|| !htmlFiles[i].startsWith(previousPrefix))
{
JUnitXMLHelper.readXmlFile(new File(htmlReportDir, htmlFiles[i]), true);
}
if (htmlFiles[i].length() > 7)
{
previousPrefix = htmlFiles[i].substring(0, 7);
}
else
{
previousPrefix = htmlFiles[i];
}
}
}
private static boolean containsFile(String[] files, String fileName)
{
for (int i = 0; i < files.length; i++)
{
if (files[i].equals(fileName))
return true;
}
return false;
}
/**
* Use the ant 'java' task to run the test.xml
* file and the specified target.
*/
private static void runTestAntScript(String testName, String target) throws IOException
{
Java task = new Java();
task.setTaskName("java");
task.setProject(new Project());
task.init();
// Call ant launcher. Requires ant-lancher.jar.
task.setClassname("org.apache.tools.ant.launch.Launcher");
task.setFork(true);
AntUtil.transferCoberturaDataFileProperty(task);
task.createArg().setValue("-f");
task.createArg().setValue(BASEDIR + "/build.xml");
task.createArg().setValue(target);
task.setFailonerror(true);
// Set output to go to a temp file
File outputFile = Util.createTemporaryTextFile("cobertura-test");
task.setOutput(outputFile);
// Set the classpath to the same classpath as this JVM
Path classpath = task.createClasspath();
PathElement pathElement = classpath.createPathElement();
pathElement.setPath(System.getProperty("java.class.path"));
try
{
task.execute();
}
finally
{
if (outputFile.exists())
{
// Put the contents of the output file in the exception
System.out.println("\n\n\nOutput from Ant for " + testName
+ " test:\n----------------------------------------\n"
+ Util.getText(outputFile) + "----------------------------------------");
outputFile.delete();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?