1 26 27 package net.sourceforge.groboutils.junit.v1.parser; 28 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.InvocationTargetException ; 34 import java.lang.reflect.Constructor ; 35 36 import net.sourceforge.groboutils.junit.v1.iftc.ImplFactory; 37 38 39 47 public class IftcOrigCreator implements ITestCreator 48 { 49 private ImplFactory factories[]; 50 51 52 59 public IftcOrigCreator( ImplFactory[] f ) 60 { 61 if (f == null) 62 { 63 throw new IllegalArgumentException ("no null args"); 64 } 65 66 int len = f.length; 68 this.factories = new ImplFactory[ len ]; 69 for (int i = len; --i >= 0;) 70 { 71 if (f[i] == null) 72 { 73 throw new IllegalArgumentException ("no null args"); 74 } 75 this.factories[i] = f[i]; 76 } 77 } 78 79 80 96 public Test createTest( Class theClass, Method method ) 97 throws InstantiationException , NoSuchMethodException , 98 InvocationTargetException , IllegalAccessException , 99 ClassCastException 100 { 101 TestSuite suite = new TestSuite(); 102 103 int goodTestCount = 0; 104 for (int i = 0; i < this.factories.length; ++i) 105 { 106 Test t = createTest( theClass, createTestArguments( theClass, 107 method, this.factories[i] ) ); 108 if (t != null) 109 { 110 ++goodTestCount; 111 suite.addTest( t ); 112 } 113 } 114 115 if (goodTestCount <= 0) 116 { 117 suite.addTest( TestClassCreator.createWarningTest( 118 "No factories or valid instances for test class "+ 119 theClass.getName()+", method "+method.getName()+"." ) ); 120 } 121 122 return suite; 123 } 124 125 126 131 public boolean canCreate( Class theClass ) 132 { 133 try 134 { 135 Constructor c = getConstructor( theClass ); 136 return (c != null); 137 } 138 catch (Exception ex) 139 { 140 return false; 141 } 142 } 143 144 145 161 protected Constructor getConstructor( final Class theClass ) 162 throws NoSuchMethodException 163 { 164 return theClass.getConstructor( 165 getConstructorArgTypes( theClass ) ); 166 } 167 168 169 175 protected Class [] getConstructorArgTypes( Class theClass ) 176 { 177 183 184 return new Class [] { 185 String .class, 186 ImplFactory.class 187 }; 188 } 189 190 191 197 protected Object [] createTestArguments( Class theClass, Method method, 198 ImplFactory implf ) 199 { 200 return new Object [] { method.getName(), implf }; 201 } 202 203 204 216 protected Test createTest( Class theClass, Object [] constructorArgs ) 217 throws InstantiationException , NoSuchMethodException , 218 InvocationTargetException , IllegalAccessException , 219 ClassCastException 220 { 221 Constructor c = getConstructor( theClass ); 222 Test t; 223 try 224 { 225 t = (Test)c.newInstance( constructorArgs ); 226 } 227 catch (IllegalArgumentException iae) 228 { 229 StringBuffer args = new StringBuffer ( 230 "Arguments didn't match for constructor " ); 231 args.append( c ).append( " in class " ).append( 232 theClass.getName() ).append( ". Arguments = [" ); 233 for (int i = 0; i < constructorArgs.length; ++i) 234 { 235 if (i > 0) 236 { 237 args.append( ", " ); 238 } 239 args.append( constructorArgs[i].getClass().getName() ). 240 append( " = '" ). 241 append( constructorArgs[i] ). 242 append( "'" ); 243 } 244 args.append("]: ").append( iae ); 245 throw new InstantiationException ( args.toString() ); 246 } 247 return t; 248 } 249 250 251 252 253 254 269 270 } 271 272 | Popular Tags |