View Javadoc

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.ArrayList;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.ResourceBundle;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletContext;
30  import javax.portlet.PortletRequest;
31  
32  /***
33   * Tests basic attribute retrieval and storage functions within
34   * the portlet request, session, and context objects.
35   *
36   * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
37   */
38  public class ResourceBundleTest extends AbstractReflectivePortletTest {
39  
40      public String getTestSuiteName() {
41          return "Resource Bundle Test";
42      }
43  
44      public Map doPrerequisiteAction(PortletContext context, ActionRequest req,
45                                      ActionResponse res) {
46          return new HashMap();
47  
48      }
49  //
50  // Begin Request Tests
51  //
52      protected TestResult checkResourceBundleExists(PortletConfig config,
53                                                     PortletRequest req) {
54          TestResult res = new TestResult();
55          res.setName("Get ResourceBundle Test");
56          res.setDesc("Retrieves the resource bundle and ensures it's not null.");
57  
58          ResourceBundle rb = config.getResourceBundle(req.getLocale());
59          if(rb != null) {
60              res.setReturnCode(TestResult.PASSED);
61          }
62          else {
63              res.setReturnCode(TestResult.FAILED);
64              res.setResults(TestResult.FAILED);
65          }
66          return res;
67      }
68      protected TestResult checkGetNames(PortletConfig config, PortletRequest req) {
69          TestResult res = new TestResult();
70          res.setName("Get Property Keys Test");
71          res.setDesc("Retrieve the property names and ensure that the required keys are present.");
72  
73          ArrayList keys = new ArrayList();
74          keys.add("javax.portlet.title");
75          keys.add("javax.portlet.short-title");
76          keys.add("javax.portlet.keywords");
77  
78          ResourceBundle rb = config.getResourceBundle(req.getLocale());
79          Enumeration enumerator= rb.getKeys();
80          while(enumerator.hasMoreElements()) {
81              String key = enumerator.nextElement().toString();
82              keys.remove(key);
83          }
84  
85          if(keys.size() < 1) {
86              res.setReturnCode(TestResult.PASSED);
87          }
88          else {
89              res.setReturnCode(TestResult.FAILED);
90              StringBuffer sb = new StringBuffer();
91              Iterator it = keys.iterator();
92              while(it.hasNext()) {
93                  sb.append(it.next()+", ");
94              }
95  
96              res.setResults("Keys: "+sb.toString()+" are not defined.");
97          }
98  
99          return res;
100     }
101 
102     protected TestResult checkGetGermanBundle(PortletConfig config,
103                                              PortletRequest req) {
104         return doGenericLocaleRequiredFields(config, req, Locale.GERMAN);
105     }
106 
107     protected TestResult checkGetEnglishBundle(PortletConfig config,
108                                               PortletRequest req) {
109         return doGenericLocaleRequiredFields(config, req, Locale.ENGLISH);
110     }
111 
112     /*
113     protected TestResult checkGetDfltBundle(PortletConfig config,
114                                             PortletRequest req) {
115         return doGenericLocaleRequiredFields(config, req, new Locale("dflt"));
116     }
117     */
118 
119     private TestResult doGenericLocaleRequiredFields(PortletConfig config,
120                                           PortletRequest req,
121                                           Locale locale) {
122 
123         TestResult res = new TestResult();
124         res.setName("Get Portlet Info for "+locale.getLanguage());
125         res.setDesc("Retrieve the title and ensure it's set properly");
126 
127         String prefix = "_"+locale.getLanguage();
128 
129         if(!isBundleDeclared()) {
130             prefix = "";
131         }
132         Map params = getInitParameters();
133         ResourceBundle rb = config.getResourceBundle(locale);
134 
135         String eTitle = (String)params.get("title"+prefix);
136         String eSTitle = (String)params.get("shorttitle"+prefix);
137         String eKeywds = (String)params.get("keywords"+prefix);
138 
139         String title = rb.getString("javax.portlet.title");
140         String sTitle = rb.getString("javax.portlet.short-title");
141         String keywds = rb.getString("javax.portlet.keywords");
142 
143         StringBuffer sb = new StringBuffer();
144         if(title==null || !title.trim().equals(eTitle.trim())) {
145             sb.append("Title :'"+title+"' != '"+eTitle+"';");
146         }
147         if(sTitle==null || !sTitle.trim().equals(eSTitle.trim())) {
148             sb.append("Short Title :'"+sTitle+"' != '"+eSTitle+"';");
149         }
150         if(keywds==null || !keywds.trim().equals(eKeywds.trim())) {
151             sb.append("Keywords :'"+keywds+"' != '"+eKeywds+"' ");
152         }
153 
154         if(sb.length() < 1) {
155             res.setReturnCode(TestResult.PASSED);
156         }
157         else {
158             res.setReturnCode(TestResult.FAILED);
159             sb.insert(0, "The following information is not correct: ");
160             res.setResults(sb.toString());
161         }
162         return res;
163     }
164 
165     private boolean isBundleDeclared() {
166         String param = (String)getInitParameters().get("resource-bundle");
167         if(Boolean.TRUE.toString().equals(param)) {
168             return true;
169         }
170         return false;
171     }
172 
173 
174 
175 
176 
177 }