Source code: org/mentawai/util/MockAction.java
1 package org.mentawai.util;
2
3 import java.util.Locale;
4
5 import javax.swing.ActionMap;
6
7 import org.mentawai.core.Action;
8 import org.mentawai.core.ActionException;
9 import org.mentawai.core.BaseAction;
10 import org.mentawai.core.Context;
11 import org.mentawai.core.ContextMap;
12 import org.mentawai.core.Input;
13 import org.mentawai.core.InputMap;
14 import org.mentawai.core.Output;
15 import org.mentawai.core.OutputMap;
16
17 import java.lang.reflect.*;
18
19 /*
20 * Testando a action sem o mock:
21 * MyAction action = new MyAction();
22 * Input input = new InputMap();
23 * Output output = new OutputMap();
24 * Context session = new ContextMap();
25 * Context application = new ContextMap();
26 *
27 * action.serInput(input);
28 * action.setOutput(output);
29 * action.setSession(session);
30 * action.setApplication(application);
31 *
32 * Coloca as coisas no input, application e session...
33 *
34 * String result = action.execute();
35 * assertEqual(result, "balblablablal");
36 * assertEqual(output.getValue("asdasd"),asdasdasdasdas);
37 *
38 * com o mock:
39 *
40 * MyAction action = new MyAction();
41 * MockAction mockAction = new MockAction(action);
42 * mockAction.getInput().setValue("asdsaxa", adsdasdas);
43 * String result = mockAction.execute();
44 * assertEqual(result, "balblablablal");
45 * assertEqual(mockAction.getOutput().getValue("asdasd"),asdasdasdasdas);
46 */
47
48 /**
49 * <p>A mock for facilitating action testing.
50 * It internally creates mocks for the input, output, session and context.</p>
51 * Example: <br/>
52 * <br/>
53 * Testing the without this mock: <br/>
54 * <pre>
55 * MyAction action = new MyAction();
56 * Input input = new InputMap();
57 * Output output = new OutputMap();
58 * Context session = new ContextMap();
59 * Context application = new ContextMap();
60 * action.setInput(input);
61 * action.setOutput(output);
62 * action.setSession(session);
63 * action.setApplication(application);
64 * input.setValue("asdasdas", someValue)/
65 * String result = action.execute();
66 * assertEqual(result, "balblablablal");
67 * assertEqual(output.getValue("asdasd"),asdasdasdasdas);
68 * </pre>
69 * with the mock:
70 * <pre>
71 * MyAction action = new MyAction();
72 * MockAction mockAction = new MockAction(action);
73 * mockAction.getInput().setValue("asdsaxa", adsdasdas);
74 * String result = mockAction.execute();
75 * assertEqual(result, "balblablablal");
76 * assertEqual(mockAction.getOutput().getValue("asdasd"),asdasdasdasdas);
77 * </pre>
78 */
79 public class MockAction implements Action {
80
81 private Action action;
82
83 public MockAction(Action action) {
84 this.action = action;
85 init();
86 }
87
88 public MockAction(Class klass) {
89 try {
90 this.action = (Action) klass.newInstance();
91 } catch(Exception e) {
92 throw new RuntimeException(e);
93 }
94 init();
95 }
96
97 private void init() {
98 action.setInput(new InputMap());
99 action.setOutput(new OutputMap());
100 action.setSession(new ContextMap());
101 action.setApplication(new ContextMap());
102 action.setLocale(Locale.getDefault());
103 }
104
105 public String execute() throws Exception {
106 return action.execute();
107 }
108
109 public String callInnerAction(String innerAction) throws Exception {
110 Method m = getMethod(innerAction);
111 if (m != null) {
112 try {
113 return (String) m.invoke(action, null);
114 } catch(Exception e) {
115 throw new ActionException(e);
116 }
117 } else {
118 throw new ActionException("The inner action does not exist: " + innerAction);
119 }
120 }
121
122 private Method getMethod(String innerAction) {
123 try {
124 Method m = action.getClass().getDeclaredMethod(innerAction, null);
125 if (m != null) {
126 return m;
127 }
128 } catch(Exception e) {
129 e.printStackTrace();
130 }
131 return null;
132 }
133
134 public Action getAction() {
135 return action;
136 }
137
138 public Context getApplication() {
139 return action.getApplication();
140 }
141
142 public void setApplication(Context application) {
143 action.setApplication(application);
144 }
145
146 public Input getInput() {
147 return action.getInput();
148 }
149
150 public void setInput(Input input) {
151 action.setInput(input);
152 }
153
154 public Locale getLocale() {
155 return action.getLocale();
156 }
157
158 public void setLocale(Locale locale) {
159 action.setLocale(locale);
160 }
161
162 public Output getOutput() {
163 return action.getOutput();
164 }
165
166 public void setOutput(Output output) {
167 action.setOutput(output);
168 }
169
170 public Context getSession() {
171 return action.getSession();
172 }
173
174 public void setSession(Context session) {
175 action.setSession(session);
176 }
177 }