Class ImageWriterTestCase

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public abstract class ImageWriterTestCase
    extends ImageIOTestCase
    implements Closeable
    Base class for testing ImageWriter implementations. This test writes different regions and bands of an image at different sub-sampling levels, then read back the images and compare the sample values.

    To use this test, subclasses need to set the writer field to a non-null value in the prepareImageWriter(boolean) method. Example:

    public class MyImageWriterTest extends ImageWriterTestCase {
        @Override
        protected void prepareImageWriter(boolean optionallySetOutput) throws IOException {
            if (writer == null) {
                writer = new MyImageWriter();
            }
        }
    }
    The writer shall accept at least one of the following output types, in preference order:
    • ImageOutputStream - mandatory according Image I/O specification.
    • File - fallback if the writer doesn't support ImageOutputStream. This fallback exists because ImageOutputStream is hard to support when the writer is implemented by a native library.
    Since:
    3.1
    • Field Detail

      • reader

        protected ImageReader reader
        The reader to use for verifying the writer output. By default, this field is null until a reader is first needed, in which case the field is assigned to a reader instance created by ImageIO.getImageReader(ImageWriter). Subclasses can set explicitely a value to this field if they need the tests to use an other reader instead.

        ImageWriterTestCase will use only the ImageReader.read(int) method. Consequently, this reader doesn't need to support ImageReadParam usage.

    • Constructor Detail

      • ImageWriterTestCase

        protected ImageWriterTestCase()
        Creates a new test case using a default random number generator. The sub-regions, sub-samplings and source bands will be different for every test execution. If reproducible subsetting sequences are needed, use the ImageWriterTestCase(long) constructor instead.
      • ImageWriterTestCase

        protected ImageWriterTestCase​(long seed)
        Creates a new test case using a random number generator initialized to the given seed.
        Parameters:
        seed - the initial seed for the random numbers generator. Use a constant value if the tests need to be reproduced with the same sequence of image write parameters.
    • Method Detail

      • prepareImageWriter

        protected abstract void prepareImageWriter​(boolean optionallySetOutput)
                                            throws IOException
        Invoked when the image writer is about to be used for the first time. Subclasses need to create a new ImageWriter instance if needed.

        If the optionallySetOutput argument is true, then subclasses can optionally set the output to a temporary file or other object suitable to the writer. This operation is optional: if no output has been explicitely set, ImageWriterTestCase will automatically set the output to an in-memory stream or to a temporary file.

        Example:

        @Override
        protected void prepareImageWriter(boolean optionallySetOutput) throws IOException {
            if (writer == null) {
                writer = new MyImageWriter();
            }
            if (optionallySetOutput) {
                writer.setOutput(output);                  // Optional operation.
            }
        }
        This method may be invoked with a false argument value when the methods to be tested do not need an output, for example ImageWriter.canWriteRasters().
        Parameters:
        optionallySetOutput - true if this method can set the writer output (optional operation), or false if this is not yet necessary.
        Throws:
        IOException - if an error occurred while preparing the writer.
      • testOneByteBand

        public void testOneByteBand()
                             throws IOException
        Tests the ImageWriter.write method for a single band of byte values. First, this method creates an single-banded image filled with random byte values. Then, this method invokes write the image an arbitrary amount of time for the following configurations (note: any isXXXSupported field which was set to false prior the execution of this test will stay false):
        • Writes the full image once (all isXXXSupported fields set to false).
        • Writes various sub-regions (only isSubregionSupported may be true)
        • Writes at various sub-sampling (only isSubsamplingSupported may be true)
        • Reads various bands (only isSourceBandsSupported may be true)
        • A mix of sub-regions, sub-sampling and source bands
        Then the image is read again and the pixel values are compared with the corresponding pixel values of the original image.
        Throws:
        IOException - if an error occurred while writing the image or or reading it back.