AddFileUriCommentProcessor implements ResourcePreProcessor, ImportAware { @Override public void process(Resource resource, Reader reader, Writer writer) throws IOException { String uri = resource.getUri(); if (uri.startsWith(ClosureDependencyUriLocator.URI_PREFIX)) { IOUtils.copy(reader, writer); } else { if (resource.getUri().length() > MAX_FILE_LENGTH) { uri = "..." + uri.substring(uri.length() - MAX_FILE_LENGTH); } switch (resource.getType()) { case JS: addJavascriptComment(reader, writer, uri); break; case CSS: addCssBlock(reader, writer, uri); break; default: break; } } }  @Override void process(Resource resource, Reader reader, Writer writer); void addJavascriptComment(Reader reader, Writer writer, String uri); @Override boolean isImportAware(); static final String ALIAS; }
@Test public void testProcessJavascript() throws Exception { final String expectedTextWithWhitespace = "\nt;\n"; Reader reader = new StringReader("goog.provide('id');goog.require('id2'); console.log('hi');" + expectedTextWithWhitespace); StringWriter writer = new StringWriter(); final AddFileUriCommentProcessor processor = new AddFileUriCommentProcessor(); Resource resource = new Resource(); resource.setMinimize(true); resource.setType(ResourceType.JS); resource.setUri("file: processor.process(resource, reader, writer); assertTrue(writer.toString().contains(format(AddFileUriCommentProcessor.START_JS_COMMENT, resource.getUri()))); assertTrue(writer.toString().contains(format(AddFileUriCommentProcessor.END_JS_COMMENT, resource.getUri()))); assertTrue(writer.toString().contains(expectedTextWithWhitespace)); }
@Test public void testProcessCss() throws Exception { String expectedTextWithWhitespace = "\nt {} \n"; Reader reader = new StringReader("a {background-color: RED}" + expectedTextWithWhitespace); StringWriter writer = new StringWriter(); final AddFileUriCommentProcessor processor = new AddFileUriCommentProcessor(); Resource resource = new Resource(); resource.setMinimize(true); resource.setType(ResourceType.CSS); resource.setUri("file: processor.process(resource, reader, writer); assertTrue(writer.toString().contains(format(START_CSS_BLOCK, resource.getUri()))); assertTrue(writer.toString().contains(format(END_CSS_BLOCK, resource.getUri()))); assertTrue(writer.toString().contains(expectedTextWithWhitespace)); }