KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > request > TestDecodedRequestInjector


1 // Copyright 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.request;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.apache.tapestry.Tapestry;
25 import org.apache.tapestry.services.ServletRequestServicer;
26 import org.apache.tapestry.spec.ILibrarySpecification;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.request.DecodedRequestInjector}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class TestDecodedRequestInjector extends HiveMindTestCase
36 {
37     private static class ServicerFixture implements ServletRequestServicer
38     {
39         HttpServletRequest JavaDoc _request;
40
41         public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
42                 throws IOException JavaDoc, ServletException JavaDoc
43         {
44             _request = request;
45         }
46     }
47
48     private HttpServletRequest JavaDoc newRequest()
49     {
50         return (HttpServletRequest JavaDoc) newMock(HttpServletRequest JavaDoc.class);
51     }
52
53     private HttpServletResponse JavaDoc newResponse()
54     {
55         return (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
56     }
57
58     private ILibrarySpecification newSpec(boolean exists, IRequestDecoder decoder)
59     {
60         MockControl control = newControl(ILibrarySpecification.class);
61         ILibrarySpecification spec = (ILibrarySpecification) control.getMock();
62
63         spec.checkExtension(Tapestry.REQUEST_DECODER_EXTENSION_NAME);
64         control.setReturnValue(exists);
65
66         if (exists)
67         {
68             spec.getExtension(Tapestry.REQUEST_DECODER_EXTENSION_NAME, IRequestDecoder.class);
69             control.setReturnValue(decoder);
70         }
71
72         return spec;
73     }
74
75     public void testNoExtension() throws Exception JavaDoc
76     {
77         HttpServletRequest JavaDoc request = newRequest();
78         HttpServletResponse JavaDoc response = newResponse();
79         ILibrarySpecification spec = newSpec(false, null);
80
81         ServletRequestServicer servicer = (ServletRequestServicer) newMock(ServletRequestServicer.class);
82
83         servicer.service(request, response);
84
85         replayControls();
86
87         DecodedRequestInjector dri = new DecodedRequestInjector();
88
89         dri.setApplicationSpecification(spec);
90         dri.initializeService();
91
92         dri.service(request, response, servicer);
93
94         verifyControls();
95     }
96
97     public void testWithExtension() throws Exception JavaDoc
98     {
99         HttpServletRequest JavaDoc request = newRequest();
100         HttpServletResponse JavaDoc response = newResponse();
101
102         MockControl decoderc = newControl(IRequestDecoder.class);
103         IRequestDecoder decoder = (IRequestDecoder) decoderc.getMock();
104         ILibrarySpecification spec = newSpec(true, decoder);
105
106         ServicerFixture servicer = new ServicerFixture();
107
108         DecodedRequest decoded = new DecodedRequest();
109         decoded.setRequestURI("/foo/bar/baz");
110
111         decoder.decodeRequest(request);
112         decoderc.setReturnValue(decoded);
113
114         replayControls();
115
116         DecodedRequestInjector dri = new DecodedRequestInjector();
117
118         dri.setApplicationSpecification(spec);
119         dri.initializeService();
120
121         dri.service(request, response, servicer);
122
123         // Prove that the request passed down the pipeline is a wrapper
124

125         assertEquals("/foo/bar/baz", servicer._request.getRequestURI());
126
127         verifyControls();
128     }
129 }
Popular Tags