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