001: /*
002: * $Id: ServletDispatcherResultTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import javax.servlet.RequestDispatcher;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import ognl.Ognl;
028:
029: import org.apache.struts2.ServletActionContext;
030: import org.apache.struts2.StrutsStatics;
031: import org.apache.struts2.StrutsTestCase;
032:
033: import com.mockobjects.dynamic.C;
034: import com.mockobjects.dynamic.Mock;
035: import com.opensymphony.xwork2.ActionContext;
036:
037: /**
038: *
039: */
040: public class ServletDispatcherResultTest extends StrutsTestCase
041: implements StrutsStatics {
042:
043: public void testInclude() {
044: ServletDispatcherResult view = new ServletDispatcherResult();
045: view.setLocation("foo.jsp");
046:
047: Mock dispatcherMock = new Mock(RequestDispatcher.class);
048: dispatcherMock.expect("include", C.ANY_ARGS);
049:
050: Mock requestMock = new Mock(HttpServletRequest.class);
051: requestMock.expectAndReturn("getRequestDispatcher", C.args(C
052: .eq("foo.jsp")), dispatcherMock.proxy());
053:
054: Mock responseMock = new Mock(HttpServletResponse.class);
055: responseMock.expectAndReturn("isCommitted", Boolean.TRUE);
056:
057: ActionContext ac = new ActionContext(Ognl
058: .createDefaultContext(null));
059: ActionContext.setContext(ac);
060: ServletActionContext
061: .setRequest((HttpServletRequest) requestMock.proxy());
062: ServletActionContext
063: .setResponse((HttpServletResponse) responseMock.proxy());
064:
065: try {
066: view.execute(null);
067: } catch (Exception e) {
068: e.printStackTrace();
069: fail();
070: }
071:
072: dispatcherMock.verify();
073: requestMock.verify();
074: dispatcherMock.verify();
075: }
076:
077: public void testSimple() {
078: ServletDispatcherResult view = new ServletDispatcherResult();
079: view.setLocation("foo.jsp");
080:
081: Mock dispatcherMock = new Mock(RequestDispatcher.class);
082: dispatcherMock.expect("forward", C.ANY_ARGS);
083:
084: Mock requestMock = new Mock(HttpServletRequest.class);
085: requestMock.expectAndReturn("getAttribute",
086: "javax.servlet.include.servlet_path", null);
087: requestMock.expectAndReturn("getRequestDispatcher", C.args(C
088: .eq("foo.jsp")), dispatcherMock.proxy());
089: requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
090: requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
091: requestMock.matchAndReturn("getRequestURI", "foo.jsp");
092:
093: Mock responseMock = new Mock(HttpServletResponse.class);
094: responseMock.expectAndReturn("isCommitted", Boolean.FALSE);
095:
096: ActionContext ac = new ActionContext(Ognl
097: .createDefaultContext(null));
098: ActionContext.setContext(ac);
099: ServletActionContext
100: .setRequest((HttpServletRequest) requestMock.proxy());
101: ServletActionContext
102: .setResponse((HttpServletResponse) responseMock.proxy());
103:
104: try {
105: view.execute(null);
106: } catch (Exception e) {
107: e.printStackTrace();
108: fail();
109: }
110:
111: dispatcherMock.verify();
112: requestMock.verify();
113: dispatcherMock.verify();
114: }
115: }
|