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