001: /*
002: * ========================================================================
003: *
004: * Copyright 2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.util;
021:
022: import java.lang.reflect.Method;
023: import junit.framework.AssertionFailedError;
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import org.apache.cactus.Request;
027: import org.apache.cactus.WebResponse;
028:
029: /**
030: * Unit tests for the {@link TestCaseImplementChecker} class.
031: *
032: * @version $Id: TestTestCaseImplementChecker.java 238991 2004-05-22 11:34:50Z vmassol $
033: */
034: public class TestTestCaseImplementChecker extends TestCase {
035: /**
036: * normal test class for {@link #testCheckTestName}
037: */
038: class NormalTest extends TestCase {
039: /**
040: * @param theName the name of the test
041: */
042: public NormalTest(String theName) {
043: super (theName);
044: }
045:
046: /**
047: * dummy test entry
048: */
049: public void testDummy() {
050: }
051: }
052:
053: /**
054: * illegal test class for {@link #testCheckTestName}
055: */
056: class NoNameTest extends TestCase {
057: /**
058: * @param theName the name of the test
059: */
060: public NoNameTest(String theName) {
061: }
062:
063: /**
064: * dummy test entry
065: */
066: public void testDummy() {
067: }
068: }
069:
070: /**
071: * declare methods to be used by {@link #testCheckAsBeginMethod} and
072: * {@link #testCheckAsEndMethod}
073: */
074: class MethodHolder {
075: /**
076: */
077: public MethodHolder() {
078: }
079:
080: // ---- Begin Method ----
081:
082: /**
083: * @param theRequest a Request
084: */
085: public void beginNormal(Request theRequest) {
086: }
087:
088: /**
089: * @param theRequest a Request
090: * @return a dummy String
091: */
092: public String beginReturnsString(Request theRequest) {
093: return "a string";
094: }
095:
096: /**
097: * @param theRequest a Request
098: */
099: protected void beginProtected(Request theRequest) {
100: }
101:
102: /**
103: * @param theRequest a Request
104: */
105: private void beginPrivate(Request theRequest) {
106: }
107:
108: /**
109: */
110: public void beginNoParam() {
111: }
112:
113: /**
114: * @param theRequest a Request
115: * @param theObject a Object
116: */
117: public void beginWithTwoParams(Request theRequest,
118: Object theObject) {
119: }
120:
121: /**
122: * @param theString a String
123: */
124: public void beginWithStringParam(String theString) {
125: }
126:
127: // ---- End Method ----
128:
129: /**
130: * @param theResponse a WebResponse
131: */
132: public void endNormal(WebResponse theResponse) {
133: }
134:
135: /**
136: * @param theResponse a WebResponse
137: * @return a dummy String
138: */
139: public String endReturnsString(WebResponse theResponse) {
140: return "a string";
141: }
142:
143: /**
144: * @param theResponse a WebResponse
145: */
146: protected void endProtected(WebResponse theResponse) {
147: }
148:
149: /**
150: * @param theResponse a WebResponse
151: */
152: private void endPrivate(WebResponse theResponse) {
153: }
154:
155: /**
156: */
157: public void endNoParam() {
158: }
159:
160: /**
161: * @param theResponse a WebResponse
162: * @param theObject a Object
163: */
164: public void endWithTwoParams(WebResponse theResponse,
165: Object theObject) {
166: }
167:
168: /**
169: * @param theString a String
170: */
171: public void endWithStringParam(String theString) {
172: }
173: }
174:
175: /**
176: * @return a "Should not be here" message
177: */
178: private String shouldNotHere() {
179: return "shold not be here";
180: }
181:
182: /**
183: * @param theThrowable a throwable
184: * @return a "Should not be here" message with throwable information
185: */
186: private String shouldNotHere(Throwable theThrowable) {
187: return shouldNotHere() + ": "
188: + theThrowable.getClass().getName() + "["
189: + theThrowable.getMessage() + "]";
190: }
191:
192: /**
193: * @see {TestCaseImplementChecker#checkTestName(junit.framework.Test)}
194: */
195: public void testCheckTestName() {
196: Test test;
197: try {
198: test = new NormalTest("testDummy");
199: TestCaseImplementChecker.checkTestName(test);
200: } catch (Throwable t) {
201: fail(shouldNotHere(t));
202: }
203:
204: try {
205: test = new NoNameTest("testDummy");
206: TestCaseImplementChecker.checkTestName(test);
207: fail(shouldNotHere());
208: } catch (TestCaseImplementError e) {
209: assertEquals("No test name found. The test ["
210: + TestTestCaseImplementChecker.NoNameTest.class
211: .getName()
212: + "] is not properly implemented.", e.getMessage());
213: } catch (Throwable t) {
214: fail(shouldNotHere(t));
215: }
216: }
217:
218: /**
219: * @see {TestCaseImplementChecker#checkAsBeginMethod}
220: */
221: public void testCheckAsBeginMethod() {
222: //--------------------------------------------------------------------
223:
224: try {
225: Method method = MethodHolder.class.getMethod("beginNormal",
226: new Class[] { Request.class });
227: TestCaseImplementChecker.checkAsBeginMethod(method);
228: } catch (Throwable t) {
229: fail(shouldNotHere(t));
230: }
231:
232: //---------------------------------------------------------------------
233:
234: try {
235: Method method = MethodHolder.class
236: .getMethod("beginReturnsString",
237: new Class[] { Request.class });
238: TestCaseImplementChecker.checkAsBeginMethod(method);
239: fail(shouldNotHere());
240: } catch (AssertionFailedError e) {
241: assertEquals("The method [beginReturnsString] "
242: + "should return void and not [java.lang.String]",
243: e.getMessage());
244: } catch (Throwable t) {
245: fail(shouldNotHere(t));
246: }
247:
248: //---------------------------------------------------------------------
249:
250: try {
251: Method method = MethodHolder.class.getDeclaredMethod(
252: "beginProtected", new Class[] { Request.class });
253: TestCaseImplementChecker.checkAsBeginMethod(method);
254: fail(shouldNotHere());
255: } catch (AssertionFailedError e) {
256: assertEquals("The method [beginProtected] "
257: + "should be declared public", e.getMessage());
258: } catch (Throwable t) {
259: fail(shouldNotHere(t));
260: }
261:
262: //---------------------------------------------------------------------
263:
264: try {
265: Method method = MethodHolder.class.getDeclaredMethod(
266: "beginPrivate", new Class[] { Request.class });
267: TestCaseImplementChecker.checkAsBeginMethod(method);
268: fail(shouldNotHere());
269: } catch (AssertionFailedError e) {
270: assertEquals("The method [beginPrivate] "
271: + "should be declared public", e.getMessage());
272: } catch (Throwable t) {
273: fail(shouldNotHere(t));
274: }
275:
276: //---------------------------------------------------------------------
277:
278: try {
279: Method method = MethodHolder.class.getMethod(
280: "beginNoParam", new Class[] {});
281: TestCaseImplementChecker.checkAsBeginMethod(method);
282: fail(shouldNotHere());
283: } catch (AssertionFailedError e) {
284: assertEquals(
285: "The method [beginNoParam] must have 1 parameter(s), "
286: + "but 0 parameter(s) were found", e
287: .getMessage());
288: } catch (Throwable t) {
289: fail(shouldNotHere(t));
290: }
291:
292: //---------------------------------------------------------------------
293:
294: try {
295: Method method = MethodHolder.class.getMethod(
296: "beginWithTwoParams", new Class[] { Request.class,
297: Object.class });
298: TestCaseImplementChecker.checkAsBeginMethod(method);
299: fail(shouldNotHere());
300: } catch (AssertionFailedError e) {
301: assertEquals("The method [beginWithTwoParams] "
302: + "must have 1 parameter(s), "
303: + "but 2 parameter(s) were found", e.getMessage());
304: } catch (Throwable t) {
305: fail(shouldNotHere(t));
306: }
307:
308: //---------------------------------------------------------------------
309: try {
310: Method method = MethodHolder.class.getMethod(
311: "beginWithStringParam",
312: new Class[] { String.class });
313: TestCaseImplementChecker.checkAsBeginMethod(method);
314: fail(shouldNotHere());
315: } catch (AssertionFailedError e) {
316: assertEquals("The method [beginWithStringParam] "
317: + "must accept [org.apache.cactus.Request] "
318: + "as 1st parameter, but found a "
319: + "[java.lang.String] parameter instead", e
320: .getMessage());
321: } catch (Throwable t) {
322: fail(shouldNotHere(t));
323: }
324: }
325:
326: /**
327: * @see {TestCaseImplementChecker#checkAsEndMethod
328: * (java.lang.reflect.Method)}
329: */
330: public void testCheckAsEndMethod() {
331: //---------------------------------------------------------------------
332:
333: try {
334: Method method = MethodHolder.class.getMethod("endNormal",
335: new Class[] { WebResponse.class });
336: TestCaseImplementChecker.checkAsEndMethod(method);
337: } catch (Throwable t) {
338: fail(shouldNotHere(t));
339: }
340:
341: //---------------------------------------------------------------------
342:
343: try {
344: Method method = MethodHolder.class.getMethod(
345: "endReturnsString",
346: new Class[] { WebResponse.class });
347: TestCaseImplementChecker.checkAsEndMethod(method);
348: fail(shouldNotHere());
349: } catch (AssertionFailedError e) {
350: assertEquals("The method [endReturnsString] "
351: + "should return void and not [java.lang.String]",
352: e.getMessage());
353: } catch (Throwable t) {
354: fail(shouldNotHere(t));
355: }
356:
357: //---------------------------------------------------------------------
358:
359: try {
360: Method method = MethodHolder.class.getDeclaredMethod(
361: "endProtected", new Class[] { WebResponse.class });
362: TestCaseImplementChecker.checkAsEndMethod(method);
363: fail(shouldNotHere());
364: } catch (AssertionFailedError e) {
365: assertEquals("The method [endProtected] "
366: + "should be declared public", e.getMessage());
367: } catch (Throwable t) {
368: fail(shouldNotHere(t));
369: }
370:
371: //---------------------------------------------------------------------
372:
373: try {
374: Method method = MethodHolder.class.getDeclaredMethod(
375: "endPrivate", new Class[] { WebResponse.class });
376: TestCaseImplementChecker.checkAsEndMethod(method);
377: fail(shouldNotHere());
378: } catch (AssertionFailedError e) {
379: assertEquals("The method [endPrivate] "
380: + "should be declared public", e.getMessage());
381: } catch (Throwable t) {
382: fail(shouldNotHere(t));
383: }
384:
385: //---------------------------------------------------------------------
386:
387: try {
388: Method method = MethodHolder.class.getMethod("endNoParam",
389: new Class[] {});
390: TestCaseImplementChecker.checkAsEndMethod(method);
391: fail(shouldNotHere());
392: } catch (AssertionFailedError e) {
393: assertEquals(
394: "The method [endNoParam] must have 1 parameter(s), "
395: + "but 0 parameter(s) were found", e
396: .getMessage());
397: } catch (Throwable t) {
398: fail(shouldNotHere(t));
399: }
400:
401: //---------------------------------------------------------------------
402:
403: try {
404: Method method = MethodHolder.class.getMethod(
405: "endWithTwoParams", new Class[] {
406: WebResponse.class, Object.class });
407: TestCaseImplementChecker.checkAsEndMethod(method);
408: fail(shouldNotHere());
409: } catch (AssertionFailedError e) {
410: assertEquals("The method [endWithTwoParams] "
411: + "must have 1 parameter(s), "
412: + "but 2 parameter(s) were found", e.getMessage());
413: } catch (Throwable t) {
414: fail(shouldNotHere(t));
415: }
416: }
417: }
|