1 22 package org.jboss.test; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Modifier ; 26 import java.util.Properties ; 27 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 38 public class JBossTestSuite extends TestSuite 39 { 40 protected Properties props; 41 42 public JBossTestSuite(Properties props) 43 { 44 this.props = props; 45 } 46 47 public JBossTestSuite(Class theClass, String name, Properties props) 48 { 49 if (name == null) 50 name = theClass.getName(); 51 super.setName(name); 52 this.props = props; 53 54 Class superClass = theClass; 55 while (Test.class.isAssignableFrom(superClass)) 56 { 57 Method [] methods = superClass.getDeclaredMethods(); 58 Method setProps = null; 59 try 60 { 61 Class [] sig = {Properties .class}; 62 setProps = superClass.getMethod("setProps", sig); 63 } 64 catch (Throwable ignore) 65 { 66 } 67 for (int i = 0; i < methods.length; i++) 68 { 69 Method m = methods[i]; 70 if( isPublicTestMethod(m) == false ) 71 continue; 72 String testName = m.getName(); 73 Test test = createTest(theClass, testName); 74 if (setProps != null) 75 { 76 Object [] args = {props}; 77 try 78 { 79 setProps.invoke(test, args); 80 } 81 catch (Throwable t) 82 { 83 test = failure(t); 84 } 85 } 86 super.addTest(test); 87 } 88 superClass = superClass.getSuperclass(); 89 } 90 } 91 92 public JBossTestSuite(Class theClass, Properties props) 93 { 94 this(theClass, null, props); 95 } 96 97 public JBossTestSuite(String name, Properties props) 98 { 99 super(name); 100 this.props = props; 101 } 102 103 public void addTestSuite(Class testClass) 104 { 105 super.addTest(new JBossTestSuite(testClass, props)); 106 } 107 108 public static boolean isPublicTestMethod(Method m) 109 { 110 return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); 111 } 112 113 public static boolean isTestMethod(Method m) 114 { 115 String name = m.getName(); 116 Class [] parameters = m.getParameterTypes(); 117 Class returnType = m.getReturnType(); 118 return parameters.length == 0 && name.startsWith("test") 119 && returnType.equals(Void.TYPE); 120 } 121 122 125 private static Test failure(final Throwable t) 126 { 127 return new TestCase("failure") 128 { 129 protected void runTest() 130 { 131 fail(t.getMessage()); 132 } 133 }; 134 } 135 } 136 | Popular Tags |