/hg/MauveTestCoverage: 2012-01-10 Pavel Tisnovsky <ptisnovs at re...
ptisnovs at icedtea.classpath.org
ptisnovs at icedtea.classpath.org
Tue Jan 10 05:06:28 PST 2012
changeset 730c5549c0f9 in /hg/MauveTestCoverage
details: http://icedtea.classpath.org/hg/MauveTestCoverage?cmd=changeset;node=730c5549c0f9
author: Pavel Tisnovsky <ptisnovs at redhat.com>
date: Tue Jan 10 14:08:47 2012 +0100
2012-01-10 Pavel Tisnovsky <ptisnovs at redhat.com>
* src/FileUtils.java: Added new helper class.
* src/ReportGenerator.java: Updated to use HTML templates
for test coverage report.
* Makefile: Updated
diffstat:
ChangeLog | 9 +
Makefile | 3 +-
src/FileUtils.java | 233 +++++++++++++++++++++++++++++++++++++
src/ReportGenerator.java | 293 +++++++++++++++++++++++++---------------------
4 files changed, 401 insertions(+), 137 deletions(-)
diffs (truncated from 631 to 500 lines):
diff -r 61f453c6b172 -r 730c5549c0f9 ChangeLog
--- a/ChangeLog Mon Jan 09 14:57:59 2012 +0100
+++ b/ChangeLog Tue Jan 10 14:08:47 2012 +0100
@@ -1,3 +1,12 @@
+2012-01-10 Pavel Tisnovsky <ptisnovs at redhat.com>
+
+ * src/FileUtils.java:
+ Added new helper class.
+ * src/ReportGenerator.java:
+ Updated to use HTML templates for test coverage report.
+ * Makefile:
+ Updated
+
2012-01-09 Pavel Tisnovsky <ptisnovs at redhat.com>
* src/index.html:
diff -r 61f453c6b172 -r 730c5549c0f9 Makefile
--- a/Makefile Mon Jan 09 14:57:59 2012 +0100
+++ b/Makefile Tue Jan 10 14:08:47 2012 +0100
@@ -73,7 +73,8 @@
$(CLASSDIR)/PrintClassList.class \
$(CLASSDIR)/PrintPublicMethods.class \
$(CLASSDIR)/PrintTestCoverage.class \
- $(CLASSDIR)/ReportGenerator.class
+ $(CLASSDIR)/ReportGenerator.class \
+ $(CLASSDIR)/FileUtils.class
api_class_list: $(REPORTDIR) $(REPORTDIR)/$(ALL_CLASS_LIST)
diff -r 61f453c6b172 -r 730c5549c0f9 src/FileUtils.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/FileUtils.java Tue Jan 10 14:08:47 2012 +0100
@@ -0,0 +1,233 @@
+/*
+ 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.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Basic file utilities to avoid boring file handling.
+ *
+ * @author Pavel Tisnovsky
+ */
+class FileUtils
+{
+ /**
+ * Read content of given text file and return it as list of strings. No
+ * exception is thrown during reading.
+ *
+ * @param fileName
+ * name of file to be read
+ * @return list of string containing content of text file
+ */
+ static List<String> readTextFile(String fileName)
+ {
+ BufferedReader reader = null;
+ List<String> out = new LinkedList<String>();
+ try
+ {
+ // try to open file for reading
+ reader = new BufferedReader(new FileReader(fileName));
+ readAllLinesFromTextFile(reader, out);
+ }
+ catch (FileNotFoundException e)
+ {
+ // might happen - empty list is returned in this case
+ e.printStackTrace();
+ }
+ catch (IOException e)
+ {
+ // might happen - empty list is returned in this case
+ // make sure the list is empty
+ out.clear();
+ e.printStackTrace();
+ }
+ finally
+ {
+ closeBufferedReader(reader);
+ }
+ // return list containing content of text file
+ return out;
+ }
+
+ /**
+ * Write list of string to a file with given name. No exception is thrown
+ * during reading.
+ *
+ * @param fileName
+ * name of file to be read
+ * @param lines
+ * of string containing content of text file
+ */
+ static void writeTextFile(String fileName, List<String> lines)
+ {
+ BufferedWriter fout = null;
+ try
+ {
+ fout = new BufferedWriter(new FileWriter(fileName));
+ // write all lines to a text file
+ writeAllLinesToTextFile(fout, lines);
+ }
+ catch (IOException e)
+ {
+ // might happen - empty list is returned in this case
+ e.printStackTrace();
+ }
+ finally
+ {
+ closeBufferedWriter(fout);
+ }
+ }
+
+ /**
+ * Write list of string to a file with given name. No exception is thrown
+ * during reading.
+ *
+ * @param reportDirectory
+ * @param string
+ * @param lines of string containing content of text file
+ */
+ static void writeTextFile(String reportDirectory, String string, List<String> lines)
+ {
+ writeTextFile(new File(reportDirectory, string), lines);
+ }
+
+ /**
+ * Write list of string to a file with given name. No exception is thrown
+ * during reading.
+ *
+ * @param file
+ * representing file name
+ * @param lines
+ * of string containing content of text file
+ */
+ static void writeTextFile(File file, List<String> lines)
+ {
+ writeTextFile(file.getPath(), lines);
+ }
+
+ /**
+ * Try to close buffered reader.
+ *
+ * @param bufferedReader instance of buffered reader or null
+ */
+ private static void closeBufferedReader(BufferedReader bufferedReader)
+ {
+ // try to close the buffered reader
+ try
+ {
+ if (bufferedReader != null)
+ {
+ bufferedReader.close();
+ }
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Try to close buffered reader.
+ *
+ * @param bufferedWriter
+ * instance of buffered writer or null
+ */
+ private static void closeBufferedWriter(BufferedWriter bufferedWriter)
+ {
+ // try to close the buffered writer
+ try
+ {
+ if (bufferedWriter != null)
+ {
+ bufferedWriter.close();
+ }
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Read all lines from text file and add them to a list of strings.
+ *
+ * @param bufferedReader
+ * instance of buffered reader
+ * @param lines
+ * list of string to be filled
+ * @throws IOException
+ * thrown if an I/O error occurs
+ */
+ private static void readAllLinesFromTextFile(BufferedReader bufferedReader, List<String> lines) throws IOException
+ {
+ String line;
+ // read lines from a text file
+ while ((line = bufferedReader.readLine()) != null)
+ {
+ lines.add(line);
+ }
+ }
+
+ /**
+ * Write content of the list of strings to a file.
+ *
+ * @param bufferedWriter
+ * instance of buffered writer
+ * @param lines
+ * list of string
+ * @throws IOException
+ * thrown if an I/O error occurs
+ */
+ private static void writeAllLinesToTextFile(BufferedWriter bufferedWriter, List<String> lines) throws IOException
+ {
+ for (String line : lines)
+ {
+ bufferedWriter.write(line);
+ // new line character should be added after each string
+ bufferedWriter.write('\n');
+ }
+ }
+
+}
diff -r 61f453c6b172 -r 730c5549c0f9 src/ReportGenerator.java
--- a/src/ReportGenerator.java Mon Jan 09 14:57:59 2012 +0100
+++ b/src/ReportGenerator.java Tue Jan 10 14:08:47 2012 +0100
@@ -43,6 +43,8 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Set;
import java.util.TreeSet;
@@ -56,131 +58,180 @@
*/
public class ReportGenerator
{
+ /**
+ * Read all classes from a file containing its list.
+ *
+ * @param allClassListFileName
+ * @return
+ */
private static Set<String> readAllClasses(String allClassListFileName)
{
- BufferedReader reader = null;
+ // read file content
+ List<String> fileContent = FileUtils.readTextFile(allClassListFileName);
+ // set of classes should be sorted
Set<String> allClasses = new TreeSet<String>();
- try
- {
- reader = new BufferedReader(new FileReader(allClassListFileName));
- String line;
- while ((line = reader.readLine()) != null)
- {
- allClasses.add(line);
- }
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if (reader != null)
- {
- reader.close();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
+ allClasses.addAll(fileContent);
return allClasses;
}
- private static Set<String> preparePackageNames(Set<String> allClasses)
+ /**
+ * Creates file "all_packages.html" which contains link to all checked
+ * packages. Structure of this file is based on template stored in
+ * "templates/all_packages_template.html".
+ *
+ * @param reportDirectory
+ * directory where report is generated
+ * @param allClasses
+ * set of all classes
+ * @param packageNames
+ * set of package names
+ */
+ private static void printPackageListToFile(String reportDirectory, Set<String> allClasses, Set<String> packageNames)
{
- Set<String> packages = new TreeSet<String>();
- for (String className : allClasses)
+ List<String> template = FileUtils.readTextFile("templates/all_packages_template.html");
+ List<String> out = new LinkedList<String>();
+ for (String templateLine : template)
{
- String packageName = className.substring(0, className.lastIndexOf('.'));
- if (!packageName.startsWith("com.") && !packageName.startsWith("sun"))
+ // replace text in template where needed
+ if ("${PACKAGE_LIST}".equals(templateLine))
{
- packages.add(packageName);
+ addPackageList(packageNames, out);
+ }
+ else
+ {
+ out.add(templateLine);
}
}
- return packages;
+ FileUtils.writeTextFile(reportDirectory, "all_packages.html", out);
}
- private static void printPackageListToFile(String reportDirectory, Set<String> allClasses, Set<String> packageNames)
+ /**
+ * Create file containing test coverage report for given package. Structure
+ * of this file is based on template stored in
+ * "templates/all_packages_template.html".
+ *
+ * @param reportDirectory
+ * directory where report is generated
+ * @param packageName
+ * package for which the report is generated
+ * @param testedClasses
+ * set of tested classes
+ */
+ private static void printReportForPackageToFile(String reportDirectory, String packageName, Set<String> testedClasses)
{
- BufferedWriter fout = null;
- try
+ List<String> template = FileUtils.readTextFile("templates/package_template.html");
+ List<String> out = new LinkedList<String>();
+ for (String templateLine : template)
{
- fout = new BufferedWriter(new FileWriter(new File(reportDirectory, "all_packages.html")));
- fout.write("<html>\n");
- fout.write("<body>\n");
- fout.write("<h1>Package list</h1>\n");
- fout.write("<a target='ClassesListFrame' href='all_classes.html'>all classes</a><br /><br />\n");
- for (String packageName : packageNames)
+ // replace text in template where needed
+ if ("${CLASS_LIST}".equals(templateLine))
{
- fout.write("<a target='ClassesListFrame' href='" + packageName + ".html'>" + packageName + "</a><br />\n");
+ addClassList(packageName, testedClasses, out);
}
- fout.write("</body>\n");
- fout.write("</html>\n");
+ else
+ {
+ out.add(templateLine);
+ }
}
- catch (IOException e)
+ FileUtils.writeTextFile(reportDirectory, packageName + ".html", out);
+ }
+
+ /**
+ * Create file containing test coverage report for all classes. Structure of
+ * this file is based on template stored in
+ * "templates/all_classes_template.html".
+ *
+ * @param reportDirectory
+ * directory where report is generated
+ * @param usedPackageNames
+ * all checked package names
+ * @param testedClasses
+ * set of tested classes
+ */
+ private static void printReportForAllClassesInOneFile(String reportDirectory, Set<String> usedPackageNames,
+ Set<String> testedClasses)
+ {
+ List<String> template = FileUtils.readTextFile("templates/all_classes_template.html");
+ List<String> out = new LinkedList<String>();
+ for (String templateLine : template)
{
- e.printStackTrace();
+ // replace text in template where needed
+ if ("${PACKAGE_AND_CLASS_LIST}".equals(templateLine))
+ {
+ addPackageAndClassList(usedPackageNames, testedClasses, out);
+ }
+ else
+ {
+ out.add(templateLine);
+ }
}
- finally
+ FileUtils.writeTextFile(reportDirectory, "all_classes.html", out);
+ }
+
+ /**
+ * Add list of all packages to a list of string which represents generated
+ * report.
+ *
+ * @param packageNames
+ * set of package names
+ * @param out
+ * list of string which represents generated report
+ */
+ private static void addPackageList(Set<String> packageNames, List<String> out)
+ {
+ for (String packageName : packageNames)
{
- try
+ out.add("<a target='ClassesListFrame' href='" + packageName + ".html'>" + packageName + "</a><br />");
+ }
+ }
+
+ /**
+ * Add list of all classes to a list of string which represents generated
+ * report.
+ *
+ * @param packageName
+ * package for which the report is generated
+ * @param testedClasses
+ * set of tested classes
+ * @param out
+ * list of string which represents generated report
+ */
+ private static void addClassList(String packageName, Set<String> testedClasses, List<String> out)
+ {
+ for (String className : testedClasses)
+ {
+ // list only classes from given package
+ if (className.startsWith(packageName))
{
- if (fout != null)
- {
- fout.close();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
+ out.add("<a target='ResultsFrame' href='" + className + ".html'>" + className + "</a><br>");
}
}
}
- private static void createFileForPackage(String reportDirectory, String packageName, Set<String> testedClasses)
+ /**
+ * Add list of all packages and all its classes to a list of string which
+ * represents generated report.
+ *
More information about the distro-pkg-dev
mailing list