01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.internal.services;
16:
17: import static org.easymock.EasyMock.anyInt;
18: import static org.easymock.EasyMock.anyLong;
19: import static org.easymock.EasyMock.eq;
20:
21: import java.io.IOException;
22:
23: import javax.servlet.ServletOutputStream;
24: import javax.servlet.http.HttpServletResponse;
25:
26: import org.apache.tapestry.internal.test.InternalBaseTestCase;
27: import org.apache.tapestry.ioc.Resource;
28: import org.apache.tapestry.ioc.internal.util.ClasspathResource;
29: import org.apache.tapestry.services.Request;
30: import org.apache.tapestry.services.RequestGlobals;
31: import org.apache.tapestry.services.Response;
32: import org.testng.annotations.Test;
33:
34: /**
35: * Tests for the {@link ResourceStreamerImpl} class.
36: */
37: public class ResourceStreamerImplTest extends InternalBaseTestCase {
38: @Test
39: public void content_type_css() throws IOException {
40: content_type("text/css", "test.css");
41: }
42:
43: @Test
44: public void content_type_js() throws IOException {
45: content_type("text/javascript", "test.js");
46: }
47:
48: @Test
49: public void content_type_gif() throws IOException {
50: content_type("image/gif", "test.gif");
51: }
52:
53: private void content_type(String contentType, String fileName)
54: throws IOException {
55: Request request = mockRequest();
56: HttpServletResponse hsr = mockHttpServletResponse();
57:
58: train_setContentLength(hsr, anyInt());
59: train_setDateHeader(hsr, eq("Last-Modified"), anyLong());
60: train_setContentType(hsr, contentType);
61: train_getOutputStream(hsr, new TestServletOutputStream());
62:
63: replay();
64:
65: Response response = new ResponseImpl(hsr);
66: ResourceStreamer streamer = getService(ResourceStreamer.class);
67: RequestGlobals globals = getService(RequestGlobals.class);
68:
69: globals.store(request, response);
70:
71: String path = getClass().getPackage().getName().replace('.',
72: '/')
73: + "/" + fileName;
74:
75: Resource resource = new ClasspathResource(path);
76:
77: streamer.streamResource(resource);
78:
79: verify();
80: }
81:
82: private static class TestServletOutputStream extends
83: ServletOutputStream {
84: @Override
85: public void write(int b) throws IOException {
86: // Empty.
87: }
88: }
89:
90: }
|