1 26 27 package net.sourceforge.groboutils.junit.v1.parser; 28 29 import junit.framework.Test; 30 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.InvocationTargetException ; 33 import java.lang.reflect.Constructor ; 34 35 36 43 public class JUnitOrigCreator implements ITestCreator 44 { 45 61 public Test createTest( Class theClass, Method method ) 62 throws InstantiationException , NoSuchMethodException , 63 InvocationTargetException , IllegalAccessException , 64 ClassCastException 65 { 66 return createTest( theClass, 67 createTestArguments( theClass, method ) ); 68 } 69 70 71 76 public boolean canCreate( Class theClass ) 77 { 78 try 79 { 80 Constructor c = getConstructor( theClass ); 81 return (c != null); 82 } 83 catch (Exception ex) 84 { 85 return false; 86 } 87 } 88 89 90 106 protected Constructor getConstructor( final Class theClass ) 107 throws NoSuchMethodException 108 { 109 return theClass.getConstructor( 110 getConstructorArgTypes( theClass ) ); 111 } 112 113 114 121 protected Class [] getConstructorArgTypes( Class theClass ) 122 { 123 return new Class [] { String .class }; 124 } 125 126 127 133 protected Object [] createTestArguments( Class theClass, Method method ) 134 { 135 return new Object [] { method.getName() }; 136 } 137 138 139 151 protected Test createTest( Class theClass, Object [] constructorArgs ) 152 throws InstantiationException , NoSuchMethodException , 153 InvocationTargetException , IllegalAccessException , 154 ClassCastException 155 { 156 Constructor c = getConstructor( theClass ); 157 Test t; 158 try 159 { 160 t = (Test)c.newInstance( constructorArgs ); 161 } 162 catch (IllegalArgumentException iae) 163 { 164 StringBuffer args = new StringBuffer ( 165 "Arguments didn't match for constructor " ); 166 args.append( c ).append( " in class " ).append( 167 theClass.getName() ).append( ". Arguments = [" ); 168 for (int i = 0; i < constructorArgs.length; ++i) 169 { 170 if (i > 0) 171 { 172 args.append( ", " ); 173 } 174 args.append( constructorArgs[i].getClass().getName() ). 175 append( " = '" ). 176 append( constructorArgs[i] ). 177 append( "'" ); 178 } 179 args.append("]: ").append( iae ); 180 throw new InstantiationException ( args.toString() ); 181 } 182 return t; 183 } 184 } 185 186 | Popular Tags |