KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TestEmptyTag.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 java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 import org.apache.cactus.JspTestCase;
30 import org.apache.struts.taglib.SimpleBeanForTesting;
31 import org.apache.struts.util.LabelValueBean;
32
33 /**
34  * Suite of unit tests for the
35  * <code>org.apache.struts.taglib.logic.EmptyTag</code> class.
36  *
37  */

38 public class TestEmptyTag extends JspTestCase {
39     
40     /**
41      * Defines the testcase name for JUnit.
42      *
43      * @param theName the testcase's name.
44      */

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

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

62     public static Test suite() {
63         // All methods starting with "test" will be executed in the test suite.
64
return new TestSuite(TestEmptyTag.class);
65     }
66
67     private void runNameTest(String JavaDoc testKey, EmptyTag et,
68             Object JavaDoc o, int scope, String JavaDoc whichScope, boolean condition, boolean useProperty, String JavaDoc property)
69                 throws ServletException JavaDoc, JspException JavaDoc {
70
71         pageContext.setAttribute(testKey, o, scope);
72         et.setPageContext(pageContext);
73         et.setName(testKey);
74         if (useProperty){
75             et.setProperty(property);
76         }
77         et.setScope(whichScope);
78
79         assertEquals(
80             "Testing " + testKey + " with EmtpyTag in " + whichScope + " scope",
81             condition,
82             et.condition());
83     }
84
85     /**
86      * Testing <code>EmptyTag</code> using name attribute in
87      * the some scope.
88     */

89     public void testEmptyTagUsingName()
90         throws ServletException JavaDoc, JspException JavaDoc {
91         
92         String JavaDoc tst = "";
93         runNameTest("testStringEmptyString", new EmptyTag(), tst, PageContext.APPLICATION_SCOPE, "application", true, false, null);
94         runNameTest("testStringEmptyString", new EmptyTag(), tst, PageContext.SESSION_SCOPE, "session", true, false, null);
95         runNameTest("testStringEmptyString", new EmptyTag(), tst, PageContext.REQUEST_SCOPE, "request", true, false, null);
96         
97         tst = "hello";
98         runNameTest("testStringNotEmpty", new EmptyTag(), tst, PageContext.APPLICATION_SCOPE, "application", false, false, null);
99         runNameTest("testStringNotEmpty", new EmptyTag(), tst, PageContext.SESSION_SCOPE, "session", false, false, null);
100         runNameTest("testStringNotEmpty", new EmptyTag(), tst, PageContext.REQUEST_SCOPE, "request", false, false, null);
101
102         // Testing ArrayList
103
ArrayList JavaDoc lst = new ArrayList JavaDoc();
104         runNameTest("testArrayListEmpty", new EmptyTag(), lst, PageContext.APPLICATION_SCOPE, "application", true, false, null);
105         runNameTest("testArrayListEmpty", new EmptyTag(), lst, PageContext.SESSION_SCOPE, "session", true, false, null);
106         runNameTest("testArrayListEmpty", new EmptyTag(), lst, PageContext.REQUEST_SCOPE, "request", true, false, null);
107         
108         lst.add(0, "test");
109         runNameTest("testArrayListNotEmpty", new EmptyTag(), lst, PageContext.APPLICATION_SCOPE, "application", false, false, null);
110         runNameTest("testArrayListNotEmpty", new EmptyTag(), lst, PageContext.SESSION_SCOPE, "session", false, false, null);
111         runNameTest("testArrayListNotEmpty", new EmptyTag(), lst, PageContext.REQUEST_SCOPE, "request", false, false, null);
112
113         // Testing HashMap
114
HashMap JavaDoc map = new HashMap JavaDoc();
115         runNameTest("testMapEmpty", new EmptyTag(), map, PageContext.APPLICATION_SCOPE, "application", true, false, null);
116         runNameTest("testMapEmpty", new EmptyTag(), map, PageContext.SESSION_SCOPE, "session", true, false, null);
117         runNameTest("testMapEmpty", new EmptyTag(), map, PageContext.REQUEST_SCOPE, "request", true, false, null);
118         
119         map.put("testKey", "test");
120         runNameTest("testMapNotEmpty", new EmptyTag(), map, PageContext.APPLICATION_SCOPE, "application", false, false, null);
121         runNameTest("testMapNotEmpty", new EmptyTag(), map, PageContext.SESSION_SCOPE, "session", false, false, null);
122         runNameTest("testMapNotEmpty", new EmptyTag(), map, PageContext.REQUEST_SCOPE, "request", false, false, null);
123         
124     }
125     
126     /**
127      * Testing <code>EmptyTag</code> using name attribute in
128      * the some scope.
129     */

130     public void testEmptyTagUsingProperty()
131         throws ServletException JavaDoc, JspException JavaDoc {
132         
133         LabelValueBean lvb = new LabelValueBean(null, null);
134         runNameTest("testStringEmptyString", new EmptyTag(), lvb, PageContext.APPLICATION_SCOPE, "application", true, true, "value");
135         runNameTest("testStringEmptyString", new EmptyTag(), lvb, PageContext.SESSION_SCOPE, "session", true, true, "value");
136         runNameTest("testStringEmptyString", new EmptyTag(), lvb, PageContext.REQUEST_SCOPE, "request", true, true, "value");
137         
138         lvb.setValue("");
139         runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.APPLICATION_SCOPE, "application", true, true, "value");
140         runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.SESSION_SCOPE, "session", true, true, "value");
141         runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.REQUEST_SCOPE, "request", true, true, "value");
142
143         lvb.setValue("hello");
144         runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.APPLICATION_SCOPE, "application", false, true, "value");
145         runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.SESSION_SCOPE, "session", false, true, "value");
146         runNameTest("testStringNotEmpty", new EmptyTag(), lvb, PageContext.REQUEST_SCOPE, "request", false, true, "value");
147
148         // Testing ArrayList
149
SimpleBeanForTesting sbft = new SimpleBeanForTesting();
150         ArrayList JavaDoc lst = new ArrayList JavaDoc();
151         sbft.setList(lst);
152         runNameTest("testArrayListEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", true, true, "list");
153         runNameTest("testArrayListEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", true, true, "list");
154         runNameTest("testArrayListEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", true, true, "list");
155         
156         lst.add(0, "test");
157         runNameTest("testArrayListNotEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", false, true, "list");
158         runNameTest("testArrayListNotEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", false, true, "list");
159         runNameTest("testArrayListNotEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", false, true, "list");
160
161         // Testing HashMap
162
HashMap JavaDoc map = new HashMap JavaDoc();
163         sbft.setMap(map);
164         runNameTest("testMapEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", true, true, "map");
165         runNameTest("testMapEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", true, true, "map");
166         runNameTest("testMapEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", true, true, "map");
167         
168         map.put("testKey", "test");
169         runNameTest("testMapNotEmpty", new EmptyTag(), sbft, PageContext.APPLICATION_SCOPE, "application", false, true, "map");
170         runNameTest("testMapNotEmpty", new EmptyTag(), sbft, PageContext.SESSION_SCOPE, "session", false, true, "map");
171         runNameTest("testMapNotEmpty", new EmptyTag(), sbft, PageContext.REQUEST_SCOPE, "request", false, true, "map");
172         
173     }
174     
175     
176 }
177
Popular Tags