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