Skip to content

Commit

Permalink
Reverted test, glossed over flakyness in library instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldk committed Apr 4, 2023
1 parent aa2e8e5 commit c531d4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public final class BufferedImageFactory {
private int scanSize;

private ColorModel sourceColorModel;
private Hashtable sourceProperties; // ImageConsumer API dictates Hashtable
private Hashtable<?, ?> sourceProperties; // ImageConsumer API dictates Hashtable

private Object sourcePixels;

Expand Down Expand Up @@ -214,31 +214,34 @@ private synchronized void doFetch(final boolean colorModelOnly) throws ImageConv
// Wait until the producer wakes us up, by calling imageComplete
while (fetching) {
try {
wait(200l);
wait(200L);
}
catch (InterruptedException e) {
throw new ImageConversionException("Image conversion aborted: " + e.getMessage(), e);
}
}

if (consumerException != null) {
throw new ImageConversionException("Image conversion failed: " + consumerException.getMessage(), consumerException);
}
try {
if (consumerException != null) {
throw new ImageConversionException("Image conversion failed: " + consumerException.getMessage(), consumerException);
}

if (colorModelOnly) {
createColorModel();
if (colorModelOnly) {
createColorModel();
}
else {
createBuffered();
}
}
else {
createBuffered();
finally {
// Clean up, in case any objects are copied/cloned, so we can free resources
freeResources();
}
}
}

private void createColorModel() {
colorModel = sourceColorModel;

// Clean up, in case any objects are copied/cloned, so we can free resources
freeResources();
}

private void createBuffered() {
Expand All @@ -253,8 +256,9 @@ private void createBuffered() {
}
}

// Clean up, in case any objects are copied/cloned, so we can free resources
freeResources();
if (buffered == null) {
throw new ImageConversionException("Could not create BufferedImage");
}
}

private void freeResources() {
Expand Down Expand Up @@ -324,12 +328,13 @@ public void removeAllProgressListeners() {
* Converts an array of {@code int} pixels to an array of {@code short}
* pixels. The conversion is done, by masking out the
* <em>higher 16 bits</em> of the {@code int}.
*
* <p>
* For any given {@code int}, the {@code short} value is computed as
* follows:
* <blockquote>{@code
* short value = (short) (intValue & 0x0000ffff);
* }</blockquote>
* </p>
*
* @param inputPixels the pixel data to convert
* @return an array of {@code short}s, same length as {@code inputPixels}
Expand All @@ -351,7 +356,7 @@ private static short[] toShortPixels(int[] inputPixels) {
* @see BufferedImageFactory#addProgressListener
* @see BufferedImageFactory#removeProgressListener
*/
public static interface ProgressListener extends EventListener {
public interface ProgressListener extends EventListener {

/**
* Reports progress to this listener.
Expand Down Expand Up @@ -456,7 +461,7 @@ private void setColorModelOnce(final ColorModel colorModel) {
// later replaces it with the default RGB in the first setPixels call
// (this is probably allowed according to the spec, but it's a waste of time and space).
if (sourceColorModel != colorModel) {
if (/*sourceColorModel == null ||*/ sourcePixels == null) {
if (sourcePixels == null) {
sourceColorModel = colorModel;
}
else {
Expand All @@ -478,10 +483,8 @@ public void imageComplete(int status) {
producer.removeConsumer(this);
}

switch (status) {
case ImageConsumer.IMAGEERROR:
consumerException = new ImageConversionException("ImageConsumer.IMAGEERROR");
break;
if (status == ImageConsumer.IMAGEERROR) {
consumerException = new ImageConversionException("ImageConsumer.IMAGEERROR");
}

synchronized (BufferedImageFactory.this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -89,17 +88,12 @@ public void testGetBufferedImageErrorSourceString() {

// 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...
@Test(timeout = 1000)
@Test(timeout = 1000, expected = ImageConversionException.class)
public void testGetBufferedImageErrorSourceURL() {
Image source = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/META-INF/MANIFEST.MF"));

try {
BufferedImageFactory factory = new BufferedImageFactory(source);
assertNull(factory.getBufferedImage()); // Should normally not be reached
}
catch (ImageConversionException ignore) {
// This exception is allowed and *expected*, however this behavior is flaky in some environments...
}
BufferedImageFactory factory = new BufferedImageFactory(source);
factory.getBufferedImage();
}

@Test
Expand Down

0 comments on commit c531d4f

Please sign in to comment.