/** * Override this to provide your own {@link ExecutorService} * * @return An {@link ExecutorService} to use for the compiler pool. */ protected ExecutorService createThreadPool() { ThreadFactory threadFactory = new ClosureJSThreadFactory(); return Executors.newFixedThreadPool(threadPoolSize, threadFactory); }
@VisibleForTesting protected CompilerOptions getCompilerOptions(JsUri uri) { CompilerOptions options = defaultCompilerOptions(); return options; }
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); }
@Ignore("This class was not being run and when I ran it this test did not pass. Not familiar enough to enable it.") @Test public void testGetJsContentWithoutGoogSymbolExports() throws Exception { realOptionsMock = mockRealJsCompilerOptions(false); // without compiler = newClosureJsCompiler(null, realOptionsMock, compilerMock, cacheMock); FeatureBundle bundle = mockBundle(EXPORTS); Iterable<JsContent> actual = compiler.getJsContent(mockJsUri(false), bundle); assertEquals(EXPORT_COMPILER_STRING, getContent(actual)); }
@Test public void testCompileSuccessDeb() throws Exception { jsUriMock = mockJsUri(true); // debug realResultMock = mockRealJsResult(); realCompMock = mockRealJsCompiler(null, realResultMock, ACTUAL_COMPILER_OUTPUT); realOptionsMock = mockRealJsCompilerOptions(false); compiler = newClosureJsCompiler(realCompMock, realOptionsMock, compilerMock, cacheMock); JsResponse actual = compiler.compile(jsUriMock, EXPORT_COMPILER_CONTENTS, EXTERN); assertEquals(EXPORT_COMPILER_STRING, actual.toJsString()); assertTrue(actual.getErrors().isEmpty()); }
@Test public void testGetJsContentWithOpt() throws Exception { JsUri jsUri = mockJsUri(false); // optimized FeatureResource extRes = mockResource(true, RESOURCE_URL_DEB, RESOURCE_URL_OPT); FeatureResource intRes = mockResource(false, RESOURCE_CONTENT_DEB, RESOURCE_CONTENT_OPT); FeatureBundle bundle = mockBundle(Lists.newArrayList(extRes, intRes)); Iterable<JsContent> actual = compiler.getJsContent(jsUri, bundle); assertEquals( "document.write('<script src=\"" + RESOURCE_URL_OPT + "\"></script>');\n" + RESOURCE_CONTENT_OPT + ";\n", getContent(actual)); }
@Before public void setUp() throws Exception { cacheMock = new MockProvider(); exportResponseMock = mockJsResponse(EXPORT_COMPILER_STRING); compilerMock = mockDefaultJsCompiler(exportResponseMock, EXPORT_COMPILER_CONTENTS); executorServiceMock = EasyMock.createMock(ExecutorService.class); EasyMock.makeThreadSafe(executorServiceMock, true); }
@SuppressWarnings("unchecked") private DefaultJsCompiler mockDefaultJsCompiler(JsResponse res, Iterable<JsContent> content) { DefaultJsCompiler result = createMock(DefaultJsCompiler.class); expect(result.getJsContent(isA(JsUri.class), isA(FeatureBundle.class))) .andReturn(content).anyTimes(); expect(result.compile(isA(JsUri.class), isA(Iterable.class), isA(String.class))) .andReturn(res).anyTimes(); replay(result); return result; }
@Before public void setUp() throws Exception { compiler = new DefaultJsCompiler(); }
@Ignore("This class was not being run and when I ran it this test did not pass. Not familiar enough to enable it.") @Test public void testGetJsContentWithoutGoogSymbolExports() throws Exception { realOptionsMock = mockRealJsCompilerOptions(false); // without compiler = newClosureJsCompiler(null, realOptionsMock, compilerMock, cacheMock); FeatureBundle bundle = mockBundle(EXPORTS); Iterable<JsContent> actual = compiler.getJsContent(mockJsUri(false), bundle); assertEquals(EXPORT_COMPILER_STRING, getContent(actual)); }
@Test public void testCompileSuccessOpt() throws Exception { jsUriMock = mockJsUri(false); // opt realResultMock = mockRealJsResult(); realCompMock = mockRealJsCompiler(null, realResultMock, ACTUAL_COMPILER_OUTPUT); realOptionsMock = mockRealJsCompilerOptions(false); compiler = newClosureJsCompiler(realCompMock, realOptionsMock, compilerMock, cacheMock); JsResponse actual = compiler.compile(jsUriMock, EXPORT_COMPILER_CONTENTS, EXTERN); assertEquals(ACTUAL_COMPILER_OUTPUT, actual.toJsString()); assertTrue(actual.getErrors().isEmpty()); }
@Test public void testGetJsContentWithDeb() throws Exception { JsUri jsUri = mockJsUri(true); // debug FeatureResource extRes = mockResource(true, RESOURCE_URL_DEB, RESOURCE_URL_OPT); FeatureResource intRes = mockResource(false, RESOURCE_CONTENT_DEB, RESOURCE_CONTENT_OPT); FeatureBundle bundle = mockBundle(Lists.newArrayList(extRes, intRes)); Iterable<JsContent> actual = compiler.getJsContent(jsUri, bundle); assertEquals( "document.write('<script src=\"" + RESOURCE_URL_DEB + "\"></script>');\n" + RESOURCE_CONTENT_DEB + ";\n", getContent(actual)); }
@Before public void setUp() throws Exception { cacheMock = new MockProvider(); exportResponseMock = mockJsResponse(EXPORT_COMPILER_STRING); compilerMock = mockDefaultJsCompiler(exportResponseMock, EXPORT_COMPILER_CONTENTS); executorServiceMock = EasyMock.createMock(ExecutorService.class); EasyMock.makeThreadSafe(executorServiceMock, true); }
@Before public void setUp() throws Exception { compiler = new DefaultJsCompiler(); }
@Ignore("This class was not being run and when I ran it this test did not pass. Not familiar enough to enable it.") @Test public void testGetJsContentWithGoogSymbolExports() throws Exception { realOptionsMock = mockRealJsCompilerOptions(true); // with compiler = newClosureJsCompiler(null, realOptionsMock, compilerMock, cacheMock); FeatureBundle bundle = mockBundle(EXPORTS); Iterable<JsContent> actual = compiler.getJsContent(mockJsUri(false), bundle); assertEquals(EXPORT_COMPILER_STRING + "goog.exportSymbol('bar', bar);\n" + "goog.exportSymbol('foo', foo);\n", getContent(actual)); }
@Test public void testCompileSuccessOpt() throws Exception { jsUriMock = mockJsUri(false); // opt realResultMock = mockRealJsResult(); realCompMock = mockRealJsCompiler(null, realResultMock, ACTUAL_COMPILER_OUTPUT); realOptionsMock = mockRealJsCompilerOptions(false); compiler = newClosureJsCompiler(realCompMock, realOptionsMock, compilerMock, cacheMock); JsResponse actual = compiler.compile(jsUriMock, EXPORT_COMPILER_CONTENTS, EXTERN); assertEquals(ACTUAL_COMPILER_OUTPUT, actual.toJsString()); assertTrue(actual.getErrors().isEmpty()); }
@Test public void testGetJsContentWithOpt() throws Exception { JsUri jsUri = mockJsUri(false); // optimized FeatureResource extRes = mockResource(true, RESOURCE_URL_DEB, RESOURCE_URL_OPT); FeatureResource intRes = mockResource(false, RESOURCE_CONTENT_DEB, RESOURCE_CONTENT_OPT); FeatureBundle bundle = mockBundle(Lists.newArrayList(extRes, intRes)); Iterable<JsContent> actual = compiler.getJsContent(jsUri, bundle); assertEquals( "document.write('<script src=\"" + RESOURCE_URL_OPT + "\"></script>');\n" + RESOURCE_CONTENT_OPT + ";\n", getContent(actual)); }
@Ignore("This class was not being run and when I ran it this test did not pass. Not familiar enough to enable it.") @Test public void testGetJsContentWithGoogSymbolExports() throws Exception { realOptionsMock = mockRealJsCompilerOptions(true); // with compiler = newClosureJsCompiler(null, realOptionsMock, compilerMock, cacheMock); FeatureBundle bundle = mockBundle(EXPORTS); Iterable<JsContent> actual = compiler.getJsContent(mockJsUri(false), bundle); assertEquals(EXPORT_COMPILER_STRING + "goog.exportSymbol('bar', bar);\n" + "goog.exportSymbol('foo', foo);\n", getContent(actual)); }
@Test public void testCompileSuccessDeb() throws Exception { jsUriMock = mockJsUri(true); // debug realResultMock = mockRealJsResult(); realCompMock = mockRealJsCompiler(null, realResultMock, ACTUAL_COMPILER_OUTPUT); realOptionsMock = mockRealJsCompilerOptions(false); compiler = newClosureJsCompiler(realCompMock, realOptionsMock, compilerMock, cacheMock); JsResponse actual = compiler.compile(jsUriMock, EXPORT_COMPILER_CONTENTS, EXTERN); assertEquals(EXPORT_COMPILER_STRING, actual.toJsString()); assertTrue(actual.getErrors().isEmpty()); }
@Test public void testGetJsContentWithDeb() throws Exception { JsUri jsUri = mockJsUri(true); // debug FeatureResource extRes = mockResource(true, RESOURCE_URL_DEB, RESOURCE_URL_OPT); FeatureResource intRes = mockResource(false, RESOURCE_CONTENT_DEB, RESOURCE_CONTENT_OPT); FeatureBundle bundle = mockBundle(Lists.newArrayList(extRes, intRes)); Iterable<JsContent> actual = compiler.getJsContent(jsUri, bundle); assertEquals( "document.write('<script src=\"" + RESOURCE_URL_DEB + "\"></script>');\n" + RESOURCE_CONTENT_DEB + ";\n", getContent(actual)); }