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