View Javadoc

1   package fit;
2   
3   // Copyright (c) 2002 Cunningham & Cunningham, Inc.
4   // Released under the terms of the GNU General Public License version 2 or later.
5   
6   import java.lang.reflect.Method;
7   
8   public class ActionFixture extends Fixture {
9       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 = loadFixture(cells.more.text());  
29      }
30  
31      public void enter() throws Exception {
32          Method method = method(1);
33          Class type = method.getParameterTypes()[0];
34          String text = cells.more.more.text();
35          Object args[] = {TypeAdapter.on(actor, type).parse(text)};
36          method.invoke(actor, args);
37      }
38  
39      public void press() throws Exception {
40          method(0).invoke(actor, empty);
41      }
42  
43      public void check() throws Exception {
44          TypeAdapter adapter = TypeAdapter.on(actor, method(0));
45          check (cells.more.more, adapter);
46      }
47  
48      // Utility //////////////////////////////////
49  
50      protected Method method(int args) throws NoSuchMethodException {
51          return method(camel(cells.more.text()), args);
52      }
53  
54      protected Method method(String test, int args) throws NoSuchMethodException {
55          Method methods[] = actor.getClass().getMethods();
56          Method result = null;
57          for (int i=0; i<methods.length; i++) {
58              Method m = methods[i];
59              if (m.getName().equals(test) && m.getParameterTypes().length == args) {
60                  if (result==null) {
61                      result = m;
62                  } else {
63                      throw new NoSuchMethodException("too many implementations");
64                  }
65              }
66          }
67          if (result==null) {
68              throw new NoSuchMethodException();
69          }
70          return result;
71      }
72  }