@Test
public void testDispose() throws FileNotFoundException, IOException {

    final File tiff = TestData.file(MosaicTest.class, "sample0.tif");
    final TIFFImageReader reader = (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) new TIFFImageReaderSpi()
            .createReaderInstance();
    final ImageInputStream stream = ImageIO.createImageInputStream(tiff);
    reader.setInput(stream);
    RenderedImage image = ImageReadDescriptor.create(stream, 0, false, false, false, null,
            null, null, reader, null);
    DisposeStopper stopper = new DisposeStopper(image);
    boolean readSuccess = true;
    try {
        // Try to dispose. It shouldn't since we are using the wrapper.
        ImageUtilities.disposeImage(stopper);
        assertNotNull(image);
        // I still can get data since using the stopper the image isn't disposed
        assertEquals(187, image.getData().getSample(1, 5, 0));
        ImageUtilities.disposeImage(image);
        image.getData().getSample(1, 5, 0);
    } catch (RuntimeException ioe) {
        // The dispose on the image (without using the disposeStopper wrapper)
        // should have successfully disposed so the read can't success
        readSuccess = false;
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (Throwable t) {
            }
        }
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
            }
        }
    }
    assertFalse(readSuccess);
}