1 | /** |
2 | * Copyright 2008 ATG DUST Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * |
7 | * You may obtain a copy of the License at 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 limitations under the License. |
13 | */ |
14 | |
15 | package atg.servlet; |
16 | |
17 | import java.nio.ByteBuffer; |
18 | import java.util.Map; |
19 | |
20 | |
21 | |
22 | /** |
23 | * Utility methods for setting up Servlet based tests. |
24 | * |
25 | * @author Adam Belmont |
26 | * |
27 | */ |
28 | public class ServletTestUtils { |
29 | |
30 | public static final String CLASS_VERSION = "$Id:$"; |
31 | |
32 | // ----------------------------- |
33 | /** |
34 | * Creates a new DynamoHtttpServletRequest object that can be used in a unit |
35 | * test. The request is setup with an InputStream, OutputStream and the given |
36 | * set of request parameters. |
37 | * |
38 | * @param pParmeters |
39 | * A set of request parameters that this request should initially be |
40 | * populated with |
41 | * @param pBufferSize |
42 | * The size in bytes of the backing buffer holding stream data for |
43 | * this request |
44 | * @param pMethod |
45 | * The HTTP method for this request. For example GET,POST,PUT |
46 | */ |
47 | public TestingDynamoHttpServletRequest createDynamoHttpServletRequest( |
48 | Map<String, String> pParameters, int pBufferSize, String pMethod) { |
49 | GenericHttpServletRequest greq = new GenericHttpServletRequest(); |
50 | DynamoHttpServletRequest request = new DynamoHttpServletRequest(); |
51 | request.setRequest(greq); |
52 | ByteBuffer buffer = ByteBuffer.allocate(pBufferSize); |
53 | request.setMethod(pMethod); |
54 | setParametersFromMap(request,pParameters); |
55 | return new TestingDynamoHttpServletRequest(request, buffer); |
56 | } |
57 | |
58 | // ----------------------------- |
59 | /** |
60 | * Converts the keys and values from the given map into request |
61 | * parameters on the given request object |
62 | * @param pRequest |
63 | * @param pParameters |
64 | */ |
65 | private void setParametersFromMap(DynamoHttpServletRequest pRequest, |
66 | Map<String, String> pParameters) { |
67 | for (String s : pParameters.keySet()) { |
68 | pRequest.setParameter(s, pParameters.get(s)); |
69 | } |
70 | } |
71 | // ----------------------------- |
72 | /** |
73 | * Creates a new DynamoHtttpServletResponse object that can be used in a unit |
74 | * test. |
75 | * @deprecated Use the version that takes a TestingDynamoHttpServletRequest parameter |
76 | */ |
77 | public DynamoHttpServletResponse createDynamoHttpServletResponse() { |
78 | return createDynamoHttpServletResponse(null); |
79 | } |
80 | |
81 | // ----------------------------- |
82 | /** |
83 | * Creates a new DynamoHtttpServletResponse object that can be used in a unit |
84 | * test. |
85 | * |
86 | */ |
87 | public TestingDynamoHttpServletResponse createDynamoHttpServletResponse(TestingDynamoHttpServletRequest pRequest) { |
88 | DynamoHttpServletResponse response = new DynamoHttpServletResponse(); |
89 | GenericHttpServletResponse gresponse = new GenericHttpServletResponse(); |
90 | ByteArrayServletOutputStream out = new ByteArrayServletOutputStream(); |
91 | response.setRequest(pRequest); |
92 | response.setResponse(gresponse); |
93 | response.setOutputStream(out); |
94 | gresponse.setOutputStream(out); |
95 | return new TestingDynamoHttpServletResponse(response); |
96 | } |
97 | } |
98 | |