Unit test fixes and improvements
This commit is contained in:
parent
bc71da5e02
commit
ae071e7d71
@ -9,7 +9,7 @@ import javax.imageio.ImageIO;
|
||||
|
||||
public class Graphics2DExample {
|
||||
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D graphics2D = resizedImage.createGraphics();
|
||||
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
|
||||
|
@ -8,7 +8,7 @@ import java.io.IOException;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class ImageScaledInstanceExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
|
||||
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
bufferedImage.getGraphics()
|
||||
|
@ -8,11 +8,11 @@ import javax.imageio.ImageIO;
|
||||
import org.imgscalr.Scalr;
|
||||
|
||||
public class ImgscalrExample {
|
||||
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
|
||||
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) {
|
||||
return Scalr.resize(originalImage, targetWidth);
|
||||
}
|
||||
|
||||
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
|
||||
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
|
||||
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
|
||||
}
|
||||
|
||||
|
@ -4,13 +4,14 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
|
||||
public class ThumbnailatorExample {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
|
||||
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws IOException {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
Thumbnails.of(originalImage)
|
||||
.size(targetWidth, targetHeight)
|
||||
|
@ -1,9 +1,7 @@
|
||||
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;
|
||||
|
||||
@ -16,90 +14,58 @@ import javax.imageio.ImageIO;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Graphics2DExampleTest {
|
||||
@Test
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsNotThrownAndImageIsGenerated() 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;
|
||||
}
|
||||
BufferedImage outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
assertNotNull(outputImage);
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
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;
|
||||
|
||||
@ -16,90 +14,56 @@ import javax.imageio.ImageIO;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ImageScaledInstanceExampleTest {
|
||||
@Test
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
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;
|
||||
|
||||
@ -16,90 +14,56 @@ import javax.imageio.ImageIO;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ImgscalrExampleTest {
|
||||
@Test
|
||||
|
||||
@Test(expected = Test.None.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenTargetWidthIsZero_thenImageIsCreated() 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;
|
||||
}
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenTargetHeightIsZero_thenImageIsCreated() 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;
|
||||
}
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||
@Test(expected = Exception.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
boolean errorThrown = false;
|
||||
BufferedImage outputImage = null;
|
||||
try {
|
||||
outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
|
||||
} catch (Exception e) {
|
||||
errorThrown = true;
|
||||
}
|
||||
BufferedImage outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
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;
|
||||
|
||||
@ -16,90 +14,55 @@ import javax.imageio.ImageIO;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MarvinExampleTest {
|
||||
@Test
|
||||
@Test(expected = Test.None.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||
@Test(expected = Exception.class)
|
||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
|
||||
int targetWidth = 200;
|
||||
int targetHeight = 200;
|
||||
boolean errorThrown = false;
|
||||
BufferedImage outputImage = null;
|
||||
try {
|
||||
outputImage = MarvinExample.resizeImage(null, targetWidth, targetHeight);
|
||||
} catch (Exception e) {
|
||||
errorThrown = true;
|
||||
}
|
||||
BufferedImage outputImage = MarvinExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
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;
|
||||
|
||||
@ -16,90 +14,55 @@ import javax.imageio.ImageIO;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ThumbnailatorExampleTest {
|
||||
@Test
|
||||
@Test(expected = Test.None.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertNotNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
||||
@Test(expected = Test.None.class)
|
||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertFalse(errorThrown);
|
||||
assertEquals(outputImage.getWidth(), targetWidth);
|
||||
assertEquals(outputImage.getHeight(), targetHeight);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = Exception.class)
|
||||
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;
|
||||
}
|
||||
BufferedImage outputImage = ThumbnailatorExample.resizeImage(null, targetWidth, targetHeight);
|
||||
|
||||
assertTrue(errorThrown);
|
||||
assertNull(outputImage);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user