Mercurial > hg > MauveTestCoverage
changeset 2:8612fcdfab82
2012-01-06 Pavel Tisnovsky <ptisnovs@redhat.com>
* src/PrintClassList.java:
Fixed: closing Java archive when it is read
Refactoring, added JavaDoc to all methods
* src/PrintTestCoverage.java:
Fixed exception thrown if the log file does not exists
It's ok because some classes are not covered by tests at all.
author | Pavel Tisnovsky <ptisnovs@redhat.com> |
---|---|
date | Fri, 06 Jan 2012 12:25:10 +0100 |
parents | 17f81193758a |
children | 61f453c6b172 |
files | ChangeLog src/PrintClassList.java src/PrintTestCoverage.java |
diffstat | 3 files changed, 85 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Thu Jan 05 16:38:45 2012 +0100 +++ b/ChangeLog Fri Jan 06 12:25:10 2012 +0100 @@ -1,6 +1,15 @@ +2012-01-06 Pavel Tisnovsky <ptisnovs@redhat.com> + + * src/PrintClassList.java: + Fixed: closing Java archive when it is read + Refactoring, added JavaDoc to all methods + * src/PrintTestCoverage.java: + Fixed exception thrown if the log file does not exists + It's ok because some classes are not covered by tests at all. + 2012-01-05 Pavel Tisnovsky <ptisnovs@redhat.com> - * adding Makefile: + * Makefile: Make sure that ./bin and ./report directories are created during build.
--- a/src/PrintClassList.java Thu Jan 05 16:38:45 2012 +0100 +++ b/src/PrintClassList.java Fri Jan 06 12:25:10 2012 +0100 @@ -45,49 +45,95 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; + + /** - * Generate and print list of all public classes which are stored in given JAR - * archive. This JAR archive should be accessible through classloader (this - * means that -cp should be used in certain situations). + * This class is used to generate and print list of all public classes which are + * stored in specified JAR archive. This JAR archive should be accessible + * through classloader (this means that -cp should be used in certain + * situations). * - * This tool could be used against "rt.jar" + * This tool could be used (and is plannet to be used) against "rt.jar" * * @author Pavel Tisnovsky */ public class PrintClassList { /** - * Generate and print sorted list of public classes. + * Default path to system-wide rt.jar. + */ + private static final String DEFAULT_PATH_TO_SYSTEM_RT_JAR = "/usr/lib/jvm/java-1.6.0/jre/lib/rt.jar"; + + /** + * Generate and print sorted list of public all classes. * * @param pathToJarArchive * path to rt.jar or other JAR archive to be investigated. */ private static void generateClassList(String pathToJarArchive) { + // it's better to print sorted class names + // and TreeSet sorted its elements by default + Set<String> setOfClassNames = new TreeSet<String>(); + // try to read all public classes from Java archive + readAllPublicClassesFromJarFile(pathToJarArchive, setOfClassNames); + // now we have the list filled, time to print them + printAllPublicClassNames(setOfClassNames); + } + + /** + * Read all public classes from Java archive + * + * @param pathToJarArchive + * path to Java archive + * @param setOfClassNames + * set containing all public class names + */ + private static void readAllPublicClassesFromJarFile(String pathToJarArchive, Set<String> setOfClassNames) + { + JarFile jarFile = null; try { // open the archive and acquire all its entries - JarFile jarFile = new JarFile(pathToJarArchive); - Enumeration<JarEntry> entries = jarFile.entries(); + jarFile = new JarFile(pathToJarArchive); - // it's better to print sorted class names - // and TreeSet sorted its elements by default - Set<String> setOfClassNames = new TreeSet<String>(); + // entries inside Java archive + Enumeration<JarEntry> entries = jarFile.entries(); // test each JAR entry while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String className = generateClassName(entry.getName()); - // only public classes are interesting + // only public classes are interesting at this moment if (isPublicClass(className)) { setOfClassNames.add(className); } } - // now we have the list filled, time to print it - for (String className : setOfClassNames) { - System.out.println(className); - } } catch (IOException e) { e.printStackTrace(); } + finally { + // Java archive should be closed in all cases + if (jarFile != null) { + try { + jarFile.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * Print all public class names + * + * @param setOfClassNames + * set containing all public class names + */ + private static void printAllPublicClassNames(Set<String> setOfClassNames) + { + for (String className : setOfClassNames) { + System.out.println(className); + } } /** @@ -102,7 +148,7 @@ private static boolean isPublicClass(String className) { try { Class clazz = Class.forName(className); - // interfaces are not our job + // interfaces are not our job at this moment if (clazz.isInterface()) { return false; } @@ -114,26 +160,26 @@ } catch (ClassNotFoundException e) { // it might happen because jar file could - // include other files + // include other types of files files return false; } catch (UnsatisfiedLinkError e) { - // it might happen too + // it might happen too in some cases return false; } catch (ExceptionInInitializerError e) { - // it might happen too + // it might happen too in some cases return false; } catch (NoClassDefFoundError e) { - // it might happen too + // it might happen too in some cases return false; } return true; } /** - * Tries to change given JAR entry into proper class name. + * Try to change given JAR entry into proper class name. * * @param name * JAR entry name @@ -160,9 +206,12 @@ * * @param args * first argument should contains path to JAR file. + * If the first argument does not exists, constant + * path is used instead. */ public static void main(String[] args) { - String pathToRtJar = "/usr/lib/jvm/java-1.6.0/jre/lib/rt.jar"; + String pathToRtJar = DEFAULT_PATH_TO_SYSTEM_RT_JAR; + // path to rt.jar could be specified as first command line argument if (args.length == 1) { pathToRtJar = args[0]; }
--- a/src/PrintTestCoverage.java Thu Jan 05 16:38:45 2012 +0100 +++ b/src/PrintTestCoverage.java Fri Jan 06 12:25:10 2012 +0100 @@ -818,9 +818,12 @@ Set<String> methodSet = new TreeSet<String>(); String className = testedClassName.replace('.', '/'); File list = new File(pathToTests, className); + + /* check if directory containing tests for given class name exists */ if (!list.isDirectory()) { - throw new RuntimeException(list.getAbsolutePath() + " is not a proper directory name"); + System.err.println(list.getAbsolutePath() + " is not a proper directory name"); + return; } for (String fileName : list.list()) {