private JsContent newComment(FeatureBundle bundle, boolean start) { String tag = start ? "start" : "end"; return JsContent.fromFeature( "\n/* [" + tag + "] feature=" + bundle.getName() + " */\n", "[comment-marker-" + tag + ']', bundle, null); }
/** * Prepends JS to the output. */ public JsResponseBuilder prependJs(String content, String name, boolean noCompile) { return prependJs(JsContent.fromText(content, name, noCompile)); }
private boolean canAddContent(JsContent jsContent) { return jsContent.get().length() > 0; } }
@Test public void compilerIsRun() throws Exception { JsUri jsUri = control.createMock(JsUri.class); JsResponseBuilder builder = new JsResponseBuilder().setCacheTtlSecs(1234).setStatusCode(200) .appendJs("content1:", "source1") .appendJs("content2", "source2") .appendJs(JsContent.fromFeature("content3:", "source3", mockBundle("extern3"), null)) .appendJs(JsContent.fromFeature("content4:", "source4", mockBundle("extern4"), null)); JsResponse outputResponse = new JsResponseBuilder().appendJs("content3", "s3").build(); JsRequest request = control.createMock(JsRequest.class); expect(request.getJsUri()).andReturn(jsUri); expect(compiler.compile(same(jsUri), eq(builder.build().getAllJsContent()), isA(String.class))).andReturn(outputResponse); control.replay(); boolean status = processor.process(request, builder); control.verify(); assertTrue(status); JsResponse compResult = builder.build(); assertEquals(200, compResult.getStatusCode()); assertEquals(1234, compResult.getCacheTtlSecs()); assertEquals("content3", compResult.toJsString()); Iterator<JsContent> outIterator = compResult.getAllJsContent().iterator(); JsContent firstOut = outIterator.next(); assertEquals("content3", firstOut.get()); assertEquals("s3", firstOut.getSource()); assertFalse(outIterator.hasNext()); }
@Before public void setUp() throws Exception { GadgetContext ctx = new GadgetContext(); Provider<GadgetContext> contextProviderMock = Providers.of(ctx); FeatureResource resource = mockResource(EXPORT_JS_DEB); FeatureRegistry.FeatureBundle bundle = mockExportJsBundle(resource); LookupResult lookupMock = mockLookupResult(bundle); final FeatureRegistry featureRegistryMock = mockRegistry(lookupMock); FeatureRegistryProvider registryProvider = new FeatureRegistryProvider() { public FeatureRegistry get(String repository) { return featureRegistryMock; } }; textJsContent1 = JsContent.fromText(TEXT_CONTENT_1, "source1"); textJsContent2 = JsContent.fromText(TEXT_CONTENT_2, "source2"); featureJsContent1 = JsContent.fromFeature(FEATURE_CONTENT_1, "source3", mockBundle(EXPORTS_1), null); featureJsContent2 = JsContent.fromFeature(FEATURE_CONTENT_2, "source4", mockBundle(EXPORTS_2), null); featureJsContent3 = JsContent.fromFeature(FEATURE_CONTENT_3, "source5", mockBundle(EXPORTS_3), null); processor = new ExportJsProcessor(registryProvider, contextProviderMock); }
protected CompileResult doCompileContent(JsContent content, CompilerOptions options, List<SourceFile> externs) throws CompilerException { Compiler compiler = new Compiler(getErrorManager()); // We shouldn't reuse compilers // disable JS Closure Compiler internal thread compiler.disableThreads(); SourceFile source = SourceFile.fromCode(content.getSource(), content.get()); Result result = compiler.compile(externs, Lists.newArrayList(source), options); if (result.errors.length > 0) { throw new CompilerException(result.errors); } return new CompileResult(compiler, result); }
boolean compile = !code.isNoCompile() && !compileLevel.equals("none"); future = Futures.immediateFuture(new CompileResult(code.get()));
@Test public void skipsEmptyContent() throws Exception { builder.appendJs("number 1", "num1"); builder.appendJs("", "num2"); builder.appendJs("number 3", "num3"); builder.prependJs("number 4", "num4"); builder.prependJs("", "num5"); Iterator<JsContent> allJsContent = builder.build().getAllJsContent().iterator(); assertEquals("num4", allJsContent.next().getSource()); assertEquals("num1", allJsContent.next().getSource()); assertEquals("num3", allJsContent.next().getSource()); assertFalse(allJsContent.hasNext()); } }
public static JsContent fromFeature(String content, String source, FeatureBundle bundle, FeatureResource resource, boolean noCompile) { return new JsContent(content, source, bundle, resource, noCompile); }
public boolean process(JsRequest jsRequest, JsResponseBuilder builder) { ImmutableList.Builder<JsContent> jsBuilder = ImmutableList.builder(); FeatureBundle lastFeature = null; for (JsContent js : builder.build().getAllJsContent()) { FeatureBundle feature = js.getFeatureBundle(); // Entering a new feature, from none/text. if (lastFeature == null && feature != null) { jsBuilder.add(newComment(feature, true)); // Entering a new feature, from another feature. } else if (lastFeature != null && feature != null && lastFeature != feature) { jsBuilder.add(newComment(lastFeature, false)); jsBuilder.add(newComment(feature, true)); // Entering a text, from a feature } else if (lastFeature != null && feature == null) { jsBuilder.add(newComment(lastFeature, false)); } jsBuilder.add(js); lastFeature = feature; } // If there is a last feature. if (lastFeature != null) { jsBuilder.add(newComment(lastFeature, false)); } builder.clearJs().appendAllJs(jsBuilder.build()); return true; }
private boolean isCajole(JsContent js) { FeatureResource resource = js.getFeatureResource(); if (resource != null) { Map<String, String> attribs = resource.getAttribs(); if (attribs != null) { String attrib = attribs.get(UriCommon.Param.CAJOLE.getKey()); return ATTRIB_VALUE.equals(attrib); } } return false; } }
@Test public void compilerIsRun() throws Exception { JsUri jsUri = control.createMock(JsUri.class); JsResponseBuilder builder = new JsResponseBuilder().setCacheTtlSecs(1234).setStatusCode(200) .appendJs("content1:", "source1") .appendJs("content2", "source2") .appendJs(JsContent.fromFeature("content3:", "source3", mockBundle("extern3"), null)) .appendJs(JsContent.fromFeature("content4:", "source4", mockBundle("extern4"), null)); JsResponse outputResponse = new JsResponseBuilder().appendJs("content3", "s3").build(); JsRequest request = control.createMock(JsRequest.class); expect(request.getJsUri()).andReturn(jsUri); expect(compiler.compile(same(jsUri), eq(builder.build().getAllJsContent()), isA(String.class))).andReturn(outputResponse); control.replay(); boolean status = processor.process(request, builder); control.verify(); assertTrue(status); JsResponse compResult = builder.build(); assertEquals(200, compResult.getStatusCode()); assertEquals(1234, compResult.getCacheTtlSecs()); assertEquals("content3", compResult.toJsString()); Iterator<JsContent> outIterator = compResult.getAllJsContent().iterator(); JsContent firstOut = outIterator.next(); assertEquals("content3", firstOut.get()); assertEquals("s3", firstOut.getSource()); assertFalse(outIterator.hasNext()); }
@Before public void setUp() throws Exception { GadgetContext ctx = new GadgetContext(); Provider<GadgetContext> contextProviderMock = Providers.of(ctx); FeatureResource resource = mockResource(EXPORT_JS_DEB); FeatureRegistry.FeatureBundle bundle = mockExportJsBundle(resource); LookupResult lookupMock = mockLookupResult(bundle); final FeatureRegistry featureRegistryMock = mockRegistry(lookupMock); FeatureRegistryProvider registryProvider = new FeatureRegistryProvider() { public FeatureRegistry get(String repository) { return featureRegistryMock; } }; textJsContent1 = JsContent.fromText(TEXT_CONTENT_1, "source1"); textJsContent2 = JsContent.fromText(TEXT_CONTENT_2, "source2"); featureJsContent1 = JsContent.fromFeature(FEATURE_CONTENT_1, "source3", mockBundle(EXPORTS_1), null); featureJsContent2 = JsContent.fromFeature(FEATURE_CONTENT_2, "source4", mockBundle(EXPORTS_2), null); featureJsContent3 = JsContent.fromFeature(FEATURE_CONTENT_3, "source5", mockBundle(EXPORTS_3), null); processor = new ExportJsProcessor(registryProvider, contextProviderMock); }
@Test public void skipsEmptyContent() throws Exception { builder.appendJs("number 1", "num1"); builder.appendJs("", "num2"); builder.appendJs("number 3", "num3"); builder.prependJs("number 4", "num4"); builder.prependJs("", "num5"); Iterator<JsContent> allJsContent = builder.build().getAllJsContent().iterator(); assertEquals("num4", allJsContent.next().getSource()); assertEquals("num1", allJsContent.next().getSource()); assertEquals("num3", allJsContent.next().getSource()); assertFalse(allJsContent.hasNext()); } }
public static JsContent fromFeature(String content, String source, FeatureBundle bundle, FeatureResource resource) { return new JsContent(content, source, bundle, resource, false); }
public boolean process(JsRequest jsRequest, JsResponseBuilder builder) throws JsException { JsUri jsUri = jsRequest.getJsUri(); ImmutableList.Builder<JsContent> resp = ImmutableList.builder(); FeatureRegistry featureRegistry = getFeatureRegistry(jsUri); boolean needExports = false; FeatureBundle last = null; if (!jsUri.isJsload()) { for (JsContent jsc : builder.build().getAllJsContent()) { FeatureBundle current = jsc.getFeatureBundle(); if (last != null && current != last) { needExports |= appendExportJsStatements(resp, jsUri, last); } resp.add(jsc); last = current; } if (last != null) { needExports |= appendExportJsStatements(resp, jsUri, last); } } builder.clearJs(); if (needExports) { builder.appendAllJs(getSurfaceJsContents(featureRegistry, FEATURE_NAME)); } builder.appendAllJs(resp.build()); return true; }
public Iterable<JsContent> getJsContent(JsUri jsUri, FeatureBundle bundle) { List<JsContent> jsContent = Lists.newLinkedList(); for (FeatureResource resource : bundle.getResources()) { String content = getFeatureContent(jsUri, resource); content = (content != null) ? content : ""; if (resource.isExternal()) { // Support external/type=url feature serving through document.write() jsContent.add(JsContent.fromFeature("document.write('<script src=\"" + content + "\"></script>')", "[external:" + content + ']', bundle, resource)); } else { jsContent.add(JsContent.fromFeature(content, resource.getName(), bundle, resource)); } jsContent.add(JsContent.fromFeature(";\n", "[separator]", bundle, resource)); } return jsContent; }
@Test public void testMultipleFeaturesWithInBetweenTexts() throws Exception { JsContent text1 = JsContent.fromText("text1", "source1"); JsContent text2 = JsContent.fromText("text2", "source2"); JsContent text3 = JsContent.fromText("text3", "source3"); JsContent js1 = JsContent.fromFeature("content1", "source4", mockBundle("bundle1"), null); JsContent js2 = JsContent.fromFeature("content2", "source5", mockBundle("bundle2"), null); JsResponseBuilder builder = newBuilder(text1, js1, text2, js2, text3); control.replay(); boolean actualReturn = processor.process(null, builder); JsResponse actualResponse = builder.build(); control.verify(); assertTrue(actualReturn); assertEquals( "text1" + "\n/* [start] feature=bundle1 */\n" + "content1" + "\n/* [end] feature=bundle1 */\n" + "text2" + "\n/* [start] feature=bundle2 */\n" + "content2" + "\n/* [end] feature=bundle2 */\n" + "text3", actualResponse.toJsString()); }
/** * Helper to append JS to the response w/ a name. */ public JsResponseBuilder appendJs(String content, String name) { return appendJs(JsContent.fromText(content, name)); }
/** * Returns the JavaScript code to serve. */ public String toJsString() { if (codeString == null) { StringBuilder sb = new StringBuilder(); for (JsContent js : getAllJsContent()) { sb.append(js.get()); } codeString = sb.toString(); } return codeString; }