KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > portlet > test > SimpleAttributeTest


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

16 package org.apache.pluto.portalImpl.portlet.test;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.portlet.ActionRequest;
23 import javax.portlet.ActionResponse;
24 import javax.portlet.PortletContext;
25 import javax.portlet.PortletRequest;
26 import javax.portlet.PortletSession;
27
28 /**
29  * Tests basic attribute retrieval and storage functions within
30  * the portlet request, session, and context objects.
31  *
32  * @author <a HREF="ddewolf@apache.org">David H. DeWolf</a>
33  */

34 public class SimpleAttributeTest extends AbstractReflectivePortletTest {
35     private static final String JavaDoc KEY = "org.apache.pluto.testsuite.BOGUS_KEY";
36     private static final String JavaDoc VAL = "! TEST VAL !";
37
38     public String JavaDoc getTestSuiteName() {
39         return "Simple Attribute Test";
40     }
41
42     public Map JavaDoc doPrerequisiteAction(PortletContext context, ActionRequest req,
43                                     ActionResponse res) {
44         return new HashMap JavaDoc();
45
46     }
47 //
48
// Begin Request Tests
49
//
50
protected TestResult checkGetNullAttribute(PortletRequest req) {
51         TestResult res = new TestResult();
52         res.setName("Retrieve Missing Request Attribute Test");
53         res.setDesc("Retrieves an attribute bound to an invalid key set are retrieved as null");
54
55         Object JavaDoc val = req.getAttribute(KEY);
56         if(val != null) {
57             res.setReturnCode(TestResult.FAILED);
58             res.setResults("Retrieved value: '"+val+"' for attribute '"+KEY+"'");
59         }
60         else {
61             res.setReturnCode(TestResult.PASSED);
62         }
63         return res;
64     }
65
66
67     protected TestResult checkSetAttribute(PortletRequest req) {
68         TestResult res = new TestResult();
69         res.setName("Set Request Attribute Test");
70         res.setDesc("Sets and retrieves portlet request attribute.");
71
72         req.setAttribute(KEY, VAL);
73         Object JavaDoc val = req.getAttribute(KEY);
74         if(!VAL.equals(val)) {
75             res.setReturnCode(TestResult.FAILED);
76             res.setResults("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
77         }
78         else {
79             res.setReturnCode(TestResult.PASSED);
80         }
81
82         req.removeAttribute(KEY);
83         return res;
84     }
85
86     protected TestResult checkRemoveAttribute(PortletRequest req) {
87         TestResult res = new TestResult();
88         res.setName("Remove Request Attribute Test");
89         res.setDesc("Sets, removes and retrieves portlet request attribute.");
90
91         req.setAttribute(KEY, VAL);
92         req.removeAttribute(KEY);
93         Object JavaDoc val = req.getAttribute(KEY);
94         if(val!=null) {
95             res.setReturnCode(TestResult.FAILED);
96             res.setResults("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
97         }
98         else {
99             res.setReturnCode(TestResult.PASSED);
100         }
101
102         return res;
103     }
104
105     protected TestResult checkEnumerateAttributes(PortletRequest req) {
106         TestResult res = new TestResult();
107         res.setName("Enumerate Request Attribute Names Test");
108         res.setDesc("Sets request attributes and enumerates over them.");
109
110         int count = 5;
111         for(int i=0;i<count;i++) {
112             req.setAttribute(KEY+"."+i,VAL);
113         }
114
115         int found = 0;
116         Enumeration JavaDoc enumerator= req.getAttributeNames();
117         while(enumerator.hasMoreElements()) {
118             if(enumerator.nextElement().toString().startsWith(KEY)) {
119                 found++;
120             }
121         }
122
123         if(count != found) {
124             res.setReturnCode(TestResult.FAILED);
125             res.setResults("Expected "+count+" attributes. Found "+found);
126         }
127         else {
128             res.setReturnCode(TestResult.PASSED);
129         }
130         return res;
131     }
132
133 //
134
// Begin Session Tests
135
//
136

137     protected TestResult checkGetNullAttribute(PortletSession session) {
138         TestResult res = new TestResult();
139         res.setName("Retrieve Missing Session Attribute Test");
140         res.setDesc("Retrieves an attribute bound to an invalid key set are retrieved as null");
141
142         Object JavaDoc val = session.getAttribute(KEY);
143         if(val != null) {
144             res.setReturnCode(TestResult.FAILED);
145             res.setResults("Retrieved value: '"+val+"' for attribute '"+KEY+"'");
146         }
147         else {
148             res.setReturnCode(TestResult.PASSED);
149         }
150         return res;
151     }
152
153     protected TestResult checkSetAttribute(PortletSession session) {
154         TestResult res = new TestResult();
155         res.setName("Set Attribute Test");
156         res.setDesc("Sets and retrieves portlet sessionuest attribute.");
157
158         session.setAttribute(KEY, VAL);
159         Object JavaDoc val = session.getAttribute(KEY);
160         if(!VAL.equals(val)) {
161             res.setReturnCode(TestResult.FAILED);
162             res.setResults("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
163         }
164         else {
165             res.setReturnCode(TestResult.PASSED);
166         }
167
168         session.removeAttribute(KEY);
169         return res;
170     }
171
172     protected TestResult checkRemoveAttribute(PortletSession session) {
173         TestResult res = new TestResult();
174         res.setName("Remove Session Attribute Test");
175         res.setDesc("Sets, removes and retrieves portlet request attribute.");
176
177         session.setAttribute(KEY, VAL);
178         session.removeAttribute(KEY);
179         Object JavaDoc val = session.getAttribute(KEY);
180         if(val!=null) {
181             res.setReturnCode(TestResult.FAILED);
182             res.setResults("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
183         }
184         else {
185             res.setReturnCode(TestResult.PASSED);
186         }
187
188         return res;
189     }
190
191     protected TestResult checkEnumerateAttributes(PortletSession req) {
192         TestResult res = new TestResult();
193         res.setName("Enumerate Session Attribute Names Test");
194         res.setDesc("Sets request attributes and enumerates over them.");
195
196         int count = 5;
197         for(int i=0;i<count;i++) {
198             req.setAttribute(KEY+"."+i,VAL);
199         }
200
201         int found = 0;
202         Enumeration JavaDoc enumerator= req.getAttributeNames();
203         while(enumerator.hasMoreElements()) {
204             if(enumerator.nextElement().toString().startsWith(KEY)) {
205                 found++;
206             }
207         }
208
209         if(count != found) {
210             res.setReturnCode(TestResult.FAILED);
211             res.setResults("Expected "+count+" attributes. Found "+found);
212         }
213         else {
214             res.setReturnCode(TestResult.PASSED);
215         }
216         return res;
217     }
218
219 //
220
// Context Tests
221
//
222

223     protected TestResult checkGetNullAttribute(PortletContext context) {
224         TestResult res = new TestResult();
225         res.setName("Retrieve Missing Context Attribute Test");
226         res.setDesc("Retrieves an attribute bound to an invalid key set are retrieved as null");
227
228         Object JavaDoc val = context.getAttribute(KEY);
229         if(val != null) {
230             res.setReturnCode(TestResult.FAILED);
231             res.setResults("Retrieved value: '"+val+"' for attribute '"+KEY+"'");
232         }
233         else {
234             res.setReturnCode(TestResult.PASSED);
235         }
236         return res;
237     }
238
239     protected TestResult checkSetAttribute(PortletContext context) {
240         TestResult res = new TestResult();
241         res.setName("Set Attribute Test");
242         res.setDesc("Sets and retrieves portlet contextuest attribute.");
243
244         context.setAttribute(KEY, VAL);
245         Object JavaDoc val = context.getAttribute(KEY);
246         if(!VAL.equals(val)) {
247             res.setReturnCode(TestResult.FAILED);
248             res.setResults("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
249         }
250         else {
251             res.setReturnCode(TestResult.PASSED);
252         }
253
254         context.removeAttribute(KEY);
255         return res;
256     }
257
258     protected TestResult checkRemoveAttribute(PortletContext context) {
259         TestResult res = new TestResult();
260         res.setName("Remove Context Attribute Test");
261         res.setDesc("Sets, removes and retrieves portlet request attribute.");
262
263         context.setAttribute(KEY, VAL);
264         context.removeAttribute(KEY);
265         Object JavaDoc val = context.getAttribute(KEY);
266         if(val!=null) {
267             res.setReturnCode(TestResult.FAILED);
268             res.setResults("Retrieved value: '"+val+"' - Expected '"+VAL+"'");
269         }
270         else {
271             res.setReturnCode(TestResult.PASSED);
272         }
273
274         return res;
275     }
276
277     protected TestResult checkEnumerateAttributes(PortletContext req) {
278         TestResult res = new TestResult();
279         res.setName("Enumerate Context Attribute Names Test");
280         res.setDesc("Sets request attributes and enumerates over them.");
281
282         int count = 5;
283         for(int i=0;i<count;i++) {
284             req.setAttribute(KEY+"."+i,VAL);
285         }
286
287         int found = 0;
288         Enumeration JavaDoc enumerator= req.getAttributeNames();
289         while(enumerator.hasMoreElements()) {
290             if(enumerator.nextElement().toString().startsWith(KEY)) {
291                 found++;
292             }
293         }
294
295         if(count != found) {
296             res.setReturnCode(TestResult.FAILED);
297             res.setResults("Expected "+count+" attributes. Found "+found);
298         }
299         else {
300             res.setReturnCode(TestResult.PASSED);
301         }
302         return res;
303     }
304
305 }
306
Popular Tags