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