1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.shale.test.mock; |
19 | |
|
20 | |
import java.lang.reflect.InvocationTargetException; |
21 | |
import java.lang.reflect.Method; |
22 | |
|
23 | |
import javax.faces.application.Application; |
24 | |
import javax.faces.component.StateHolder; |
25 | |
import javax.faces.component.UIComponentBase; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.el.EvaluationException; |
28 | |
import javax.faces.el.MethodBinding; |
29 | |
import javax.faces.el.MethodNotFoundException; |
30 | |
import javax.faces.el.ReferenceSyntaxException; |
31 | |
import javax.faces.el.ValueBinding; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class MockMethodBinding extends MethodBinding implements StateHolder { |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public MockMethodBinding() { |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public MockMethodBinding(Application application, String ref, |
64 | 11 | Class[] args) { |
65 | |
|
66 | 11 | this.application = application; |
67 | 11 | this.args = args; |
68 | 11 | if (ref.startsWith("#{") && ref.endsWith("}")) { |
69 | |
ref = ref.substring(2, ref.length() - 1); |
70 | |
} |
71 | 11 | this.ref = ref; |
72 | 11 | int period = ref.lastIndexOf("."); |
73 | 11 | if (period < 0) { |
74 | |
throw new ReferenceSyntaxException(ref); |
75 | |
} |
76 | 11 | vb = application.createValueBinding(ref.substring(0, period)); |
77 | 11 | name = ref.substring(period + 1); |
78 | 11 | if (name.length() < 1) { |
79 | |
throw new ReferenceSyntaxException(ref); |
80 | |
} |
81 | |
|
82 | 11 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private Application application; |
89 | |
private Class args[]; |
90 | |
private String name; |
91 | |
private String ref; |
92 | |
private ValueBinding vb; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public Object invoke(FacesContext context, Object[] params) |
100 | |
throws EvaluationException, MethodNotFoundException { |
101 | |
|
102 | 4 | if (context == null) { |
103 | |
throw new NullPointerException(); |
104 | |
} |
105 | 4 | Object base = vb.getValue(context); |
106 | 4 | if (base == null) { |
107 | |
throw new EvaluationException("Cannot find object via expression \"" |
108 | |
+ vb.getExpressionString() + "\""); |
109 | |
} |
110 | 4 | Method method = method(base); |
111 | |
try { |
112 | 4 | return (method.invoke(base, params)); |
113 | |
} catch (IllegalAccessException e) { |
114 | |
throw new EvaluationException(e); |
115 | |
} catch (InvocationTargetException e) { |
116 | |
throw new EvaluationException(e.getTargetException()); |
117 | |
} |
118 | |
|
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
public Class getType(FacesContext context) { |
124 | |
|
125 | 7 | Object base = vb.getValue(context); |
126 | 7 | Method method = method(base); |
127 | 7 | Class returnType = method.getReturnType(); |
128 | 7 | if ("void".equals(returnType.getName())) { |
129 | 3 | return (null); |
130 | |
} else { |
131 | 4 | return (returnType); |
132 | |
} |
133 | |
|
134 | |
} |
135 | |
|
136 | |
|
137 | |
public String getExpressionString() { |
138 | |
return "#{" + ref + "}"; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public Object saveState(FacesContext context) { |
146 | |
Object values[] = new Object[4]; |
147 | |
values[0] = name; |
148 | |
values[1] = ref; |
149 | |
values[2] = UIComponentBase.saveAttachedState(context, vb); |
150 | |
values[3] = args; |
151 | |
return (values); |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
public void restoreState(FacesContext context, Object state) { |
157 | |
Object values[] = (Object[]) state; |
158 | |
name = (String) values[0]; |
159 | |
ref = (String) values[1]; |
160 | |
vb = (ValueBinding) UIComponentBase.restoreAttachedState(context, |
161 | |
values[2]); |
162 | |
args = (Class []) values[3]; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | 11 | private boolean transientFlag = false; |
170 | |
|
171 | |
|
172 | |
|
173 | |
public boolean isTransient() { |
174 | |
return (this.transientFlag); |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
public void setTransient(boolean transientFlag) { |
180 | |
this.transientFlag = transientFlag; |
181 | |
} |
182 | |
|
183 | |
|
184 | |
public int hashCode() { |
185 | |
if (ref == null) { |
186 | |
return 0; |
187 | |
} else { |
188 | |
return ref.hashCode(); |
189 | |
} |
190 | |
} |
191 | |
|
192 | |
|
193 | |
public boolean equals(Object otherObj) { |
194 | |
MockMethodBinding other = null; |
195 | |
|
196 | |
if (!(otherObj instanceof MockMethodBinding)) { |
197 | |
return false; |
198 | |
} |
199 | |
other = (MockMethodBinding) otherObj; |
200 | |
|
201 | |
if (this.ref != other.ref) { |
202 | |
|
203 | |
if (null != this.ref && null != other.ref) { |
204 | |
if (!this.ref.equals(other.ref)) { |
205 | |
return false; |
206 | |
} |
207 | |
} |
208 | |
return false; |
209 | |
} |
210 | |
|
211 | |
|
212 | |
if (this.args != other.args) { |
213 | |
if (this.args.length != other.args.length) { |
214 | |
return false; |
215 | |
} |
216 | |
for (int i = 0, len = this.args.length; i < len; i++) { |
217 | |
if (this.args[i] != other.args[i]) { |
218 | |
if (!this.ref.equals(other.ref)) { |
219 | |
return false; |
220 | |
} |
221 | |
} |
222 | |
} |
223 | |
} |
224 | |
return true; |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
Method method(Object base) { |
237 | |
|
238 | 11 | Class clazz = base.getClass(); |
239 | |
try { |
240 | 11 | return (clazz.getMethod(name, args)); |
241 | |
} catch (NoSuchMethodException e) { |
242 | |
throw new MethodNotFoundException(ref + ": " + e.getMessage()); |
243 | |
} |
244 | |
|
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
} |