KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > IfTagTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.mockobjects.servlet.MockHttpServletRequest;
8 import com.mockobjects.servlet.MockPageContext;
9 import com.opensymphony.xwork.ActionContext;
10 import com.opensymphony.xwork.util.OgnlValueStack;
11 import junit.framework.TestCase;
12
13 import javax.servlet.jsp.JspException JavaDoc;
14 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
15
16
17 /**
18  * @author $Author: plightbo $
19  * @version $Revision: 1.6 $
20  */

21 public class IfTagTest extends TestCase {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     IfTag tag;
25     MockPageContext pageContext;
26     OgnlValueStack stack;
27
28     //~ Methods ////////////////////////////////////////////////////////////////
29

30     public void testNonBooleanTest() {
31         // set up the stack
32
Foo foo = new Foo();
33         foo.setNum(1);
34         stack.push(foo);
35
36         // set up the test
37
tag.setTest("num");
38
39         int result = 0;
40
41         try {
42             result = tag.doStartTag();
43         } catch (JspException JavaDoc e) {
44             e.printStackTrace();
45             fail();
46         }
47
48         assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
49
50         try {
51             result = tag.doEndTag();
52         } catch (JspException JavaDoc e) {
53             e.printStackTrace();
54             fail();
55         }
56     }
57
58     public void testTestError() {
59         // set up the stack
60
Foo foo = new Foo();
61         foo.setNum(2);
62         stack.push(foo);
63
64         // set up the test
65
tag.setTest("nuuuuum == 2");
66
67         int result = 0;
68
69         try {
70             result = tag.doStartTag();
71         } catch (JspException JavaDoc e) {
72             e.printStackTrace();
73             fail();
74         }
75
76         assertEquals(TagSupport.SKIP_BODY, result);
77
78         try {
79             result = tag.doEndTag();
80         } catch (JspException JavaDoc e) {
81             e.printStackTrace();
82             fail();
83         }
84     }
85
86     public void testTestFalse() {
87         // set up the stack
88
Foo foo = new Foo();
89         foo.setNum(2);
90         stack.push(foo);
91
92         // set up the test
93
tag.setTest("num != 2");
94
95         int result = 0;
96
97         try {
98             result = tag.doStartTag();
99         } catch (JspException JavaDoc e) {
100             e.printStackTrace();
101             fail();
102         }
103
104         assertEquals(TagSupport.SKIP_BODY, result);
105
106         try {
107             result = tag.doEndTag();
108         } catch (JspException JavaDoc e) {
109             e.printStackTrace();
110             fail();
111         }
112     }
113
114     public void testTestTrue() {
115         // set up the stack
116
Foo foo = new Foo();
117         foo.setNum(2);
118         stack.push(foo);
119
120         // set up the test
121
tag.setTest("num == 2");
122
123         int result = 0;
124
125         try {
126             result = tag.doStartTag();
127         } catch (JspException JavaDoc e) {
128             e.printStackTrace();
129             fail();
130         }
131
132         assertEquals(TagSupport.EVAL_BODY_INCLUDE, result);
133
134         try {
135             result = tag.doEndTag();
136         } catch (JspException JavaDoc e) {
137             e.printStackTrace();
138             fail();
139         }
140     }
141
142     protected void setUp() throws Exception JavaDoc {
143         // create the needed objects
144
tag = new IfTag();
145         stack = new OgnlValueStack();
146
147         // create the mock http servlet request
148
MockHttpServletRequest request = new MockHttpServletRequest();
149         ActionContext.getContext().setValueStack(stack);
150         request.setupGetAttribute(stack);
151
152         // create the mock page context
153
pageContext = new MockPageContext();
154         pageContext.setRequest(request);
155
156         // associate the tag with the mock page request
157
tag.setPageContext(pageContext);
158     }
159
160     //~ Inner Classes //////////////////////////////////////////////////////////
161

162     class Foo {
163         int num;
164
165         public void setNum(int num) {
166             this.num = num;
167         }
168
169         public int getNum() {
170             return num;
171         }
172     }
173 }
174
Popular Tags