Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logging to the projects for issue#132 #228

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added jhbii/result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because it's a library, we need to move lower logger level for it.
For example: info -> debug

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));
}
}
Expand All @@ -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);
}
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/com/github/romankh3/image/comparison/LogTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

}