001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.testsuite.test;
018:
019: import org.apache.pluto.testsuite.TestResult;
020: import org.apache.pluto.testsuite.TestUtils;
021:
022: import java.util.Enumeration;
023:
024: import javax.portlet.PortletContext;
025: import javax.portlet.PortletRequest;
026: import javax.portlet.PortletSession;
027:
028: /**
029: * Tests basic attribute retrieval and storage functions within the portlet
030: * request, session, and context objects.
031: *
032: */
033: public class SimpleAttributeTest extends AbstractReflectivePortletTest {
034:
035: private static final String KEY = "org.apache.pluto.testsuite.BOGUS_KEY";
036: private static final String VAL = "! TEST VAL !";
037:
038: // Test Methods ------------------------------------------------------------
039:
040: protected TestResult checkGetNullAttribute(PortletRequest req) {
041: TestResult result = new TestResult();
042: result
043: .setDescription("Ensure that if an attribute bound to an invalid "
044: + "key is retrieved, null is returned.");
045:
046: Object val = req.getAttribute(KEY);
047: if (val == null) {
048: result.setReturnCode(TestResult.PASSED);
049: } else {
050: TestUtils.failOnAssertion("unbound attribute", val, null,
051: result);
052: }
053: return result;
054: }
055:
056: protected TestResult checkSetAttribute(PortletRequest req) {
057: TestResult result = new TestResult();
058: result.setDescription("Ensure that attributes can be set to "
059: + "portlet request.");
060:
061: req.setAttribute(KEY, VAL);
062: Object val = req.getAttribute(KEY);
063: if (VAL.equals(val)) {
064: result.setReturnCode(TestResult.PASSED);
065: } else {
066: TestUtils.failOnAssertion("attribute", val, VAL, result);
067: }
068:
069: req.removeAttribute(KEY);
070: return result;
071: }
072:
073: protected TestResult checkRemoveAttribute(PortletRequest req) {
074: TestResult result = new TestResult();
075: result
076: .setDescription("Ensure that attributes can be removed from "
077: + "portlet request.");
078:
079: req.setAttribute(KEY, VAL);
080: req.removeAttribute(KEY);
081: Object val = req.getAttribute(KEY);
082: if (val == null) {
083: result.setReturnCode(TestResult.PASSED);
084: } else {
085: TestUtils.failOnAssertion("removed attribute", val, null,
086: result);
087: }
088: return result;
089: }
090:
091: protected TestResult checkEnumerateAttributes(PortletRequest req) {
092: TestResult result = new TestResult();
093: result
094: .setDescription("Ensure that all attribute names appear in the "
095: + "attribute name enumeration returned by portlet request.");
096:
097: int count = 5;
098: for (int i = 0; i < count; i++) {
099: req.setAttribute(KEY + "." + i, VAL);
100: }
101:
102: int found = 0;
103: for (Enumeration en = req.getAttributeNames(); en
104: .hasMoreElements();) {
105: if (en.nextElement().toString().startsWith(KEY)) {
106: found++;
107: }
108: }
109:
110: if (count == found) {
111: result.setReturnCode(TestResult.PASSED);
112: } else {
113: TestUtils.failOnAssertion("count of attribute names",
114: String.valueOf(found), String.valueOf(count),
115: result);
116: }
117: return result;
118: }
119:
120: // Test Methods for Session Attributes -------------------------------------
121:
122: protected TestResult checkGetNullAttribute(PortletSession session) {
123: TestResult res = new TestResult();
124: res.setName("Retrieve Missing Session Attribute Test");
125: res
126: .setDescription("Retrieves an attribute bound to an invalid key set are retrieved as null");
127:
128: Object val = session.getAttribute(KEY);
129: if (val != null) {
130: res.setReturnCode(TestResult.FAILED);
131: res.setResultMessage("Retrieved value: '" + val
132: + "' for attribute '" + KEY + "'");
133: } else {
134: res.setReturnCode(TestResult.PASSED);
135: }
136: return res;
137: }
138:
139: protected TestResult checkSetAttribute(PortletSession session) {
140: TestResult res = new TestResult();
141: res.setName("Set Attribute Test");
142: res
143: .setDescription("Sets and retrieves portlet sessionuest attribute.");
144:
145: session.setAttribute(KEY, VAL);
146: Object val = session.getAttribute(KEY);
147: if (!VAL.equals(val)) {
148: res.setReturnCode(TestResult.FAILED);
149: res.setResultMessage("Retrieved value: '" + val
150: + "' - Expected '" + VAL + "'");
151: } else {
152: res.setReturnCode(TestResult.PASSED);
153: }
154:
155: session.removeAttribute(KEY);
156: return res;
157: }
158:
159: protected TestResult checkRemoveAttribute(PortletSession session) {
160: TestResult res = new TestResult();
161: res.setName("Remove Session Attribute Test");
162: res
163: .setDescription("Sets, removes and retrieves portlet request attribute.");
164:
165: session.setAttribute(KEY, VAL);
166: session.removeAttribute(KEY);
167: Object val = session.getAttribute(KEY);
168: if (val != null) {
169: res.setReturnCode(TestResult.FAILED);
170: res.setResultMessage("Retrieved value: '" + val
171: + "' - Expected '" + VAL + "'");
172: } else {
173: res.setReturnCode(TestResult.PASSED);
174: }
175:
176: return res;
177: }
178:
179: protected TestResult checkEnumerateAttributes(PortletSession session) {
180:
181: TestResult result = new TestResult();
182: result
183: .setDescription("Sets session attributes and enumerates over them.");
184:
185: int count = 5;
186: for (int i = 0; i < count; i++) {
187: session.setAttribute(KEY + "." + i, VAL);
188: }
189:
190: int found = 0;
191: for (Enumeration en = session.getAttributeNames(); en
192: .hasMoreElements();) {
193: String name = (String) en.nextElement();
194: if (name.startsWith(KEY)) {
195: found++;
196: }
197: }
198:
199: if (count != found) {
200: result.setReturnCode(TestResult.FAILED);
201: result.setResultMessage("Expected " + count
202: + " attributes. " + "Found " + found);
203: } else {
204: result.setReturnCode(TestResult.PASSED);
205: }
206: return result;
207: }
208:
209: //
210: // Context Tests
211: //
212:
213: protected TestResult checkGetNullAttribute(PortletContext context) {
214: TestResult res = new TestResult();
215: res.setName("Retrieve Missing Context Attribute Test");
216: res
217: .setDescription("Retrieves an attribute bound to an invalid key set are retrieved as null");
218:
219: Object val = context.getAttribute(KEY);
220: if (val != null) {
221: res.setReturnCode(TestResult.FAILED);
222: res.setResultMessage("Retrieved value: '" + val
223: + "' for attribute '" + KEY + "'");
224: } else {
225: res.setReturnCode(TestResult.PASSED);
226: }
227: return res;
228: }
229:
230: protected TestResult checkSetAttribute(PortletContext context) {
231: TestResult res = new TestResult();
232: res.setName("Set Attribute Test");
233: res
234: .setDescription("Sets and retrieves portlet contextuest attribute.");
235:
236: context.setAttribute(KEY, VAL);
237: Object val = context.getAttribute(KEY);
238: if (!VAL.equals(val)) {
239: res.setReturnCode(TestResult.FAILED);
240: res.setResultMessage("Retrieved value: '" + val
241: + "' - Expected '" + VAL + "'");
242: } else {
243: res.setReturnCode(TestResult.PASSED);
244: }
245:
246: context.removeAttribute(KEY);
247: return res;
248: }
249:
250: protected TestResult checkRemoveAttribute(PortletContext context) {
251: TestResult res = new TestResult();
252: res.setName("Remove Context Attribute Test");
253: res
254: .setDescription("Sets, removes and retrieves portlet request attribute.");
255:
256: context.setAttribute(KEY, VAL);
257: context.removeAttribute(KEY);
258: Object val = context.getAttribute(KEY);
259: if (val != null) {
260: res.setReturnCode(TestResult.FAILED);
261: res.setResultMessage("Retrieved value: '" + val
262: + "' - Expected '" + VAL + "'");
263: } else {
264: res.setReturnCode(TestResult.PASSED);
265: }
266:
267: return res;
268: }
269:
270: protected TestResult checkEnumerateAttributesInContext(
271: PortletContext context) {
272: TestResult result = new TestResult();
273: result.setDescription("Sets attributes in portlet context "
274: + "and enumerates over them.");
275:
276: int count = 5;
277: for (int i = 0; i < count; i++) {
278: context.setAttribute(KEY + "." + i, VAL);
279: }
280:
281: int found = 0;
282: for (Enumeration en = context.getAttributeNames(); en
283: .hasMoreElements();) {
284: if (en.nextElement().toString().startsWith(KEY)) {
285: found++;
286: }
287: }
288:
289: if (count == found) {
290: result.setReturnCode(TestResult.PASSED);
291: } else {
292: result.setReturnCode(TestResult.FAILED);
293: result.setResultMessage("Expected " + count
294: + " attributes. " + "Found " + found);
295: }
296: return result;
297: }
298:
299: }
|