1 15 package org.apache.tapestry.engine; 16 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 import javax.servlet.http.HttpSession ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.tapestry.IRequestCycle; 23 import org.apache.tapestry.junit.TapestryTestCase; 24 import org.apache.tapestry.services.AbsoluteURLBuilder; 25 import org.easymock.MockControl; 26 27 33 public class TestRestartService extends TapestryTestCase 34 { 35 public void testNoSession() throws Exception 36 { 37 MockControl requestControl = newControl(HttpServletRequest .class); 38 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 39 40 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 41 42 MockControl builderControl = newControl(AbsoluteURLBuilder.class); 43 AbsoluteURLBuilder builder = (AbsoluteURLBuilder) builderControl.getMock(); 44 45 MockControl cycleControl = newControl(IRequestCycle.class); 46 IRequestCycle cycle = (IRequestCycle) cycleControl.getMock(); 47 48 50 request.getSession(); 51 requestControl.setReturnValue(null); 52 53 cycle.getAbsoluteURL("/app"); 54 cycleControl.setReturnValue("http://myserver/app"); 55 56 builder.constructURL("http://myserver/app"); 57 builderControl.setReturnValue("http://myserver/app"); 58 59 response.sendRedirect("http://myserver/app"); 60 61 replayControls(); 62 63 RestartService s = new RestartService(); 64 s.setBuilder(builder); 65 s.setRequest(request); 66 s.setResponse(response); 67 s.setServletPath("/app"); 68 69 s.service(cycle); 70 71 verifyControls(); 72 } 73 74 public void testWithSession() throws Exception 75 { 76 MockControl requestControl = newControl(HttpServletRequest .class); 77 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 78 79 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 80 81 MockControl builderControl = newControl(AbsoluteURLBuilder.class); 82 AbsoluteURLBuilder builder = (AbsoluteURLBuilder) builderControl.getMock(); 83 84 MockControl cycleControl = newControl(IRequestCycle.class); 85 IRequestCycle cycle = (IRequestCycle) cycleControl.getMock(); 86 87 HttpSession session = (HttpSession ) newMock(HttpSession .class); 88 89 91 request.getSession(); 92 requestControl.setReturnValue(session); 93 94 session.invalidate(); 95 96 cycle.getAbsoluteURL("/tap"); 97 cycleControl.setReturnValue("http://myserver/tap"); 98 99 builder.constructURL("http://myserver/tap"); 100 builderControl.setReturnValue("http://myserver/tap"); 101 102 response.sendRedirect("http://myserver/tap"); 103 104 replayControls(); 105 106 RestartService s = new RestartService(); 107 s.setBuilder(builder); 108 s.setRequest(request); 109 s.setResponse(response); 110 s.setServletPath("/tap"); 111 112 s.service(cycle); 113 114 verifyControls(); 115 } 116 117 public void testErrorInvalidatingSession() throws Exception 118 { 119 MockControl requestControl = newControl(HttpServletRequest .class); 120 HttpServletRequest request = (HttpServletRequest ) requestControl.getMock(); 121 122 HttpServletResponse response = (HttpServletResponse ) newMock(HttpServletResponse .class); 123 124 MockControl builderControl = newControl(AbsoluteURLBuilder.class); 125 AbsoluteURLBuilder builder = (AbsoluteURLBuilder) builderControl.getMock(); 126 127 MockControl sessionControl = newControl(HttpSession .class); 128 HttpSession session = (HttpSession ) sessionControl.getMock(); 129 130 MockControl cycleControl = newControl(IRequestCycle.class); 131 IRequestCycle cycle = (IRequestCycle) cycleControl.getMock(); 132 133 Log log = (Log) newMock(Log.class); 134 135 IllegalStateException ex = new IllegalStateException (); 136 137 139 request.getSession(); 140 requestControl.setReturnValue(session); 141 142 session.invalidate(); 143 sessionControl.setThrowable(ex); 144 145 log.warn("Exception thrown invalidating HttpSession.", ex); 146 147 cycle.getAbsoluteURL("/app"); 148 cycleControl.setReturnValue("http://myserver/app"); 149 150 builder.constructURL("http://myserver/app"); 151 builderControl.setReturnValue("http://myserver/app"); 152 153 response.sendRedirect("http://myserver/app"); 154 155 replayControls(); 156 157 RestartService s = new RestartService(); 158 s.setBuilder(builder); 159 s.setRequest(request); 160 s.setResponse(response); 161 s.setLog(log); 162 s.setServletPath("/app"); 163 164 s.service(cycle); 165 166 verifyControls(); 167 } 168 169 } | Popular Tags |