KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > logic > TestPresentTag


1 /*
2  * $Id: TestPresentTag.java 54929 2004-10-16 16:38:42Z 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 package org.apache.struts.taglib.logic;
19  
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 import org.apache.cactus.JspTestCase;
26 import org.apache.cactus.WebRequest;
27 import org.apache.struts.util.LabelValueBean;
28
29 /**
30  * Suite of unit tests for the
31  * <code>org.apache.struts.taglib.logic.PresentTag</code> class.
32  *
33  */

34 public class TestPresentTag extends JspTestCase {
35     protected final static String JavaDoc COOKIE_KEY = "org.apache.struts.taglib.logic.COOKIE_KEY";
36     protected final static String JavaDoc HEADER_KEY = "org.apache.struts.taglib.logic.HEADER_KEY";
37     protected final static String JavaDoc PARAMETER_KEY = "org.apache.struts.taglib.logic.PARAMETER_KEY";
38
39     /**
40      * Defines the testcase name for JUnit.
41      *
42      * @param theName the testcase's name.
43      */

44     public TestPresentTag(String JavaDoc theName) {
45         super(theName);
46     }
47
48     /**
49      * Start the tests.
50      *
51      * @param theArgs the arguments. Not used
52      */

53     public static void main(String JavaDoc[] theArgs) {
54         junit.awtui.TestRunner.main(new String JavaDoc[] {TestPresentTag.class.getName()});
55     }
56
57     /**
58      * @return a test suite (<code>TestSuite</code>) that includes all methods
59      * starting with "test"
60      */

61     public static Test suite() {
62         // All methods starting with "test" will be executed in the test suite.
63
return new TestSuite(TestPresentTag.class);
64     }
65
66     //----- Test initApplication() method --------------------------------------
67

68     /**
69      * Verify that there is an application scope object in scope using the <code>PresentTag</code>.
70     */

71     public void testApplicationScopeObjectPresent() throws ServletException JavaDoc, JspException JavaDoc {
72         PresentTag pt = new PresentTag();
73         String JavaDoc testKey = "testApplicationScopePresent";
74         String JavaDoc testStringValue = "abc";
75         
76         pageContext.setAttribute(testKey, testStringValue, PageContext.APPLICATION_SCOPE);
77         pt.setPageContext(pageContext);
78     pt.setName(testKey);
79     pt.setScope("application");
80     
81         assertEquals("Value present (not null)", true, pt.condition(true));
82     }
83
84     /**
85      * Verify that there is an application scope object is not in scope using the <code>PresentTag</code>.
86     */

87     public void testApplicationScopeObjectNotPresent() throws ServletException JavaDoc, JspException JavaDoc {
88         PresentTag pt = new PresentTag();
89         String JavaDoc testKey = "testApplicationScopeNotPresent";
90
91         pt.setPageContext(pageContext);
92     pt.setName(testKey);
93     pt.setScope("application");
94     
95         assertEquals("Value not present (null)", false, pt.condition(true));
96     }
97     
98     /**
99      * Verify that there is an session scope object in scope using the <code>PresentTag</code>.
100     */

101     public void testSessionScopeObjectPresent() throws ServletException JavaDoc, JspException JavaDoc {
102         PresentTag pt = new PresentTag();
103         String JavaDoc testKey = "testSessionScopePresent";
104         String JavaDoc testStringValue = "abc";
105         
106         pageContext.setAttribute(testKey, testStringValue, PageContext.SESSION_SCOPE);
107         pt.setPageContext(pageContext);
108     pt.setName(testKey);
109     pt.setScope("session");
110     
111         assertEquals("Value present (not null)", true, pt.condition(true));
112     }
113
114     /**
115      * Verify that there is an session scope object is not in scope using the <code>PresentTag</code>.
116     */

117     public void testSessionScopeObjectNotPresent() throws ServletException JavaDoc, JspException JavaDoc {
118         PresentTag pt = new PresentTag();
119         String JavaDoc testKey = "testSessionScopeNotPresent";
120
121         pt.setPageContext(pageContext);
122     pt.setName(testKey);
123     pt.setScope("session");
124     
125         assertEquals("Value present (not null)", false, pt.condition(true));
126     }
127     
128     /**
129      * Verify that there is an request scope object in scope using the <code>PresentTag</code>.
130     */

131     public void testRequestScopeObjectPresent() throws ServletException JavaDoc, JspException JavaDoc {
132         PresentTag pt = new PresentTag();
133         String JavaDoc testKey = "testRequestScopePresent";
134         String JavaDoc testStringValue = "abc";
135         pt.setScope("request");
136         
137         pageContext.setAttribute(testKey, testStringValue, PageContext.REQUEST_SCOPE);
138         pt.setPageContext(pageContext);
139     pt.setName(testKey);
140     
141         assertEquals("Value present (not null)", true, pt.condition(true));
142     }
143
144     /**
145      * Verify that there is an request scope object is not in scope using the <code>PresentTag</code>.
146     */

147     public void testRequestScopeObjectNotPresent() throws ServletException JavaDoc, JspException JavaDoc {
148         PresentTag pt = new PresentTag();
149         String JavaDoc testKey = "testRequestScopeNotPresent";
150
151         pt.setPageContext(pageContext);
152     pt.setName(testKey);
153     pt.setScope("request");
154     
155         assertEquals("Value not present (null)", false, pt.condition(true));
156     }
157     
158     /**
159      * Verify that there is an page scope object in scope using the <code>PresentTag</code>.
160     */

161     public void testPageScopeObjectPresent() throws ServletException JavaDoc, JspException JavaDoc {
162         PresentTag pt = new PresentTag();
163         String JavaDoc testKey = "testPageScopePresent";
164         String JavaDoc testStringValue = "abc";
165         pt.setScope("page");
166         
167         pageContext.setAttribute(testKey, testStringValue, PageContext.PAGE_SCOPE);
168         pt.setPageContext(pageContext);
169     pt.setName(testKey);
170     
171         assertEquals("Value present (not null)", true, pt.condition(true));
172     }
173
174     /**
175      * Verify that there is an page scope object is not in scope using the <code>PresentTag</code>.
176     */

177     public void testPageScopeObjectNotPresent() throws ServletException JavaDoc, JspException JavaDoc {
178         PresentTag pt = new PresentTag();
179         String JavaDoc testKey = "testPageScopeNotPresent";
180
181         pt.setPageContext(pageContext);
182     pt.setName(testKey);
183     pt.setScope("page");
184     
185         assertEquals("Value not present (null)", false, pt.condition(true));
186     }
187    
188     /**
189      * Verify that there is a LabelValueBean in application scope
190      * and test to see if it has a getValue() using the <code>PresentTag</code>.
191     */

192     public void testApplicationScopePropertyPresent()
193         throws ServletException JavaDoc, JspException JavaDoc {
194         PresentTag pt = new PresentTag();
195         String JavaDoc testKey = "testApplicationScopePropertyPresent";
196         
197         String JavaDoc testStringValue = "The Value";
198         LabelValueBean lvb = new LabelValueBean("The Key", testStringValue);
199         
200         pageContext.setAttribute(
201             testKey,
202             lvb,
203             PageContext.APPLICATION_SCOPE);
204         pt.setPageContext(pageContext);
205         pt.setName(testKey);
206         pt.setScope("application");
207         
208         pt.setProperty("value");
209         assertEquals("Property present (not null)", true, pt.condition(true));
210     }
211
212     /**
213      * Verify that there is a LabelValueBean in application scope
214      * and test to see if it has a getValue() that returns null
215      * using the <code>PresentTag</code>.
216     */

217     public void testApplicationScopePropertyNotPresent()
218         throws ServletException JavaDoc, JspException JavaDoc {
219         PresentTag pt = new PresentTag();
220         String JavaDoc testKey = "testApplicationScopePropertyPresent";
221         
222         String JavaDoc testStringValue = null;
223         LabelValueBean lvb = new LabelValueBean("The Key", testStringValue);
224         
225         pageContext.setAttribute(
226             testKey,
227             lvb,
228             PageContext.APPLICATION_SCOPE);
229         pt.setPageContext(pageContext);
230         pt.setName(testKey);
231         pt.setScope("application");
232         
233         pt.setProperty("value");
234         assertEquals("Property present (not null)", false, pt.condition(true));
235     }
236
237     /**
238      * Verify that there is a LabelValueBean in Request scope
239      * and test to see if it has a getValue() using the <code>PresentTag</code>.
240     */

241     public void testRequestScopePropertyPresent()
242         throws ServletException JavaDoc, JspException JavaDoc {
243         PresentTag pt = new PresentTag();
244         String JavaDoc testKey = "testRequestScopePropertyPresent";
245         
246         String JavaDoc testStringValue = "The Value";
247         LabelValueBean lvb = new LabelValueBean("The Key", testStringValue);
248         
249         pageContext.setAttribute(
250             testKey,
251             lvb,
252             PageContext.REQUEST_SCOPE);
253         pt.setPageContext(pageContext);
254         pt.setName(testKey);
255         pt.setScope("request");
256         
257         pt.setProperty("value");
258         assertEquals("Property present (not null)", true, pt.condition(true));
259     }
260
261     /**
262      * Verify that there is a LabelValueBean in Request scope
263      * and test to see if it has a getValue() that returns null
264      * using the <code>PresentTag</code>.
265     */

266     public void testRequestScopePropertyNotPresent()
267         throws ServletException JavaDoc, JspException JavaDoc {
268         PresentTag pt = new PresentTag();
269         String JavaDoc testKey = "testRequestScopePropertyNotPresent";
270         
271         String JavaDoc testStringValue = null;
272         LabelValueBean lvb = new LabelValueBean("The Key", testStringValue);
273         
274         pageContext.setAttribute(
275             testKey,
276             lvb,
277             PageContext.REQUEST_SCOPE);
278         pt.setPageContext(pageContext);
279         pt.setName(testKey);
280         pt.setScope("request");
281         
282         pt.setProperty("value");
283         assertEquals("Property present (not null)", false, pt.condition(true));
284     }
285
286     /**
287      * Create cookie for testCookiePresent method test.
288     */

289     /* FIXME: Cactus does not send cookies?
290     public void beginCookiePresent(WebRequest testRequest) {
291        testRequest.addCookie(COOKIE_KEY, "cookie value");
292     }
293     */

294
295     /**
296      * Verify that there is an cookie using the <code>PresentTag</code>.
297     */

298     /* FIXME: Cactus does not send cookies?
299     public void testCookiePresent() throws ServletException, JspException {
300         PresentTag pt = new PresentTag();
301
302         pt.setPageContext(pageContext);
303     pt.setCookie(COOKIE_KEY);
304
305         assertEquals("Cookie present", true, pt.condition(true));
306     }
307     */

308
309     /**
310      * Verify that there isn't an cookie using the <code>PresentTag</code>.
311     */

312     public void testCookieNotPresent() throws ServletException JavaDoc, JspException JavaDoc {
313         PresentTag pt = new PresentTag();
314
315         pt.setPageContext(pageContext);
316     pt.setCookie(COOKIE_KEY);
317     
318         assertEquals("Cookie not present", false, pt.condition(true));
319     }
320
321     /**
322      * Create header for testHeaderPresent method test.
323     */

324     public void beginHeaderPresent(WebRequest testRequest) {
325        testRequest.addHeader(HEADER_KEY, "header value");
326     }
327
328     /**
329      * Verify that there is an header using the <code>PresentTag</code>.
330     */

331     public void testHeaderPresent() throws ServletException JavaDoc, JspException JavaDoc {
332         PresentTag pt = new PresentTag();
333
334         pt.setPageContext(pageContext);
335     pt.setHeader(HEADER_KEY);
336     
337         assertEquals("Header present", true, pt.condition(true));
338     }
339
340     /**
341      * Verify that there isn't an header using the <code>PresentTag</code>.
342     */

343     public void testHeaderNotPresent() throws ServletException JavaDoc, JspException JavaDoc {
344         PresentTag pt = new PresentTag();
345
346         pt.setPageContext(pageContext);
347     pt.setHeader(HEADER_KEY);
348     
349         assertEquals("Header not present", false, pt.condition(true));
350     }
351
352     /**
353      * Create parameter for testParameterPresent method test.
354     */

355     public void beginParameterPresent(WebRequest testRequest) {
356        testRequest.addParameter(PARAMETER_KEY, "parameter value");
357     }
358
359     /**
360      * Verify that there is an parameter using the <code>PresentTag</code>.
361     */

362     public void testParameterPresent() throws ServletException JavaDoc, JspException JavaDoc {
363         PresentTag pt = new PresentTag();
364
365         pt.setPageContext(pageContext);
366     pt.setParameter(PARAMETER_KEY);
367
368         assertEquals("Parameter present", true, pt.condition(true));
369     }
370
371     /**
372      * Verify that there isn't an parameter using the <code>PresentTag</code>.
373     */

374     public void testParameterNotPresent() throws ServletException JavaDoc, JspException JavaDoc {
375         PresentTag pt = new PresentTag();
376
377         pt.setPageContext(pageContext);
378     pt.setParameter(PARAMETER_KEY);
379     
380         assertEquals("Parameter not present", false, pt.condition(true));
381     }
382     
383 }
384
Popular Tags