1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.freemarker.tags;
23
24 import java.io.StringWriter;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30
31 import org.apache.struts2.StrutsTestCase;
32 import org.apache.struts2.components.Component;
33 import org.apache.struts2.views.freemarker.tags.TagModel;
34 import org.springframework.mock.web.MockHttpServletRequest;
35 import org.springframework.mock.web.MockHttpServletResponse;
36
37 import com.opensymphony.xwork2.ActionContext;
38 import com.opensymphony.xwork2.ognl.OgnlValueStack;
39
40 import freemarker.ext.util.WrapperTemplateModel;
41 import freemarker.template.AdapterTemplateModel;
42 import freemarker.template.TemplateBooleanModel;
43 import freemarker.template.TemplateCollectionModel;
44 import freemarker.template.TemplateHashModel;
45 import freemarker.template.TemplateModel;
46 import freemarker.template.TemplateModelException;
47 import freemarker.template.TemplateModelIterator;
48 import freemarker.template.TemplateNumberModel;
49 import freemarker.template.TemplateScalarModel;
50 import freemarker.template.TemplateSequenceModel;
51
52 public class TagModelTest extends StrutsTestCase {
53
54 final Object ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT = new Object();
55 final Object WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT = new Object();
56
57 public void testUnwrapMap() throws Exception {
58
59 MockHttpServletRequest request = new MockHttpServletRequest();
60 MockHttpServletResponse response = new MockHttpServletResponse();
61 OgnlValueStack stack = (OgnlValueStack)ActionContext.getContext().getValueStack();
62
63 Map params = new LinkedHashMap();
64
65
66
67
68 params.put("property1", new TemplateBooleanModel() {
69 public boolean getAsBoolean() throws TemplateModelException {
70 return true;
71 }
72 });
73 params.put("property2", new TemplateBooleanModel() {
74 public boolean getAsBoolean() throws TemplateModelException {
75 return false;
76 }
77 });
78
79
80 params.put("property3", new TemplateScalarModel() {
81 public String getAsString() throws TemplateModelException {
82 return "toby";
83 }
84 });
85 params.put("property4", new TemplateScalarModel() {
86 public String getAsString() throws TemplateModelException {
87 return "phil";
88 }
89 });
90
91
92 params.put("property5", new TemplateNumberModel() {
93 public Number getAsNumber() throws TemplateModelException {
94 return new Integer("10");
95 }
96 });
97 params.put("property6", new TemplateNumberModel() {
98 public Number getAsNumber() throws TemplateModelException {
99 return new Float("1.1");
100 }
101 });
102
103
104 params.put("property7", new TemplateHashModel() {
105 public TemplateModel get(String arg0) throws TemplateModelException {
106 return null;
107 }
108
109 public boolean isEmpty() throws TemplateModelException {
110 return true;
111 }
112 });
113
114
115 params.put("property8", new TemplateSequenceModel() {
116 public TemplateModel get(int arg0) throws TemplateModelException {
117 return null;
118 }
119
120 public int size() throws TemplateModelException {
121 return 0;
122 }
123 });
124
125
126 params.put("property9", new TemplateCollectionModel() {
127 public TemplateModelIterator iterator() throws TemplateModelException {
128 return new TemplateModelIterator() {
129 private Iterator i;
130 {
131 i = Collections.EMPTY_LIST.iterator();
132 }
133
134 public boolean hasNext() throws TemplateModelException {
135 return i.hasNext();
136 }
137
138 public TemplateModel next() throws TemplateModelException {
139 return (TemplateModel) i.next();
140 }
141 };
142 }
143 });
144
145
146 params.put("property10", new AdapterTemplateModel() {
147 public Object getAdaptedObject(Class arg0) {
148 return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
149 }
150 });
151
152
153 params.put("property11", new WrapperTemplateModel() {
154 public Object getWrappedObject() {
155 return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
156 }
157 });
158
159 TagModel tagModel = new TagModel(stack, request, response) {
160 protected Component getBean() {
161 return null;
162 }
163 };
164
165 Map result = tagModel.unwrapParameters(params);
166
167 assertNotNull(result);
168 assertEquals(result.size(), 11);
169 assertEquals(result.get("property1"), Boolean.TRUE);
170 assertEquals(result.get("property2"), Boolean.FALSE);
171 assertEquals(result.get("property3"), "toby");
172 assertEquals(result.get("property4"), "phil");
173 assertEquals(result.get("property5"), new Integer(10));
174 assertEquals(result.get("property6"), new Float(1.1));
175 assertNotNull(result.get("property7"));
176 assertTrue(result.get("property7") instanceof Map);
177 assertNotNull(result.get("property8"));
178 assertTrue(result.get("property8") instanceof Collection);
179 assertNotNull(result.get("property9"));
180 assertTrue(result.get("property9") instanceof Collection);
181 assertEquals(result.get("property10"),
182 ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
183 assertEquals(result.get("property11"),
184 WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);
185 }
186
187 public void testGetWriter() throws Exception {
188
189 OgnlValueStack stack = (OgnlValueStack)ActionContext.getContext().getValueStack();
190
191 final InternalBean bean = new InternalBean(stack);
192
193 MockHttpServletRequest request = new MockHttpServletRequest();
194 MockHttpServletResponse response = new MockHttpServletResponse();
195
196 Map params = new LinkedHashMap();
197
198
199
200
201 params.put("property1", new TemplateBooleanModel() {
202 public boolean getAsBoolean() throws TemplateModelException {
203 return true;
204 }
205 });
206 params.put("property2", new TemplateBooleanModel() {
207 public boolean getAsBoolean() throws TemplateModelException {
208 return false;
209 }
210 });
211
212
213 params.put("property3", new TemplateScalarModel() {
214 public String getAsString() throws TemplateModelException {
215 return "toby";
216 }
217 });
218 params.put("property4", new TemplateScalarModel() {
219 public String getAsString() throws TemplateModelException {
220 return "phil";
221 }
222 });
223
224
225 params.put("property5", new TemplateNumberModel() {
226 public Number getAsNumber() throws TemplateModelException {
227 return new Integer("10");
228 }
229 });
230 params.put("property6", new TemplateNumberModel() {
231 public Number getAsNumber() throws TemplateModelException {
232 return new Float("1.1");
233 }
234 });
235
236
237 params.put("property7", new TemplateHashModel() {
238 public TemplateModel get(String arg0) throws TemplateModelException {
239 return null;
240 }
241
242 public boolean isEmpty() throws TemplateModelException {
243 return true;
244 }
245 });
246
247
248 params.put("property8", new TemplateSequenceModel() {
249 public TemplateModel get(int arg0) throws TemplateModelException {
250 return null;
251 }
252
253 public int size() throws TemplateModelException {
254 return 0;
255 }
256 });
257
258
259 params.put("property9", new TemplateCollectionModel() {
260 public TemplateModelIterator iterator() throws TemplateModelException {
261 return new TemplateModelIterator() {
262 private Iterator i;
263 {
264 i = Collections.EMPTY_LIST.iterator();
265 }
266
267 public boolean hasNext() throws TemplateModelException {
268 return i.hasNext();
269 }
270
271 public TemplateModel next() throws TemplateModelException {
272 return (TemplateModel) i.next();
273 }
274 };
275 }
276 });
277
278
279 params.put("property10", new AdapterTemplateModel() {
280 public Object getAdaptedObject(Class arg0) {
281 return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
282 }
283 });
284
285
286 params.put("property11", new WrapperTemplateModel() {
287 public Object getWrappedObject() {
288 return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
289 }
290 });
291
292 TagModel tagModel = new TagModel(stack, request, response) {
293 protected Component getBean() {
294 return bean;
295 }
296 };
297
298 tagModel.getWriter(new StringWriter(), params);
299
300 assertNotNull(bean);
301 assertEquals(bean.getProperty1(), true);
302 assertEquals(bean.getProperty2(), false);
303 assertEquals(bean.getProperty3(), "toby");
304 assertEquals(bean.getProperty4(), "phil");
305 assertEquals(bean.getProperty5(), new Integer(10));
306 assertEquals(bean.getProperty6(), new Float(1.1));
307 assertNotNull(bean.getProperty7());
308 assertTrue(bean.getProperty7() instanceof Map);
309 assertNotNull(bean.getProperty8());
310 assertTrue(bean.getProperty8() instanceof Collection);
311 assertNotNull(bean.getProperty9());
312 assertTrue(bean.getProperty9() instanceof Collection);
313 assertEquals(bean.getProperty10(), ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
314 assertEquals(bean.getProperty11(), WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);
315 }
316
317 public static class InternalBean extends Component {
318
319 public InternalBean(OgnlValueStack stack) {
320 super(stack);
321 }
322
323 private boolean property1 = false;
324 private boolean property2 = true;
325
326 private String property3;
327 private String property4;
328
329 private Integer property5;
330 private Float property6;
331
332 private Map property7;
333 private Collection property8;
334 private Collection property9;
335
336 private Object property10;
337 private Object property11;
338
339 public void setProperty1(boolean property1) {
340 this.property1 = property1;
341 }
342
343 public boolean getProperty1() {
344 return property1;
345 }
346
347 public void setProperty2(boolean property2) {
348 this.property2 = property2;
349 }
350
351 public boolean getProperty2() {
352 return property2;
353 }
354
355 public void setProperty3(String property3) {
356 this.property3 = property3;
357 }
358
359 public String getProperty3() {
360 return this.property3;
361 }
362
363 public void setProperty4(String property4) {
364 this.property4 = property4;
365 }
366
367 public String getProperty4() {
368 return this.property4;
369 }
370
371 public void setProperty5(Integer property5) {
372 this.property5 = property5;
373 }
374
375 public Integer getProperty5() {
376 return this.property5;
377 }
378
379 public void setProperty6(Float property6) {
380 this.property6 = property6;
381 }
382
383 public Float getProperty6() {
384 return this.property6;
385 }
386
387 public void setProperty7(Map property7) {
388 this.property7 = property7;
389 }
390
391 public Map getProperty7() {
392 return property7;
393 }
394
395 public void setProperty8(Collection property8) {
396 this.property8 = property8;
397 }
398
399 public Collection getProperty8() {
400 return property8;
401 }
402
403 public void setProperty9(Collection property9) {
404 this.property9 = property9;
405 }
406
407 public Collection getProperty9() {
408 return this.property9;
409 }
410
411 public void setProperty10(Object property10) {
412 this.property10 = property10;
413 }
414
415 public Object getProperty10() {
416 return this.property10;
417 }
418
419 public void setProperty11(Object property11) {
420 this.property11 = property11;
421 }
422
423 public Object getProperty11() {
424 return this.property11;
425 }
426
427 }
428 }