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.expect;
018    import static org.easymock.EasyMock.expectLastCall;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.tapestry.IRequestCycle;
022    import org.testng.annotations.Test;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    import javax.servlet.http.HttpSession;
027    
028    /**
029     * Tests for {@link org.apache.tapestry.engine.RestartService}.
030     * 
031     * @author Howard M. Lewis Ship
032     * @since 4.0
033     */
034    @Test
035    public class RestartServiceTest extends ServiceTestCase
036    {
037        public void testNoSession() throws Exception
038        {
039            HttpServletRequest request = newServletRequest();
040            HttpServletResponse response = newServletResponse();
041            IRequestCycle cycle = newCycle();
042    
043            // Training
044    
045            trainGetSession(request, false, null);
046            trainGetAbsoluteURL(cycle, "/app", "http://myserver/app");
047    
048            response.sendRedirect("http://myserver/app");
049    
050            replay();
051    
052            RestartService s = new RestartService();
053            s.setRequest(request);
054            s.setResponse(response);
055            s.setServletPath("/app");
056    
057            s.service(cycle);
058    
059            verify();
060        }
061    
062        private void trainGetSession(HttpServletRequest request, boolean create, HttpSession session)
063        {
064            expect(request.getSession(create)).andReturn(session);
065        }
066    
067        private HttpServletResponse newServletResponse()
068        {
069            return newMock(HttpServletResponse.class);
070        }
071    
072        private HttpServletRequest newServletRequest()
073        {
074            return newMock(HttpServletRequest.class);
075        }
076    
077        public void testWithSession() throws Exception
078        {
079            HttpServletRequest request = newServletRequest();
080            HttpServletResponse response = newServletResponse();
081            HttpSession session = newHttpSession();
082    
083            IRequestCycle cycle = newCycle();
084    
085            // Training
086    
087            trainGetSession(request, false, session);
088    
089            session.invalidate();
090    
091            trainGetAbsoluteURL(cycle, "/app", "http://myserver/app");
092    
093            response.sendRedirect("http://myserver/app");
094    
095            replay();
096    
097            RestartService s = new RestartService();
098            s.setRequest(request);
099            s.setResponse(response);
100            s.setServletPath("/app");
101    
102            s.service(cycle);
103    
104            verify();
105        }
106    
107        private HttpSession newHttpSession()
108        {
109            return newMock(HttpSession.class);
110        }
111    
112        public void testErrorInvalidatingSession() throws Exception
113        {
114            HttpServletRequest request = newServletRequest();
115            HttpServletResponse response = newServletResponse();
116            HttpSession session = newHttpSession();
117            Log log = newLog();
118            Throwable ex = new IllegalStateException("Bad state");
119    
120            IRequestCycle cycle = newCycle();
121    
122            // Training
123    
124            trainGetSession(request, false, session);
125    
126            session.invalidate();
127            expectLastCall().andThrow(ex);
128    
129            log.warn("Exception thrown invalidating HttpSession.", ex);
130    
131            trainGetAbsoluteURL(cycle, "/app", "http://myserver/app");
132    
133            response.sendRedirect("http://myserver/app");
134    
135            replay();
136    
137            RestartService s = new RestartService();
138            s.setRequest(request);
139            s.setResponse(response);
140            s.setServletPath("/app");
141            s.setLog(log);
142    
143            s.service(cycle);
144    
145            verify();
146        }
147    }