KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > model > test > CheckboxTagTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.model.test;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.tagext.Tag JavaDoc;
12
13 import com.inversoft.junit.JspTestCase;
14 import com.inversoft.verge.mvc.view.jsp.html.FormTag;
15 import com.inversoft.verge.mvc.view.jsp.model.CheckboxTag;
16 import com.inversoft.verge.util.ScopeConstants;
17
18
19 /**
20  * <p>
21  * This class has the test cases for the checkbox tag
22  * </p>
23  *
24  * @author Brian Pontarelli
25  * @since 2.0
26  * @version 2.0
27  */

28 public class CheckboxTagTest extends JspTestCase {
29
30     /**
31      * Constructor for ImgTagTest.
32      * @param name
33      */

34     public CheckboxTagTest(String JavaDoc name) {
35         super(name);
36         setLocal(true);
37     }
38
39
40     /**
41      * Tests the link to a web object
42      */

43     public void testLinkWeb() {
44         Bean bean = new Bean();
45         request.setAttribute("bean", bean);
46         //pageContext.setAttribute("beanVar", bean);
47

48         FormTag parent = new FormTag();
49         CheckboxTag tag = new CheckboxTag();
50         tag.setParent(parent);
51         tag.setPageContext(pageContext);
52         tag.setId("test");
53         tag.setName("test");
54         tag.setModel("bean.text");
55         tag.setValue("someValue");
56
57         try {
58             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
59             String JavaDoc tagStr = getPageContext().getMockOut().getText();
60             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\"" +
61                 " value=\"someValue\"/>" +
62                 TestTools.createWebLink("bean.text",
63                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
64                     ScopeConstants.REQUEST_INT);
65
66             System.out.println("Checkbox tag: " + tagStr);
67             System.out.println("Expected: " + compStr);
68             assertEquals("Should be checkbox", tagStr, compStr);
69         } catch (JspException JavaDoc e) {
70             fail(e.toString());
71         }
72     }
73
74     /**
75      * Tests the checked
76      */

77     public void testChecked() {
78         Bean bean = new Bean();
79         bean.setText("testValue");
80         request.setAttribute("bean", bean);
81         //pageContext.setAttribute("beanVar", bean);
82

83         FormTag parent = new FormTag();
84         CheckboxTag tag = new CheckboxTag();
85         tag.setParent(parent);
86         tag.setPageContext(pageContext);
87         tag.setId("test");
88         tag.setName("test");
89         tag.setValue("testValue");
90         tag.setModel("bean.text");
91
92         try {
93             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
94             String JavaDoc tagStr = getPageContext().getMockOut().getText();
95             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
96                 " value=\"testValue\" checked/>" +
97                 TestTools.createWebLink("bean.text",
98                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
99                     ScopeConstants.REQUEST_INT);
100
101             System.out.println("Checkbox tag: " + tagStr);
102             System.out.println("Compare string: " + compStr);
103             assertEquals("Should be checkbox", tagStr, compStr);
104         } catch (JspException JavaDoc e) {
105             fail(e.toString());
106         }
107     }
108
109     /**
110      * Tests that the tag does not retrieve the value from the bean
111      */

112     public void testNoGet() {
113         Bean bean = new Bean();
114         bean.setText("noGet");
115         request.setAttribute("bean", bean);
116
117         FormTag parent = new FormTag();
118         CheckboxTag tag = new CheckboxTag();
119         tag.setParent(parent);
120         tag.setPageContext(pageContext);
121         tag.setId("test");
122         tag.setName("test");
123         tag.setValue("noGet");
124         tag.setModel("bean.text");
125         tag.setGetValue(false);
126
127         try {
128             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
129             String JavaDoc tagStr = getPageContext().getMockOut().getText();
130             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
131                 " value=\"noGet\"/>" +
132                 TestTools.createWebLink("bean.text",
133                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
134                     ScopeConstants.REQUEST_INT);
135
136             System.out.println("Checkbox tag: " + tagStr);
137             System.out.println("Compare string: " + compStr);
138             assertEquals("Should be checkbox", tagStr, compStr);
139         } catch (JspException JavaDoc e) {
140             fail(e.toString());
141         }
142     }
143
144     /**
145      * Tests that the tag does setup the web link
146      */

147     public void testNoSet() {
148         Bean bean = new Bean();
149         bean.setText("noGet");
150         request.setAttribute("bean", bean);
151
152         FormTag parent = new FormTag();
153         CheckboxTag tag = new CheckboxTag();
154         tag.setParent(parent);
155         tag.setPageContext(pageContext);
156         tag.setId("test");
157         tag.setName("test");
158         tag.setValue("noGet");
159         tag.setModel("bean.text");
160         tag.setSetValue(false);
161
162         try {
163             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
164             String JavaDoc tagStr = getPageContext().getMockOut().getText();
165             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
166                 " value=\"noGet\" checked/>";
167
168             System.out.println("Checkbox tag: " + tagStr);
169             System.out.println("Compare string: " + compStr);
170             assertEquals("Should be checkbox", tagStr, compStr);
171         } catch (JspException JavaDoc e) {
172             fail(e.toString());
173         }
174     }
175
176     /**
177      * Tests the checked flag is okay when the tag instance is reused
178      */

179     public void testCheckedTagReuse() {
180         Bean bean = new Bean();
181         bean.setText("testValue");
182         request.setAttribute("bean", bean);
183         //pageContext.setAttribute("beanVar", bean);
184

185         FormTag parent = new FormTag();
186         CheckboxTag tag = new CheckboxTag();
187         tag.setParent(parent);
188         tag.setPageContext(pageContext);
189         tag.setId("test");
190         tag.setName("test");
191         tag.setValue("testValue");
192         tag.setModel("bean.text");
193
194         try {
195             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
196             String JavaDoc tagStr = getPageContext().getMockOut().getText();
197             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
198                 " value=\"testValue\" checked/>" +
199                 TestTools.createWebLink("bean.text",
200                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
201                     ScopeConstants.REQUEST_INT);
202
203             System.out.println("Checkbox tag: " + tagStr);
204             System.out.println("Compare string: " + compStr);
205             assertEquals("Should be checkbox", tagStr, compStr);
206         } catch (JspException JavaDoc e) {
207             fail(e.toString());
208         }
209
210
211         // Reset the out
212
getPageContext().getMockOut().clear();
213
214         // Setup the bean to uncheck the tag
215
bean.setText("notTheSameValue");
216
217         try {
218             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
219             String JavaDoc tagStr = getPageContext().getMockOut().getText();
220             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
221                 " value=\"testValue\"/>" +
222                 TestTools.createWebLink("bean.text",
223                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
224                     ScopeConstants.REQUEST_INT);
225
226             System.out.println("Checkbox tag: " + tagStr);
227             System.out.println("Compare string: " + compStr);
228             assertEquals("Should be checkbox", tagStr, compStr);
229         } catch (JspException JavaDoc e) {
230             fail(e.toString());
231         }
232     }
233
234     /**
235      * Tests the checkbox gets checked using EL
236      */

237     public void testCheckedEL() {
238         Bean bean = new Bean();
239         bean.setText("testValue");
240         request.setAttribute("bean", bean);
241         //pageContext.setAttribute("beanVar", bean);
242
pageContext.setAttribute("propertyName", "text");
243
244         FormTag parent = new FormTag();
245         CheckboxTag tag = new CheckboxTag();
246         tag.setParent(parent);
247         tag.setPageContext(pageContext);
248         tag.setId("test");
249         tag.setName("test");
250         tag.setValue("testValue");
251         tag.setModel("bean.${propertyName}");
252
253         try {
254             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
255             String JavaDoc tagStr = getPageContext().getMockOut().getText();
256             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
257                 " value=\"testValue\" checked/>" +
258                 TestTools.createWebLink("bean.text",
259                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
260                     ScopeConstants.REQUEST_INT);
261
262             System.out.println("Checkbox tag: " + tagStr);
263             System.out.println("Compare string: " + compStr);
264             assertEquals("Should be checkbox", tagStr, compStr);
265         } catch (JspException JavaDoc e) {
266             fail(e.toString());
267         }
268     }
269
270     /**
271      * Tests the checked
272      */

273     public void testCheckedWithObjects() {
274         Bean bean = new Bean();
275         Bean test = new Bean("textOne", "sameValue");
276         bean.setLocal(test);
277         request.setAttribute("bean", bean);
278         //pageContext.setAttribute("beanVar", bean);
279

280         FormTag parent = new FormTag();
281         CheckboxTag tag = new CheckboxTag();
282         tag.setParent(parent);
283         tag.setPageContext(pageContext);
284         tag.setId("test");
285         tag.setName("test");
286
287         Bean compare = new Bean("textTwo", "sameValue");
288         tag.setValue(compare);
289         tag.setModel("bean.local");
290
291         try {
292             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
293             String JavaDoc tagStr = getPageContext().getMockOut().getText();
294             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
295                 " value=\"textTwo\" checked/>" +
296                 TestTools.createWebLink("bean.local",
297                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
298                     ScopeConstants.REQUEST_INT);
299
300             System.out.println("Checkbox tag: " + tagStr);
301             System.out.println("Compare string: " + compStr);
302             assertEquals("Should be checkbox", tagStr, compStr);
303         } catch (JspException JavaDoc e) {
304             fail(e.toString());
305         }
306     }
307
308     /**
309      * Tests that the tag does not retrieve its value from the bean
310      */

311     public void testNoGetObjects() {
312         Bean bean = new Bean();
313         Bean test = new Bean("noGet", "sameValue");
314         bean.setLocal(test);
315         request.setAttribute("bean", bean);
316
317         FormTag parent = new FormTag();
318         CheckboxTag tag = new CheckboxTag();
319         tag.setParent(parent);
320         tag.setPageContext(pageContext);
321         tag.setId("test");
322         tag.setName("test");
323         tag.setGetValue(false);
324
325         Bean compare = new Bean("noGet", "sameValue");
326         tag.setValue(compare);
327         tag.setModel("bean.local");
328
329         try {
330             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
331             String JavaDoc tagStr = getPageContext().getMockOut().getText();
332             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
333                 " value=\"noGet\"/>" +
334                 TestTools.createWebLink("bean.local",
335                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
336                     ScopeConstants.REQUEST_INT);
337
338             System.out.println("Checkbox tag: " + tagStr);
339             System.out.println("Compare string: " + compStr);
340             assertEquals("Should be checkbox", tagStr, compStr);
341         } catch (JspException JavaDoc e) {
342             fail(e.toString());
343         }
344     }
345
346     /**
347      * Tests that re-using the tag instance is okay
348      */

349     public void testTagReuse() {
350         Bean bean = new Bean();
351         Bean test = new Bean("textOne", "sameValue");
352         bean.setLocal(test);
353         request.setAttribute("bean", bean);
354         //pageContext.setAttribute("beanVar", bean);
355

356         FormTag parent = new FormTag();
357         CheckboxTag tag = new CheckboxTag();
358         tag.setParent(parent);
359         tag.setPageContext(pageContext);
360         tag.setId("test");
361         tag.setName("test");
362
363         Bean compare = new Bean("textTwo", "sameValue");
364         tag.setValue(compare);
365         tag.setModel("bean.local");
366
367         try {
368             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
369             String JavaDoc tagStr = getPageContext().getMockOut().getText();
370             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
371                 " value=\"textTwo\" checked/>" +
372                 TestTools.createWebLink("bean.local",
373                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
374                     ScopeConstants.REQUEST_INT);
375
376             System.out.println("Checkbox tag: " + tagStr);
377             System.out.println("Compare string: " + compStr);
378             assertEquals("Should be checkbox", tagStr, compStr);
379         } catch (JspException JavaDoc e) {
380             fail(e.toString());
381         }
382
383         // Clear the out stream
384
getPageContext().getMockOut().clear();
385
386         Bean compare2 = new Bean("textThree", "notSameValue");
387         tag.setValue(compare2);
388         tag.setModel("bean.local");
389
390         try {
391             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
392             String JavaDoc tagStr = getPageContext().getMockOut().getText();
393             String JavaDoc compStr = "<input type=\"checkbox\" id=\"test\" name=\"test\""+
394                 " value=\"textThree\"/>" +
395                 TestTools.createWebLink("bean.local",
396                     "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
397                     ScopeConstants.REQUEST_INT);
398
399             System.out.println("Checkbox tag: " + tagStr);
400             System.out.println("Compare string: " + compStr);
401             assertEquals("Should unchecked because checked should have been reset",
402                 tagStr, compStr);
403         } catch (JspException JavaDoc e) {
404             fail(e.toString());
405         }
406     }
407 }
Popular Tags