KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > engine > TestRestartService


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.engine;
16
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import javax.servlet.http.HttpSession JavaDoc;
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 /**
28  * Tests for {@link org.apache.tapestry.engine.RestartService}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestRestartService extends TapestryTestCase
34 {
35     public void testNoSession() throws Exception JavaDoc
36     {
37         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
38         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
39
40         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.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         // Training
49

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 JavaDoc
75     {
76         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
77         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
78
79         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.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 JavaDoc session = (HttpSession JavaDoc) newMock(HttpSession JavaDoc.class);
88
89         // Training
90

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 JavaDoc
118     {
119         MockControl requestControl = newControl(HttpServletRequest JavaDoc.class);
120         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) requestControl.getMock();
121
122         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
123
124         MockControl builderControl = newControl(AbsoluteURLBuilder.class);
125         AbsoluteURLBuilder builder = (AbsoluteURLBuilder) builderControl.getMock();
126
127         MockControl sessionControl = newControl(HttpSession JavaDoc.class);
128         HttpSession JavaDoc session = (HttpSession JavaDoc) 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 JavaDoc ex = new IllegalStateException JavaDoc();
136
137         // Training
138

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