Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 07:28:57 UTC
27   119   16   9
16   59   0.59   3
3     5.33  
1    
 
  EachClassHasATestCase       Line # 32 27 16 89.1% 0.8913044
 
  (4)
 
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  4 toggle public EachClassHasATestCase() {
48  4 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  4 toggle public void testIfEveryClassIsTested() {
71  4 String prefix = getPackagePrefix();
72  4 if (!prefix.endsWith(".")) {
73  4 prefix += ".";
74    }
75  4 final ClassFinder finder = new ClassFinder();
76  4 final Set allMatchingClassesInPath
77    = finder.findSubclasses(Object.class.getName(), prefix);
78  4 final Iterator i = allMatchingClassesInPath.iterator();
79  4 final Set classesToTest = new TreeSet(ClassFinder.CLASS_COMPARATOR);
80  4 final Set testClasses = new TreeSet(ClassFinder.CLASS_COMPARATOR);
81  1356 while (i.hasNext()) {
82  1352 Class c = (Class) i.next();
83  1352 if (c.getName().indexOf("$") < 0 && !Modifier.isInterface(c.getModifiers())
84    && c.getName().indexOf(".test.") < 0
85    && !c.getName().endsWith("TestSuite")) {
86  470 if (c.getName().endsWith("Test")) {
87    // System.out.println("adding test class " + c);
88  165 testClasses.add(c.getName());
89    } else {
90    // System.out.println(c + " " + finder.getLocationOf(c));
91  305 classesToTest.add(c);
92    }
93   
94   
95    }
96    }
97  4 final Set result = new TreeSet(ClassFinder.CLASS_COMPARATOR);
98  4 final Iterator j = classesToTest.iterator();
99  309 while (j.hasNext()) {
100  305 Class c = (Class) j.next();
101  305 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  171 result.add(c.getName());
105    } else {
106    // System.out.println("no need to test " + c.getName() + " mod: " +c.getModifiers());
107    }
108    }
109  4 if (!result.isEmpty()) {
110  2 System.out.println("The following classes have no test classes:");
111  2 System.out.println(StringUtility.asLines(result));
112  2 if (failIfTestClassesAreMissing()) {
113  0 fail("the following classes have no test classes: "
114    + StringUtility.toString(result));
115    }
116    }
117    }
118   
119    }