1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.views.jsp;
23
24 import javax.servlet.RequestDispatcher;
25
26 import org.apache.struts2.StrutsException;
27 import org.apache.struts2.components.Include;
28 import org.easymock.MockControl;
29
30 import com.mockobjects.servlet.MockRequestDispatcher;
31
32 /***
33 * Unit test of {@link IncludeTag}.
34 *
35 */
36 public class IncludeTagTest extends AbstractTagTest {
37
38 private MockControl controlRequestDispatcher;
39 private RequestDispatcher mockRequestDispatcher;
40
41 private IncludeTag tag;
42
43 public void testNoURL() throws Exception {
44 try {
45 tag.doStartTag();
46 tag.doEndTag();
47 fail("Should have thrown exception as no URL is specified in setValue");
48 } catch (StrutsException e) {
49 assertEquals("tag 'include', field 'value': You must specify the URL to include. Example: /foo.jsp", e.getMessage());
50 }
51 }
52
53 public void testIncludeNoParam() throws Exception {
54 mockRequestDispatcher.include(null, null);
55 controlRequestDispatcher.setVoidCallable();
56 controlRequestDispatcher.replay();
57
58 tag.setValue("person/list.jsp");
59 tag.doStartTag();
60 tag.doEndTag();
61
62 controlRequestDispatcher.verify();
63 assertEquals("/person/list.jsp", request.getRequestDispatherString());
64 assertEquals("", writer.toString());
65 }
66
67 public void testIncludeWithParameters() throws Exception {
68 mockRequestDispatcher.include(null, null);
69 controlRequestDispatcher.setVoidCallable();
70 controlRequestDispatcher.replay();
71
72 tag.setValue("person/create.jsp");
73 tag.doStartTag();
74
75 Include include = (Include) tag.getComponent();
76 include.addParameter("user", "Santa Claus");
77 tag.doEndTag();
78
79 controlRequestDispatcher.verify();
80 assertEquals("/person/create.jsp?user=Santa+Claus", request.getRequestDispatherString());
81 assertEquals("", writer.toString());
82 }
83
84 public void testIncludeRelative2Dots() throws Exception {
85
86 mockRequestDispatcher.include(null, null);
87 controlRequestDispatcher.setVoidCallable();
88 controlRequestDispatcher.replay();
89
90 request.setupGetServletPath("app/manager");
91 tag.setValue("../car/view.jsp");
92 tag.doStartTag();
93 tag.doEndTag();
94
95 controlRequestDispatcher.verify();
96 assertEquals("/car/view.jsp", request.getRequestDispatherString());
97 assertEquals("", writer.toString());
98 }
99
100 protected void setUp() throws Exception {
101 super.setUp();
102 request.setupGetRequestDispatcher(new MockRequestDispatcher());
103 tag = new IncludeTag();
104
105 controlRequestDispatcher = MockControl.createNiceControl(RequestDispatcher.class);
106
107 controlRequestDispatcher.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
108 mockRequestDispatcher = (RequestDispatcher) controlRequestDispatcher.getMock();
109
110 request.setupGetRequestDispatcher(mockRequestDispatcher);
111 tag.setPageContext(pageContext);
112 tag.setPageContext(pageContext);
113 }
114
115 protected void tearDown() throws Exception {
116 super.tearDown();
117 tag = null;
118 controlRequestDispatcher = null;
119 mockRequestDispatcher = null;
120 }
121
122 }