WebpAutoFix implements AutoFix { public void apply() { Path newFilePath = filePath.resolveSibling(MoreFiles.getNameWithoutExtension(filePath) + ".webp"); try (InputStream inputStream = new FileInputStream(new File(filePath.toString()))) { CountingInputStream countingStream = new CountingInputStream(inputStream); BufferedImage bufferedImage = WebpSuggester.safelyParseImage(countingStream); long oldSize = countingStream.getCount(); byte[] webpBytes = webpConverter.encodeLosslessWebp(bufferedImage); Files.write(newFilePath, webpBytes); Files.delete(filePath); } catch (IOException | ImageReadException e) { throw new RuntimeException(e); } } WebpAutoFix(Path filePath); @VisibleForTesting  WebpAutoFix(Path filePath, WebpConverter webpConverter); void apply(); boolean equals(Object other); int hashCode();  }
@Test public void appliesFix() throws Exception { FakeWebpConverter fakeConverter = new FakeWebpConverter(); byte[] bytes = new byte[] {0, 1, 2, 4, 8}; fakeConverter.setFakeData(bytes); File pngFile = File.createTempFile("foo", ".png"); Files.copy(getTestDataFile("webp/drawing.png"), pngFile); Path pngPath = pngFile.toPath(); new WebpAutoFix(pngPath, fakeConverter).apply(); File webpFile = new File( pngPath .resolveSibling(MoreFiles.getNameWithoutExtension(pngPath) + ".webp") .toString()); assertThat(pngFile.exists()).isFalse(); assertThat(webpFile.exists()).isTrue(); assertThat(Files.asByteSource(webpFile).read()).isEqualTo(bytes); webpFile.delete(); }