1 4 5 package net.sourceforge.groboutils.junit.v1.parser; 6 7 import java.util.Vector ; 8 import java.util.Enumeration ; 9 10 import java.io.PrintWriter ; 11 import java.io.StringWriter ; 12 13 import java.lang.reflect.Method ; 14 import java.lang.reflect.Modifier ; 15 16 import junit.framework.TestSuite; 17 import junit.framework.TestCase; 18 import junit.framework.Test; 19 20 import org.apache.log4j.Logger; 21 22 23 38 public class TestClassParser 39 { 40 private static final Logger LOG = Logger.getLogger( 41 TestClassParser.class ); 42 43 private Class testClass; 44 Vector testMethods = new Vector (); 45 private Vector warnings = new Vector (); 46 47 48 56 public TestClassParser(final Class theClass) 57 { 58 if (theClass == null) 59 { 60 throw new IllegalArgumentException ("no null arguments"); 61 } 62 this.testClass = theClass; 63 64 if (testClass( theClass )) 65 { 66 discoverTestMethods( theClass ); 67 } 68 } 69 70 71 74 75 84 public String [] getWarnings() 85 { 86 String w[] = new String [ this.warnings.size() ]; 87 this.warnings.copyInto( w ); 88 return w; 89 } 90 91 92 95 public void clearWarnings() 96 { 97 this.warnings.removeAllElements(); 98 } 99 100 101 106 public Method [] getTestMethods() 107 { 108 Method m[] = new Method [ this.testMethods.size() ]; 109 this.testMethods.copyInto( m ); 110 return m; 111 } 112 113 114 119 public String getName() 120 { 121 return this.testClass.getName(); 122 } 123 124 125 131 public Class getTestClass() 132 { 133 return this.testClass; 134 } 135 136 137 140 141 148 protected boolean testClass( final Class theClass ) 149 { 150 boolean result = true; 151 if (!Modifier.isPublic( theClass.getModifiers() )) 152 { 153 warning("Class " + theClass.getName() + " is not public."); 154 result = false; 155 } 156 if (!Test.class.isAssignableFrom( theClass )) 157 { 158 warning("Class " + theClass.getName() + 159 " does not implement "+Test.class.getName() ); 160 result = false; 161 } 162 return result; 163 } 164 165 166 172 protected void discoverTestMethods( final Class theClass ) 173 { 174 Class superClass = theClass; 175 Vector names = new Vector (); 176 while (Test.class.isAssignableFrom( superClass )) 177 { 178 Method [] methods = superClass.getDeclaredMethods(); 179 for (int i = 0; i < methods.length; i++) 180 { 181 addTestMethod( methods[i], names ); 182 } 183 superClass = superClass.getSuperclass(); 184 } 185 } 186 187 188 195 protected void addTestMethod( Method m, Vector names ) 196 { 197 String name = m.getName(); 198 if (names.contains(name) || this.testMethods.contains( m )) 199 { 200 return; 201 } 202 203 if (isPublicTestMethod(m)) 204 { 205 names.addElement(name); 206 207 this.testMethods.addElement( m ); 208 } 209 else 210 { 211 if (isTestMethod(m)) 213 { 214 warning("Test method isn't public: "+m.getName()); 215 } 216 } 217 } 218 219 220 228 protected boolean isPublicTestMethod( Method m ) 229 { 230 return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); 231 } 232 233 242 protected boolean isTestMethod( Method m ) 243 { 244 String name= m.getName(); 245 Class [] parameters= m.getParameterTypes(); 246 Class returnType= m.getReturnType(); 247 return parameters.length == 0 && name.startsWith("test") && 248 returnType.equals(Void.TYPE); 249 } 250 251 252 257 protected void warning( final String message ) 258 { 259 LOG.debug( "WARNING: "+message ); 260 this.warnings.addElement( message ); 261 } 262 } 263 | Popular Tags |