1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-07 02:40:52 +00:00
plantuml/test/test/utils/ImageTestUtils.java
The-Lum e11d974ce5 refactor: place net.sourceforge.plantuml.test on test.utils
Create `test` folder with:
- `example`
- `utils`

And put all `net.sourceforge.plantuml.test` on `test.utils`.
_[no other change]_
2024-02-12 19:13:02 +00:00

70 lines
2.4 KiB
Java

package test.utils;
import static org.assertj.core.api.Assertions.assertThat;
import java.awt.image.BufferedImage;
import java.util.Comparator;
import org.opentest4j.AssertionFailedError;
import net.sourceforge.plantuml.klimt.color.ColorHSB;
// Beware there is also https://github.com/assertj/assertj-swing which has some image comparisons that might help us.
// It does not compare using HSB, so we have built that ourselves.
public class ImageTestUtils {
public static void assertImagesEqual(BufferedImage expected, BufferedImage actual) {
assertImagesEqual(expected, actual, new Comparator<ColorHSB>() {
@Override
public int compare(ColorHSB expected, ColorHSB actual) {
return expected.getRGB() - actual.getRGB();
}
});
}
/**
* Compares images using {@link ColorHSB}.
*/
public static void assertImagesEqual(BufferedImage expected, BufferedImage actual, Comparator<ColorHSB> comparator) {
assertImageSizeEqual(expected, actual);
final int height = expected.getHeight();
final int width = expected.getWidth();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final ColorHSB expectedColor = new ColorHSB(expected.getRGB(x, y));
final ColorHSB actualColor = new ColorHSB(actual.getRGB(x, y));
if (comparator.compare(expectedColor, actualColor) != 0) {
String expectedString = expectedColor.toString();
String actualString = String.format("%s at:<[%d, %d]>", actualColor, x, y);
throw new AssertionFailedError(
String.format("expected:%s but was:%s", expectedString, actualString),
expectedString, actualString
);
}
}
}
}
public static void assertImageSizeEqual(BufferedImage expected, BufferedImage actual) {
assertThat(expected).isNotNull();
assertThat(actual).isNotNull();
final int expectedHeight = expected.getHeight();
final int expectedWidth = expected.getWidth();
final int actualHeight = actual.getHeight();
final int actualWidth = actual.getWidth();
if (expectedHeight != actualHeight || expectedWidth != actualWidth) {
String expectedString = String.format("[width=%d height=%d]", expectedWidth, expectedHeight);
String actualString = String.format("[width=%d height=%d]", actualWidth, actualHeight);
throw new AssertionFailedError(
String.format("expected:%s but was:%s", expectedString, actualString),
expectedString, actualString
);
}
}
}