001: /*
002: * Copyright (c) 2002-2007 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.freemarker.tags;
006:
007: import java.io.StringWriter;
008: import java.util.Collection;
009: import java.util.Collections;
010: import java.util.Iterator;
011: import java.util.LinkedHashMap;
012: import java.util.Map;
013:
014: import org.springframework.mock.web.MockHttpServletRequest;
015: import org.springframework.mock.web.MockHttpServletResponse;
016:
017: import com.opensymphony.webwork.components.Component;
018: import com.opensymphony.xwork.util.OgnlValueStack;
019:
020: import freemarker.ext.util.WrapperTemplateModel;
021: import freemarker.template.AdapterTemplateModel;
022: import freemarker.template.TemplateBooleanModel;
023: import freemarker.template.TemplateCollectionModel;
024: import freemarker.template.TemplateHashModel;
025: import freemarker.template.TemplateModel;
026: import freemarker.template.TemplateModelException;
027: import freemarker.template.TemplateModelIterator;
028: import freemarker.template.TemplateNumberModel;
029: import freemarker.template.TemplateScalarModel;
030: import freemarker.template.TemplateSequenceModel;
031:
032: import junit.framework.TestCase;
033:
034: /**
035: * @author tmjee
036: * @version $Date$ $Id$
037: */
038: public class TagModelTest extends TestCase {
039:
040: final Object ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT = new Object();
041: final Object WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT = new Object();
042:
043: public void testUnwrapMap() throws Exception {
044:
045: MockHttpServletRequest request = new MockHttpServletRequest();
046: MockHttpServletResponse response = new MockHttpServletResponse();
047: OgnlValueStack stack = new OgnlValueStack();
048:
049: Map params = new LinkedHashMap();
050:
051: // Try to test out the commons Freemarker's Template Model
052:
053: // TemplateBooleanModel
054: params.put("property1", new TemplateBooleanModel() {
055: public boolean getAsBoolean() throws TemplateModelException {
056: return true;
057: }
058: });
059: params.put("property2", new TemplateBooleanModel() {
060: public boolean getAsBoolean() throws TemplateModelException {
061: return false;
062: }
063: });
064:
065: // TemplateScalarModel
066: params.put("property3", new TemplateScalarModel() {
067: public String getAsString() throws TemplateModelException {
068: return "toby";
069: }
070: });
071: params.put("property4", new TemplateScalarModel() {
072: public String getAsString() throws TemplateModelException {
073: return "phil";
074: }
075: });
076:
077: // TemplateNumberModel
078: params.put("property5", new TemplateNumberModel() {
079: public Number getAsNumber() throws TemplateModelException {
080: return new Integer("10");
081: }
082: });
083: params.put("property6", new TemplateNumberModel() {
084: public Number getAsNumber() throws TemplateModelException {
085: return new Float("1.1");
086: }
087: });
088:
089: // TemplateHashModel
090: params.put("property7", new TemplateHashModel() {
091: public TemplateModel get(String arg0)
092: throws TemplateModelException {
093: return null;
094: }
095:
096: public boolean isEmpty() throws TemplateModelException {
097: return true;
098: }
099: });
100:
101: // TemplateSequenceModel
102: params.put("property8", new TemplateSequenceModel() {
103: public TemplateModel get(int arg0)
104: throws TemplateModelException {
105: return null;
106: }
107:
108: public int size() throws TemplateModelException {
109: return 0;
110: }
111: });
112:
113: // TemplateCollectionModel
114: params.put("property9", new TemplateCollectionModel() {
115: public TemplateModelIterator iterator()
116: throws TemplateModelException {
117: return new TemplateModelIterator() {
118: private Iterator i;
119: {
120: i = Collections.EMPTY_LIST.iterator();
121: }
122:
123: public boolean hasNext()
124: throws TemplateModelException {
125: return i.hasNext();
126: }
127:
128: public TemplateModel next()
129: throws TemplateModelException {
130: return (TemplateModel) i.next();
131: }
132: };
133: }
134: });
135:
136: // AdapterTemplateModel
137: params.put("property10", new AdapterTemplateModel() {
138: public Object getAdaptedObject(Class arg0) {
139: return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
140: }
141: });
142:
143: // WrapperTemplateModel
144: params.put("property11", new WrapperTemplateModel() {
145: public Object getWrappedObject() {
146: return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
147: }
148: });
149:
150: TagModel tagModel = new TagModel(stack, request, response) {
151: protected Component getBean() {
152: return null;
153: }
154: };
155:
156: Map result = tagModel.unwrapParameters(params);
157:
158: assertNotNull(result);
159: assertEquals(result.size(), 11);
160: assertEquals(result.get("property1"), Boolean.TRUE);
161: assertEquals(result.get("property2"), Boolean.FALSE);
162: assertEquals(result.get("property3"), "toby");
163: assertEquals(result.get("property4"), "phil");
164: assertEquals(result.get("property5"), new Integer(10));
165: assertEquals(result.get("property6"), new Float(1.1));
166: assertNotNull(result.get("property7"));
167: assertTrue(result.get("property7") instanceof Map);
168: assertNotNull(result.get("property8"));
169: assertTrue(result.get("property8") instanceof Collection);
170: assertNotNull(result.get("property9"));
171: assertTrue(result.get("property9") instanceof Collection);
172: assertEquals(result.get("property10"),
173: ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
174: assertEquals(result.get("property11"),
175: WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);
176: }
177:
178: public void testGetWriter() throws Exception {
179:
180: OgnlValueStack stack = new OgnlValueStack();
181:
182: final InternalBean bean = new InternalBean(stack);
183:
184: MockHttpServletRequest request = new MockHttpServletRequest();
185: MockHttpServletResponse response = new MockHttpServletResponse();
186:
187: Map params = new LinkedHashMap();
188:
189: // Try to test out the commons Freemarker's Template Model
190:
191: // TemplateBooleanModel
192: params.put("property1", new TemplateBooleanModel() {
193: public boolean getAsBoolean() throws TemplateModelException {
194: return true;
195: }
196: });
197: params.put("property2", new TemplateBooleanModel() {
198: public boolean getAsBoolean() throws TemplateModelException {
199: return false;
200: }
201: });
202:
203: // TemplateScalarModel
204: params.put("property3", new TemplateScalarModel() {
205: public String getAsString() throws TemplateModelException {
206: return "toby";
207: }
208: });
209: params.put("property4", new TemplateScalarModel() {
210: public String getAsString() throws TemplateModelException {
211: return "phil";
212: }
213: });
214:
215: // TemplateNumberModel
216: params.put("property5", new TemplateNumberModel() {
217: public Number getAsNumber() throws TemplateModelException {
218: return new Integer("10");
219: }
220: });
221: params.put("property6", new TemplateNumberModel() {
222: public Number getAsNumber() throws TemplateModelException {
223: return new Float("1.1");
224: }
225: });
226:
227: // TemplateHashModel
228: params.put("property7", new TemplateHashModel() {
229: public TemplateModel get(String arg0)
230: throws TemplateModelException {
231: return null;
232: }
233:
234: public boolean isEmpty() throws TemplateModelException {
235: return true;
236: }
237: });
238:
239: // TemplateSequenceModel
240: params.put("property8", new TemplateSequenceModel() {
241: public TemplateModel get(int arg0)
242: throws TemplateModelException {
243: return null;
244: }
245:
246: public int size() throws TemplateModelException {
247: return 0;
248: }
249: });
250:
251: // TemplateCollectionModel
252: params.put("property9", new TemplateCollectionModel() {
253: public TemplateModelIterator iterator()
254: throws TemplateModelException {
255: return new TemplateModelIterator() {
256: private Iterator i;
257: {
258: i = Collections.EMPTY_LIST.iterator();
259: }
260:
261: public boolean hasNext()
262: throws TemplateModelException {
263: return i.hasNext();
264: }
265:
266: public TemplateModel next()
267: throws TemplateModelException {
268: return (TemplateModel) i.next();
269: }
270: };
271: }
272: });
273:
274: // AdapterTemplateModel
275: params.put("property10", new AdapterTemplateModel() {
276: public Object getAdaptedObject(Class arg0) {
277: return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
278: }
279: });
280:
281: // WrapperTemplateModel
282: params.put("property11", new WrapperTemplateModel() {
283: public Object getWrappedObject() {
284: return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
285: }
286: });
287:
288: TagModel tagModel = new TagModel(stack, request, response) {
289: protected Component getBean() {
290: return bean;
291: }
292: };
293:
294: tagModel.getWriter(new StringWriter(), params);
295:
296: assertNotNull(bean);
297: assertEquals(bean.getProperty1(), true);
298: assertEquals(bean.getProperty2(), false);
299: assertEquals(bean.getProperty3(), "toby");
300: assertEquals(bean.getProperty4(), "phil");
301: assertEquals(bean.getProperty5(), new Integer(10));
302: assertEquals(bean.getProperty6(), new Float(1.1));
303: assertNotNull(bean.getProperty7());
304: assertTrue(bean.getProperty7() instanceof Map);
305: assertNotNull(bean.getProperty8());
306: assertTrue(bean.getProperty8() instanceof Collection);
307: assertNotNull(bean.getProperty9());
308: assertTrue(bean.getProperty9() instanceof Collection);
309: assertEquals(bean.getProperty10(),
310: ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
311: assertEquals(bean.getProperty11(),
312: WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);
313: }
314:
315: /**
316: * @author tmjee
317: * @version $Date$ $Id$
318: */
319: public static class InternalBean extends Component {
320:
321: public InternalBean(OgnlValueStack stack) {
322: super (stack);
323: }
324:
325: private boolean property1 = false; // inverse of the expected result, so we could test that it works
326: private boolean property2 = true; // inverse of the expected result, so we could test that it works
327:
328: private String property3;
329: private String property4;
330:
331: private Integer property5;
332: private Float property6;
333:
334: private Map property7;
335: private Collection property8;
336: private Collection property9;
337:
338: private Object property10;
339: private Object property11;
340:
341: public void setProperty1(boolean property1) {
342: this .property1 = property1;
343: }
344:
345: public boolean getProperty1() {
346: return property1;
347: }
348:
349: public void setProperty2(boolean property2) {
350: this .property2 = property2;
351: }
352:
353: public boolean getProperty2() {
354: return property2;
355: }
356:
357: public void setProperty3(String property3) {
358: this .property3 = property3;
359: }
360:
361: public String getProperty3() {
362: return this .property3;
363: }
364:
365: public void setProperty4(String property4) {
366: this .property4 = property4;
367: }
368:
369: public String getProperty4() {
370: return this .property4;
371: }
372:
373: public void setProperty5(Integer property5) {
374: this .property5 = property5;
375: }
376:
377: public Integer getProperty5() {
378: return this .property5;
379: }
380:
381: public void setProperty6(Float property6) {
382: this .property6 = property6;
383: }
384:
385: public Float getProperty6() {
386: return this .property6;
387: }
388:
389: public void setProperty7(Map property7) {
390: this .property7 = property7;
391: }
392:
393: public Map getProperty7() {
394: return property7;
395: }
396:
397: public void setProperty8(Collection property8) {
398: this .property8 = property8;
399: }
400:
401: public Collection getProperty8() {
402: return property8;
403: }
404:
405: public void setProperty9(Collection property9) {
406: this .property9 = property9;
407: }
408:
409: public Collection getProperty9() {
410: return this .property9;
411: }
412:
413: public void setProperty10(Object property10) {
414: this .property10 = property10;
415: }
416:
417: public Object getProperty10() {
418: return this .property10;
419: }
420:
421: public void setProperty11(Object property11) {
422: this .property11 = property11;
423: }
424:
425: public Object getProperty11() {
426: return this.property11;
427: }
428:
429: }
430: }
|