001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test.web.framework;
018:
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.servlet.Servlet;
024:
025: import org.apache.log4j.Logger;
026: import org.springframework.mock.web.MockHttpServletRequest;
027: import org.springframework.mock.web.MockHttpServletResponse;
028:
029: /**
030: * An InteractionController which uses Spring mock servlet objects against
031: * a specified Servlet instance
032: * @author Aaron Hamid (arh14 at cornell dot edu)
033: */
034: public class LocalInteractionController implements
035: InteractionController {
036: private static final Logger LOG = Logger
037: .getLogger(LocalInteractionController.class);
038:
039: private Servlet servlet;
040:
041: public LocalInteractionController(Servlet servlet) {
042: this .servlet = servlet;
043: }
044:
045: public String submit(String method, String uri, Script script)
046: throws Exception {
047: MockHttpServletRequest request = createServletRequest(method,
048: uri, script);
049: request.setMethod(method);
050: Iterator it = script.getState().getRequest().entrySet()
051: .iterator();
052: while (it.hasNext()) {
053: Map.Entry entry = (Map.Entry) it.next();
054: String name = entry.getKey().toString();
055: Object value = entry.getValue();
056: if (value instanceof List) {
057: List l = (List) value;
058: request.addParameter(name, (String[]) l
059: .toArray(new String[l.size()]));
060: } else if (value instanceof String) {
061: request.addParameter(name, (String) value);
062: } else {
063: LOG.warn("Invalid parameter value type for parameter '"
064: + name + "': " + value.getClass());
065: }
066: }
067: MockHttpServletResponse response = createServletResponse(
068: method, uri, script, request);
069:
070: servlet.service(request, response);
071:
072: response.flushBuffer();
073: return response.getContentAsString();
074: }
075:
076: /**
077: * Template method that subclasses can override to return custom MockHttpServletRequest subclasses
078: * @param method the method
079: * @param uri the request uri
080: * @param script the script
081: * @return a MockHttpServletRequest
082: */
083: protected MockHttpServletRequest createServletRequest(
084: String method, String uri, Script script) throws Exception {
085: MockHttpServletRequest request = new MockHttpServletRequest(
086: method, uri);
087: String user = script.getState().getUser();
088: if (user != null) {
089: request.setRemoteUser(user);
090: }
091: return request;
092: }
093:
094: /**
095: * Template method that subclasses can override to return custom MockHttpServletResponse subclasses
096: * @param method the method
097: * @param uri the request uri
098: * @param script the script
099: * @return a MockHttpServletResponse
100: */
101: protected MockHttpServletResponse createServletResponse(
102: String method, String uri, Script script,
103: MockHttpServletRequest request) throws Exception {
104: return new MockHttpServletResponse();
105: }
106: }
|