001: package com.mockrunner.test.consistency;
002:
003: import java.lang.reflect.Method;
004: import java.lang.reflect.Modifier;
005: import java.util.ArrayList;
006: import java.util.Arrays;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011:
012: import junit.framework.TestCase;
013:
014: import com.mockrunner.base.BasicHTMLOutputTestCase;
015: import com.mockrunner.base.BasicWebTestCase;
016: import com.mockrunner.base.HTMLOutputModule;
017: import com.mockrunner.base.HTMLOutputTestCase;
018: import com.mockrunner.base.WebTestCase;
019: import com.mockrunner.base.WebTestModule;
020: import com.mockrunner.connector.BasicConnectorTestCaseAdapter;
021: import com.mockrunner.connector.ConnectorTestCaseAdapter;
022: import com.mockrunner.connector.ConnectorTestModule;
023: import com.mockrunner.ejb.BasicEJBTestCaseAdapter;
024: import com.mockrunner.ejb.EJBTestCaseAdapter;
025: import com.mockrunner.ejb.EJBTestModule;
026: import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
027: import com.mockrunner.jdbc.JDBCTestCaseAdapter;
028: import com.mockrunner.jdbc.JDBCTestModule;
029: import com.mockrunner.jms.BasicJMSTestCaseAdapter;
030: import com.mockrunner.jms.JMSTestCaseAdapter;
031: import com.mockrunner.jms.JMSTestModule;
032: import com.mockrunner.servlet.BasicServletTestCaseAdapter;
033: import com.mockrunner.servlet.ServletTestCaseAdapter;
034: import com.mockrunner.servlet.ServletTestModule;
035: import com.mockrunner.struts.ActionTestCaseAdapter;
036: import com.mockrunner.struts.ActionTestModule;
037: import com.mockrunner.struts.BasicActionTestCaseAdapter;
038: import com.mockrunner.tag.BasicTagTestCaseAdapter;
039: import com.mockrunner.tag.TagTestCaseAdapter;
040: import com.mockrunner.tag.TagTestModule;
041:
042: public class AdapterConsistencyTest extends TestCase {
043: private Map adapterMap;
044: private Map exceptionMap;
045: private List failureList;
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: failureList = new ArrayList();
050: adapterMap = new HashMap();
051: exceptionMap = new HashMap();
052: initializeTestModules();
053: }
054:
055: protected void initializeTestModules() {
056: addAdapter(ServletTestModule.class,
057: ServletTestCaseAdapter.class);
058: addAdapter(ServletTestModule.class,
059: BasicServletTestCaseAdapter.class);
060: addExceptionMethod(ServletTestModule.class, "getOutput");
061:
062: addAdapter(TagTestModule.class, TagTestCaseAdapter.class);
063: addAdapter(TagTestModule.class, BasicTagTestCaseAdapter.class);
064: addExceptionMethod(TagTestModule.class, "getOutput");
065:
066: addAdapter(ActionTestModule.class, ActionTestCaseAdapter.class);
067: addAdapter(ActionTestModule.class,
068: BasicActionTestCaseAdapter.class);
069: addExceptionMethod(ActionTestModule.class, "getOutput");
070:
071: addAdapter(EJBTestModule.class, EJBTestCaseAdapter.class);
072: addAdapter(EJBTestModule.class, BasicEJBTestCaseAdapter.class);
073:
074: addAdapter(JDBCTestModule.class, JDBCTestCaseAdapter.class);
075: addAdapter(JDBCTestModule.class, BasicJDBCTestCaseAdapter.class);
076:
077: addAdapter(JMSTestModule.class, JMSTestCaseAdapter.class);
078: addAdapter(JMSTestModule.class, BasicJMSTestCaseAdapter.class);
079:
080: addAdapter(ConnectorTestModule.class,
081: ConnectorTestCaseAdapter.class);
082: addAdapter(ConnectorTestModule.class,
083: BasicConnectorTestCaseAdapter.class);
084:
085: addAdapter(HTMLOutputModule.class, HTMLOutputTestCase.class);
086: addAdapter(HTMLOutputModule.class,
087: BasicHTMLOutputTestCase.class);
088:
089: addAdapter(WebTestModule.class, WebTestCase.class);
090: addAdapter(WebTestModule.class, BasicWebTestCase.class);
091: }
092:
093: protected void addAdapter(Class module, Class adapter) {
094: List adapterList = (List) adapterMap.get(module);
095: if (null == adapterList) {
096: adapterList = new ArrayList();
097: adapterMap.put(module, adapterList);
098: exceptionMap.put(module, new ArrayList());
099: }
100: adapterList.add(adapter);
101: }
102:
103: protected void addExceptionMethod(Class module, String method) {
104: List exceptionList = (List) exceptionMap.get(module);
105: if (null == exceptionList) {
106: exceptionList = new ArrayList();
107: exceptionMap.put(module, exceptionList);
108: }
109: exceptionList.add(method);
110: }
111:
112: public void testAdapterConsistency() {
113: Iterator modules = adapterMap.keySet().iterator();
114: while (modules.hasNext()) {
115: Class currentModule = (Class) modules.next();
116: List adapterList = (List) adapterMap.get(currentModule);
117: for (int ii = 0; ii < adapterList.size(); ii++) {
118: Class currentAdapter = (Class) adapterList.get(ii);
119: doTestAdapter(currentModule, currentAdapter);
120: }
121: }
122: dumpFailures();
123: }
124:
125: private void doTestAdapter(Class currentModule, Class currentAdapter) {
126: Method[] adapterMethods = currentAdapter.getDeclaredMethods();
127: Method[] moduleMethods = currentModule.getDeclaredMethods();
128: for (int ii = 0; ii < moduleMethods.length; ii++) {
129: Method currentMethod = moduleMethods[ii];
130: if (shouldMethodBeChecked(currentMethod, currentModule)) {
131: if (!isMethodDeclared(currentMethod, adapterMethods)) {
132: addFailureEntry(currentModule, currentAdapter,
133: currentMethod);
134: }
135: }
136: }
137: }
138:
139: private boolean shouldMethodBeChecked(Method theMethod, Class module) {
140: if (!Modifier.isPublic(theMethod.getModifiers()))
141: return false;
142: List exceptionMethods = (List) exceptionMap.get(module);
143: if (null != exceptionMethods
144: && exceptionMethods.contains(theMethod.getName())) {
145: return false;
146: }
147: return true;
148: }
149:
150: private boolean isMethodDeclared(Method theMethod,
151: Method[] otherClassMethods) {
152: for (int ii = 0; ii < otherClassMethods.length; ii++) {
153: Method currentMethod = otherClassMethods[ii];
154: if (areMethodsEqual(theMethod, currentMethod)) {
155: return true;
156: }
157: }
158: return false;
159: }
160:
161: private boolean areMethodsEqual(Method aMethod, Method anotherMethod) {
162: if (Modifier.isPrivate(anotherMethod.getModifiers()))
163: return false;
164: if (!aMethod.getName().equals(anotherMethod.getName()))
165: return false;
166: if (!aMethod.getReturnType().equals(
167: anotherMethod.getReturnType()))
168: return false;
169: Class[] aMethodParams = aMethod.getParameterTypes();
170: Class[] anotherMethodParams = anotherMethod.getParameterTypes();
171: return Arrays.equals(aMethodParams, anotherMethodParams);
172: }
173:
174: private void addFailureEntry(Class currentModule,
175: Class currentAdapter, Method missingMethod) {
176: failureList.add(new FailureEntry(currentModule, currentAdapter,
177: missingMethod));
178: }
179:
180: private void dumpFailures() {
181: for (int ii = 0; ii < failureList.size(); ii++) {
182: FailureEntry currentEntry = (FailureEntry) failureList
183: .get(ii);
184: Class currentModule = currentEntry.getModule();
185: Class currentAdapter = currentEntry.getAdapter();
186: Method currentMethod = currentEntry.getMethod();
187: System.out.println("Failure in " + currentAdapter.getName()
188: + " (Module: " + currentModule.getName() + ")");
189: System.out.println("The following method is missing:");
190: System.out.println();
191: Class[] params = currentMethod.getParameterTypes();
192: System.out.println("Name: " + currentMethod.getName());
193: System.out.println("Return type: "
194: + currentMethod.getReturnType().getName());
195: if (null != params && params.length > 0) {
196: System.out.print("Parameter: ");
197: for (int yy = 0; yy < params.length; yy++) {
198: Class currentParam = params[yy];
199: System.out.print(currentParam.getName() + "; ");
200: }
201: System.out.println();
202: }
203: System.out.println();
204: }
205: assertTrue(failureList.isEmpty());
206: }
207:
208: private class FailureEntry {
209: private Class module;
210: private Class adapter;
211: private Method method;
212:
213: public FailureEntry(Class module, Class adapter, Method method) {
214: this .module = module;
215: this .adapter = adapter;
216: this .method = method;
217: }
218:
219: public Class getAdapter() {
220: return adapter;
221: }
222:
223: public Method getMethod() {
224: return method;
225: }
226:
227: public Class getModule() {
228: return module;
229: }
230: }
231: }
|