KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TestELBaseTag.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.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
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 import org.w3c.dom.Element JavaDoc;
34
35
36 public class TestELBaseTag
37     extends JspTagTestCase {
38     protected static final String JavaDoc REQUIRED_HREF_VALUE_KEY =
39             "RequiredHRefValue";
40     protected static final String JavaDoc REQUIRED_TARGET_VALUE_KEY =
41             "RequiredTargetValue";
42     protected ELBaseTag elBaseTag = null;
43
44     public TestELBaseTag(String JavaDoc theName) {
45         super(theName);
46     }
47
48     public static void main(String JavaDoc[] args) {
49         junit.awtui.TestRunner.main(
50                 new String JavaDoc[] { TestELBaseTag.class.getName() });
51     }
52
53     public static Test suite() {
54         return new TestSuite(TestELBaseTag.class);
55     }
56
57     public void setUp() {
58         elBaseTag = new ELBaseTag();
59         elBaseTag.setPageContext(pageContext);
60     }
61
62     public void tearDown() {
63         elBaseTag = null;
64     }
65
66     /**
67      * Method to get the required value of the "href" attribute, based on
68      * values in the request. This is taken directly from
69      * "BaseTag.doStartTag()", which is exactly what we're testing, so they had
70      * better match.
71      */

72     private String JavaDoc getRequiredHrefValue(HttpServletRequest JavaDoc request) {
73         // This sequence of assignments is taken from
74
// "BaseTag.doStartTag(), so it'd better match.
75
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76         sb.append(request.getScheme());
77         sb.append("://");
78         sb.append(request.getServerName());
79
80         if ((request.getScheme().equals("http") &&
81                 (request.getServerPort() != 80)) ||
82             (request.getScheme().equals("https") &&
83                 (request.getServerPort() != 443))) {
84             sb.append(":");
85             sb.append(request.getServerPort());
86         }
87
88         sb.append(request.getRequestURI());
89
90         String JavaDoc requiredHrefValue = sb.toString();
91
92         return (requiredHrefValue);
93     }
94
95     /**
96      * Tests the value of the "href" attribute, based on the values in the
97      * request.
98      */

99     public void testHref()
100                   throws ServletException JavaDoc, JspException JavaDoc {
101         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)pageContext.getResponse();
102         String JavaDoc requiredHrefValue = getRequiredHrefValue(request);
103         System.out.println("requiredHrefValue[" + requiredHrefValue + "]");
104         response.addHeader(REQUIRED_HREF_VALUE_KEY, requiredHrefValue);
105         response.addHeader("abc", "def");
106         response.addHeader("ghi", "jkl");
107         response.addHeader("mno", "pqr");
108 // response.addHeader("stuvwx", requiredHrefValue);
109
response.addHeader("stuvwx", "abc");
110
111         int startTagReturn = elBaseTag.doStartTag();
112     }
113
114     public void endHref(com.meterware.httpunit.WebResponse testResponse) {
115         try {
116             TestHelper.printResponse(testResponse);
117
118             Element JavaDoc docElement = testResponse.getDOM().getDocumentElement();
119             DOMHelper.printNode(docElement);
120
121             HashMap JavaDoc attrMap = new HashMap JavaDoc();
122             DOMHelper.recordFoundAttributes(testResponse.getDOM(),
123                                             "/html/head/base", attrMap);
124             DOMHelper.verifyAttributesPresent(attrMap, new String JavaDoc[] { "href" },
125                                               false);
126             String JavaDoc header =
127                 testResponse.getHeaderField(REQUIRED_HREF_VALUE_KEY);
128             System.out.println("[header[" + header + "]]");
129             checkAttrValue(attrMap, testResponse, REQUIRED_HREF_VALUE_KEY,
130                            "base", "href");
131         } catch (Exception JavaDoc ex) {
132             ex.printStackTrace();
133             fail();
134         }
135     }
136
137     /**
138      * Tests the "target" attribute, based on a particular value set into the
139      * target property, using the EL engine to evalute the property value.
140      */

141     public void testTarget()
142                     throws ServletException JavaDoc, JspException JavaDoc {
143         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)pageContext.getResponse();
144         String JavaDoc requiredTargetValue = "flork";
145         response.addHeader(REQUIRED_TARGET_VALUE_KEY, requiredTargetValue);
146
147         String JavaDoc varName = "targetVar";
148         pageContext.setAttribute(varName, requiredTargetValue);
149         elBaseTag.setTargetExpr("${" + varName + "}");
150
151         int startTagReturn = elBaseTag.doStartTag();
152     }
153
154     public void endTarget(com.meterware.httpunit.WebResponse testResponse) {
155         try {
156             TestHelper.printResponse(testResponse);
157
158             Element JavaDoc docElement = testResponse.getDOM().getDocumentElement();
159             DOMHelper.printNode(docElement);
160
161             HashMap JavaDoc attrMap = new HashMap JavaDoc();
162             DOMHelper.recordFoundAttributes(testResponse.getDOM(),
163                                             "/html/head/base", attrMap);
164             DOMHelper.verifyAttributesPresent(attrMap,
165                                               new String JavaDoc[] { "href", "target" },
166                                               false);
167             checkAttrValue(attrMap, testResponse, REQUIRED_TARGET_VALUE_KEY,
168                            "base", "target");
169         } catch (Exception JavaDoc ex) {
170             ex.printStackTrace();
171             fail();
172         }
173     }
174
175     /**
176      * Tests the "target" attribute, like in "testTarget", but specifies a
177      * variable name in the EL expression which doesn't exist, which should
178      * result in an empty "target" attribute in the output.
179      */

180     public void testNonexistentVariable()
181                                  throws ServletException JavaDoc, JspException JavaDoc {
182         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)pageContext.getResponse();
183         String JavaDoc requiredTargetValue = "";
184         response.addHeader(REQUIRED_TARGET_VALUE_KEY, requiredTargetValue);
185
186         String JavaDoc varName = "targetVar";
187         pageContext.setAttribute(varName, "flork");
188         elBaseTag.setTargetExpr("${" + varName + "x" + "}");
189
190         int startTagReturn = elBaseTag.doStartTag();
191     }
192
193     public void endNonexistentVariable(com.meterware.httpunit.WebResponse testResponse) {
194         try {
195             TestHelper.printResponse(testResponse);
196
197             Element JavaDoc docElement = testResponse.getDOM().getDocumentElement();
198             DOMHelper.printNode(docElement);
199
200             HashMap JavaDoc attrMap = new HashMap JavaDoc();
201             DOMHelper.recordFoundAttributes(testResponse.getDOM(),
202                                             "/html/head/base", attrMap);
203             DOMHelper.verifyAttributesPresent(attrMap,
204                                               new String JavaDoc[] { "href", "target" },
205                                               false);
206             checkAttrValue(attrMap, testResponse, REQUIRED_TARGET_VALUE_KEY,
207                            "base", "target");
208         } catch (Exception JavaDoc ex) {
209             ex.printStackTrace();
210             fail();
211         }
212     }
213 }
214
Popular Tags