KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > portlet > test > AbstractReflectivePortletTest


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.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.portlet.PortletConfig;
23 import javax.portlet.PortletContext;
24 import javax.portlet.PortletRequest;
25 import javax.portlet.PortletResponse;
26 import javax.portlet.PortletSession;
27
28 import org.apache.pluto.portalImpl.portlet.TestConfig;
29
30 /**
31  * @author <a HREF="ddewolf@apache.org">David H. DeWolf</a>
32  */

33 public abstract class AbstractReflectivePortletTest implements PortletTest {
34
35     private Map JavaDoc initParameters;
36     private TestConfig config;
37
38     public TestResults doTest(PortletConfig config,
39                               PortletContext context,
40                               PortletRequest req,
41                               PortletResponse res) {
42         TestResults results = new TestResults(getTestSuiteName());
43         
44         Class JavaDoc klass = getClass();
45         Method JavaDoc[] methods = klass.getDeclaredMethods();
46
47         for(int i = 0; i<methods.length;i++) {
48             if(methods[i].getName().startsWith("check")) {
49                 try {
50                     results.add(invoke(methods[i], config, context, req, res));
51                 }
52                 catch(Exception JavaDoc e) {
53                     e.printStackTrace();
54                     TestResult result = new TestResult();
55                     result.setName(methods[i].getName());
56                     result.setDesc("Unknown");
57                     result.setReturnCode(TestResult.FAILED);
58                     result.setResults(e.getMessage());
59                     results.add(result);
60                 }
61             }
62         }
63         return results;
64     }
65
66     public void init(TestConfig config) {
67         this.initParameters = config.getInitParameters();
68         this.config = config;
69     }
70
71     private TestResult invoke(Method JavaDoc method, PortletConfig config,
72                              PortletContext context,
73                              PortletRequest req, PortletResponse res)
74     throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
75
76         TestResult result = null;
77         Class JavaDoc[] paramTypes= method.getParameterTypes();
78         Object JavaDoc[] paramValues = new Object JavaDoc[paramTypes.length];
79
80         for(int i=0;i<paramTypes.length;i++) {
81             if(paramTypes[i].equals(PortletContext.class)) {
82                 paramValues[i] = context;
83             }
84             if(paramTypes[i].equals(PortletRequest.class)) {
85                 paramValues[i] = req;
86             }
87             if(paramTypes[i].equals(PortletResponse.class)) {
88                 paramValues[i] = res;
89             }
90             if(paramTypes[i].equals(PortletSession.class)) {
91                 paramValues[i] = req.getPortletSession();
92             }
93             if(paramTypes[i].equals(PortletConfig.class)) {
94                 paramValues[i] = config;
95             }
96         }
97         result = (TestResult)method.invoke(this, paramValues);
98         return result;
99     }
100
101     public Map JavaDoc getRenderParameters(PortletRequest req) {
102         Map JavaDoc map = new java.util.HashMap JavaDoc();
103         return map;
104     }
105
106     public Map JavaDoc getInitParameters() {
107         return initParameters;
108     }
109
110     public TestConfig getConfig() {
111         return config;
112     }
113 }
114
Popular Tags