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.jsp.tagext.TagSupport;
25
26 import junit.framework.TestCase;
27
28 import org.apache.struts2.ServletActionContext;
29 import org.apache.struts2.StrutsTestCase;
30 import org.apache.struts2.components.If;
31
32 import com.mockobjects.servlet.MockJspWriter;
33 import com.mockobjects.servlet.MockPageContext;
34 import com.opensymphony.xwork2.ActionContext;
35 import com.opensymphony.xwork2.ognl.OgnlValueStack;
36 import com.opensymphony.xwork2.util.ValueStack;
37 import com.opensymphony.xwork2.util.ValueStackFactory;
38
39 /***
40 *
41 */
42 public class ElseIfTagTest extends StrutsTestCase {
43
44 protected MockPageContext pageContext;
45 protected MockJspWriter jspWriter;
46 protected ValueStack stack;
47
48
49 public void testIfIsFalseElseIfIsTrue() throws Exception {
50 stack.getContext().put(If.ANSWER, Boolean.FALSE);
51
52 ElseIfTag tag = new ElseIfTag();
53 tag.setPageContext(pageContext);
54 tag.setTest("true");
55
56 int result = tag.doStartTag();
57 tag.doEndTag();
58
59 assertEquals(result, TagSupport.EVAL_BODY_INCLUDE);
60 }
61
62 public void testIfIsFalseElseIfIsFalse() throws Exception {
63 stack.getContext().put(If.ANSWER, Boolean.FALSE);
64
65 ElseIfTag tag = new ElseIfTag();
66 tag.setPageContext(pageContext);
67 tag.setTest("false");
68
69 int result = tag.doStartTag();
70 tag.doEndTag();
71
72 assertEquals(result, TagSupport.SKIP_BODY);
73 }
74
75 public void testIfIsTrueElseIfIsTrue() throws Exception {
76 stack.getContext().put(If.ANSWER, Boolean.TRUE);
77
78 ElseIfTag tag = new ElseIfTag();
79 tag.setPageContext(pageContext);
80 tag.setTest("true");
81
82 int result = tag.doStartTag();
83 tag.doEndTag();
84
85 assertEquals(result, TagSupport.SKIP_BODY);
86 }
87
88 public void testIfIsTrueElseIfIsFalse() throws Exception {
89 stack.getContext().put(If.ANSWER, Boolean.TRUE);
90
91 ElseIfTag tag = new ElseIfTag();
92 tag.setPageContext(pageContext);
93 tag.setTest("false");
94
95 int result = tag.doStartTag();
96 tag.doEndTag();
97
98 assertEquals(result, TagSupport.SKIP_BODY);
99 }
100
101
102 protected void setUp() throws Exception {
103 super.setUp();
104 stack = ActionContext.getContext().getValueStack();
105
106 jspWriter = new MockJspWriter();
107
108 StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
109
110 StrutsMockServletContext servletContext = new StrutsMockServletContext();
111 servletContext.setServletInfo("not-weblogic");
112
113 pageContext = new MockPageContext();
114 pageContext.setJspWriter(jspWriter);
115 pageContext.setRequest(request);
116 pageContext.setServletContext(servletContext);
117
118 request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
119 }
120
121
122 }