1 26 27 package net.sourceforge.groboutils.junit.v1.parser; 28 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.InvocationTargetException ; 31 32 import junit.framework.Test; 33 34 import org.apache.log4j.Logger; 35 36 37 45 public class DelegateTestCreator implements ITestCreator 46 { 47 private static final Logger LOG = Logger.getLogger( 48 DelegateTestCreator.class ); 49 50 private ITestCreator[] creators; 51 52 53 58 public DelegateTestCreator( ITestCreator[] tc ) 59 { 60 if (tc == null || tc.length <= 0) 61 { 62 throw new IllegalArgumentException ("no null args"); 63 } 64 65 int len = tc.length; 66 this.creators = new ITestCreator[ len ]; 67 for (int i = len; --i >= 0;) 68 { 69 if (tc[i] == null) 70 { 71 throw new IllegalArgumentException ("no null args"); 72 } 73 this.creators[i] = tc[i]; 74 } 75 } 76 77 78 85 public boolean canCreate( Class theClass ) 86 { 87 for (int i = this.creators.length; --i >= 0;) 89 { 90 if (this.creators[i].canCreate( theClass )) 91 { 92 return true; 93 } 94 } 95 return false; 96 } 97 98 99 110 public Test createTest( Class theClass, Method method ) 111 throws InstantiationException , NoSuchMethodException , 112 InvocationTargetException , IllegalAccessException , 113 ClassCastException 114 { 115 for (int i = 0; i < this.creators.length; ++i) 117 { 118 ITestCreator tc = this.creators[i]; 119 try 120 { 121 if (tc.canCreate( theClass )) 122 { 123 Test t = tc.createTest( theClass, method ); 124 if (t != null) 125 { 126 return t; 127 } 128 } 129 } 130 catch (InstantiationException e) 131 { 132 LOG.info( "Failed to create test with creator "+tc+".", e ); 133 } 134 catch (NoSuchMethodException e) 135 { 136 LOG.info( "Failed to create test with creator "+tc+".", e ); 137 } 138 catch (InvocationTargetException e) 139 { 140 LOG.info( "Failed to create test with creator "+tc+".", e ); 141 } 142 catch (IllegalAccessException e) 143 { 144 LOG.info( "Failed to create test with creator "+tc+".", e ); 145 } 146 catch (ClassCastException e) 147 { 148 LOG.info( "Failed to create test with creator "+tc+".", e ); 149 } 150 } 151 152 return null; 154 } 155 } 156 157 | Popular Tags |