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.lang.reflect.Method;
19  import java.lang.reflect.InvocationTargetException;
20  import java.util.Map;
21  
22  import javax.portlet.PortletContext;
23  import javax.portlet.PortletRequest;
24  import javax.portlet.PortletResponse;
25  import javax.portlet.PortletSession;
26  import javax.portlet.PortletConfig;
27  
28  /***
29   * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
30   */
31  public abstract class AbstractReflectivePortletTest implements PortletTest {
32  
33      private Map initParameters;
34  
35      public TestResults doTest(PortletConfig config,
36                                PortletContext context,
37                                PortletRequest req,
38                                PortletResponse res) {
39          TestResults results = new TestResults(getTestSuiteName());
40          
41          Class klass = getClass();
42          Method[] methods = klass.getDeclaredMethods();
43  
44          for(int i = 0; i<methods.length;i++) {
45              if(methods[i].getName().startsWith("check")) {
46                  try {
47                      results.add(invoke(methods[i], config, context, req, res));
48                  }
49                  catch(Exception e) {
50                      e.printStackTrace();
51                      TestResult result = new TestResult();
52                      result.setName(methods[i].getName());
53                      result.setDesc("Unknown");
54                      result.setReturnCode(TestResult.FAILED);
55                      result.setResults(e.getMessage());
56                      results.add(result);
57                  }
58              }
59          }
60          return results;
61      }
62  
63      public void init(Map initParameters) {
64          this.initParameters = initParameters;
65      }
66  
67      private TestResult invoke(Method method, PortletConfig config,
68                               PortletContext context,
69                               PortletRequest req, PortletResponse res)
70      throws IllegalAccessException, InvocationTargetException {
71  
72          TestResult result = null;
73          Class[] paramTypes= method.getParameterTypes();
74          Object[] paramValues = new Object[paramTypes.length];
75  
76          for(int i=0;i<paramTypes.length;i++) {
77              if(paramTypes[i].equals(PortletContext.class)) {
78                  paramValues[i] = context;
79              }
80              if(paramTypes[i].equals(PortletRequest.class)) {
81                  paramValues[i] = req;
82              }
83              if(paramTypes[i].equals(PortletResponse.class)) {
84                  paramValues[i] = res;
85              }
86              if(paramTypes[i].equals(PortletSession.class)) {
87                  paramValues[i] = req.getPortletSession();
88              }
89              if(paramTypes[i].equals(PortletConfig.class)) {
90                  paramValues[i] = config;
91              }
92          }
93          result = (TestResult)method.invoke(this, paramValues);
94          return result;
95      }
96  
97      public Map getRenderParameters(PortletRequest req) {
98          Map map = new java.util.HashMap();
99          return map;
100     }
101 
102     public Map getInitParameters() {
103         return initParameters;
104     }
105 }