functionalconditioncoveragetest.java

来自「java覆盖率测试工具」· Java 代码 · 共 448 行 · 第 1/2 页

JAVA
448
字号
/*
 * Cobertura - http://cobertura.sourceforge.net/
 *
 * Copyright (C) 2006 John Lewis
 * Copyright (C) 2006 Mark Doliner
 * 
 * Note: This file is dual licensed under the GPL and the Apache
 * Source License 1.1 (so that it can be used from both the main
 * Cobertura classes and the ant tasks).
 *
 * Cobertura is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * Cobertura is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cobertura; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

package net.sourceforge.cobertura.ant;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;
import net.sourceforge.cobertura.reporting.JUnitXMLHelper;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Path.PathElement;
import org.jdom.Attribute;
import org.jdom.DataConversionException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;

import test.condition.ConditionCalls;

/**
 * These tests generally exec ant to run a test.xml file.  A different target is used for
 * each test.  The text.xml file sets up a test, instruments, runs junit, and generates a
 * coverage xml report.  Then the xml report is parsed and checked.
 * 
 * @author jwlewi
 */
public class FunctionalConditionCoverageTest extends TestCase
{

	private final static File BASEDIR = new File((System.getProperty("basedir") != null) ? System
			.getProperty("basedir") : ".", "examples/functionalconditiontest");

	private final static String CONDITION_MISSING_TRUE = "50%";
	private final static String CONDITION_MISSING_FALSE = "50%";
	
	private final static Map testInfoMap = new HashMap();
	
	static
	{
		ConditionTestInfo[] expectedConditions;
		TestInfo info;
		
		/*
		 * Load expected information into testInfoMap for each method.
		 */
		expectedConditions = new ConditionTestInfo[1];
		expectedConditions[0] = new ConditionTestInfo("0", "jump", CONDITION_MISSING_FALSE);

		info = new TestInfo(ConditionCalls.CALL_CONDITION_LINE_NUMBER, "50% (1/2)", expectedConditions);
		info.setIgnoreLineNumber(ConditionCalls.CALL_IGNORE_LINE_NUMBER);

		testInfoMap.put("call", info);
		
		expectedConditions = new ConditionTestInfo[1];
		expectedConditions[0] = new ConditionTestInfo("0", "switch", "33%");
		
		info = new TestInfo(ConditionCalls.LOOKUP_SWITCH_LINE_NUMBER, "33% (1/3)", expectedConditions);

		testInfoMap.put("callLookupSwitch", info);
		
		expectedConditions = new ConditionTestInfo[1];
		expectedConditions[0] = new ConditionTestInfo("0", "switch", "10%");

		info = new TestInfo(ConditionCalls.TABLE_SWITCH_LINE_NUMBER, "10% (1/10)", expectedConditions);

		testInfoMap.put("callTableSwitch", info);
		
		expectedConditions = new ConditionTestInfo[3];
		expectedConditions[0] = new ConditionTestInfo("0", "jump", CONDITION_MISSING_TRUE);
		expectedConditions[1] = new ConditionTestInfo("1", "jump", "0%");
		expectedConditions[2] = new ConditionTestInfo("2", "jump", CONDITION_MISSING_FALSE);

		info = new TestInfo(ConditionCalls.MULTI_CONDITION_LINE_NUMBER, "33% (2/6)", expectedConditions);

		testInfoMap.put("callMultiCondition", info);
		
		expectedConditions = new ConditionTestInfo[3];
		expectedConditions[0] = new ConditionTestInfo("0", "jump", CONDITION_MISSING_FALSE);
		expectedConditions[1] = new ConditionTestInfo("1", "jump", CONDITION_MISSING_FALSE);
		expectedConditions[2] = new ConditionTestInfo("2", "jump", "0%");

		info = new TestInfo(ConditionCalls.MULTI_CONDITION2_LINE_NUMBER, "33% (2/6)", expectedConditions);

		testInfoMap.put("callMultiCondition2", info);
	};
	
	private static class TestInfo
	{
		int conditionNumber;
		String expectedLineConditionCoverage;
		ConditionTestInfo[] expectedConditions;
		Integer ignoreLineNumber;

		TestInfo(int conditionNumber, String expectedLineConditionCoverage, ConditionTestInfo[] expectedConditions)
		{
			this.conditionNumber = conditionNumber;
			this.expectedLineConditionCoverage = expectedLineConditionCoverage;
			this.expectedConditions = expectedConditions;
		}

		public void setIgnoreLineNumber(int number) {
			ignoreLineNumber = new Integer(number);
		}
	}
	private static class ConditionTestInfo
	{
		String number;
		String type;
		String coverage;
		
		ConditionTestInfo(String number, String type, String coverage)
		{
			this.number = number;
			this.type = type;
			this.coverage = coverage;
		}
	}

	public static void testConditionCoverage() throws Exception
	{
		runTestAntScript("condition-coverage", "test-condition-coverage");
		verify("condition-coverage");
	}

	private static void verify(String testName) throws Exception
	{
		verifyXml(testName);
		verifyHtml(testName);
	}

	private static void verifyXml(String testName) throws Exception
	{
		// Get a list of all classes listed in the XML report
		List classesList = getClassElements();
		assertTrue("Test " + testName + ": Did not find any classes listed in the XML report.",
				classesList.size() > 0);

		boolean conditionCallsClassFound = false;
		for (Iterator iter = classesList.iterator(); iter.hasNext();)
		{
			Element classElement = (Element)iter.next();
			String className = classElement.getAttributeValue("name");
			if (className.equals("test.condition.ConditionCalls"))
			{
				conditionCallsClassFound = true;
			}
			else
				fail("Test "
						+ testName
						+ ": Found a class with the name '"
						+ className
						+ "' in the XML report, but was only expecting 'test.condition.ConditionCalls'.");
			verifyClass(testName, classElement);
		}
		assertTrue("Test " + testName + ": Did not find class 'test.condition.ConditionCalls' in the XML report.",
				conditionCallsClassFound);
	}

	/**
	 * Use XPath to get all <class> elements in the
	 * cobertura.xml file under the given directory.
	 * @return A list of JDOM Elements.
	 */
	private static List getClassElements() throws IOException, JDOMException
	{
		File xmlFile = new File(BASEDIR, "reports/cobertura-xml/coverage.xml");
		Document document = JUnitXMLHelper.readXmlFile(xmlFile, true);
		XPath xpath = XPath.newInstance("/coverage/packages/package/classes/class");
		List classesList = xpath.selectNodes(document);
		return classesList;
	}

	/**
	 * Verify that the class's condition information is correct.
	 */
	private static void verifyClass(String testName, Element classElement)
	{
		// Get a list of methods
		Element methodsElement = classElement.getChild("methods");
		List methodList = methodsElement.getChildren("method");
		assertTrue("Test " + testName + ": Did not find any methods listed in the class "
				+ classElement.getAttributeValue("name"), methodList.size() > 0);
		List methodsFound = new ArrayList();
		for (Iterator iter = methodList.iterator(); iter.hasNext();)
		{
			Element methodElement = (Element)iter.next();
			String methodName = methodElement.getAttributeValue("name");
			TestInfo info = (TestInfo) testInfoMap.get(methodName);
			if (info != null)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?