1 26 27 package net.sourceforge.groboutils.junit.v1.parser; 28 29 import java.util.Vector ; 30 import java.util.Enumeration ; 31 import java.io.PrintWriter ; 32 import java.io.StringWriter ; 33 34 import java.lang.reflect.Method ; 35 import java.lang.reflect.InvocationTargetException ; 36 37 import junit.framework.TestSuite; 38 import junit.framework.TestCase; 39 import junit.framework.Test; 40 41 import org.apache.log4j.Logger; 42 43 44 59 public class TestClassCreator 60 { 61 private static final Logger LOG = Logger.getLogger( 62 TestClassCreator.class ); 63 64 private ITestCreator creator; 65 private Vector warnings = new Vector (); 66 67 68 75 public TestClassCreator( final ITestCreator tc ) 76 { 77 if (tc == null) 78 { 79 throw new IllegalArgumentException ("no null arguments"); 80 } 81 this.creator = tc; 82 } 83 84 85 88 89 98 public String [] getWarnings() 99 { 100 String w[] = new String [ this.warnings.size() ]; 101 this.warnings.copyInto( w ); 102 return w; 103 } 104 105 106 109 public void clearWarnings() 110 { 111 this.warnings.removeAllElements(); 112 } 113 114 115 122 public Test[] createTests( TestClassParser tcp ) 123 { 124 Vector t = new Vector (); 125 if (this.creator.canCreate( tcp.getTestClass() )) 126 { 127 Method m[] = tcp.getTestMethods(); 128 for (int i = 0; i < m.length; ++i) 129 { 130 try 131 { 132 Test tt = this.creator.createTest( 133 tcp.getTestClass(), m[i] ); 134 if (tt != null) 135 { 136 t.addElement( tt ); 137 } 138 else 139 { 140 warning( "Could not create test for class " + 141 tcp.getTestClass().getName() + " and method " + 142 m[i].getName() + "." ); 143 } 144 } 145 catch (InstantiationException ie) 146 { 147 warning( "Method " + m[i].getName() + 148 " could not be added as a test: " + 149 ie ); 150 } 151 catch (NoSuchMethodException nsme) 152 { 153 warning( "No valid constructor for " + 154 tcp.getTestClass().getName() + 155 ": " + nsme ); 156 } 157 catch (InvocationTargetException ite) 158 { 159 warning( "Construction of class " + 160 tcp.getTestClass().getName() + 161 " caused an exception: "+ ite.getTargetException() ); 162 } 163 catch (IllegalAccessException iae) 164 { 165 warning( "Protection on constructor for class "+ 166 tcp.getTestClass().getName() + 167 " was invalid: " + iae ); 168 } 169 catch (ClassCastException cce) 170 { 171 warning( "Class " + tcp.getTestClass().getName() + 172 " is not of Test type." ); 173 } 174 } 175 } 176 else 177 { 178 warning( "TestCreator does not know how to handle class " + 179 tcp.getTestClass().getName() + "." ); 180 } 181 182 Test tt[] = new Test[ t.size() ]; 183 t.copyInto( tt ); 184 return tt; 185 } 186 187 188 196 public Test[] createWarningTests( TestClassParser tcp ) 197 { 198 String s1[] = getWarnings(); 199 String s2[] = tcp.getWarnings(); 200 Test t[] = new Test[ s1.length + s2.length ]; 201 for (int i = 0; i < s1.length; ++i) 202 { 203 t[i] = createWarningTest( s1[i] ); 204 } 205 for (int i = 0; i < s2.length; ++i) 206 { 207 t[i+s1.length] = createWarningTest( s2[i] ); 208 } 209 return t; 210 } 211 212 213 219 public TestSuite createTestSuite( TestClassParser tcp ) 220 { 221 TestSuite ts = new TestSuite( tcp.getName() ); 222 Test t[] = createTests( tcp ); 223 for (int i = 0; i < t.length; ++i) 224 { 225 if (t[i] != null) 226 { 227 ts.addTest( t[i] ); 228 } 229 } 230 return ts; 231 } 232 233 234 241 public TestSuite createAllTestSuite( TestClassParser tcp ) 242 { 243 TestSuite ts = createTestSuite( tcp ); 244 Test t[] = createWarningTests( tcp ); 245 for (int i = 0; i < t.length; ++i) 246 { 247 if (t[i] != null) 248 { 249 ts.addTest( t[i] ); 250 } 251 } 252 return ts; 253 } 254 255 256 263 public static Test createWarningTest( final String message ) 264 { 265 return new TestCase( "warning" ) { 266 protected void runTest() { 267 fail( message ); 268 } 269 }; 270 } 271 272 273 274 277 278 283 protected void warning( final String message ) 284 { 285 LOG.info( "WARNING: "+message ); 286 this.warnings.addElement( message ); 287 } 288 } 289 | Popular Tags |