KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > strutsel > taglib > html > TestELButtonTag


1 /*
2  * $Id: TestELButtonTag.java 54933 2004-10-16 17:04:52Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.strutsel.taglib.html;
20
21 import java.util.HashMap JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 import org.apache.struts.util.LabelValueBean;
30 import org.apache.strutsel.taglib.utils.DOMHelper;
31 import org.apache.strutsel.taglib.utils.JspTagTestCase;
32 import org.apache.strutsel.taglib.utils.TestHelper;
33
34
35 public class TestELButtonTag
36     extends JspTagTestCase {
37     protected static final String JavaDoc REQUIRED_DISABLED_VALUE_KEY =
38             "RequiredDisabledValue";
39     protected static final String JavaDoc REQUIRED_TYPE_VALUE_KEY =
40             "RequiredTypeValue";
41     protected static final String JavaDoc REQUIRED_VALUE_VALUE_KEY =
42             "RequiredValueValue";
43     protected ELButtonTag elButtonTag = null;
44
45     protected static String JavaDoc[] eventHandlers =
46     {
47         "onblur", "onchange", "onclick", "ondblclick", "onfocus", "onkeydown",
48         "onkeypress", "onkeyup", "onmousedown", "onmousemove", "onmouseout",
49         "onmouseover", "onmouseup"
50     };
51
52     public TestELButtonTag(String JavaDoc theName) {
53         super(theName);
54     }
55
56     public static void main(String JavaDoc[] args) {
57         junit.awtui.TestRunner.main(
58                 new String JavaDoc[] { TestELButtonTag.class.getName() });
59     }
60
61     public static Test suite() {
62         return new TestSuite(TestELButtonTag.class);
63     }
64
65     public void setUp() {
66         elButtonTag = new ELButtonTag();
67         elButtonTag.setPageContext(pageContext);
68     }
69
70     public void tearDown() {
71         elButtonTag = null;
72     }
73
74     /**
75      * Tests a plain "button" tag, with all default attribute values.
76      */

77     public void testPlain()
78                    throws ServletException JavaDoc, JspException JavaDoc {
79         HttpServletResponse JavaDoc response =
80             (HttpServletResponse JavaDoc)pageContext.getResponse();
81         String JavaDoc requiredTypeValue = "button";
82         response.addHeader(REQUIRED_TYPE_VALUE_KEY, requiredTypeValue);
83
84         String JavaDoc requiredValueValue = "Click";
85         response.addHeader(REQUIRED_VALUE_VALUE_KEY, requiredValueValue);
86
87         int startTagReturn = elButtonTag.doStartTag();
88         int afterBodyReturn = elButtonTag.doAfterBody();
89         int endTagReturn = elButtonTag.doEndTag();
90     }
91
92     public void endPlain(com.meterware.httpunit.WebResponse testResponse) {
93         try {
94             TestHelper.printResponse(testResponse);
95
96             org.w3c.dom.Document JavaDoc document = testResponse.getDOM();
97             DOMHelper.printNode(document.getDocumentElement());
98
99             HashMap JavaDoc attrMap = new HashMap JavaDoc();
100             DOMHelper.recordFoundAttributes(testResponse.getDOM(),
101                                             "/html/body/input", attrMap);
102             DOMHelper.verifyAttributesPresent(attrMap,
103                                               new String JavaDoc[] { "type", "value" },
104                                               false);
105             checkAttrValue(attrMap, testResponse, REQUIRED_TYPE_VALUE_KEY,
106                            "button", "type");
107             checkAttrValue(attrMap, testResponse, REQUIRED_VALUE_VALUE_KEY,
108                            "button", "value");
109         } catch (Exception JavaDoc ex) {
110             ex.printStackTrace();
111             fail();
112         }
113     }
114
115     /**
116      * Tests the "disabled" attribute.
117      */

118     public void testDisabled()
119                       throws ServletException JavaDoc, JspException JavaDoc {
120
121         elButtonTag.setDisabledExpr("true");
122
123         String JavaDoc requiredDisabledValue = "disabled";
124         response.addHeader(REQUIRED_DISABLED_VALUE_KEY, requiredDisabledValue);
125
126         int startTagReturn = elButtonTag.doStartTag();
127         int afterBodyReturn = elButtonTag.doAfterBody();
128         int endTagReturn = elButtonTag.doEndTag();
129     }
130
131     public void endDisabled(com.meterware.httpunit.WebResponse testResponse) {
132         try {
133             TestHelper.printResponse(testResponse);
134
135             org.w3c.dom.Document JavaDoc document = testResponse.getDOM();
136             DOMHelper.printNode(document.getDocumentElement());
137
138             HashMap JavaDoc attrMap = new HashMap JavaDoc();
139             DOMHelper.recordFoundAttributes(testResponse.getDOM(),
140                                             "/html/body/input", attrMap);
141             DOMHelper.verifyAttributesPresent(attrMap,
142                                               new String JavaDoc[] { "disabled" },
143                                               true);
144             checkAttrValue(attrMap, testResponse, REQUIRED_DISABLED_VALUE_KEY,
145                            "button", "disabled");
146         } catch (Exception JavaDoc ex) {
147             ex.printStackTrace();
148             fail();
149         }
150     }
151
152     public void testEventHandlers()
153         throws ServletException JavaDoc, JspException JavaDoc
154     {
155         LabelValueBean bean = new LabelValueBean("junk", "stuff");
156         pageContext.setAttribute("testFormBean", bean);
157
158         String JavaDoc expr = "${" + "testFormBean.value" + "}";
159
160         elButtonTag.setOnblurExpr(expr);
161         elButtonTag.setOnchangeExpr(expr);
162         elButtonTag.setOnclickExpr(expr);
163         elButtonTag.setOndblclickExpr(expr);
164         elButtonTag.setOnfocusExpr(expr);
165         elButtonTag.setOnkeydownExpr(expr);
166         elButtonTag.setOnkeypressExpr(expr);
167         elButtonTag.setOnkeyupExpr(expr);
168         elButtonTag.setOnmousedownExpr(expr);
169         elButtonTag.setOnmousemoveExpr(expr);
170         elButtonTag.setOnmouseoutExpr(expr);
171         elButtonTag.setOnmouseoverExpr(expr);
172         elButtonTag.setOnmouseupExpr(expr);
173
174         int startTagReturn = elButtonTag.doStartTag();
175         int afterBodyReturn = elButtonTag.doAfterBody();
176         int endTagReturn = elButtonTag.doEndTag();
177     }
178
179     public void
180         endEventHandlers(com.meterware.httpunit.WebResponse testResponse) {
181         try {
182             TestHelper.printResponse(testResponse);
183
184             org.w3c.dom.Document JavaDoc document = testResponse.getDOM();
185             DOMHelper.printNode(document.getDocumentElement());
186
187             HashMap JavaDoc attrMap = new HashMap JavaDoc();
188             DOMHelper.recordFoundAttributes(testResponse.getDOM(),
189                                             "/html/body/input", attrMap);
190             DOMHelper.verifyAttributesPresent(attrMap, eventHandlers, true);
191             for (int ctr = 0; ctr < eventHandlers.length; ++ ctr) {
192                 if (!((String JavaDoc) attrMap.get(eventHandlers[ctr])).
193                     equals("stuff")) {
194                     fail();
195                 }
196             }
197             
198         } catch (Exception JavaDoc ex) {
199             ex.printStackTrace();
200             fail();
201         }
202     }
203 }
204
Popular Tags