diff --git a/jhbii/result.png b/jhbii/result.png new file mode 100644 index 0000000..ad5ba95 Binary files /dev/null and b/jhbii/result.png differ diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java index 0231095..31bb651 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparisonUtil.java @@ -15,11 +15,16 @@ import java.io.InputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; +import java.util.logging.*; /** * Tools for the {@link ImageComparison} object. */ public final class ImageComparisonUtil { + /** + * The Logger object. + */ + static Logger logger = Logger.getLogger("com.github.romankh3.image.comparison.ImageComparisonUtil"); /** * Make a copy of the {@link BufferedImage} object. @@ -45,19 +50,24 @@ public static BufferedImage readImageFromResources(String path) throws ImageComp File imageFile = new File(path); if (imageFile.exists()) { try { + logger.info("Read image form User Successfully."); return ImageIO.read(imageFile); } catch (IOException e) { + logger.severe("Cannot read image from the file, path = "+ path); throw new ImageComparisonException(String.format("Cannot read image from the file, path=%s", path), e); } } else { InputStream inputStream = ImageComparisonUtil.class.getClassLoader().getResourceAsStream(path); if (inputStream != null) { try { + logger.info("Reading image file from path."); return ImageIO.read(inputStream); } catch (IOException e) { + logger.severe("Cannot read image from the file, path="+path); throw new ImageComparisonException(String.format("Cannot read image from the file, path=%s", path), e); } } else { + logger.severe("Image with path = "+ path + " not found"); throw new ImageNotFoundException(String.format("Image with path = %s not found", path)); } } @@ -75,11 +85,14 @@ public static void saveImage(File pathFile, BufferedImage image) throws ImageCom // make dir if it's not using from Gradle. boolean dirExists = dir == null || dir.isDirectory() || dir.mkdirs(); if (!dirExists) { + logger.severe("Unable to create directory " + dir); throw new ImageComparisonException("Unable to create directory " + dir); } try { + logger.info("Write the result image to the file directory"); ImageIO.write(image, "png", pathFile); } catch (IOException e) { + logger.severe("Cannot save image to path=" + pathFile.getAbsolutePath()); throw new ImageComparisonException( String.format("Cannot save image to path=%s", pathFile.getAbsolutePath()), e); } diff --git a/src/test/java/com/github/romankh3/image/comparison/LogTest.java b/src/test/java/com/github/romankh3/image/comparison/LogTest.java new file mode 100644 index 0000000..2795639 --- /dev/null +++ b/src/test/java/com/github/romankh3/image/comparison/LogTest.java @@ -0,0 +1,39 @@ +package com.github.romankh3.image.comparison; + +import com.github.romankh3.image.comparison.model.ImageComparisonResult; +import com.github.romankh3.image.comparison.model.ImageComparisonState; +import org.junit.jupiter.api.Test; + +import java.awt.image.BufferedImage; +import java.io.File; + +import static org.junit.jupiter.api.Assertions.*; + +public class LogTest { + @Test + public void logTest1(){ + //load images to be compared: + try{ + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expcted.png"); + fail(); + } + catch (Exception e){ + assertEquals(e.getMessage(), "Image with path = expcted.png not found"); + } + } + + @Test + public void logTest2(){ + //load images to be compared: + try{ + BufferedImage expectedImage = ImageComparisonUtil.readImageFromResources("expected.png"); + BufferedImage actualImage = ImageComparisonUtil.readImageFromResources("actul.jpg"); + ImageComparisonResult imageComparisonResult1 = new ImageComparison(expectedImage, actualImage).compareImages(); + fail(); + } + catch (Exception e){ + assertEquals(e.getMessage(), "Image with path = actul.jpg not found"); + } + } + +}