1 20 package org.apache.cactus.internal.util; 21 22 import java.lang.reflect.Method ; 23 import java.lang.reflect.Modifier ; 24 25 import junit.framework.Test; 26 27 import org.apache.cactus.Request; 28 29 33 public final class TestCaseImplementChecker 34 { 35 41 private TestCaseImplementChecker() 42 { 43 } 44 45 50 public static void checkTestName(Test theTest) 51 throws TestCaseImplementError 52 { 53 if (theTest == null) 54 { 55 return; 56 } 57 58 if (JUnitVersionHelper.getTestCaseName(theTest) == null) 59 { 60 throw new TestCaseImplementError("No test name found. The test [" 61 + theTest.getClass().getName() 62 + "] is not properly implemented."); 63 } 64 } 65 66 70 private static String numeric(int theNum) 71 { 72 switch(theNum) 73 { 74 case 1: return "1st"; 75 case 2: return "2nd"; 76 case 3: return "3rd"; 77 default: return (theNum + "th"); 78 } 79 } 80 81 86 private static void checkReturnType(Method theMethod, Class theType) 87 throws TestCaseImplementError 88 { 89 if (!theMethod.getReturnType().equals(theType)) 90 { 91 throw new TestCaseImplementError("The method [" 92 + theMethod.getName() 93 + "] should return " + theType 94 + " and not [" + theMethod.getReturnType().getName() 95 + "]"); 96 } 97 } 98 99 103 private static void isPublic(Method theMethod) 104 throws TestCaseImplementError 105 { 106 if (!Modifier.isPublic(theMethod.getModifiers())) 107 { 108 throw new TestCaseImplementError("The method [" 109 + theMethod.getName() 110 + "] should be declared public"); 111 } 112 } 113 114 120 private static void checkParameterCount(Method theMethod, Class [] theParams) 121 throws TestCaseImplementError 122 { 123 Class [] parameters = theMethod.getParameterTypes(); 124 if (parameters.length != theParams.length) 125 { 126 throw new TestCaseImplementError("The method [" 127 + theMethod.getName() 128 + "] must have " + theParams.length + " parameter(s), " 129 + "but " + parameters.length + " parameter(s) were found"); 130 } 131 } 132 133 139 private static void checkParameterTypes(Method theMethod, Class [] theParams) 140 throws TestCaseImplementError 141 { 142 checkParameterCount(theMethod, theParams); 143 144 Class [] parameters = theMethod.getParameterTypes(); 145 for (int i = 0; i < parameters.length; i++) 146 { 147 Class expected = theParams[i]; 148 Class actual = parameters[i]; 149 if (!expected.isAssignableFrom(actual)) 150 { 151 throw new TestCaseImplementError("The method [" 152 + theMethod.getName() 153 + "] must accept [" + expected.getName() + "] as " 154 + numeric(i + 1) 155 + " parameter, but found a [" 156 + actual.getName() + "] parameter instead"); 157 } 158 } 159 } 160 161 167 private static void checkAsCactusMethod(Method theMethod) 168 throws TestCaseImplementError 169 { 170 checkReturnType(theMethod, Void.TYPE); 171 isPublic(theMethod); 172 } 173 174 187 public static void checkAsBeginMethod(Method theMethod) 188 throws TestCaseImplementError 189 { 190 checkAsCactusMethod(theMethod); 191 checkParameterTypes(theMethod, new Class []{Request.class}); 192 } 193 194 207 public static void checkAsEndMethod(Method theMethod) 208 throws TestCaseImplementError 209 { 210 checkAsCactusMethod(theMethod); 211 checkParameterCount(theMethod, new Class []{Object .class}); 212 } 213 } 214 | Popular Tags |