This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit defbef6e4a7ec537e96598eb3b9139f28dd7d173 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Mon Sep 18 16:20:08 2023 +0200 Tune test utilities in preparation for the addition of GeoTIFF writer. --- .../test/org/apache/sis/image/TiledImageMock.java | 38 +++++++++++++---- .../org/apache/sis/io/stream/ByteArrayChannel.java | 49 ++++++++++++++++++---- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/endorsed/src/org.apache.sis.feature/test/org/apache/sis/image/TiledImageMock.java b/endorsed/src/org.apache.sis.feature/test/org/apache/sis/image/TiledImageMock.java index 9426fc852d..e5a0031733 100644 --- a/endorsed/src/org.apache.sis.feature/test/org/apache/sis/image/TiledImageMock.java +++ b/endorsed/src/org.apache.sis.feature/test/org/apache/sis/image/TiledImageMock.java @@ -41,7 +41,7 @@ import org.apache.sis.coverage.grid.j2d.WritableTiledImage; import org.apache.sis.util.ArraysExt; import org.apache.sis.util.internal.Numerics; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** @@ -180,7 +180,19 @@ public final class TiledImageMock extends PlanarImage implements WritableRendere } /** - * Returns a gray scale color model if the data type is byte, or {@code null} otherwise. + * Sets the color model. This method can be invoked at most once. + * + * @param cm the color model to use. + */ + public synchronized void setColorModel(final ColorModel cm) { + assertNull(colorModel, "Already initialized."); + assertTrue(cm.isCompatibleSampleModel(sampleModel)); + colorModel = cm; + } + + /** + * Returns the color model. + * The default value is a gray scale color model if the data type is byte, or {@code null} otherwise. * More color models may be supported in future versions if there is a need for them. */ @Override @@ -269,6 +281,14 @@ public final class TiledImageMock extends PlanarImage implements WritableRendere StrictMath.floorDiv(oy, tileHeight) + minTileY, true).setSample(x, y, b, value); } + /** + * Initializes the sample values of all all bands in all tiles to testing values. + * The sample value pattern is described by {@link #initializeAllTiles(int[], int)}. + */ + public void initializeAllTiles() { + initializeAllTiles(ArraysExt.range(0, sampleModel.getNumBands()), 0); + } + /** * Initializes the sample values of all tiles to testing values. * The sample values will be 4 digits numbers of the form "BTYX" where: @@ -369,7 +389,7 @@ public final class TiledImageMock extends PlanarImage implements WritableRendere */ @Override public synchronized Raster getTile(final int tileX, final int tileY) { - assertFalse("isTileAcquired", isTileAcquired); // See javadoc. + assertFalse(isTileAcquired, "isTileAcquired"); // See javadoc. return tile(tileX, tileY, false); } @@ -378,7 +398,7 @@ public final class TiledImageMock extends PlanarImage implements WritableRendere */ @Override public synchronized WritableRaster getWritableTile(final int tileX, final int tileY) { - assertFalse("isTileAcquired", isTileAcquired); + assertFalse(isTileAcquired, "isTileAcquired"); final WritableRaster raster = tile(tileX, tileY, true); isTileAcquired = true; acquiredTileX = tileX; @@ -420,9 +440,9 @@ public final class TiledImageMock extends PlanarImage implements WritableRendere */ @Override public synchronized void releaseWritableTile(final int tileX, final int tileY) { - assertTrue("isTileAcquired", isTileAcquired); - assertEquals("tileX", acquiredTileX, tileX); - assertEquals("tileY", acquiredTileY, tileY); + assertTrue(isTileAcquired, "isTileAcquired"); + assertEquals(acquiredTileX, tileX, "tileX"); + assertEquals(acquiredTileY, tileY, "tileY"); isTileAcquired = false; } @@ -476,8 +496,8 @@ public final class TiledImageMock extends PlanarImage implements WritableRendere final int minY = r.getMinY(); final int tx = ImageUtilities.pixelToTileX(this, minX); final int ty = ImageUtilities.pixelToTileY(this, minY); - assertEquals("Unsupported operation.", tx, ImageUtilities.pixelToTileX(this, minX + r.getWidth() - 1)); - assertEquals("Unsupported operation.", ty, ImageUtilities.pixelToTileX(this, minY + r.getHeight() - 1)); + assertEquals(tx, ImageUtilities.pixelToTileX(this, minX + r.getWidth() - 1), "Unsupported operation."); + assertEquals(ty, ImageUtilities.pixelToTileX(this, minY + r.getHeight() - 1), "Unsupported operation."); tile(tx, ty, true).setRect(r); } diff --git a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/ByteArrayChannel.java b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/ByteArrayChannel.java index 4c3f71375f..322eb53cc7 100644 --- a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/ByteArrayChannel.java +++ b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/io/stream/ByteArrayChannel.java @@ -26,6 +26,7 @@ import org.apache.sis.util.ArgumentChecks; /** * A readable and writable channel backed by an array. * Used for testing {@link ChannelDataOutput} and {@link ChannelImageOutputStream} classes. + * Can also be used for testing {@code DataStore} writers. * * @author Rémi Maréchal (Geomatys) * @author Martin Desruisseaux (Geomatys) @@ -33,7 +34,7 @@ import org.apache.sis.util.ArgumentChecks; * @see ChannelDataOutputTest * @see ChannelImageOutputStream */ -final class ByteArrayChannel implements SeekableByteChannel { +public final class ByteArrayChannel implements SeekableByteChannel { /** * Bytes array where to write the data. * The length of this array is the capacity. @@ -62,15 +63,29 @@ final class ByteArrayChannel implements SeekableByteChannel { * @param isDataValid {@code true} if the channel should be initialized with all the {@code data} elements, * or {@code false} if the channel should be considered initially empty. */ - ByteArrayChannel(final byte[] data, final boolean isDataValid) { + public ByteArrayChannel(final byte[] data, final boolean isDataValid) { this.data = data; if (isDataValid) { limit = data.length; } } + /** + * Creates a view over the data array as a buffer. + * The buffer limit is the number of bytes written and the position is zero. + * + * @return a view over the array of bytes written. + */ + public ByteBuffer toBuffer() { + return ByteBuffer.wrap(data, 0, limit); + } + /** * Reads a sequence of bytes from this channel into the given buffer. + * + * @param dst where to store the bytes read. + * @return number of bytes actually read. + * @throws IOException if the channel is closed. */ @Override public int read(final ByteBuffer dst) throws IOException { @@ -86,19 +101,31 @@ final class ByteArrayChannel implements SeekableByteChannel { /** * Writes a sequence of bytes to this channel from the given buffer. + * + * @param src the bytes to write. + * @return number of bytes actually written. + * @throws IOException if the channel is closed. */ @Override public int write(final ByteBuffer src) throws IOException { ensureOpen(); final int length = src.remaining(); - src.get(data, position, length); + try { + src.get(data, position, length); + } catch (IndexOutOfBoundsException e) { + throw new AssertionError("Buffer overflow, not necessarily a bug in the main code. " + + "It may be the destination buffer allocated by the test which is too small. " + + "See the `ByteArrayChannel` construction.", e); + } position += length; limit = StrictMath.max(limit, position); return length; } /** - * Returns this channel position. + * {@return this channel position}. + * + * @throws IOException if the channel is closed. */ @Override public long position() throws IOException { @@ -108,6 +135,9 @@ final class ByteArrayChannel implements SeekableByteChannel { /** * Sets this channel position. + * + * @param newPosition the new position. + * @throws IOException if the channel is closed. */ @Override public SeekableByteChannel position(final long newPosition) throws IOException { @@ -118,7 +148,9 @@ final class ByteArrayChannel implements SeekableByteChannel { } /** - * Returns the current size. + * {@return the current size}. + * + * @throws IOException if the channel is closed. */ @Override public long size() throws IOException { @@ -128,6 +160,9 @@ final class ByteArrayChannel implements SeekableByteChannel { /** * Truncates the data to the given size. + * + * @param size the new size. + * @throws IOException if the channel is closed. */ @Override public SeekableByteChannel truncate(final long size) throws IOException { @@ -147,7 +182,7 @@ final class ByteArrayChannel implements SeekableByteChannel { } /** - * Tells whether or not this channel is open. + * {@return whether or not this channel is open}. */ @Override public boolean isOpen() { @@ -158,7 +193,7 @@ final class ByteArrayChannel implements SeekableByteChannel { * Closes this channel. */ @Override - public void close() throws IOException { + public void close() { isClosed = true; } }