KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > portlet > TestPortlet


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 /*
17
18  */

19
20 package org.apache.pluto.portalImpl.portlet;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.portlet.ActionRequest;
30 import javax.portlet.ActionResponse;
31 import javax.portlet.GenericPortlet;
32 import javax.portlet.PortletContext;
33 import javax.portlet.PortletException;
34 import javax.portlet.PortletRequest;
35 import javax.portlet.PortletRequestDispatcher;
36 import javax.portlet.PortletSession;
37 import javax.portlet.RenderRequest;
38 import javax.portlet.RenderResponse;
39 import javax.portlet.WindowState;
40
41 import org.apache.pluto.portalImpl.portlet.test.ActionTest;
42 import org.apache.pluto.portalImpl.portlet.test.NoOpTest;
43 import org.apache.pluto.portalImpl.portlet.test.PortletTest;
44 import org.apache.pluto.portalImpl.portlet.test.TestResults;
45
46 public class TestPortlet extends GenericPortlet {
47
48     private List JavaDoc configs;
49     private Map JavaDoc tests;
50
51     public void init() throws PortletException {
52         String JavaDoc configFile = getInitParameter("config");
53         if(configFile==null) {
54             configFile = "/WEB-INF/testsuite-config.xml";
55         }
56
57         InputStream JavaDoc in = getPortletContext().getResourceAsStream(configFile);
58         if( in !=null ) {
59             TestConfigFactory fact = new TestConfigFactory();
60             try {
61                 configs = fact.createTests(in);
62                 tests = new HashMap JavaDoc();
63                 Iterator JavaDoc it = configs.iterator();
64                 int i = 0;
65                 while(it.hasNext()) {
66                     TestConfig config = (TestConfig)it.next();
67                     String JavaDoc name= config.getTestClassName();
68                     PortletTest test = null;
69                     if(name != null) {
70                         Class JavaDoc cl = Class.forName(config.getTestClassName());
71                         test = (PortletTest)cl.newInstance();
72                     }
73                     else {
74                         test = new NoOpTest();
75                     }
76                     test.init(config);
77                     tests.put(String.valueOf(i++), test);
78                 }
79             }
80             catch (Throwable JavaDoc t) {
81                 throw new PortletException("Unable to read configuration", t);
82             }
83         }
84         else {
85             throw new IllegalStateException JavaDoc("Configuration File Not Found");
86         }
87     }
88
89
90     public void processAction (ActionRequest request,
91                                ActionResponse response)
92     throws PortletException, java.io.IOException JavaDoc {
93
94         String JavaDoc testId = getTestId(request);
95         PortletTest test = (PortletTest)tests.get(testId);
96
97         if(test!=null && test instanceof ActionTest) {
98             TestResults results = test.doTest(getPortletConfig(),
99                                               getPortletContext(),
100                                               request, response);
101             request.getPortletSession().setAttribute(test.getClass().getName(), results);
102         }
103         Map JavaDoc renderParameters = null;
104
105         if(test!=null) {
106             renderParameters = test.getRenderParameters(request);
107         }
108
109         if(renderParameters==null) {
110             renderParameters = new java.util.HashMap JavaDoc();
111         }
112
113         renderParameters.put("testId", new String JavaDoc[] {testId});
114         response.setRenderParameters(renderParameters);
115     }
116
117     public void doView(RenderRequest request,
118                        RenderResponse response)
119     throws PortletException, IOException JavaDoc {
120
121         String JavaDoc testId = getTestId(request);
122
123         TestConfig config = null;
124         if(testId != null) {
125             config = (TestConfig)configs.get(Integer.parseInt(testId));
126         }
127
128         PortletTest test = (PortletTest)tests.get(testId);
129
130         WindowState state = request.getWindowState();
131         if (!state.equals(WindowState.MINIMIZED)) {
132             response.setContentType("text/html");
133
134             if(test != null && !(test instanceof ActionTest) ) {
135                 TestResults results = test.doTest(getPortletConfig(),
136                                                   getPortletContext(),
137                                                   request, response);
138                 request.setAttribute("results", results);
139             }
140             else if(test != null) {
141                 PortletSession session = request.getPortletSession();
142                 TestResults results = (TestResults)session.getAttribute(test.getClass().getName());
143                 request.setAttribute("results", results);
144             }
145
146             if(testId == null) {
147                 request.setAttribute("tests", configs);
148             }
149             else {
150                 TestConfig next = null;
151                 TestConfig prev = null;
152                 int index = configs.indexOf(test.getConfig());
153                 if(index==0) {
154                     prev = (TestConfig)configs.get(configs.size()-1);
155                     next = (TestConfig)configs.get(index+1);
156                 }
157                 else if(index == configs.size()-1) {
158                     prev = (TestConfig)configs.get(index-1);
159                     next = (TestConfig)configs.get(0);
160                 }
161                 else {
162                     prev = (TestConfig)configs.get(index-1);
163                     next = (TestConfig)configs.get(index+1);
164                 }
165                 request.setAttribute("prevTest", prev);
166                 request.setAttribute("nextTest", next);
167             }
168
169             PortletContext context = getPortletContext();
170             PortletRequestDispatcher rd = null;
171             if(config != null) {
172                 rd = context.getRequestDispatcher(config.getDisplayURI());
173             }
174             else {
175                 rd = context.getRequestDispatcher("/jsp/introduction.jsp");
176             }
177             rd.include(request,response);
178         }
179     }
180
181
182     protected void doEdit(RenderRequest req, RenderResponse res)
183     throws PortletException, IOException JavaDoc {
184         WindowState state = req.getWindowState();
185         if(!state.equals(WindowState.MINIMIZED)) {
186             PortletContext context = getPortletContext();
187             PortletRequestDispatcher rd = context.getRequestDispatcher("/jsp/edit.jsp");
188             rd.include(req,res);
189         }
190     }
191
192     protected void doHelp(RenderRequest req, RenderResponse res)
193     throws PortletException, IOException JavaDoc {
194         WindowState state = req.getWindowState();
195         if(!state.equals(WindowState.MINIMIZED)) {
196             PortletContext context = getPortletContext();
197             PortletRequestDispatcher rd = context.getRequestDispatcher("/jsp/help.jsp");
198             rd.include(req,res);
199         }
200     }
201
202     private String JavaDoc getTestId(PortletRequest req) {
203         String JavaDoc testId = req.getParameter("testId");
204         String JavaDoc previous = req.getParameter("previousTestId");
205         String JavaDoc next = req.getParameter("nextTestId");
206
207         if((testId == null || testId.trim().length()==0)
208            && next == null && previous == null && tests.size() > 0) {
209             return null;
210         }
211         // Retrieve the test which is next to the previous
212
else if(testId == null && previous !=null) {
213             int pId = Integer.parseInt(previous);
214             if(pId >= configs.size()-1) {
215                 testId = "0";
216             }
217             else {
218                 testId = String.valueOf(pId+1);
219             }
220         }
221         // Retrieve the test which is previous to the next
222
else if(testId == null && next !=null) {
223             int nId = Integer.parseInt(next);
224             if(nId <= 0) {
225                 testId = String.valueOf(configs.size()-1);
226             }
227             else {
228                 testId = String.valueOf(nId - 1);
229             }
230         }
231
232         return testId;
233     }
234 }
235
Popular Tags