01: package com.mockrunner.example.tag;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import javax.el.ValueExpression;
07: import javax.servlet.jsp.JspApplicationContext;
08: import javax.servlet.jsp.JspFactory;
09:
10: import com.mockrunner.mock.web.JasperJspFactory;
11: import com.mockrunner.tag.BasicTagTestCaseAdapter;
12:
13: /**
14: * Example test for the {@link HtmlTextTag}.
15: * The example demonstrates how to use deferred expressions
16: * with the <code>#{...}</code> syntax. Mockrunner supports the
17: * evaluation of expressions with the <code>${...}</code>
18: * and <code>#{...}</code> syntax. You have to set
19: * {@link com.mockrunner.mock.web.JasperJspFactory} as the default
20: * JSP factory. The {@link com.mockrunner.mock.web.MockJspFactory},
21: * which is the predefined JSP factory, does not support the
22: * evaluation of expressions. Expression evaluation only works
23: * with the unified Expression Language API introduced with JSP 2.1.
24: */
25: public class HtmlTextTagTest extends BasicTagTestCaseAdapter {
26: protected void setUp() throws Exception {
27: super .setUp();
28: getWebMockObjectFactory().setDefaultJspFactory(
29: new JasperJspFactory()
30: .configure(getWebMockObjectFactory()));
31: }
32:
33: private ValueExpression prepareExpression(String expression) {
34: JspApplicationContext applicationContext = JspFactory
35: .getDefaultFactory().getJspApplicationContext(
36: getWebMockObjectFactory().getMockPageContext()
37: .getServletContext());
38: return applicationContext.getExpressionFactory()
39: .createValueExpression(
40: getWebMockObjectFactory().getMockPageContext()
41: .getELContext(), expression,
42: String.class);
43: }
44:
45: public void testDoTag() {
46: Map attributeMap = new HashMap();
47: attributeMap.put("name", "myname");
48: attributeMap.put("value", prepareExpression("#{person.name}"));
49: createNestedTag(HtmlTextTag.class, attributeMap);
50: Person person = new Person();
51: person.setName("Jane");
52: getWebMockObjectFactory().getMockSession().setAttribute(
53: "person", person);
54: processTagLifecycle();
55: verifyOutput("<input type=\"text\" name=\"myname\" value=\"Jane\"/>");
56: }
57: }
|