private GeneratorRegistry addGeneratorRegistryToConfig(JawrConfig config, String type) { GeneratorRegistry generatorRegistry = new GeneratorRegistry(type) { private static final long serialVersionUID = 1L; public boolean isHandlingCssImage(String cssResourcePath) { boolean result = false; if (cssResourcePath.startsWith("jar:")) { result = true; } return result; } }; generatorRegistry.setConfig(config); config.setGeneratorRegistry(generatorRegistry); return generatorRegistry; }
@Test public void testURLImgClasspathCssRewriting() { // Set the properties Properties props = new Properties(); props.setProperty(JawrConfig.JAWR_CSS_CLASSPATH_HANDLE_IMAGE, "true"); config = new JawrConfig("css", props); ServletContext servletContext = new MockServletContext(); config.setContext(servletContext); config.setServletMapping("/css"); config.setCharsetName("UTF-8"); addGeneratorRegistryToConfig(config, "css"); // Set up the Image servlet Jawr config props = new Properties(); JawrConfig imgServletJawrConfig = new JawrConfig(JawrConstant.BINARY_TYPE, props); GeneratorRegistry generatorRegistry = addGeneratorRegistryToConfig(imgServletJawrConfig, JawrConstant.BINARY_TYPE); generatorRegistry.setResourceReaderHandler(rsHandler); imgServletJawrConfig.setServletMapping("/cssImg/"); BinaryResourcesHandler imgRsHandler = new BinaryResourcesHandler(imgServletJawrConfig, rsHandler, null); servletContext.setAttribute(JawrConstant.BINARY_CONTEXT_ATTRIBUTE, imgRsHandler); status = new BundleProcessingStatus( BundleProcessingStatus.BUNDLE_PROCESSING_TYPE, bundle, null, config); // Css data StringBuffer data = new StringBuffer("background-image:url(jar:style/images/logo.png);"); // Css path String filePath = "style/default/assets/someCSS.css"; // Expected: goes 3 back to the context path, then add the CSS image servlet mapping, // then go to the image path // the image is at classPath:/style/images/someImage.gif String expectedURL = "background-image:url(../../../cssImg/jar_cb3015770054/style/images/logo.png);"; status.setLastPathAdded(filePath); String result = processor.postProcessBundle(status, data).toString(); assertEquals("URL was not rewritten properly", expectedURL, result); }