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 java.util.ArrayList;
020: import java.util.Enumeration;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Locale;
024: import java.util.Map;
025: import java.util.ResourceBundle;
026:
027: import javax.portlet.PortletConfig;
028: import javax.portlet.PortletRequest;
029:
030: import org.apache.pluto.testsuite.TestResult;
031:
032: /**
033: * Tests basic attribute retrieval and storage functions within
034: * the portlet request, session, and context objects.
035: *
036: */
037: public class ResourceBundleTest extends AbstractReflectivePortletTest {
038:
039: // Static Constant Definitions ---------------------------------------------
040:
041: /** Key for portlet title defined in portlet.xml/init-param. */
042: private static final String TITLE_KEY = "javax.portlet.title";
043:
044: /** Key for portlet short title defined in portlet.xml/init-param. */
045: private static final String SHORT_TITLE_KEY = "javax.portlet.short-title";
046:
047: /** Key for portlet keywords defined in portlet.xml/init-param. */
048: private static final String KEYWORDS_KEY = "javax.portlet.keywords";
049:
050: /**
051: * Parameter name for if resource bundle is declared in
052: * <code>testsuite-config/init-param</code>.
053: */
054: private static final String BUNDLE_DECLARED_PARAM = "resource-bundle";
055:
056: /**
057: * Parameter name for portlet title in
058: * <code>testsuite-config/init-param</code>.
059: */
060: private static final String TITLE_PARAM = "title";
061:
062: /**
063: * Parameter name for portlet short title in
064: * <code>testsuite-config/init-param</code>.
065: */
066: private static final String SHORT_TITLE_PARAM = "short-title";
067:
068: /**
069: * Parameter name for portlet keywords in
070: * <code>testsuite-config/init-param</code>.
071: */
072: private static final String KEYWORDS_PARAM = "keywords";
073:
074: // Test Methods ------------------------------------------------------------
075:
076: protected TestResult checkResourceBundleExists(
077: PortletConfig config, PortletRequest request) {
078: TestResult result = new TestResult();
079: result
080: .setDescription("Ensure the resource bundle is not null.");
081:
082: ResourceBundle bundle = config.getResourceBundle(request
083: .getLocale());
084: if (bundle != null) {
085: result.setReturnCode(TestResult.PASSED);
086: } else {
087: result.setReturnCode(TestResult.FAILED);
088: result
089: .setResultMessage("Unable to retrieve resource bundle "
090: + "for locale: " + request.getLocale());
091: }
092: return result;
093: }
094:
095: protected TestResult checkGetNames(PortletConfig config,
096: PortletRequest request) {
097: TestResult result = new TestResult();
098: result
099: .setDescription("Retrieve the property names and ensure that "
100: + "the required keys are present.");
101:
102: List requiredKeys = new ArrayList();
103: requiredKeys.add(TITLE_KEY);
104: requiredKeys.add(SHORT_TITLE_KEY);
105: requiredKeys.add(KEYWORDS_KEY);
106:
107: ResourceBundle bundle = config.getResourceBundle(request
108: .getLocale());
109: if (bundle == null) {
110: result.setReturnCode(TestResult.WARNING);
111: result
112: .setResultMessage("A function upon which this test depends "
113: + "failed to execute as expected. "
114: + "Check the other test results in this test suite.");
115: return result;
116: }
117:
118: for (Enumeration en = bundle.getKeys(); en.hasMoreElements();) {
119: String key = (String) en.nextElement();
120: requiredKeys.remove(key);
121: }
122:
123: if (requiredKeys.isEmpty()) {
124: result.setReturnCode(TestResult.PASSED);
125: } else {
126: result.setReturnCode(TestResult.FAILED);
127: StringBuffer buffer = new StringBuffer();
128: for (Iterator it = requiredKeys.iterator(); it.hasNext();) {
129: buffer.append(it.next()).append(", ");
130: }
131: result.setResultMessage("Required keys ["
132: + buffer.toString()
133: + "] are missing in the resource bundle.");
134: }
135: return result;
136: }
137:
138: protected TestResult checkGetGermanBundle(PortletConfig config,
139: PortletRequest request) {
140: return doGenericLocaleRequiredFields(config, request,
141: Locale.GERMAN);
142: }
143:
144: protected TestResult checkGetEnglishBundle(PortletConfig config,
145: PortletRequest request) {
146: return doGenericLocaleRequiredFields(config, request,
147: Locale.ENGLISH);
148: }
149:
150: /*
151: protected TestResult checkGetDfltBundle(PortletConfig config,
152: PortletRequest req) {
153: return doGenericLocaleRequiredFields(config, req, new Locale("dflt"));
154: }
155: */
156:
157: // Private Methods ---------------------------------------------------------
158: private TestResult doGenericLocaleRequiredFields(
159: PortletConfig config, PortletRequest request, Locale locale) {
160: TestResult result = new TestResult();
161: result
162: .setDescription("Retrieve the title and ensure it's set properly "
163: + "under locale " + locale);
164:
165: // Retrieve title, short title and keywords from portlet resource bundle.
166: ResourceBundle bundle = config.getResourceBundle(locale);
167: if (bundle == null) {
168: result.setReturnCode(TestResult.WARNING);
169: result
170: .setResultMessage("A function upon which this test depends "
171: + "failed to execute as expected. "
172: + "Check the other test results in this test suite.");
173: return result;
174: }
175: String title = bundle.getString(TITLE_KEY);
176: String shortTitle = bundle.getString(SHORT_TITLE_KEY);
177: String keywords = bundle.getString(KEYWORDS_KEY);
178:
179: // Retrieve expected title, short title and keywords from test config.
180: String suffix = isBundleDeclared() ? ("_" + locale
181: .getLanguage()) : "";
182: Map initParams = getInitParameters();
183: String expectedTitle = (String) initParams.get(TITLE_PARAM
184: + suffix);
185: String expectedShortTitle = (String) initParams
186: .get(SHORT_TITLE_PARAM + suffix);
187: String expectedKeywords = (String) initParams
188: .get(KEYWORDS_PARAM + suffix);
189:
190: // Assert that values retrieved from resource bundler are expected.
191: boolean inconsistent = false;
192: StringBuffer buffer = new StringBuffer();
193: buffer.append("The following information is not correct: ");
194: if (title == null || expectedTitle == null
195: || !title.trim().equals(expectedTitle.trim())) {
196: inconsistent = true;
197: buffer.append("Inconsistent title: '").append(title)
198: .append("' != '").append(expectedTitle).append(
199: "'; ");
200: }
201: if (shortTitle == null || expectedShortTitle == null
202: || !shortTitle.trim().equals(expectedShortTitle.trim())) {
203: inconsistent = true;
204: buffer.append("Inconsistent short title: '").append(
205: shortTitle).append("' != '").append(
206: expectedShortTitle).append("'; ");
207: }
208: if (keywords == null || expectedKeywords == null
209: || !keywords.trim().equals(expectedKeywords.trim())) {
210: inconsistent = true;
211: buffer.append("Inconsistent keywords: '").append(keywords)
212: .append("' != '").append(expectedKeywords).append(
213: "'; ");
214: }
215:
216: if (!inconsistent) {
217: result.setReturnCode(TestResult.PASSED);
218: } else {
219: result.setReturnCode(TestResult.FAILED);
220: result.setResultMessage(buffer.toString());
221: }
222: return result;
223: }
224:
225: private boolean isBundleDeclared() {
226: String bundleDeclared = (String) getInitParameters().get(
227: BUNDLE_DECLARED_PARAM);
228: if (Boolean.TRUE.toString().equalsIgnoreCase(bundleDeclared)) {
229: return true;
230: } else {
231: return false;
232: }
233: }
234:
235: }
|