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