URLTemplateSource extends AbstractTemplateSource { @Override public long lastModified() { synchronized (this) { if (lastModified == null) { lastModified = lastModified(resource); } return lastModified; } } URLTemplateSource(final String filename, final URL resource); @Override String content(final Charset charset); @Override String filename(); @Override long lastModified();  }
@Test public void openConnectionThrowIOException() throws IOException { URL url = PowerMock.createMock(URL.class); expect(url.openConnection()).andThrow(new IOException()); String filename = "home.hbs"; Object[] mocks = {url}; PowerMock.replay(mocks); assertEquals(-1, new URLTemplateSource(filename, url).lastModified()); PowerMock.verify(mocks); }
@Test public void closeOpenedConnection() throws IOException { InputStream is = PowerMock.createMock(InputStream.class); is.close(); URLConnection uc = PowerMock.createMock(URLConnection.class); expect(uc.getLastModified()).andReturn(123L); expect(uc.getInputStream()).andReturn(is); URL url = PowerMock.createMock(URL.class); expect(url.openConnection()).andReturn(uc); String filename = "home.hbs"; Object[] mocks = {url, uc, is}; PowerMock.replay(mocks); assertEquals(123L, new URLTemplateSource(filename, url).lastModified()); PowerMock.verify(mocks); }
@Test public void lastModifiedFromJar() throws IOException { String jarFilename = "app.jar"; URL jarUrl = PowerMock.createMock(URL.class); expect(jarUrl.getProtocol()).andReturn("file"); expect(jarUrl.getFile()).andReturn(jarFilename); JarURLConnection juc = PowerMock.createMock(JarURLConnection.class); expect(juc.getJarFileURL()).andReturn(jarUrl); URL url = PowerMock.createMock(URL.class); expect(url.openConnection()).andReturn(juc); String filename = "home.hbs"; Object[] mocks = {url, juc, jarUrl}; PowerMock.replay(mocks); assertEquals(0, new URLTemplateSource(filename, url).lastModified()); PowerMock.verify(mocks); }