001// Licensed under the Apache License, Version 2.0 (the "License"); 002// you may not use this file except in compliance with the License. 003// You may obtain a copy of the License at 004// 005// http://www.apache.org/licenses/LICENSE-2.0 006// 007// Unless required by applicable law or agreed to in writing, software 008// distributed under the License is distributed on an "AS IS" BASIS, 009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 010// See the License for the specific language governing permissions and 011// limitations under the License. 012 013package org.apache.tapestry5.internal.test; 014 015import org.apache.tapestry5.ContentType; 016import org.apache.tapestry5.internal.services.CookieSink; 017import org.apache.tapestry5.internal.services.CookieSource; 018import org.apache.tapestry5.ioc.MappedConfiguration; 019import org.apache.tapestry5.ioc.ObjectLocator; 020import org.apache.tapestry5.ioc.OrderedConfiguration; 021import org.apache.tapestry5.ioc.ServiceBinder; 022import org.apache.tapestry5.ioc.annotations.Contribute; 023import org.apache.tapestry5.ioc.annotations.Local; 024import org.apache.tapestry5.ioc.services.ServiceOverride; 025import org.apache.tapestry5.services.MarkupRendererFilter; 026import org.apache.tapestry5.services.Request; 027import org.apache.tapestry5.services.RequestFilter; 028import org.apache.tapestry5.services.Response; 029import org.apache.tapestry5.services.ResponseCompressionAnalyzer; 030import org.apache.tapestry5.services.assets.CompressionAnalyzer; 031import org.apache.tapestry5.test.PageTester; 032 033/** 034 * Used in conjunction with {@link PageTester} to mock up and/or stub out portions of Tapestry that 035 * need to be handled differently when testing. 036 */ 037@SuppressWarnings("rawtypes") 038public class PageTesterModule 039{ 040 public static final String TEST_MODE = "test"; 041 042 public static void bind(ServiceBinder binder) 043 { 044 binder.bind(TestableRequest.class, TestableRequestImpl.class); 045 binder.bind(TestableResponse.class, TestableResponseImpl.class); 046 } 047 048 @Contribute(ServiceOverride.class) 049 public static void setupTestableOverrides(MappedConfiguration<Class, Object> configuration, @Local 050 TestableRequest request, @Local 051 TestableResponse response, final ObjectLocator locator) 052 { 053 configuration.add(Request.class, request); 054 configuration.add(Response.class, response); 055 056 TestableCookieSinkSource cookies = new TestableCookieSinkSource(); 057 058 configuration.add(CookieSink.class, cookies); 059 configuration.add(CookieSource.class, cookies); 060 061 // With the significant changes to the handling of assets in 5.4, we introduced a problem: 062 // We were checking at page render time whether to generate URLs for normal or compressed 063 // assets and that peeked at the HttpServletRequest global, which isn't set up by PageTester. 064 // What we're doing here is using a hacked version of that code to force GZip support 065 // on. 066 configuration.add(ResponseCompressionAnalyzer.class, new ResponseCompressionAnalyzer() 067 { 068 public boolean isGZipEnabled(ContentType contentType) 069 { 070 return locator.getObject(CompressionAnalyzer.class, null).isCompressable(contentType.getMimeType()); 071 } 072 073 public boolean isGZipSupported() 074 { 075 return true; 076 } 077 }); 078 } 079 080 public static void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration) 081 { 082 configuration.addInstance("EndOfRequestCleanup", EndOfRequestCleanupFilter.class, "before:StaticFiles"); 083 } 084 085 public static void contributeMarkupRenderer(OrderedConfiguration<MarkupRendererFilter> configuration) 086 { 087 configuration.addInstance("CaptureRenderedDocument", CaptureRenderedDocument.class, "before:DocumentLinker"); 088 } 089}