KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TestNotPresentTag.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.PageContext JavaDoc;
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.cactus.JspTestCase;
26 import org.apache.cactus.WebRequest;
27
28 /**
29  * Suite of unit tests for the
30  * <code>org.apache.struts.taglib.logic.PresentTag</code> class.
31  *
32  */

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

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

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

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

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

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

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

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

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

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

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

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

177     public void testPageScopeObjectNotPresent() throws ServletException JavaDoc, javax.servlet.jsp.JspException JavaDoc {
178         NotPresentTag npt = new NotPresentTag();
179         String JavaDoc testKey = "testPageScopeNotPresent";
180
181         npt.setPageContext(pageContext);
182     npt.setName(testKey);
183     npt.setScope("page");
184     
185         assertEquals("Value not present (null)", true, npt.condition(false));
186     }
187
188     /**
189      * Create cookie for testCookiePresent method test.
190     */

191     /* FIXME: Cactus does not send cookies?
192     public void beginCookiePresent(WebRequest testRequest) {
193        testRequest.addCookie(COOKIE_KEY, "cookie value");
194     }
195     */

196
197     /**
198      * Verify that there is an cookie using the <code>PresentTag</code>.
199     */

200     /* FIXME: Cactus does not send cookies?
201     public void testCookiePresent() throws ServletException, javax.servlet.jsp.JspException {
202         NotPresentTag npt = new NotPresentTag();
203
204         npt.setPageContext(pageContext);
205     npt.setCookie(COOKIE_KEY);
206
207         assertEquals("Cookie present", false, npt.condition(false));
208     }
209     */

210
211     /**
212      * Verify that there isn't an cookie using the <code>PresentTag</code>.
213     */

214     public void testCookieNotPresent() throws ServletException JavaDoc, javax.servlet.jsp.JspException JavaDoc {
215         NotPresentTag npt = new NotPresentTag();
216
217         npt.setPageContext(pageContext);
218     npt.setCookie(COOKIE_KEY);
219     
220         assertEquals("Cookie not present", true, npt.condition(false));
221     }
222
223     /**
224      * Create header for testHeaderPresent method test.
225     */

226     public void beginHeaderPresent(WebRequest testRequest) {
227        testRequest.addHeader(HEADER_KEY, "header value");
228     }
229
230     /**
231      * Verify that there is an header using the <code>PresentTag</code>.
232     */

233     public void testHeaderPresent() throws ServletException JavaDoc, javax.servlet.jsp.JspException JavaDoc {
234         NotPresentTag npt = new NotPresentTag();
235
236         npt.setPageContext(pageContext);
237     npt.setHeader(HEADER_KEY);
238     
239         assertEquals("Header present", false, npt.condition(false));
240     }
241
242     /**
243      * Verify that there isn't an header using the <code>PresentTag</code>.
244     */

245     public void testHeaderNotPresent() throws ServletException JavaDoc, javax.servlet.jsp.JspException JavaDoc {
246         NotPresentTag npt = new NotPresentTag();
247
248         npt.setPageContext(pageContext);
249     npt.setHeader(HEADER_KEY);
250     
251         assertEquals("Header not present", true, npt.condition(false));
252     }
253
254     /**
255      * Create parameter for testParameterPresent method test.
256     */

257     public void beginParameterPresent(WebRequest testRequest) {
258        testRequest.addParameter(PARAMETER_KEY, "parameter value");
259     }
260
261     /**
262      * Verify that there is an parameter using the <code>PresentTag</code>.
263     */

264     public void testParameterPresent() throws ServletException JavaDoc, javax.servlet.jsp.JspException JavaDoc {
265         NotPresentTag npt = new NotPresentTag();
266
267         npt.setPageContext(pageContext);
268     npt.setParameter(PARAMETER_KEY);
269
270         assertEquals("Parameter present", false, npt.condition(false));
271     }
272
273     /**
274      * Verify that there isn't an parameter using the <code>PresentTag</code>.
275     */

276     public void testParameterNotPresent() throws ServletException JavaDoc, javax.servlet.jsp.JspException JavaDoc {
277         NotPresentTag npt = new NotPresentTag();
278
279         npt.setPageContext(pageContext);
280     npt.setParameter(PARAMETER_KEY);
281     
282         assertEquals("Parameter not present", true, npt.condition(false));
283     }
284     
285 }
286
Popular Tags