Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests on EncoderUtils to increase overall code coverage #378

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/main/java/org/jfree/chart/encoders/EncoderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* ----------------
Expand Down Expand Up @@ -78,7 +78,7 @@ public static byte[] encode(BufferedImage image, String format)
*/
public static byte[] encode(BufferedImage image, String format,
boolean encodeAlpha) throws IOException {
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
encodeAlpha);
return imageEncoder.encode(image);
}
Expand All @@ -95,7 +95,7 @@ public static byte[] encode(BufferedImage image, String format,
*/
public static byte[] encode(BufferedImage image, String format,
float quality) throws IOException {
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
quality);
return imageEncoder.encode(image);
}
Expand All @@ -114,8 +114,8 @@ public static byte[] encode(BufferedImage image, String format,
*/
public static byte[] encode(BufferedImage image, String format,
float quality, boolean encodeAlpha) throws IOException {
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
quality, encodeAlpha);
ImageEncoder imageEncoder =
ImageEncoderFactory.newInstance(format,quality, encodeAlpha);
return imageEncoder.encode(image);
}

Expand Down Expand Up @@ -145,8 +145,7 @@ public static void writeBufferedImage(BufferedImage image, String format,
*/
public static void writeBufferedImage(BufferedImage image, String format,
OutputStream outputStream, float quality) throws IOException {
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
quality);
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,quality);
imageEncoder.encode(image, outputStream);
}

Expand All @@ -162,8 +161,7 @@ public static void writeBufferedImage(BufferedImage image, String format,
*/
public static void writeBufferedImage(BufferedImage image, String format,
OutputStream outputStream, boolean encodeAlpha) throws IOException {
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
encodeAlpha);
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,encodeAlpha);
imageEncoder.encode(image, outputStream);
}

Expand All @@ -182,8 +180,7 @@ public static void writeBufferedImage(BufferedImage image, String format,
public static void writeBufferedImage(BufferedImage image, String format,
OutputStream outputStream, float quality, boolean encodeAlpha)
throws IOException {
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
quality, encodeAlpha);
ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,quality, encodeAlpha);
imageEncoder.encode(image, outputStream);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* ------------------------
Expand All @@ -40,7 +40,7 @@
* implementations (DG);
* 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
* 06-Jul-2008 : Remove encoder only used in JDK 1.3 (DG);
*
*
*/

package org.jfree.chart.encoders;
Expand Down Expand Up @@ -68,6 +68,7 @@ public class ImageEncoderFactory {
private static void init() {
encoders = new HashMap<>();
encoders.put("jpeg", "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
encoders.put("jpg", "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
encoders.put("png", "org.jfree.chart.encoders.SunPNGEncoderAdapter");
}

Expand Down
227 changes: 227 additions & 0 deletions src/test/java/org/jfree/chart/encoders/EncoderUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package org.jfree.chart.encoders;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import static java.awt.image.BufferedImage.TYPE_INT_RGB;
import static org.junit.jupiter.api.Assertions.*;

class EncoderUtilTest {
private BufferedImage bufferedImage;
private ByteArrayOutputStream baos;
private final String defaultBufferValue = "test";
private final String defaultFormat = "jpeg";
private final String defaultInvalidFormat = "test";
private final float defaultQualityLevel = 0.4f;

@BeforeEach
void setUp() throws IOException {
bufferedImage = new BufferedImage(1, 1, TYPE_INT_RGB);
baos = new ByteArrayOutputStream();
baos.write(defaultBufferValue.getBytes());
}

@Test
void encodeThrowExceptionWhenRequiredInputsAreNull(){
assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(null, null)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(bufferedImage, null)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(bufferedImage, "pippo")
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(null, null, 0f)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(null, null, true));

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(bufferedImage, null, true)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(null, null, defaultQualityLevel, true)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(null, null, defaultQualityLevel, false)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(bufferedImage, null, defaultQualityLevel, true)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(bufferedImage, null, defaultQualityLevel, false)
);
}

@Test
void throwsExceptionIfFormatIsInvalid(){
assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.encode(bufferedImage, defaultInvalidFormat, defaultQualityLevel, false)
);

}

@Test
void canDecodeValidImageFormats() throws IOException {
canEncode("jpg");
canEncode("jpeg");
canEncode("png");
}

private void canEncode(String format) throws IOException {
byte[] result = EncoderUtil.encode(bufferedImage, format);
assertNotNull(result);
}

@Test
void canEncodeWithAlphaTransparency() throws IOException {
byte[] encoded = EncoderUtil.encode(bufferedImage, defaultFormat, true);
assertNotNull(encoded);
}

@Test
void canEncodeWithoutAlphaTransparency() throws IOException {
byte[] encoded = EncoderUtil.encode(bufferedImage, defaultFormat, false);
assertNotNull(encoded);
}

@Test
void canEncodeWithQualityLevel() throws IOException {
byte[] encoded = EncoderUtil.encode(bufferedImage, defaultFormat, defaultQualityLevel);
assertNotNull(encoded);
}

@Test
void canEncodeWithQualityAndTransparency() throws IOException {
byte[] encoded = EncoderUtil.encode(bufferedImage, defaultFormat, defaultQualityLevel, true);
assertNotNull(encoded);
}

@Test
void canEncodeWithQualityButWithoutTransparency() throws IOException {
byte[] encoded = EncoderUtil.encode(bufferedImage, defaultFormat, defaultQualityLevel, false);
assertNotNull(encoded);
}

@Test
void writeBufferedImage_throwsExceptionWithAllNulls(){
assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(null, null, null));

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(null, null, null, defaultQualityLevel)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(null, null, null, true)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(null, null, null, true)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(null, null, null,defaultQualityLevel, true)
);
}

@Test
void writeBufferedImage_throwsExceptionWhenFormatIsNull(){
assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(bufferedImage, null, null));

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(bufferedImage, null, null, defaultQualityLevel)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(bufferedImage, null, null,defaultQualityLevel, true)
);
}

@Test
void writeBufferedImage_throwsExceptionWhenOutputStreamIsNull(){
assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, null, defaultQualityLevel)
);

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, null));

assertThrows(
IllegalArgumentException.class,
() -> EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, null,defaultQualityLevel, true)
);
}

@Test
void testWriteBufferedImage() throws IOException {
EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, baos);
assertTrue(baos.toString().contains(defaultBufferValue));
}

@Test
void canWriteBufferedImageWithQuality() throws IOException {
EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, baos, defaultQualityLevel);
assertTrue(baos.toString().contains(defaultBufferValue));
}

@Test
void canWriteBufferedImageWithTransparency() throws IOException {
EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, baos, true);
assertTrue(baos.toString().contains(defaultBufferValue));
}

@Test
void canWriteBufferedImageWithoutTransparency() throws IOException {
EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, baos, false);
assertTrue(baos.toString().contains(defaultBufferValue));
}

@Test
void canWriteBufferedImageWithQualityAndTransparency() throws IOException {
EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, baos, defaultQualityLevel, true);
assertTrue(baos.toString().contains(defaultBufferValue));
}

@Test
void canWriteBufferedImageWithQualityButWithoutTransparency() throws IOException {
EncoderUtil.writeBufferedImage(bufferedImage, defaultFormat, baos, defaultQualityLevel, false);
assertTrue(baos.toString().contains(defaultBufferValue));
}
}