1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.pluto.portalImpl.portlet;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
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 configs;
49 private Map tests;
50
51 public void init() throws PortletException {
52 String configFile = getInitParameter("config");
53 if(configFile==null) {
54 configFile = "/WEB-INF/testsuite-config.xml";
55 }
56
57 InputStream in = getPortletContext().getResourceAsStream(configFile);
58 if( in !=null ) {
59 TestConfigFactory fact = new TestConfigFactory();
60 try {
61 configs = fact.createTests(in);
62 tests = new HashMap();
63 Iterator it = configs.iterator();
64 int i = 0;
65 while(it.hasNext()) {
66 TestConfig config = (TestConfig)it.next();
67 String name= config.getTestClassName();
68 PortletTest test = null;
69 if(name != null) {
70 Class 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 t) {
81 throw new PortletException("Unable to read configuration", t);
82 }
83 }
84 else {
85 throw new IllegalStateException("Configuration File Not Found");
86 }
87 }
88
89
90 public void processAction (ActionRequest request,
91 ActionResponse response)
92 throws PortletException, java.io.IOException {
93
94 String 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 renderParameters = null;
104
105 if(test!=null) {
106 renderParameters = test.getRenderParameters(request);
107 }
108
109 if(renderParameters==null) {
110 renderParameters = new java.util.HashMap();
111 }
112
113 renderParameters.put("testId", new String[] {testId});
114 response.setRenderParameters(renderParameters);
115 }
116
117 public void doView(RenderRequest request,
118 RenderResponse response)
119 throws PortletException, IOException {
120
121 String 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 {
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 {
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 getTestId(PortletRequest req) {
203 String testId = req.getParameter("testId");
204 String previous = req.getParameter("previousTestId");
205 String 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
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
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 }