01: package fit;
02:
03: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
04: // Released under the terms of the GNU General Public License version 2 or later.
05:
06: import java.lang.reflect.Method;
07:
08: public class ActionFixture extends Fixture {
09: protected Parse cells;
10: public static Fixture actor;
11: protected static Class empty[] = {};
12:
13: // Traversal ////////////////////////////////
14:
15: public void doCells(Parse cells) {
16: this .cells = cells;
17: try {
18: Method action = getClass().getMethod(cells.text(), empty);
19: action.invoke(this , empty);
20: } catch (Exception e) {
21: exception(cells, e);
22: }
23: }
24:
25: // Actions //////////////////////////////////
26:
27: public void start() throws Exception {
28: actor = (Fixture) (Class.forName(cells.more.text())
29: .newInstance());
30: }
31:
32: public void enter() throws Exception {
33: Method method = method(1);
34: Class type = method.getParameterTypes()[0];
35: String text = cells.more.more.text();
36: Object args[] = { TypeAdapter.on(actor, type).parse(text) };
37: method.invoke(actor, args);
38: }
39:
40: public void press() throws Exception {
41: method(0).invoke(actor, empty);
42: }
43:
44: public void check() throws Exception {
45: TypeAdapter adapter = TypeAdapter.on(actor, method(0));
46: check(cells.more.more, adapter);
47: }
48:
49: // Utility //////////////////////////////////
50:
51: protected Method method(int args) throws NoSuchMethodException {
52: return method(camel(cells.more.text()), args);
53: }
54:
55: protected Method method(String test, int args)
56: throws NoSuchMethodException {
57: Method methods[] = actor.getClass().getMethods();
58: Method result = null;
59: for (int i = 0; i < methods.length; i++) {
60: Method m = methods[i];
61: if (m.getName().equals(test)
62: && m.getParameterTypes().length == args) {
63: if (result == null) {
64: result = m;
65: } else {
66: throw new NoSuchMethodException(
67: "too many implementations");
68: }
69: }
70: }
71: if (result == null) {
72: throw new NoSuchMethodException();
73: }
74: return result;
75: }
76: }
|