View Javadoc

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