View Javadoc

1   /*
2    * Copyright 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;
19  
20  import javax.portlet.PortletContext;
21  import javax.portlet.PortletSession;
22  
23  /***
24   * <B>TODO</B>: Document
25   * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
26   * @version 1.0
27   * @since Sep 15, 2004
28   */
29  public class ContextInitParameterTest extends AbstractReflectivePortletTest  {
30      private static final String TEST_PARAM_NAME = "test-parameter-name";
31      private static final String TEST_PARAM_VAL = "test-parameter-val";
32  
33      public ContextInitParameterTest() {
34  
35      }
36  
37      public String getTestSuiteName() {
38          return "Context Init Parameter Test";
39      }
40  
41      protected TestResult checkEnumerationContainsElements(PortletContext ctx) {
42          TestResult res = new TestResult();
43          res.setName("Get Init Parameter Enumeration Test");
44          res.setDesc("Test the initialization parameter enumeration.");
45  
46          Enumeration enumerator= ctx.getInitParameterNames();
47          if (enumerator.hasMoreElements()) {
48              res.setReturnCode(TestResult.PASSED);
49          }
50          else {
51              res.setReturnCode(TestResult.FAILED);
52              res.setResults("No init parameters found.");
53          }
54          return res;
55      }
56  
57      protected TestResult checkEnumerationContainsNames(PortletContext ctx) {
58          TestResult res = new TestResult();
59          res.setName("Init Parameter Names in Enumeration Test");
60          res.setDesc("Test to make sure that the expected init parameters exist.");
61  
62          boolean found = false;
63          Enumeration enumerator= ctx.getInitParameterNames();
64          while(enumerator.hasMoreElements()) {
65              String name = (String)enumerator.nextElement();
66              if(TEST_PARAM_NAME.equals(name)) {
67                  found = true;
68              }
69          }
70  
71          if(found) {
72              res.setReturnCode(TestResult.PASSED);
73          }
74          else {
75              res.setReturnCode(TestResult.FAILED);
76              res.setResults("Expected init parameter '"+TEST_PARAM_NAME+"' no found.");
77          }
78  
79          return res;
80      }
81  
82      protected TestResult checkGetInitParameter(PortletContext context) {
83          TestResult res = new TestResult();
84          res.setName("Init Parameter Retrieved Test");
85          res.setDesc("Test to make sure that init parameters are successfully retrieved.");
86  
87          String val = context.getInitParameter(TEST_PARAM_NAME);
88          if(TEST_PARAM_VAL.equals(val)) {
89              res.setReturnCode(TestResult.PASSED);
90          }
91          else {
92              res.setReturnCode(TestResult.FAILED);
93              res.setResults("Expected value not found for key '"+TEST_PARAM_NAME+"'.  Found '"+TEST_PARAM_VAL+"'");
94          }
95          return res;
96      }
97  
98      protected TestResult checkGetContextFromSession(PortletSession session) {
99          TestResult res = new TestResult();
100         res.setName("PortletContext Retrieved From Session Test");
101         res.setDesc("Test ensures that the PortletContext can be retrieved from the session.");
102 
103         PortletContext ctx = session.getPortletContext();
104         if(ctx != null) {
105             res.setReturnCode(TestResult.PASSED);
106         }
107         else {
108             res.setReturnCode(TestResult.FAILED);
109             res.setResults("Portlet Context is Null");
110         }
111         return res;
112     }
113 }
114