Skip to content

Commit 2cb44d1

Browse files
committed
Added unit tests and fixed some naming issues.
1 parent d3802bf commit 2cb44d1

10 files changed

Lines changed: 546 additions & 14 deletions

File tree

image-processing/src/main/java/com/baeldung/image/resize/core/Graphics2DExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, i
2020
public static void main(String[] args) throws IOException {
2121
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
2222
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
23-
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
23+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-graphics2d.jpg"));
2424
}
2525
}

image-processing/src/main/java/com/baeldung/image/resize/core/ImageScaledInstanceExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, i
1919
public static void main(String[] args) throws IOException {
2020
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
2121
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
22-
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
22+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-scaledinstance.jpg"));
2323
}
2424
}

image-processing/src/main/java/com/baeldung/image/resize/imagescalr/ImagescalrExample.java renamed to image-processing/src/main/java/com/baeldung/image/resize/imgscalr/ImgscalrExample.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.image.resize.imagescalr;
1+
package com.baeldung.image.resize.imgscalr;
22

33
import java.awt.image.BufferedImage;
44
import java.io.File;
@@ -7,18 +7,18 @@
77

88
import org.imgscalr.Scalr;
99

10-
public class ImagescalrExample {
11-
static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
10+
public class ImgscalrExample {
11+
public static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
1212
return Scalr.resize(originalImage, targetWidth);
1313
}
1414

15-
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
15+
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) throws Exception {
1616
return Scalr.resize(originalImage, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, targetWidth, targetHeight, Scalr.OP_ANTIALIAS);
1717
}
1818

1919
public static void main(String[] args) throws Exception {
2020
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
2121
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
22-
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
22+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-imgscalr.jpg"));
2323
}
2424
}
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
package com.baeldung.image.resize.marvin;
22

3+
import java.awt.image.BufferedImage;
4+
import java.io.File;
5+
import java.io.IOException;
6+
7+
import javax.imageio.ImageIO;
8+
39
import org.marvinproject.image.transform.scale.Scale;
410

511
import marvin.image.MarvinImage;
6-
import marvin.io.MarvinImageIO;
712

813
public class MarvinExample {
9-
static void resizeImage(String originalImagePath, int targetWidth, int targetHeight, String outputImagePath) {
10-
MarvinImage image = MarvinImageIO.loadImage(originalImagePath);
14+
static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
15+
MarvinImage image = new MarvinImage(originalImage);
1116
Scale scale = new Scale();
1217
scale.load();
1318
scale.setAttribute("newWidth", targetWidth);
1419
scale.setAttribute("newHeight", targetHeight);
1520
scale.process(image.clone(), image, null, null, false);
16-
MarvinImageIO.saveImage(image, outputImagePath);
21+
return image.getBufferedImage();
1722
}
1823

19-
public static void main(String args[]) {
20-
resizeImage("src/main/resources/images/sampleImage.jpg", 200, 200, "src/main/resources/images/sampleImage1.jpg");
24+
public static void main(String args[]) throws IOException {
25+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
26+
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
27+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-marvin.jpg"));
2128
}
2229
}

image-processing/src/main/java/com/baeldung/image/resize/thumbnailator/ThumbnailatorExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, i
2525
public static void main(String[] args) throws Exception {
2626
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
2727
BufferedImage outputImage = resizeImage(originalImage, 200, 200);
28-
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage1.jpg"));
28+
ImageIO.write(outputImage, "jpg", new File("src/main/resources/images/sampleImage-resized-thumbnailator.jpg"));
2929
}
3030
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.image.resize.core;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
9+
10+
import java.awt.image.BufferedImage;
11+
import java.io.File;
12+
import java.io.IOException;
13+
14+
import javax.imageio.ImageIO;
15+
16+
import org.junit.Test;
17+
18+
public class Graphics2DExampleTest {
19+
@Test
20+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
21+
int targetWidth = 200;
22+
int targetHeight = 200;
23+
BufferedImage outputImage = null;
24+
boolean errorThrown = false;
25+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
26+
try {
27+
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
28+
} catch (Exception e) {
29+
errorThrown = true;
30+
}
31+
32+
assertFalse(errorThrown);
33+
assertNotNull(outputImage);
34+
}
35+
36+
@Test
37+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
38+
int targetWidth = 200;
39+
int targetHeight = 200;
40+
boolean errorThrown = false;
41+
BufferedImage outputImage = null;
42+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
43+
assertNotEquals(originalImage.getWidth(), targetWidth);
44+
assertNotEquals(originalImage.getHeight(), targetHeight);
45+
try {
46+
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
47+
} catch (Exception e) {
48+
errorThrown = true;
49+
}
50+
51+
assertFalse(errorThrown);
52+
assertEquals(outputImage.getWidth(), targetWidth);
53+
assertEquals(outputImage.getHeight(), targetHeight);
54+
}
55+
56+
@Test
57+
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
58+
int targetWidth = 0;
59+
int targetHeight = 200;
60+
boolean errorThrown = false;
61+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
62+
BufferedImage outputImage = null;
63+
try {
64+
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
65+
} catch (Exception e) {
66+
errorThrown = true;
67+
}
68+
69+
assertTrue(errorThrown);
70+
assertNull(outputImage);
71+
}
72+
73+
@Test
74+
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
75+
int targetWidth = 200;
76+
int targetHeight = 0;
77+
boolean errorThrown = false;
78+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
79+
BufferedImage outputImage = null;
80+
try {
81+
outputImage = Graphics2DExample.resizeImage(originalImage, targetWidth, targetHeight);
82+
} catch (Exception e) {
83+
errorThrown = true;
84+
}
85+
86+
assertTrue(errorThrown);
87+
assertNull(outputImage);
88+
}
89+
90+
@Test
91+
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
92+
int targetWidth = 200;
93+
int targetHeight = 200;
94+
boolean errorThrown = false;
95+
BufferedImage outputImage = null;
96+
try {
97+
outputImage = Graphics2DExample.resizeImage(null, targetWidth, targetHeight);
98+
} catch (Exception e) {
99+
errorThrown = true;
100+
}
101+
102+
assertTrue(errorThrown);
103+
assertNull(outputImage);
104+
}
105+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.image.resize.core;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
9+
10+
import java.awt.image.BufferedImage;
11+
import java.io.File;
12+
import java.io.IOException;
13+
14+
import javax.imageio.ImageIO;
15+
16+
import org.junit.Test;
17+
18+
public class ImageScaledInstanceExampleTest {
19+
@Test
20+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenImageGeneratedWithoutError() throws IOException {
21+
int targetWidth = 200;
22+
int targetHeight = 200;
23+
BufferedImage outputImage = null;
24+
boolean errorThrown = false;
25+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
26+
try {
27+
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
28+
} catch (Exception e) {
29+
errorThrown = true;
30+
}
31+
32+
assertFalse(errorThrown);
33+
assertNotNull(outputImage);
34+
}
35+
36+
@Test
37+
public void whenOriginalImageExistsAndTargetSizesAreNotZero_thenOutputImageSizeIsValid() throws Exception {
38+
int targetWidth = 200;
39+
int targetHeight = 200;
40+
boolean errorThrown = false;
41+
BufferedImage outputImage = null;
42+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
43+
assertNotEquals(originalImage.getWidth(), targetWidth);
44+
assertNotEquals(originalImage.getHeight(), targetHeight);
45+
try {
46+
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
47+
} catch (Exception e) {
48+
errorThrown = true;
49+
}
50+
51+
assertFalse(errorThrown);
52+
assertEquals(outputImage.getWidth(), targetWidth);
53+
assertEquals(outputImage.getHeight(), targetHeight);
54+
}
55+
56+
@Test
57+
public void whenTargetWidthIsZero_thenErrorIsThrown() throws IOException {
58+
int targetWidth = 0;
59+
int targetHeight = 200;
60+
boolean errorThrown = false;
61+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
62+
BufferedImage outputImage = null;
63+
try {
64+
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
65+
} catch (Exception e) {
66+
errorThrown = true;
67+
}
68+
69+
assertTrue(errorThrown);
70+
assertNull(outputImage);
71+
}
72+
73+
@Test
74+
public void whenTargetHeightIsZero_thenErrorIsThrown() throws IOException {
75+
int targetWidth = 200;
76+
int targetHeight = 0;
77+
boolean errorThrown = false;
78+
BufferedImage originalImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
79+
BufferedImage outputImage = null;
80+
try {
81+
outputImage = ImageScaledInstanceExample.resizeImage(originalImage, targetWidth, targetHeight);
82+
} catch (Exception e) {
83+
errorThrown = true;
84+
}
85+
86+
assertTrue(errorThrown);
87+
assertNull(outputImage);
88+
}
89+
90+
@Test
91+
public void whenOriginalImageDoesNotExist_thenErrorIsThrown() throws IOException {
92+
int targetWidth = 200;
93+
int targetHeight = 200;
94+
boolean errorThrown = false;
95+
BufferedImage outputImage = null;
96+
try {
97+
outputImage = ImageScaledInstanceExample.resizeImage(null, targetWidth, targetHeight);
98+
} catch (Exception e) {
99+
errorThrown = true;
100+
}
101+
102+
assertTrue(errorThrown);
103+
assertNull(outputImage);
104+
}
105+
}

0 commit comments

Comments
 (0)