Added unit tests and fixed some naming issues.
This commit is contained in:
parent
4020798958
commit
bc71da5e02
@ -20,6 +20,6 @@ public class Graphics2DExample {
|
|||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
|
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-graphics2d.jpg"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,6 @@ public class ImageScaledInstanceExample {
|
|||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
|
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-scaledinstance.jpg"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.image.resize.imagescalr;
|
package com.baeldung.image.resize.imgscalr;
|
||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -7,18 +7,18 @@ import javax.imageio.ImageIO;
|
|||||||
|
|
||||||
import org.imgscalr.Scalr;
|
import org.imgscalr.Scalr;
|
||||||
|
|
||||||
public class ImagescalrExample {
|
public class ImgscalrExample {
|
||||||
static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
|
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
|
||||||
return Scalr.resize(originalImage, targetWidth);
|
return Scalr.resize(originalImage, targetWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
|
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
|
||||||
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
|
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
|
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-imgscalr.jpg"));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,22 +1,29 @@
|
|||||||
package com.baeldung.image.resize.marvin;
|
package com.baeldung.image.resize.marvin;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import org.marvinproject.image.transform.scale.Scale;
|
import org.marvinproject.image.transform.scale.Scale;
|
||||||
|
|
||||||
import marvin.image.MarvinImage;
|
import marvin.image.MarvinImage;
|
||||||
import marvin.io.MarvinImageIO;
|
|
||||||
|
|
||||||
public class MarvinExample {
|
public class MarvinExample {
|
||||||
static void resizeImage(String originalImagePath, int targetWidth, int targetHeight, String outputImagePath) {
|
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||||
MarvinImage image = MarvinImageIO.loadImage(originalImagePath);
|
MarvinImage image = new MarvinImage(originalImage);
|
||||||
Scale scale = new Scale();
|
Scale scale = new Scale();
|
||||||
scale.load();
|
scale.load();
|
||||||
scale.setAttribute("newWidth", targetWidth);
|
scale.setAttribute("newWidth", targetWidth);
|
||||||
scale.setAttribute("newHeight", targetHeight);
|
scale.setAttribute("newHeight", targetHeight);
|
||||||
scale.process(image.clone(), image, null, null, false);
|
scale.process(image.clone(), image, null, null, false);
|
||||||
MarvinImageIO.saveImage(image, outputImagePath);
|
return image.getBufferedImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String args[]) {
|
public static void main(String args[]) throws IOException {
|
||||||
resizeImage("src/main/resources/images/sampleImage.jpg", 200, 200, "src/main/resources/images/sampleImage1.jpg");
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||||
|
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-marvin.jpg"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,6 @@ public class ThumbnailatorExample {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
|
||||||
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
|
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-thumbnailator.jpg"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,105 @@
|
|||||||
|
package com.baeldung.image.resize.core;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Graphics2DExampleTest {
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
try {
|
||||||
|
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertNotNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
|
try {
|
||||||
|
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 0;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 0;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
package com.baeldung.image.resize.core;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ImageScaledInstanceExampleTest {
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
try {
|
||||||
|
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertNotNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
|
try {
|
||||||
|
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 0;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 0;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
package com.baeldung.image.resize.imgscalr;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ImgscalrExampleTest {
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
try {
|
||||||
|
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertNotNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
|
try {
|
||||||
|
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 0;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 0;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
package com.baeldung.image.resize.marvin;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MarvinExampleTest {
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
try {
|
||||||
|
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertNotNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
|
try {
|
||||||
|
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 0;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 0;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = MarvinExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
package com.baeldung.image.resize.thumbnailator;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ThumbnailatorExampleTest {
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
try {
|
||||||
|
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertNotNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
|
try {
|
||||||
|
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(errorThrown);
|
||||||
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 0;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 0;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||||
|
int targetWidth = 200;
|
||||||
|
int targetHeight = 200;
|
||||||
|
boolean errorThrown = false;
|
||||||
|
BufferedImage outputImage = null;
|
||||||
|
try {
|
||||||
|
outputImage = ThumbnailatorExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorThrown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(errorThrown);
|
||||||
|
assertNull(outputImage);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user