Class ImageReaderTestCase
- Object
-
- TestCase
-
- ImageIOTestCase
-
- ImageReaderTestCase
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public abstract class ImageReaderTestCase extends ImageIOTestCase implements Closeable
Base class for testingImageReaderimplementations. This test reads different regions and bands of an image at different sub-sampling levels, and compares the results with the complete image. If the image reader can also provide GeoAPI metadata objects, then this class will verify the consistency of some basic attributes. For example it will verify that the grid envelope (if any) is consistent with the image width and height.To use this test, subclasses need to set the
readerfield to a non-null value in theprepareImageReader(boolean)method. The reader input shall be set by the subclass when requested by the caller. Example:public class MyImageReaderTest extends ImageReaderTestCase { @Override protected void prepareImageReader(boolean setInput) throws IOException { if (reader == null) { reader = new MyImageReader(); } if (setInput) { reader.setInput(ImageIO.createImageInputStream(new File("MyTestImage"))); } } }Subclasses inherit the following tests:
In addition, subclasses may consider to override the following methods:
getMetadata(Class, IIOMetadata)- to extract metadata objects from specific nodes.close()- to modify the policy of reader disposal.
- Since:
- 3.1
-
-
Field Summary
Fields Modifier and Type Field Description protected ImageReaderreaderThe image reader to test.-
Fields inherited from class ImageIOTestCase
isSourceBandsSupported, isSubregionSupported, isSubsamplingOffsetSupported, isSubsamplingSupported, sampleToleranceThreshold
-
Fields inherited from class TestCase
configurationTip, listener, units, validators
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedImageReaderTestCase()Creates a new test case using a default random number generator.protectedImageReaderTestCase(long seed)Creates a new test case using a random number generator initialized to the given seed.
-
Method Summary
Modifier and Type Method Description voidclose()Disposes the reader (if non-null) after each test.protected <T> TgetMetadata(Class<T> type, IIOMetadata metadata)Returns the user object of the given type found in the given Image I/O metadata, ornullif none.protected abstract voidprepareImageReader(boolean setInput)voidtestImageMetadata()voidtestReadAsBufferedImage()Tests theImageReader.readmethod.voidtestReadAsRaster()Tests theImageReader.readRastermethod.voidtestReadAsRenderedImage()Tests theImageReader.readAsRenderedImagemethod.voidtestStreamMetadata()-
Methods inherited from class TestCase
configuration, factories, factories, getEnabledFlags
-
-
-
-
Field Detail
-
reader
protected ImageReader reader
The image reader to test. This field must be set by subclasses in theprepareImageReader(boolean)method.
-
-
Constructor Detail
-
ImageReaderTestCase
protected ImageReaderTestCase()
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 theImageReaderTestCase(long)constructor instead.
-
ImageReaderTestCase
protected ImageReaderTestCase(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 read parameters.
-
-
Method Detail
-
prepareImageReader
protected abstract void prepareImageReader(boolean setInput) throws IOException
Invoked when the image reader is about to be used for the first time, or when its input needs to be reinitialized. Subclasses need to create a newImageReaderinstance if needed and set its input in this method.This method may be invoked more than once during the same test if the input became invalid. This may occur because the tests will read the same image many time in different ways, and not all input streams can seek back to the beginning of the image stream. In such case,
ImageReaderTestCasewill close the input and invoke this method in order to get a freshImageInputStream.Example:
This method may be invoked with a@Override protected void prepareImageReader(boolean setInput) throws IOException { if (reader == null) { reader = new MyImageReader(); } if (setInput) { reader.setInput(ImageIO.createImageInputStream(new File("MyTestImage"))); } }falseargument value when the methods to be tested don't need an input, for exampleImageReader.canReadRaster().- Parameters:
setInput-trueif this method shall set the reader input, orfalseif this is not yet necessary.- Throws:
IOException- if an error occurred while preparing the reader.
-
getMetadata
protected <T> T getMetadata(Class<T> type, IIOMetadata metadata) throws IOException
Returns the user object of the given type found in the given Image I/O metadata, ornullif none. The default implementation gets the tree of nodes for all metadata formats except the standard format, then iterates down the tree in search for a user object of the given type. If an ambiguity is found, this method conservatively returnstrue.Implementors are encouraged to override this method if they can look for metadata in a more accurate way.
See
testStreamMetadata()andtestImageMetadata()for a list of types requested by the defaultImageReaderTestCaseimplementation.- Type Parameters:
T- the compile-time type of the object to search.- Parameters:
type- the type of the object to search.metadata- the metadata where to search for the object.- Returns:
- the user object of the given type, or
nullif not found. - Throws:
IOException- if this method requires an I/O operation and that operation failed.
-
testStreamMetadata
public void testStreamMetadata() throws IOException
TestsImageReader.getStreamMetadata(). The default implementation invokesgetMetadata(Metadata.class,stream metadata), then validates the following properties:- Throws:
IOException- if an error occurred while reading the metadata.
-
testImageMetadata
public void testImageMetadata() throws IOException
TestsImageReader.getImageMetadata(int). The default implementation invokesgetMetadata(Class, IIOMetadata)for the types listed below, then validates their properties:Metadata.getIdentificationInfo(): seetestStreamMetadata()Extent: Validate the spatial extent, if any.Grid.getExtent()GridEnvelope: Verify that the span in at least one dimension is equals to the image width, and a span in another dimension is equals to the image height.
- Throws:
IOException- if an error occurred while reading the metadata.
-
testReadAsBufferedImage
public void testReadAsBufferedImage() throws IOException
Tests theImageReader.readmethod. First, this method reads the full image with a call toImageReader.read(int). Then, this method invokesImageReader.read(int, ImageReadParam)an arbitrary amount of time for the following configurations (note: anyisXXXSupportedfield which was set tofalseprior the execution of this test will stayfalse):- Reads the full image once (all
isXXXSupportedfields set tofalse). - Reads various sub-regions (only
isSubregionSupportedmay betrue) - Reads at various sub-sampling (only
isSubsamplingSupportedmay betrue) - Reads various bands (only
isSourceBandsSupportedmay betrue) - A mix of sub-regions, sub-sampling and source bands
Note: in the spirit of JUnit, this test should have been splitted in smaller test cases: one for sub-regions, one for sub-samplings, etc. However in this particular case, consolidation of those tests in a single method provides the following benefits:- The potentially large complete image is read only once.
- If the tests for subregions or subsamplings fails, avoid the test mixing both.
- Less methods to override if the implementor want to provide his own test.
- Throws:
IOException- if an error occurred while reading the image.
- Reads the full image once (all
-
testReadAsRenderedImage
public void testReadAsRenderedImage() throws IOException
Tests theImageReader.readAsRenderedImagemethod. This method performs the same test thantestReadAsBufferedImage(), except that theImageReader.readAsRenderedImage(int, ImageReadParam)method is invoked instead thanImageReader.read(int, ImageReadParam).- Throws:
IOException- if an error occurred while reading the image.
-
testReadAsRaster
public void testReadAsRaster() throws IOException
Tests theImageReader.readRastermethod. This method performs the same test thantestReadAsBufferedImage(), except that theImageReader.readRaster(int, ImageReadParam)method is invoked instead thanImageReader.read(int, ImageReadParam).This test is ignored if
ImageReader.canReadRaster()returnsfalse.- Throws:
IOException- if an error occurred while reading the raster.
-
close
public void close() throws IOException
Disposes the reader (if non-null) after each test. The default implementation performs the following cleanup:- If the reader input is closeable, closes it.
- Invokes
ImageReader.reset()for clearing the input and listeners. - Invokes
ImageReader.dispose()for performing additional resource disposal, if any. - Sets the
readerfield tonullfor preventing accidental use.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an error occurred while closing the input stream.- See Also:
ImageReader.reset(),ImageReader.dispose()
-
-