01: package com.mockrunner.example.tag;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import javax.servlet.http.HttpServletRequest;
07:
08: import com.mockrunner.mock.web.WebMockObjectFactory;
09: import com.mockrunner.tag.AbstractDynamicChild;
10: import com.mockrunner.tag.BasicTagTestCaseAdapter;
11: import com.mockrunner.tag.NestedSimpleTag;
12:
13: /**
14: * Example test for the {@link FilterImagesTag}.
15: * The example demonstrates how to test simple tags
16: * and the usage of dynamic childs (i.e. simulated expressions or scriptlets).
17: * The simulated body of the tag consists of a static <i>img</i>-tag
18: * and a dynamic version that takes the url from request. The JSP
19: * for the dynamic version may look like
20: * <pre>
21: * <img src="${requestScope.url}">
22: * </pre>
23: * The EL expression is simulated by a subclass of
24: * {@link com.mockrunner.tag.AbstractDynamicChild}.
25: */
26: public class FilterImagesTagTest extends BasicTagTestCaseAdapter {
27: private void prepareFilterImagesTag(String filterAttribute) {
28: Map attributeMap = new HashMap();
29: attributeMap.put("filter", filterAttribute);
30: NestedSimpleTag tag = (NestedSimpleTag) createNestedTag(
31: FilterImagesTag.class, attributeMap);
32: getWebMockObjectFactory().getMockRequest().setAttribute("url",
33: "/testimage.jpg");
34: tag.addTextChild("<img src=\"/anotherimage.jpg\">");
35: tag.addDynamicChild(new SessionImageChild(
36: getWebMockObjectFactory()));
37: }
38:
39: public void testDoNotFilter() {
40: prepareFilterImagesTag("false");
41: processTagLifecycle();
42: verifyOutputContains("src=\"/anotherimage.jpg\"");
43: verifyOutputContains("src=\"/testimage.jpg\"");
44: assertTrue(-1 == getOutput().indexOf("dummy.jpg"));
45: }
46:
47: public void testDoFilter() {
48: prepareFilterImagesTag("true");
49: processTagLifecycle();
50: verifyOutputContains("src=\"dummy.jpg\"");
51: assertTrue(-1 == getOutput().indexOf("anotherimage.jpg"));
52: assertTrue(-1 == getOutput().indexOf("testimage.jpg"));
53: }
54:
55: private class SessionImageChild extends AbstractDynamicChild {
56: public SessionImageChild(WebMockObjectFactory factory) {
57: super (factory);
58: }
59:
60: public Object evaluate() {
61: HttpServletRequest request = getWebMockObjectFactory()
62: .getMockRequest();
63: return "<img src=\"" + request.getAttribute("url") + "\">";
64: }
65: }
66: }
|