KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpSession JavaDoc;
21
22 import org.apache.hivemind.test.HiveMindTestCase;
23 import org.apache.tapestry.IPage;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.services.LinkFactory;
26 import org.apache.tapestry.services.ResponseRenderer;
27 import org.apache.tapestry.web.WebRequest;
28 import org.apache.tapestry.web.WebSession;
29 import org.easymock.MockControl;
30
31 /**
32  * Common utilities for building tests for {@link org.apache.tapestry.engine.IEngineService}s.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public abstract class ServiceTestCase extends HiveMindTestCase
38 {
39     protected IRequestCycle newRequestCycle()
40     {
41         return (IRequestCycle) newMock(IRequestCycle.class);
42     }
43
44     protected IPage newPage(String JavaDoc name)
45     {
46         MockControl control = newControl(IPage.class);
47         IPage result = (IPage) control.getMock();
48
49         result.getPageName();
50         control.setReturnValue(name);
51
52         return result;
53     }
54
55     protected HttpServletRequest JavaDoc newRequest(HttpSession JavaDoc session)
56     {
57         MockControl control = newControl(HttpServletRequest JavaDoc.class);
58         HttpServletRequest JavaDoc result = (HttpServletRequest JavaDoc) control.getMock();
59
60         result.getSession();
61         control.setReturnValue(session);
62
63         return result;
64     }
65     
66     protected WebRequest newWebRequest(WebSession session)
67     {
68         MockControl control = newControl(WebRequest.class);
69         WebRequest result = (WebRequest) control.getMock();
70
71         result.getSession(false);
72         control.setReturnValue(session);
73
74         return result;
75     }
76
77     protected HttpServletRequest JavaDoc newRequest(boolean create, HttpSession JavaDoc session)
78     {
79         MockControl control = newControl(HttpServletRequest JavaDoc.class);
80         HttpServletRequest JavaDoc result = (HttpServletRequest JavaDoc) control.getMock();
81
82         result.getSession(create);
83         control.setReturnValue(session);
84
85         return result;
86     }
87
88     protected WebRequest newWebRequest(boolean create, WebSession session)
89     {
90         MockControl control = newControl(WebRequest.class);
91         WebRequest result = (WebRequest) control.getMock();
92
93         result.getSession(create);
94         control.setReturnValue(session);
95
96         return result;
97     }
98
99     protected HttpSession JavaDoc newSession(boolean isNew)
100     {
101         MockControl control = newControl(HttpSession JavaDoc.class);
102         HttpSession JavaDoc session = (HttpSession JavaDoc) control.getMock();
103
104         session.isNew();
105         control.setReturnValue(isNew);
106
107         return session;
108     }
109
110     protected WebSession newWebSession(boolean isNew)
111     {
112         MockControl control = newControl(WebSession.class);
113         WebSession session = (WebSession) control.getMock();
114
115         session.isNew();
116         control.setReturnValue(isNew);
117
118         return session;
119     }
120
121     
122     protected HttpSession JavaDoc newSession()
123     {
124         return (HttpSession JavaDoc) newMock(HttpSession JavaDoc.class);
125     }
126
127     protected WebSession newWebSession()
128     {
129         return (WebSession) newMock(WebSession.class);
130     }
131
132     protected ILink newLink()
133     {
134         return (ILink) newMock(ILink.class);
135     }
136
137     protected LinkFactory newLinkFactory(IRequestCycle cycle, Map JavaDoc parameters, boolean stateful,
138             ILink link)
139     {
140         MockControl control = newControl(LinkFactory.class);
141         LinkFactory lf = (LinkFactory) control.getMock();
142
143         lf.constructLink(cycle, parameters, stateful);
144
145         control.setReturnValue(link);
146
147         return lf;
148     }
149
150     protected ResponseRenderer newResponseRenderer()
151     {
152         return (ResponseRenderer) newMock(ResponseRenderer.class);
153     }
154
155 }
Popular Tags