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 {
|
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);
|
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
Graphics2D graphics2D = resizedImage.createGraphics();
|
Graphics2D graphics2D = resizedImage.createGraphics();
|
||||||
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
|
graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
|
||||||
|
@ -8,7 +8,7 @@ import java.io.IOException;
|
|||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
public class ImageScaledInstanceExample {
|
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);
|
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_DEFAULT);
|
||||||
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
BufferedImage bufferedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
bufferedImage.getGraphics()
|
bufferedImage.getGraphics()
|
||||||
|
@ -8,11 +8,11 @@ import javax.imageio.ImageIO;
|
|||||||
import org.imgscalr.Scalr;
|
import org.imgscalr.Scalr;
|
||||||
|
|
||||||
public class ImgscalrExample {
|
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);
|
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);
|
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.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import net.coobird.thumbnailator.Thumbnails;
|
import net.coobird.thumbnailator.Thumbnails;
|
||||||
|
|
||||||
public class ThumbnailatorExample {
|
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();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
Thumbnails.of(originalImage)
|
Thumbnails.of(originalImage)
|
||||||
.size(targetWidth, targetHeight)
|
.size(targetWidth, targetHeight)
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.baeldung.image.resize.core;
|
package com.baeldung.image.resize.core;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
@ -16,90 +14,58 @@ import javax.imageio.ImageIO;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class Graphics2DExampleTest {
|
public class Graphics2DExampleTest {
|
||||||
@Test
|
|
||||||
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
BufferedImage outputImage = null;
|
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
try {
|
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertNotNull(outputImage);
|
assertNotNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage outputImage = null;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
try {
|
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertEquals(outputImage.getWidth(), targetWidth);
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
assertEquals(outputImage.getHeight(), targetHeight);
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 0;
|
int targetWidth = 0;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 0;
|
int targetHeight = 0;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
public void whenOriginalImageDoesNotExist_thenErrorIsNotThrownAndImageIsGenerated() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
BufferedImage outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
BufferedImage outputImage = null;
|
|
||||||
try {
|
|
||||||
outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
assertNotNull(outputImage);
|
||||||
assertNull(outputImage);
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.baeldung.image.resize.core;
|
package com.baeldung.image.resize.core;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
@ -16,90 +14,56 @@ import javax.imageio.ImageIO;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ImageScaledInstanceExampleTest {
|
public class ImageScaledInstanceExampleTest {
|
||||||
@Test
|
|
||||||
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
BufferedImage outputImage = null;
|
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
try {
|
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertNotNull(outputImage);
|
assertNotNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage outputImage = null;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
try {
|
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertEquals(outputImage.getWidth(), targetWidth);
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
assertEquals(outputImage.getHeight(), targetHeight);
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 0;
|
int targetWidth = 0;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 0;
|
int targetHeight = 0;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
BufferedImage outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
BufferedImage outputImage = null;
|
|
||||||
try {
|
|
||||||
outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.baeldung.image.resize.imgscalr;
|
package com.baeldung.image.resize.imgscalr;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
@ -16,90 +14,56 @@ import javax.imageio.ImageIO;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ImgscalrExampleTest {
|
public class ImgscalrExampleTest {
|
||||||
@Test
|
|
||||||
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
BufferedImage outputImage = null;
|
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
try {
|
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertNotNull(outputImage);
|
assertNotNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage outputImage = null;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
try {
|
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertEquals(outputImage.getWidth(), targetWidth);
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
assertEquals(outputImage.getHeight(), targetHeight);
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetWidthIsZero_thenImageIsCreated() throws IOException {
|
||||||
int targetWidth = 0;
|
int targetWidth = 0;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
assertNotNull(outputImage);
|
||||||
assertNull(outputImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetHeightIsZero_thenImageIsCreated() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 0;
|
int targetHeight = 0;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = ImgscalrExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
assertNotNull(outputImage);
|
||||||
assertNull(outputImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
BufferedImage outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
BufferedImage outputImage = null;
|
|
||||||
try {
|
|
||||||
outputImage = ImgscalrExample.resizeImage(null, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.baeldung.image.resize.marvin;
|
package com.baeldung.image.resize.marvin;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
@ -16,90 +14,55 @@ import javax.imageio.ImageIO;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class MarvinExampleTest {
|
public class MarvinExampleTest {
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
BufferedImage outputImage = null;
|
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
try {
|
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertNotNull(outputImage);
|
assertNotNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage outputImage = null;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
try {
|
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertEquals(outputImage.getWidth(), targetWidth);
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
assertEquals(outputImage.getHeight(), targetHeight);
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 0;
|
int targetWidth = 0;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 0;
|
int targetHeight = 0;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = MarvinExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
BufferedImage outputImage = MarvinExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
BufferedImage outputImage = null;
|
|
||||||
try {
|
|
||||||
outputImage = MarvinExample.resizeImage(null, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.baeldung.image.resize.thumbnailator;
|
package com.baeldung.image.resize.thumbnailator;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
@ -16,90 +14,55 @@ import javax.imageio.ImageIO;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ThumbnailatorExampleTest {
|
public class ThumbnailatorExampleTest {
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
BufferedImage outputImage = null;
|
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
try {
|
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertNotNull(outputImage);
|
assertNotNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Test.None.class)
|
||||||
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
|
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
BufferedImage outputImage = null;
|
|
||||||
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
|
||||||
assertNotEquals(originalImage.getWidth(), targetWidth);
|
assertNotEquals(originalImage.getWidth(), targetWidth);
|
||||||
assertNotEquals(originalImage.getHeight(), targetHeight);
|
assertNotEquals(originalImage.getHeight(), targetHeight);
|
||||||
try {
|
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertFalse(errorThrown);
|
|
||||||
assertEquals(outputImage.getWidth(), targetWidth);
|
assertEquals(outputImage.getWidth(), targetWidth);
|
||||||
assertEquals(outputImage.getHeight(), targetHeight);
|
assertEquals(outputImage.getHeight(), targetHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 0;
|
int targetWidth = 0;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 0;
|
int targetHeight = 0;
|
||||||
boolean errorThrown = false;
|
|
||||||
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 = null;
|
BufferedImage outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
||||||
try {
|
|
||||||
outputImage = ThumbnailatorExample.resizeImage(originalImage, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = Exception.class)
|
||||||
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
|
||||||
int targetWidth = 200;
|
int targetWidth = 200;
|
||||||
int targetHeight = 200;
|
int targetHeight = 200;
|
||||||
boolean errorThrown = false;
|
BufferedImage outputImage = ThumbnailatorExample.resizeImage(null, targetWidth, targetHeight);
|
||||||
BufferedImage outputImage = null;
|
|
||||||
try {
|
|
||||||
outputImage = ThumbnailatorExample.resizeImage(null, targetWidth, targetHeight);
|
|
||||||
} catch (Exception e) {
|
|
||||||
errorThrown = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(errorThrown);
|
|
||||||
assertNull(outputImage);
|
assertNull(outputImage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user