From 926359d9d2526af41ca9ac85b11bde03fdf9a723 Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Wed, 21 Mar 2012 10:13:03 +0100 Subject: [PATCH] New test cases. Better exception handling of IMAGEERROR. --- .../image/BufferedImageFactory.java | 9 +-- .../image/BufferedImageFactoryTestCase.java | 71 +++++++++++++++--- .../common-image/src/test/resources/tux.gif | Bin 0 -> 5822 bytes 3 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 common/common-image/src/test/resources/tux.gif diff --git a/common/common-image/src/main/java/com/twelvemonkeys/image/BufferedImageFactory.java b/common/common-image/src/main/java/com/twelvemonkeys/image/BufferedImageFactory.java index 461e5139..69de39b3 100755 --- a/common/common-image/src/main/java/com/twelvemonkeys/image/BufferedImageFactory.java +++ b/common/common-image/src/main/java/com/twelvemonkeys/image/BufferedImageFactory.java @@ -59,7 +59,7 @@ public final class BufferedImageFactory { private int percentageDone; private ImageProducer producer; - private boolean error; + private ImageConversionException consumerException; private volatile boolean fetching; private boolean readColorModelOnly; @@ -217,8 +217,8 @@ public final class BufferedImageFactory { } } - if (error) { - throw new ImageConversionException("Image conversion failed: ImageConsumer.IMAGEERROR."); + if (consumerException != null) { + throw new ImageConversionException("Image conversion failed: " + consumerException.getMessage(), consumerException); } if (pColorModelOnly) { @@ -475,8 +475,7 @@ public final class BufferedImageFactory { switch (pStatus) { case ImageConsumer.IMAGEERROR: - Thread.dumpStack(); - error = true; + consumerException = new ImageConversionException("ImageConsumer.IMAGEERROR"); break; } diff --git a/common/common-image/src/test/java/com/twelvemonkeys/image/BufferedImageFactoryTestCase.java b/common/common-image/src/test/java/com/twelvemonkeys/image/BufferedImageFactoryTestCase.java index 131f05fd..fa6cb48a 100644 --- a/common/common-image/src/test/java/com/twelvemonkeys/image/BufferedImageFactoryTestCase.java +++ b/common/common-image/src/test/java/com/twelvemonkeys/image/BufferedImageFactoryTestCase.java @@ -1,5 +1,6 @@ package com.twelvemonkeys.image; +import org.junit.Ignore; import org.junit.Test; import java.awt.*; @@ -7,9 +8,11 @@ import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.ImageProducer; +import java.awt.image.IndexColorModel; import java.net.URL; import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * BufferedImageFactoryTestCase @@ -29,19 +32,24 @@ public class BufferedImageFactoryTestCase { new BufferedImageFactory((ImageProducer) null); } - // Image source = Toolkit.getDefaultToolkit().createImage((byte[]) null); // - NPE in Toolkit, ok + // NPE in Toolkit, ok + @Test(expected = RuntimeException.class) + public void testGetBufferedImageErrorSourceByteArray() { + Image source = Toolkit.getDefaultToolkit().createImage((byte[]) null); - @Test(timeout = 1000, expected = IllegalArgumentException.class) - public void testGetBufferedImageErrorSourceIP() { + new BufferedImageFactory(source); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetBufferedImageErrorSourceImageProducer() { Image source = Toolkit.getDefaultToolkit().createImage((ImageProducer) null); new BufferedImageFactory(source); } - // TODO: This is a quite serious bug, but it can be argued that the bug is in the - // Toolkit, allowing such images in the first place... In any case, there's - // not much we can do, except until someone is bored and kills the app... :-P -/* + // TODO: This is a quite serious bug, however, the bug is in the Toolkit, allowing such images in the first place... + // In any case, there's not much we can do, except until someone is bored and kills the app/thread... :-P + @Ignore("Bug in Toolkit") @Test(timeout = 1000, expected = ImageConversionException.class) public void testGetBufferedImageErrorSourceString() { Image source = Toolkit.getDefaultToolkit().createImage((String) null); @@ -49,7 +57,6 @@ public class BufferedImageFactoryTestCase { BufferedImageFactory factory = new BufferedImageFactory(source); factory.getBufferedImage(); } -*/ // This is a little random, and it would be nicer if we could throw an IllegalArgumentException on create. // Unfortunately, the API doesn't allow this... @@ -94,7 +101,53 @@ public class BufferedImageFactoryTestCase { } } - // TODO: Test a GIF or PNG with PLTE chunk, and make sure we get an IndexColorModel + @Test + public void testGetBufferedImageGIF() { + URL resource = getClass().getResource("/tux.gif"); + assertNotNull(resource); + Image source = Toolkit.getDefaultToolkit().createImage(resource); + assertNotNull(source); + + BufferedImageFactory factory = new BufferedImageFactory(source); + BufferedImage image = factory.getBufferedImage(); + + assertEquals(250, image.getWidth()); + assertEquals(250, image.getHeight()); + + assertEquals(Transparency.BITMASK, image.getTransparency()); + + // All corners of image should be fully transparent + assertEquals(0, image.getRGB(0, 0) >>> 24); + assertEquals(0, image.getRGB(249, 0) >>> 24); + assertEquals(0, image.getRGB(0, 249) >>> 24); + assertEquals(0, image.getRGB(249, 249) >>> 24); + } + + @Test + public void testGetColorModelGIF() { + URL resource = getClass().getResource("/tux.gif"); + assertNotNull(resource); + Image source = Toolkit.getDefaultToolkit().createImage(resource); + assertNotNull(source); + + BufferedImageFactory factory = new BufferedImageFactory(source); + ColorModel colorModel = factory.getColorModel(); + + assertNotNull(colorModel); + + assertEquals(3, colorModel.getNumColorComponents()); + assertEquals(ColorSpace.getInstance(ColorSpace.CS_sRGB), colorModel.getColorSpace()); + assertTrue(colorModel instanceof IndexColorModel); + + assertTrue(colorModel.hasAlpha()); + assertEquals(4, colorModel.getNumComponents()); + assertTrue(((IndexColorModel) colorModel).getTransparentPixel() >= 0); + assertEquals(Transparency.BITMASK, colorModel.getTransparency()); + + for (int i = 0; i < colorModel.getNumComponents(); i++) { + assertEquals(8, colorModel.getComponentSize(i)); + } + } @Test public void testGetBufferedImageSubsampled() { diff --git a/common/common-image/src/test/resources/tux.gif b/common/common-image/src/test/resources/tux.gif new file mode 100644 index 0000000000000000000000000000000000000000..378cd229495ce7ce91396946fff568b990465ecd GIT binary patch literal 5822 zcmeH}`8O1N!-v1KV8&9`F@s_#du8l{YK(o&R@|BxOT>f`WzS&j6xr8C*_T4rvW$Hz zk(%r`-Q1~=C6N~I^ZfSy5zlo#f5COmbEG(mdva+%|3aASJb#-;k0ie0LxibuOc6JVifk8SL92^{{gYofk z79Frs0E@+1OaY6TU~zHrVwE0qlG5V%3l>|27I0$c~Vg~AFBAu9U6|cV~H@@Q*vL@Z!N;-#0s;<>OJI2?{q75*(Ny3y>21@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