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