diff --git a/image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java b/image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java index 4be3158089..d52a2dac1b 100644 --- a/image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java +++ b/image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java @@ -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); diff --git a/image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java b/image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java index 44b71bb1d0..a6e252f3eb 100644 --- a/image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java +++ b/image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java @@ -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() diff --git a/image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java b/image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java index dbabbd280c..10d4217de6 100644 --- a/image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java +++ b/image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java @@ -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); } diff --git a/image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java b/image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java index f925eace96..2296c1649d 100644 --- a/image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java +++ b/image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java @@ -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) diff --git a/image-processing/src/test/java/com/baeldung/image/resize/core/Graphics2DExampleTest.java b/image-processing/src/test/java/com/baeldung/image/resize/core/Graphics2DExampleTest.java index 2b5c05bdde..7d60f5dde3 100644 --- a/image-processing/src/test/java/com/baeldung/image/resize/core/Graphics2DExampleTest.java +++ b/image-processing/src/test/java/com/baeldung/image/resize/core/Graphics2DExampleTest.java @@ -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); } } diff --git a/image-processing/src/test/java/com/baeldung/image/resize/core/ImageScaledInstanceExampleTest.java b/image-processing/src/test/java/com/baeldung/image/resize/core/ImageScaledInstanceExampleTest.java index f3ca1e05c0..bab9852bc0 100644 --- a/image-processing/src/test/java/com/baeldung/image/resize/core/ImageScaledInstanceExampleTest.java +++ b/image-processing/src/test/java/com/baeldung/image/resize/core/ImageScaledInstanceExampleTest.java @@ -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); } } diff --git a/image-processing/src/test/java/com/baeldung/image/resize/imgscalr/ImgscalrExampleTest.java b/image-processing/src/test/java/com/baeldung/image/resize/imgscalr/ImgscalrExampleTest.java index 0f36bf5429..8c6b152a2e 100644 --- a/image-processing/src/test/java/com/baeldung/image/resize/imgscalr/ImgscalrExampleTest.java +++ b/image-processing/src/test/java/com/baeldung/image/resize/imgscalr/ImgscalrExampleTest.java @@ -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); } } diff --git a/image-processing/src/test/java/com/baeldung/image/resize/marvin/MarvinExampleTest.java b/image-processing/src/test/java/com/baeldung/image/resize/marvin/MarvinExampleTest.java index 20d0431f8f..cbe7e21e8a 100644 --- a/image-processing/src/test/java/com/baeldung/image/resize/marvin/MarvinExampleTest.java +++ b/image-processing/src/test/java/com/baeldung/image/resize/marvin/MarvinExampleTest.java @@ -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); } } diff --git a/image-processing/src/test/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExampleTest.java b/image-processing/src/test/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExampleTest.java index bef7ec744d..e807af317d 100644 --- a/image-processing/src/test/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExampleTest.java +++ b/image-processing/src/test/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExampleTest.java @@ -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); } }