KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > TestEngineServiceLink


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.junit;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.commons.codec.net.URLCodec;
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.Tapestry;
23 import org.apache.tapestry.engine.EngineServiceLink;
24 import org.apache.tapestry.services.ServiceConstants;
25 import org.apache.tapestry.web.WebRequest;
26 import org.easymock.MockControl;
27
28 /**
29  * Tests for {@link org.apache.tapestry.engine.EngineServiceLink}.
30  *
31  * @author Howard Lewis Ship
32  * @since 3.0
33  */

34 public class TestEngineServiceLink extends TapestryTestCase
35 {
36     private URLCodec _urlCodec = new URLCodec();
37
38     private static final String JavaDoc ENCODING = "utf-8";
39
40     private Map JavaDoc buildParameters(String JavaDoc serviceName, String JavaDoc[] serviceParameters)
41     {
42         Map JavaDoc result = new HashMap JavaDoc();
43
44         result.put(ServiceConstants.SERVICE, serviceName);
45         result.put(ServiceConstants.PARAMETER, serviceParameters);
46
47         return result;
48     }
49
50     /** @since 4.0 */
51     private WebRequest newRequest()
52     {
53         return (WebRequest) newMock(WebRequest.class);
54     }
55
56     /** @since 4.0 */
57     public void testGetURLWithParameters()
58     {
59         WebRequest request = newRequest();
60         MockControl control = newControl(IRequestCycle.class);
61         IRequestCycle rc = (IRequestCycle) control.getMock();
62
63         rc.encodeURL("/context/servlet?service=myservice");
64         control.setReturnValue("/context/servlet?service=myservice;encoded");
65
66         EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec,
67                 request, buildParameters("myservice", null), true);
68
69         replayControls();
70
71         assertEquals("/context/servlet?service=myservice;encoded", l.getURL());
72
73         verifyControls();
74
75         checkList("parameterNames", new String JavaDoc[]
76         { Tapestry.SERVICE_QUERY_PARAMETER_NAME, Tapestry.PARAMETERS_QUERY_PARAMETER_NAME }, l
77                 .getParameterNames());
78
79     }
80
81     public void testGetURLWithServiceParameters()
82     {
83         WebRequest request = newRequest();
84
85         IRequestCycle rc = (IRequestCycle) newMock(IRequestCycle.class);
86
87         EngineServiceLink l = new EngineServiceLink(rc, "/ctx/app", ENCODING, _urlCodec, request,
88                 buildParameters("foo", new String JavaDoc[]
89                 { "godzilla", "frodo" }), false);
90
91         replayControls();
92
93         assertEquals("/ctx/app?service=foo&sp=godzilla&sp=frodo", l.getURL());
94
95         verifyControls();
96     }
97
98     /** @since 4.0 */
99
100     public void testGetURLSansParameters()
101     {
102         WebRequest request = newRequest();
103
104         MockControl control = newControl(IRequestCycle.class);
105         IRequestCycle rc = (IRequestCycle) control.getMock();
106
107         EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec,
108                 request, buildParameters("myservice", null), true);
109
110         rc.encodeURL("/context/servlet");
111         control.setReturnValue("/context/servlet;encoded");
112
113         replayControls();
114
115         assertEquals("/context/servlet;encoded", l.getURL(null, false));
116
117         verifyControls();
118     }
119
120     /** @since 4.0 */
121
122     public void testGetURLWithAnchor()
123     {
124         WebRequest request = newRequest();
125         IRequestCycle rc = (IRequestCycle) newMock(IRequestCycle.class);
126
127         EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec,
128                 request, buildParameters("myservice", null), false);
129
130         replayControls();
131
132         assertEquals("/context/servlet#anchor", l.getURL("anchor", false));
133
134         verifyControls();
135     }
136
137     public void testGetURLWithAnchorAndParameters() throws Exception JavaDoc
138     {
139         WebRequest request = newRequest();
140         IRequestCycle rc = (IRequestCycle) newMock(IRequestCycle.class);
141
142         EngineServiceLink l = new EngineServiceLink(rc, "/context/servlet", ENCODING, _urlCodec,
143                 request, buildParameters("myservice", null), false);
144
145         replayControls();
146
147         assertEquals("/context/servlet?service=myservice#anchor", l.getURL("anchor", true));
148
149         verifyControls();
150     }
151
152     public void testGetAbsoluteURL() throws Exception JavaDoc
153     {
154         MockControl control = newControl(IRequestCycle.class);
155         IRequestCycle rc = (IRequestCycle) control.getMock();
156
157         MockControl requestc = newControl(WebRequest.class);
158         WebRequest request = (WebRequest) requestc.getMock();
159
160         EngineServiceLink l = new EngineServiceLink(rc, "/ctx/app", ENCODING, _urlCodec, request,
161                 buildParameters("myservice", null), false);
162
163         request.getScheme();
164         requestc.setReturnValue("HTTP");
165
166         request.getServerName();
167         requestc.setReturnValue("TESTSERVER.COM");
168
169         request.getServerPort();
170         requestc.setReturnValue(9187);
171
172         replayControls();
173
174         assertEquals("HTTP://TESTSERVER.COM:9187/ctx/app?service=myservice", l.getAbsoluteURL());
175
176         verifyControls();
177     }
178
179     public void testGetAbsoluteURLWithOverrides() throws Exception JavaDoc
180     {
181         MockControl control = newControl(IRequestCycle.class);
182         IRequestCycle rc = (IRequestCycle) control.getMock();
183
184         MockControl requestc = newControl(WebRequest.class);
185         WebRequest request = (WebRequest) requestc.getMock();
186
187         EngineServiceLink l = new EngineServiceLink(rc, "/ctx/app", ENCODING, _urlCodec, request,
188                 buildParameters("myservice", null), false);
189
190         replayControls();
191
192         assertEquals("https://myserver.net:9100/ctx/app?service=myservice", l.getAbsoluteURL(
193                 "https",
194                 "myserver.net",
195                 9100,
196                 null,
197                 true));
198
199         verifyControls();
200     }
201 }
Popular Tags