From 01703b4a536b72ede219af03b097364013d90ea9 Mon Sep 17 00:00:00 2001 From: Jan Sladek <Jan.Sladek@mni.thm.de> Date: Fri, 13 Mar 2015 12:26:51 +0100 Subject: [PATCH] Implemented JUnit-Test for ImageUtils.java. Closes #15369. Fixed StringIndexOutOfBoundsException. --- src/main/java/de/thm/arsnova/ImageUtils.java | 3 + .../java/de/thm/arsnova/ImageUtilsTest.java | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/test/java/de/thm/arsnova/ImageUtilsTest.java diff --git a/src/main/java/de/thm/arsnova/ImageUtils.java b/src/main/java/de/thm/arsnova/ImageUtils.java index 19cb75c40..9983b4951 100644 --- a/src/main/java/de/thm/arsnova/ImageUtils.java +++ b/src/main/java/de/thm/arsnova/ImageUtils.java @@ -134,6 +134,9 @@ public class ImageUtils { else { final int extensionStartIndex = IMAGE_PREFIX_START.length(); final int extensionEndIndex = maybeImage.indexOf(IMAGE_PREFIX_MIDDLE); + if (extensionEndIndex < 0) { + return null; + } final String imageWithoutPrefix = maybeImage.substring(extensionEndIndex); diff --git a/src/test/java/de/thm/arsnova/ImageUtilsTest.java b/src/test/java/de/thm/arsnova/ImageUtilsTest.java new file mode 100644 index 000000000..0a8237721 --- /dev/null +++ b/src/test/java/de/thm/arsnova/ImageUtilsTest.java @@ -0,0 +1,63 @@ +package de.thm.arsnova; + +import static de.thm.arsnova.ImageUtils.isBase64EncodedImage; +import static de.thm.arsnova.ImageUtils.extractImageInfo; +import static de.thm.arsnova.ImageUtils.IMAGE_PREFIX_START; +import static de.thm.arsnova.ImageUtils.IMAGE_PREFIX_MIDDLE; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class ImageUtilsTest { + + @Test + public void testNullIsNoValidBase64String() { + assertFalse("\"null\" is no valid Base64 String.", isBase64EncodedImage(null)); + } + + @Test + public void testEmptyStringIsNoValidBase64String() { + assertFalse("The empty String is no valid Base64 String.", isBase64EncodedImage("")); + } + + @Test + public void testWrongStringIsNoValidBase64String() { + final String[] fakeStrings = new String[] { + "data:picture/png;base64,IMAGE-DATA", + "data:image/png;base63,IMAGE-DATA" + }; + + for (String fakeString : fakeStrings) { + assertFalse( + String.format("The String %s is not a valid Base64 String.", fakeString), + isBase64EncodedImage(fakeString) + ); + } + } + + @Test + public void testValidBase64String() { + final String imageString = String.format("%spng%sIMAGE-DATA", IMAGE_PREFIX_START, IMAGE_PREFIX_MIDDLE); + assertTrue(isBase64EncodedImage(imageString)); + } + + @Test + public void testImageInfoExtraction() { + final String extension = "png"; + final String imageData = "IMAGE-DATA"; + final String imageString = String.format("%s%s%s%s", IMAGE_PREFIX_START, + extension, IMAGE_PREFIX_MIDDLE, imageData); + + final String[] imageInfo = extractImageInfo(imageString); + assertNotNull(imageInfo); + + assertEquals("Extracted information doesn't match its specification.", 2, imageInfo.length); + + assertEquals("Extracted extension is invalid.", extension, imageInfo[0]); + assertEquals("Extracted Base64-Image String is invalid.", imageData, imageInfo[1]); + } + +} -- GitLab