001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.engine;
016    
017    import static org.easymock.EasyMock.checkOrder;
018    import static org.easymock.EasyMock.expect;
019    
020    import java.util.Map;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.apache.tapestry.IComponent;
025    import org.apache.tapestry.IPage;
026    import org.apache.tapestry.IRequestCycle;
027    import org.apache.tapestry.services.LinkFactory;
028    import org.apache.tapestry.services.ResponseRenderer;
029    import org.apache.tapestry.web.WebRequest;
030    import org.apache.tapestry.web.WebSession;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpSession;
034    
035    /**
036     * Common utilities for building tests for {@link org.apache.tapestry.engine.IEngineService}s.
037     * 
038     * @author Howard M. Lewis Ship
039     * @since 4.0
040     */
041    public abstract class ServiceTestCase extends BaseComponentTestCase
042    {
043    
044        protected IPage newPage(String name)
045        {
046            IPage result = newMock(IPage.class);
047            checkOrder(result, false);
048            
049            expect(result.getPageName()).andReturn(name);
050            return result;
051        }
052    
053        protected HttpServletRequest newRequest(HttpSession session)
054        {
055            HttpServletRequest result = newMock(HttpServletRequest.class);
056    
057            expect(result.getSession()).andReturn(session);
058    
059            return result;
060        }
061    
062        protected WebRequest newWebRequest(WebSession session)
063        {
064            WebRequest result = newMock(WebRequest.class);
065    
066            expect(result.getSession(false)).andReturn(session);
067    
068            return result;
069        }
070    
071        protected HttpServletRequest newRequest(boolean create, HttpSession session)
072        {
073            HttpServletRequest result = newMock(HttpServletRequest.class);
074    
075            expect(result.getSession(create)).andReturn(session);
076    
077            return result;
078        }
079    
080        protected WebRequest newWebRequest(boolean create, WebSession session)
081        {
082            WebRequest result = newMock(WebRequest.class);
083    
084            expect(result.getSession(create)).andReturn(session);
085    
086            return result;
087        }
088    
089        protected HttpSession newSession(boolean isNew)
090        {
091            HttpSession session = newSession();
092    
093            expect(session.isNew()).andReturn(isNew);
094    
095            return session;
096        }
097    
098        protected WebSession newWebSession(boolean isNew)
099        {
100            WebSession session = newWebSession();
101            checkOrder(session, false);
102            
103            expect(session.isNew()).andReturn(isNew);
104    
105            return session;
106        }
107    
108        protected HttpSession newSession()
109        {
110            return newMock(HttpSession.class);
111        }
112    
113        protected WebSession newWebSession()
114        {
115            return newMock(WebSession.class);
116        }
117    
118        protected ILink newLink()
119        {
120            return newMock(ILink.class);
121        }
122    
123        protected LinkFactory newLinkFactory(Map parameters, boolean stateful, ILink link)
124        {
125            LinkFactory lf = newMock(LinkFactory.class);
126    
127            expect(lf.constructLink(null, false, parameters, stateful)).andReturn(link);
128    
129            return lf;
130        }
131    
132        protected ResponseRenderer newResponseRenderer()
133        {
134            return newMock(ResponseRenderer.class);
135        }
136    
137        protected void trainConstructLink(LinkFactory linkFactory, IEngineService service,
138                boolean post, Map parameters, boolean stateful, ILink link)
139        {
140            expect(linkFactory.constructLink(service, post, parameters, stateful)).andReturn(link);
141        }
142    
143        protected LinkFactory newLinkFactory()
144        {
145            return newMock(LinkFactory.class);
146        }
147    
148        protected void trainGetPage(IRequestCycle cycle, IPage page)
149        {
150            expect(cycle.getPage()).andReturn(page);
151        }
152    
153        protected void trainGetNestedComponent(IPage page, String idPath, IComponent component)
154        {
155            expect(page.getNestedComponent(idPath)).andReturn(component);
156        }
157    
158        protected void trainGetPage(IRequestCycle cycle, String pageName, IPage page)
159        {
160            expect(cycle.getPage(pageName)).andReturn(page);
161        }
162    
163        protected void trainExtractListenerParameters(LinkFactory factory, IRequestCycle cycle, Object[] parameters)
164        {
165            expect(factory.extractListenerParameters(cycle)).andReturn(parameters);
166        }
167    
168        protected void trainGetAbsoluteURL(IRequestCycle cycle, String shortURL, String fullURL)
169        {
170            expect(cycle.getAbsoluteURL(shortURL)).andReturn(fullURL);
171        }
172        
173        protected Log newLog()
174        {
175            return newMock(Log.class);
176        }
177    
178    }