GuavaTemplateCache implements TemplateCache { @Override public Template get(final TemplateSource source, final Parser parser) throws IOException { requireNonNull(source, "The source is required."); requireNonNull(parser, "The parser is required."); try { return cache.get(key(source), () -> parser.parse(source)); } catch (ExecutionException ex) { throw launderThrowable(source, ex.getCause()); } } GuavaTemplateCache(final Cache<TemplateSource, Template> cache); @Override void clear(); @Override void evict(final TemplateSource source); @Override Template get(final TemplateSource source, final Parser parser); @Override GuavaTemplateCache setReload(final boolean reload);  }
@SuppressWarnings("unchecked") @Test(expected = IllegalStateException.class) public void executionExceptionWithRuntimeException() throws IOException, ExecutionException { TemplateSource source = createMock(TemplateSource.class); Parser parser = createMock(Parser.class); Cache<TemplateSource, Template> cache = createMock(Cache.class); expect(cache.get(eq(source), isA(Callable.class))).andThrow( new ExecutionException(new IllegalStateException())); replay(cache, source, parser); new GuavaTemplateCache(cache).get(source, parser); verify(cache, source, parser); }
@SuppressWarnings("unchecked") @Test(expected = Error.class) public void executionExceptionWithError() throws IOException, ExecutionException { TemplateSource source = createMock(TemplateSource.class); Parser parser = createMock(Parser.class); Cache<TemplateSource, Template> cache = createMock(Cache.class); expect(cache.get(eq(source), isA(Callable.class))).andThrow( new ExecutionException(new Error())); replay(cache, source, parser); new GuavaTemplateCache(cache).get(source, parser); verify(cache, source, parser); }
@SuppressWarnings("unchecked") @Test(expected = HandlebarsException.class) public void executionExceptionWithCheckedException() throws IOException, ExecutionException { TemplateSource source = createMock(TemplateSource.class); Parser parser = createMock(Parser.class); Cache<TemplateSource, Template> cache = createMock(Cache.class); expect(cache.get(eq(source), isA(Callable.class))).andThrow( new ExecutionException(new IOException())); replay(cache, source, parser); new GuavaTemplateCache(cache).get(source, parser); verify(cache, source, parser); }
@Test public void get() throws IOException { Cache<TemplateSource, Template> cache = CacheBuilder.newBuilder() .build(); TemplateSource source = new URLTemplateSource("/template.hbs", getClass().getResource( "/template.hbs")); Template template = createMock(Template.class); Parser parser = createMock(Parser.class); expect(parser.parse(source)).andReturn(template); replay(parser, template); assertEquals(template, new GuavaTemplateCache(cache).get(source, parser)); assertEquals(template, new GuavaTemplateCache(cache).get(source, parser)); verify(parser, template); }