001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.self;
003:
004: import junit.framework.Assert;
005: import junit.framework.AssertionFailedError;
006: import junit.framework.TestCase;
007:
008: import java.lang.reflect.Method;
009:
010: /**
011: * Helper to test that the implementation of steps complies to the way
012: * ANT instantiatiates its nested elements.
013: * <p/>
014: * There must be an addXXX method for each Element XXX with
015: * exactly one parameter that can be instantiated via dynamic invocation.
016: */
017: public class AntTest extends TestCase {
018: public AntTest(String string) {
019: super (string);
020: }
021:
022: public static void nested(Class taskClass, String nested) {
023: String methodName = "add" + nested;
024: Method[] methods = taskClass.getDeclaredMethods();
025: boolean foundMethod = false;
026: for (int i = 0; i < methods.length; i++) {
027: Method method = methods[i];
028: if (method.getName().equals(methodName)) {
029: if (foundMethod) {
030: Assert.fail("more than one method"
031: + at(taskClass, methodName));
032: }
033: foundMethod = true;
034: Class[] parms = method.getParameterTypes();
035: Assert.assertEquals("parameter count in"
036: + at(taskClass, methodName), 1, parms.length);
037: try {
038: parms[0].newInstance();
039: } catch (Exception e) {
040: Assert.fail("cannot instantiate parameter in"
041: + at(taskClass, methodName, parms[0]
042: .getName()));
043: }
044: }
045: }
046: Assert.assertTrue("no such method"
047: + at(taskClass, methodName, "?"), foundMethod);
048: }
049:
050: private static String at(Class taskClass,
051: String expectedMethodName, String param) {
052: return " " + taskClass.getName() + "." + expectedMethodName
053: + "(" + param + ")";
054: }
055:
056: private static String at(Class taskClass, String expectedMethodName) {
057: return at(taskClass, expectedMethodName, "?");
058: }
059:
060: public void testNoMethodFails() {
061: String message = ThrowAssert.assertThrows(
062: AssertionFailedError.class, new TestBlock() {
063: public void call() throws Exception {
064: nested(Object.class, "NoSuchNestedElement");
065: }
066: });
067: assertEquals(
068: "no such method java.lang.Object.addNoSuchNestedElement(?)",
069: message);
070: }
071:
072: public void addTwoMethods(Object o) {
073: }
074:
075: public void addTwoMethods(String x) {
076: }
077:
078: public void testMoreThanOneMethodFails() {
079: String message = ThrowAssert.assertThrows(
080: AssertionFailedError.class, new TestBlock() {
081: public void call() throws Exception {
082: nested(AntTest.class, "TwoMethods");
083: }
084: });
085: assertEquals(
086: "more than one method com.canoo.webtest.self.AntTest.addTwoMethods(?)",
087: message);
088: }
089:
090: public void addNoEmptyConstructor(AntTest x) {
091: }
092:
093: public void testNoEmptyCtorForParamFails() {
094: String message = ThrowAssert.assertThrows(
095: AssertionFailedError.class, new TestBlock() {
096: public void call() throws Exception {
097: nested(AntTest.class, "NoEmptyConstructor");
098: }
099: });
100: assertEquals(
101: "cannot instantiate parameter in com.canoo.webtest.self.AntTest.addNoEmptyConstructor(com.canoo.webtest.self.AntTest)",
102: message);
103: }
104:
105: public void addTooManyParams(String x, String y) {
106: }
107:
108: public void testTooManyParamsFails() {
109: String message = ThrowAssert.assertThrows(
110: AssertionFailedError.class, new TestBlock() {
111: public void call() throws Exception {
112: nested(AntTest.class, "TooManyParams");
113: }
114: });
115: assertEquals(
116: "parameter count in com.canoo.webtest.self.AntTest.addTooManyParams(?) expected:<1> but was:<2>",
117: message);
118: }
119:
120: public void testAddMethodDummyCanBeCalled() {
121: addNoEmptyConstructor(null);
122: addTooManyParams(null, null);
123: addTwoMethods(new Object());
124: addTwoMethods(new String());
125: }
126:
127: }
|