view src/ClassInfo.java @ 16:b6c8372f5723 draft

* src/ClassInfo.java: * src/FileUtils.java: * src/PrintClassList.java: * src/PrintPublicMethods.java: * src/PrintTestCoverage.java: * src/ReportGenerator.java: Minor changes - JavaDoc and refactoring.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Wed, 16 May 2012 10:59:45 +0200
parents eec2474fdaaa
children
line wrap: on
line source

/*
  Test coverage tool.

   Copyright (C) 2012 Red Hat

This tool 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, or (at your option)
any later version.

This tool 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 this tool; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;



/**
 * Instances of this class contain information about one tested class. There are
 * three sets of strings stored for each class: set of methods contained in
 * standard API, set of methods called from tests and the union of previous two
 * sets. These information are used by the test reporter.
 * 
 * @author Pavel Tisnovsky <ptisnovs@redhat.com>
 */
public class ClassInfo
{
    /**
     * Set of methods contained in standard API.
     */
    private Set<String> apiMethods;

    /**
     * Set of methods called from tests.
     */
    private Set<String> testedMethods;

    /**
     * Union of both apiMethods and testedMethods sets.
     */
    private Set<String> allMethods;

    /**
     * Constructor. It tries to read all required information
     * for tested class.
     * 
     * @param reportDirectory
     *            directory where all reports are stored
     * @param className
     *            name of tested class
     */
    public ClassInfo(String reportDirectory, String className)
    {
        // read methods described in standard API
        this.apiMethods = readApiMethods(reportDirectory, className);
        // read methods called from tests
        this.testedMethods = readTestedMethods(reportDirectory, className);
        // compute union of previous two sets
        computeAllMethodsSet();
    }

    /**
     * Compute union of allMethods and testedMethods sets.
     */
    private void computeAllMethodsSet()
    {
        this.allMethods = new TreeSet<String>();
        this.allMethods.addAll(this.apiMethods);
        this.allMethods.addAll(this.testedMethods);
    }

    /**
     * Getter for a set apiMethods.
     *
     * @return the apiMethods attribute
     */
    public Set<String> getApiMethods()
    {
        return this.apiMethods;
    }

    /**
     * Getter for a set testedMethods.
     *
     * @return the testedMethods attribute
     */
    public Set<String> getTestedMethods()
    {
        return this.testedMethods;
    }

    /**
     * Getter for a set allMethods.
     *
     * @return the allMethods attribute
     */
    public Set<String> getAllMethods()
    {
        return this.allMethods;
    }

    /**
     * Read all methods for a given class which are covered by a standard API.
     * 
     * @param reportDirectory
     *            directory where all reports are stored
     * @param testedClass
     *            name of tested class
     * @return set of all methods read from a file containing standard API
     */
    private static Set<String> readApiMethods(String reportDirectory, String testedClass)
    {
        File fileName = new File(reportDirectory, testedClass + "_api.txt");
        return readMethods(fileName);
    }

    /**
     * Read all methods for a given class which are called from tests.
     * 
     * @param reportDirectory
     *            directory where all reports are stored
     * @param testedClass
     *            name of tested class
     * @return set of all methods read from a file containing called methods
     */
    private static Set<String> readTestedMethods(String reportDirectory, String testedClass)
    {
        File fileName = new File(reportDirectory, testedClass + "_test.txt");
        return readMethods(fileName);
    }

    /**
     * Read set of method names from a text file. No exception is thrown during
     * reading.
     * 
     * @param fileName
     *            file containing methods list
     * @return set of method names
     */
    private static Set<String> readMethods(File fileName)
    {
        Set<String> allMethods = new TreeSet<String>();
        List<String> methodList = FileUtils.readTextFile(fileName.getAbsolutePath(), false);
        allMethods.addAll(methodList);
        return allMethods;
    }

}