KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > html > test > OptionTagTest


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.html.test;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.tagext.Tag JavaDoc;
12
13 import com.inversoft.junit.AutoJspTestCallback;
14 import com.inversoft.junit.JspTestCallback;
15 import com.inversoft.junit.JspTestCase;
16 import com.inversoft.verge.mvc.view.jsp.html.FormTag;
17 import com.inversoft.verge.mvc.view.jsp.html.OptionTag;
18 import com.inversoft.verge.mvc.view.jsp.html.SelectTag;
19
20
21 /**
22  * <p>
23  * This class has the tests for the options tag.
24  * </p>
25  *
26  * @author Brian Pontarelli
27  * @since 2.0
28  * @version 2.0
29  */

30 public class OptionTagTest extends JspTestCase {
31
32     /**
33      * Constructor for OptionTagTest.
34      * @param name
35      */

36     public OptionTagTest(String JavaDoc name) {
37         super(name);
38         setLocal(true);
39     }
40
41
42     /**
43      * Tests the value attribute
44      */

45     public void testValue() {
46         SelectTag parent = new SelectTag();
47         OptionTag tag = new OptionTag();
48         tag.setParent(parent);
49         tag.setPageContext(pageContext);
50         tag.setValue("test");
51
52         try {
53             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
54             String JavaDoc tagStr = getPageContext().getMockOut().getText();
55
56             System.out.println("Option tag: " + tagStr);
57             assertEquals("Should be option tag", tagStr,
58                 "<option value=\"test\"></option>");
59         } catch (JspException JavaDoc e) {
60             fail(e.toString());
61         }
62     }
63
64     /**
65      * Tests the selected attribute
66      */

67     public void testSelected() {
68         SelectTag parent = new SelectTag();
69         OptionTag tag = new OptionTag();
70         tag.setParent(parent);
71         tag.setPageContext(pageContext);
72         tag.setValue("test");
73         tag.setSelected(Boolean.TRUE);
74
75         try {
76             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
77             String JavaDoc tagStr = getPageContext().getMockOut().getText();
78
79             System.out.println("Option tag: " + tagStr);
80             assertEquals("Should be option tag", tagStr,
81                 "<option value=\"test\" selected></option>");
82         } catch (JspException JavaDoc e) {
83             fail(e.toString());
84         }
85     }
86
87     /**
88      * Tests the disabled attribute
89      */

90     public void testDisabled() {
91         SelectTag parent = new SelectTag();
92         OptionTag tag = new OptionTag();
93         tag.setParent(parent);
94         tag.setPageContext(pageContext);
95         tag.setValue("test");
96         tag.setDisabled(Boolean.TRUE);
97
98         try {
99             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
100             String JavaDoc tagStr = getPageContext().getMockOut().getText();
101
102             System.out.println("Option tag: " + tagStr);
103             assertEquals("Should be option tag", tagStr,
104                 "<option value=\"test\" disabled></option>");
105         } catch (JspException JavaDoc e) {
106             fail(e.toString());
107         }
108     }
109
110     /**
111      * Tests that the option is selected if the parent select tag has the same
112      * value.
113      */

114     public void testSelectValue() {
115         FormTag form = new FormTag();
116         SelectTag parent = new SelectTag();
117         parent.setParent(form);
118         parent.setName("sel");
119         parent.setValue("test");
120         parent.setPageContext(pageContext);
121         OptionTag tag = new OptionTag();
122         tag.setParent(parent);
123         tag.setPageContext(pageContext);
124         tag.setValue("test");
125
126         JspTestCallback callback = new AutoJspTestCallback(new Tag JavaDoc[]{tag});
127         try {
128             assertEquals("Should return EVAL_PAGE", runTag(parent, callback),
129                 Tag.EVAL_PAGE);
130             String JavaDoc tagStr = getPageContext().getMockOut().getText();
131             String JavaDoc expected = "<select name=\"sel\"><option value=\"test\" selected></option></select>";
132
133             System.out.println("Option tag: " + tagStr);
134             assertEquals(expected, tagStr);
135         } catch (JspException JavaDoc e) {
136             fail(e.toString());
137         }
138     }
139
140     /**
141      * Tests that the option is selected if the parent select tag has the same
142      * value, even with EL.
143      */

144     public void testSelectValueEL() {
145         pageContext.setAttribute("someValue", "test");
146
147         FormTag form = new FormTag();
148         SelectTag parent = new SelectTag();
149         parent.setParent(form);
150         parent.setName("sel");
151         parent.setValue("${someValue}");
152         parent.setPageContext(pageContext);
153         OptionTag tag = new OptionTag();
154         tag.setParent(parent);
155         tag.setPageContext(pageContext);
156         tag.setValue("test");
157
158         JspTestCallback callback = new AutoJspTestCallback(new Tag JavaDoc[]{tag});
159         try {
160             assertEquals("Should return EVAL_PAGE", runTag(parent, callback),
161                 Tag.EVAL_PAGE);
162             String JavaDoc tagStr = getPageContext().getMockOut().getText();
163             String JavaDoc expected = "<select name=\"sel\"><option value=\"test\" selected></option></select>";
164
165             System.out.println("Option tag: " + tagStr);
166             assertEquals(expected, tagStr);
167         } catch (JspException JavaDoc e) {
168             fail(e.toString());
169         }
170     }
171
172     /**
173      * Tests the label attribute
174      */

175     public void testLabel() {
176         SelectTag parent = new SelectTag();
177         OptionTag tag = new OptionTag();
178         tag.setParent(parent);
179         tag.setPageContext(pageContext);
180         tag.setValue("test");
181         tag.setLabel("testLabel");
182
183         try {
184             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
185             String JavaDoc tagStr = getPageContext().getMockOut().getText();
186
187             System.out.println("Option tag: " + tagStr);
188             assertEquals("Should be option tag", tagStr,
189                 "<option value=\"test\" label=\"testLabel\"></option>");
190         } catch (JspException JavaDoc e) {
191             fail(e.toString());
192         }
193     }
194 }
Popular Tags