FileDocCategorySizeDatePackage
TagModelTest.javaAPI DocExample14196Mon Jul 23 13:26:12 BST 2007org.apache.struts2.views.freemarker.tags

TagModelTest

public class TagModelTest extends org.apache.struts2.StrutsTestCase

Fields Summary
final Object
ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT
final Object
WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT
Constructors Summary
Methods Summary
public voidtestGetWriter()


        OgnlValueStack stack = new OgnlValueStack();

        final InternalBean bean = new InternalBean(stack);

        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

        Map params = new LinkedHashMap();

        // Try to test out the commons Freemarker's Template Model

        // TemplateBooleanModel
        params.put("property1", new TemplateBooleanModel() {
            public boolean getAsBoolean() throws TemplateModelException {
                return true;
            }
        });
        params.put("property2", new TemplateBooleanModel() {
            public boolean getAsBoolean() throws TemplateModelException {
                return false;
            }
        });

        // TemplateScalarModel
        params.put("property3", new TemplateScalarModel() {
            public String getAsString() throws TemplateModelException {
                return "toby";
            }
        });
        params.put("property4", new TemplateScalarModel() {
            public String getAsString() throws TemplateModelException {
                return "phil";
            }
        });

        // TemplateNumberModel
        params.put("property5", new TemplateNumberModel() {
            public Number getAsNumber() throws TemplateModelException {
                return new Integer("10");
            }
        });
        params.put("property6", new TemplateNumberModel() {
            public Number getAsNumber() throws TemplateModelException {
                return new Float("1.1");
            }
        });

        // TemplateHashModel
        params.put("property7", new TemplateHashModel() {
            public TemplateModel get(String arg0) throws TemplateModelException {
                return null;
            }

            public boolean isEmpty() throws TemplateModelException {
                return true;
            }
        });

        // TemplateSequenceModel
        params.put("property8", new TemplateSequenceModel() {
            public TemplateModel get(int arg0) throws TemplateModelException {
                return null;
            }

            public int size() throws TemplateModelException {
                return 0;
            }
        });

        // TemplateCollectionModel
        params.put("property9", new TemplateCollectionModel() {
            public TemplateModelIterator iterator() throws TemplateModelException {
                return new TemplateModelIterator() {
                    private Iterator i;
                    {
                        i = Collections.EMPTY_LIST.iterator();
                    }

                    public boolean hasNext() throws TemplateModelException {
                        return i.hasNext();
                    }

                    public TemplateModel next() throws TemplateModelException {
                        return (TemplateModel) i.next();
                    }
                };
            }
        });

        // AdapterTemplateModel
        params.put("property10", new AdapterTemplateModel() {
            public Object getAdaptedObject(Class arg0) {
                return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
            }
        });

        // WrapperTemplateModel
        params.put("property11", new WrapperTemplateModel() {
            public Object getWrappedObject() {
                return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
            }
        });

        TagModel tagModel = new TagModel(stack, request, response) {
            protected Component getBean() {
                return bean;
            }
        };

        tagModel.getWriter(new StringWriter(), params);

        assertNotNull(bean);
        assertEquals(bean.getProperty1(), true);
        assertEquals(bean.getProperty2(), false);
        assertEquals(bean.getProperty3(), "toby");
        assertEquals(bean.getProperty4(), "phil");
        assertEquals(bean.getProperty5(), new Integer(10));
        assertEquals(bean.getProperty6(), new Float(1.1));
        assertNotNull(bean.getProperty7());
        assertTrue(bean.getProperty7() instanceof Map);
        assertNotNull(bean.getProperty8());
        assertTrue(bean.getProperty8() instanceof Collection);
        assertNotNull(bean.getProperty9());
        assertTrue(bean.getProperty9() instanceof Collection);
        assertEquals(bean.getProperty10(), ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
        assertEquals(bean.getProperty11(), WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);
    
public voidtestUnwrapMap()


         

        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        OgnlValueStack stack = new OgnlValueStack();

        Map params = new LinkedHashMap();

        // Try to test out the commons Freemarker's Template Model

        // TemplateBooleanModel
        params.put("property1", new TemplateBooleanModel() {
            public boolean getAsBoolean() throws TemplateModelException {
                return true;
            }
        });
        params.put("property2", new TemplateBooleanModel() {
            public boolean getAsBoolean() throws TemplateModelException {
                return false;
            }
        });

        // TemplateScalarModel
        params.put("property3", new TemplateScalarModel() {
            public String getAsString() throws TemplateModelException {
                return "toby";
            }
        });
        params.put("property4", new TemplateScalarModel() {
            public String getAsString() throws TemplateModelException {
                return "phil";
            }
        });

        // TemplateNumberModel
        params.put("property5", new TemplateNumberModel() {
            public Number getAsNumber() throws TemplateModelException {
                return new Integer("10");
            }
        });
        params.put("property6", new TemplateNumberModel() {
            public Number getAsNumber() throws TemplateModelException {
                return new Float("1.1");
            }
        });

        // TemplateHashModel
        params.put("property7", new TemplateHashModel() {
            public TemplateModel get(String arg0) throws TemplateModelException {
                return null;
            }

            public boolean isEmpty() throws TemplateModelException {
                return true;
            }
        });

        // TemplateSequenceModel
        params.put("property8", new TemplateSequenceModel() {
            public TemplateModel get(int arg0) throws TemplateModelException {
                return null;
            }

            public int size() throws TemplateModelException {
                return 0;
            }
        });

        // TemplateCollectionModel
        params.put("property9", new TemplateCollectionModel() {
            public TemplateModelIterator iterator() throws TemplateModelException {
                return new TemplateModelIterator() {
                    private Iterator i;
                    {
                        i = Collections.EMPTY_LIST.iterator();
                    }

                    public boolean hasNext() throws TemplateModelException {
                        return i.hasNext();
                    }

                    public TemplateModel next() throws TemplateModelException {
                        return (TemplateModel) i.next();
                    }
                };
            }
        });

        // AdapterTemplateModel
        params.put("property10", new AdapterTemplateModel() {
            public Object getAdaptedObject(Class arg0) {
                return ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT;
            }
        });

        // WrapperTemplateModel
        params.put("property11", new WrapperTemplateModel() {
            public Object getWrappedObject() {
                return WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT;
            }
        });

        TagModel tagModel = new TagModel(stack, request, response) {
            protected Component getBean() {
                return null;
            }
        };

        Map result = tagModel.unwrapParameters(params);

        assertNotNull(result);
        assertEquals(result.size(), 11);
        assertEquals(result.get("property1"), Boolean.TRUE);
        assertEquals(result.get("property2"), Boolean.FALSE);
        assertEquals(result.get("property3"), "toby");
        assertEquals(result.get("property4"), "phil");
        assertEquals(result.get("property5"), new Integer(10));
        assertEquals(result.get("property6"), new Float(1.1));
        assertNotNull(result.get("property7"));
        assertTrue(result.get("property7") instanceof Map);
        assertNotNull(result.get("property8"));
        assertTrue(result.get("property8") instanceof Collection);
        assertNotNull(result.get("property9"));
        assertTrue(result.get("property9") instanceof Collection);
        assertEquals(result.get("property10"),
            ADAPTER_TEMPLATE_MODEL_CONTAINED_OBJECT);
        assertEquals(result.get("property11"),
            WRAPPING_TEMPLATE_MODEL_CONTAINED_OBJECT);