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