KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > multipart > TestMultipartDecoderFilter


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.multipart;
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.services.ServletRequestServicer;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.multipart.MultipartDecoderFilter}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestMultipartDecoderFilter extends HiveMindTestCase
34 {
35     private static class MockServicer implements ServletRequestServicer
36     {
37         HttpServletRequest JavaDoc _request;
38
39         public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
40                 throws IOException JavaDoc, ServletException JavaDoc
41         {
42             _request = request;
43         }
44     }
45
46     private HttpServletRequest JavaDoc newRequest(String JavaDoc contentType)
47     {
48         MockControl control = newControl(HttpServletRequest JavaDoc.class);
49         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
50
51         request.getContentType();
52         control.setReturnValue(contentType);
53
54         return request;
55     }
56
57     private HttpServletResponse JavaDoc newResponse()
58     {
59         return (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
60     }
61
62     public void testNormalRequest() throws Exception JavaDoc
63     {
64         HttpServletRequest JavaDoc request = newRequest("application/x-www-form-urlencoded");
65         HttpServletResponse JavaDoc response = newResponse();
66
67         MockServicer servicer = new MockServicer();
68
69         replayControls();
70
71         MultipartDecoderFilter f = new MultipartDecoderFilter();
72
73         f.service(request, response, servicer);
74
75         assertSame(request, servicer._request);
76
77         verifyControls();
78     }
79
80     public void testUploadRequest() throws Exception JavaDoc
81     {
82         HttpServletRequest JavaDoc request = newRequest("multipart/form-data");
83         HttpServletResponse JavaDoc response = newResponse();
84         HttpServletRequest JavaDoc decoded = (HttpServletRequest JavaDoc) newMock(HttpServletRequest JavaDoc.class);
85
86         MockControl control = newControl(MultipartDecoder.class);
87         MultipartDecoder decoder = (MultipartDecoder) control.getMock();
88
89         decoder.decode(request);
90         control.setReturnValue(decoded);
91
92         decoder.cleanup();
93
94         MockServicer servicer = new MockServicer();
95
96         replayControls();
97
98         MultipartDecoderFilter f = new MultipartDecoderFilter();
99         f.setDecoder(decoder);
100
101         f.service(request, response, servicer);
102
103         assertSame(decoded, servicer._request);
104
105         verifyControls();
106     }
107 }
Popular Tags