Clover Coverage Report
Coverage timestamp: Thu Feb 13 2014 22:50:26 UTC
27   119   16   9
16   59   0.59   3
3     5.33  
1    
 
  EachClassHasATestCase       Line # 32 27 16 73.9% 0.73913044
 
  (1)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.test;
17   
18    import java.lang.reflect.Modifier;
19    import java.util.Iterator;
20    import java.util.Set;
21    import java.util.TreeSet;
22   
23    import junit.framework.TestCase;
24   
25    import org.qedeq.base.utility.StringUtility;
26   
27    /**
28    * Basis class for all tests that check test class existence.
29    *
30    * @author Michael Meyling
31    */
 
32    public abstract class EachClassHasATestCase extends TestCase {
33   
34   
35    /**
36    * Constructor.
37    *
38    * @param name Test case name.
39    */
 
40  0 toggle public EachClassHasATestCase(final String name) {
41  0 super(name);
42    }
43   
44    /**
45    * Constructor.
46    */
 
47  1 toggle public EachClassHasATestCase() {
48  1 super();
49    }
50   
51    /**
52    * Get package name for those classes and the classes of their sub packages where we
53    * want test for tests.
54    *
55    * @return Tests for all classes of this package or its sub packages must occur.
56    */
57    protected abstract String getPackagePrefix();
58   
59    /**
60    * Should the test fail if a test class is missing?
61    * Otherwise the missing classes are just printed to <code>System.out</code>.
62    *
63    * @return Failure if test classes are missing?
64    */
65    protected abstract boolean failIfTestClassesAreMissing();
66   
67    /**
68    * Test if all classes have a test class.
69    */
 
70  1 toggle public void testIfEveryClassIsTested() {
71  1 String prefix = getPackagePrefix();
72  1 if (!prefix.endsWith(".")) {
73  1 prefix += ".";
74    }
75  1 final ClassFinder finder = new ClassFinder();
76  1 final Set allMatchingClassesInPath
77    = finder.findSubclasses(Object.class.getName(), prefix);
78  1 final Iterator i = allMatchingClassesInPath.iterator();
79  1 final Set classesToTest = new TreeSet(ClassFinder.CLASS_COMPARATOR);
80  1 final Set testClasses = new TreeSet(ClassFinder.CLASS_COMPARATOR);
81  136 while (i.hasNext()) {
82  135 Class c = (Class) i.next();
83  135 if (c.getName().indexOf("$") < 0 && !Modifier.isInterface(c.getModifiers())
84    && c.getName().indexOf(".test.") < 0
85    && !c.getName().endsWith("TestSuite")) {
86  42 if (c.getName().endsWith("Test")) {
87    // System.out.println("adding test class " + c);
88  21 testClasses.add(c.getName());
89    } else {
90    // System.out.println(c + " " + finder.getLocationOf(c));
91  21 classesToTest.add(c);
92    }
93   
94   
95    }
96    }
97  1 final Set result = new TreeSet(ClassFinder.CLASS_COMPARATOR);
98  1 final Iterator j = classesToTest.iterator();
99  22 while (j.hasNext()) {
100  21 Class c = (Class) j.next();
101  21 if (!testClasses.contains(c.getName() + "Test") && !c.getName().endsWith("TestCase")
102    && !c.getName().endsWith("Tester")) {
103    // System.out.println("missing test for " + c.getName() + " mod: " +c.getModifiers());
104  0 result.add(c.getName());
105    } else {
106    // System.out.println("no need to test " + c.getName() + " mod: " +c.getModifiers());
107    }
108    }
109  1 if (!result.isEmpty()) {
110  0 System.out.println("The following classes have no test classes:");
111  0 System.out.println(StringUtility.asLines(result));
112  0 if (failIfTestClassesAreMissing()) {
113  0 fail("the following classes have no test classes: "
114    + StringUtility.toString(result));
115    }
116    }
117    }
118   
119    }