PersistedScreenshotImage extends ScreenshotImage { @Override public InputStream getAsStream() { try { return getImageStream(); } catch (ResourceUnavailableException e) { LOG.debug("getAsStream from resource failed."); } if (image != null) { return getInputStreamFromImage(image); } throw new ResourceUnavailableException("Screenshot not found"); } PersistedScreenshotImage(Persister persister, PersistMetadata metadata);  PersistedScreenshotImage(Persister persister, PersistMetadata metadata, BufferedImage image); PersistMetadata getMetadata(); @Override BufferedImage get(); @Override InputStream getAsStream(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }
@Test public void testGetAsStream_image_not_exist() throws Exception { ScreenshotImage image = new PersistedScreenshotImage(persister, metadata); expectedException.expect(ResourceUnavailableException.class); image.getAsStream(); }
@Test public void testGetAsStream() throws Exception { BufferedImage expected = getImage(); ScreenshotImage image = new PersistedScreenshotImage(persister, metadata, expected); InputStream in = null; try { in = image.getAsStream(); BufferedImage actual = ImageIO.read(in); assertThat(ImageUtils.imageEquals(expected, actual), is(true)); } finally { if (in != null) { in.close(); } } assertThat(image.isImageCached(), is(true)); }