Repository: commons-compress
Updated Branches:
  refs/heads/master a2976b66b -> 72b178963


COMPRESS-327 tests and tweaks for SeekableInMemoryByteChannel

patch by Maciej Nowakowski


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/72b17896
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/72b17896
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/72b17896

Branch: refs/heads/master
Commit: 72b1789632ca7a97e5fa338ab5a60cb4d02915e6
Parents: a2976b6
Author: Stefan Bodewig <bode...@apache.org>
Authored: Sat Oct 22 18:38:38 2016 +0200
Committer: Stefan Bodewig <bode...@apache.org>
Committed: Sat Oct 22 18:38:38 2016 +0200

----------------------------------------------------------------------
 .../utils/SeekableInMemoryByteChannel.java      |  33 ++--
 .../utils/SeekableInMemoryByteChannelTest.java  | 174 +++++++++++++++++++
 2 files changed, 194 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/72b17896/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
 
b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
index 61aaf71..4bf2d7f 100644
--- 
a/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
+++ 
b/src/main/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannel.java
@@ -21,7 +21,6 @@ package org.apache.commons.compress.utils;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.ClosedChannelException;
-import java.nio.channels.NonWritableChannelException;
 import java.nio.channels.SeekableByteChannel;
 import java.util.Arrays;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -52,9 +51,10 @@ public class SeekableInMemoryByteChannel implements 
SeekableByteChannel {
     }
 
     @Override
-    public SeekableByteChannel position(long newPosition) {
-        if (newPosition > Integer.MAX_VALUE) {
-            throw new IllegalArgumentException("Position cannot exceed " + 
Integer.MAX_VALUE);
+    public SeekableByteChannel position(long newPosition) throws IOException {
+        ensureOpen();
+        if (newPosition < 0L || newPosition > Integer.MAX_VALUE) {
+            throw new IllegalArgumentException("Position has to be in range 
0.. " + Integer.MAX_VALUE);
         }
         position = (int) newPosition;
         return this;
@@ -70,17 +70,14 @@ public class SeekableInMemoryByteChannel implements 
SeekableByteChannel {
         if (size > newSize) {
             size = (int) newSize;
         }
-        if (position > size) {
-            position = size;
-        }
+        repositionIfNecessary();
         return this;
     }
 
     @Override
     public int read(ByteBuffer buf) throws IOException {
-        if (!isOpen()) {
-            throw new ClosedChannelException();
-        }
+        ensureOpen();
+        repositionIfNecessary();
         int wanted = buf.remaining();
         int possible = size - position;
         if (wanted > possible) {
@@ -103,9 +100,7 @@ public class SeekableInMemoryByteChannel implements 
SeekableByteChannel {
 
     @Override
     public int write(ByteBuffer b) throws IOException {
-        if (!isOpen()) {
-            throw new ClosedChannelException();
-        }
+        ensureOpen();
         int wanted = b.remaining();
         int possibleWithoutResize = size - position;
         if (wanted > possibleWithoutResize) {
@@ -137,4 +132,16 @@ public class SeekableInMemoryByteChannel implements 
SeekableByteChannel {
         data = Arrays.copyOf(data, len);
     }
 
+    private void ensureOpen() throws ClosedChannelException {
+        if (!isOpen()) {
+            throw new ClosedChannelException();
+        }
+    }
+
+    private void repositionIfNecessary() {
+        if (position > size) {
+            position = size;
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/72b17896/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java
 
b/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java
new file mode 100644
index 0000000..1922621
--- /dev/null
+++ 
b/src/test/java/org/apache/commons/compress/utils/SeekableInMemoryByteChannelTest.java
@@ -0,0 +1,174 @@
+package org.apache.commons.compress.utils;
+
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+
+import static org.apache.commons.compress.utils.CharsetNames.UTF_8;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class SeekableInMemoryByteChannelTest {
+
+    private final byte[] testData = "Some 
data".getBytes(Charset.forName(UTF_8));
+
+    @Test
+    public void shouldReadContentsProperly() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        ByteBuffer readBuffer = ByteBuffer.allocate(testData.length);
+        //when
+        int readCount = c.read(readBuffer);
+        //then
+        assertEquals(testData.length, readCount);
+        assertArrayEquals(testData, readBuffer.array());
+        assertEquals(testData.length, c.position());
+    }
+
+    @Test
+    public void shouldReadContentsWhenBiggerBufferSupplied() throws 
IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        ByteBuffer readBuffer = ByteBuffer.allocate(testData.length + 1);
+        //when
+        int readCount = c.read(readBuffer);
+        //then
+        assertEquals(testData.length, readCount);
+        assertArrayEquals(testData, Arrays.copyOf(readBuffer.array(), 
testData.length));
+        assertEquals(testData.length, c.position());
+    }
+
+    @Test
+    public void shouldReadDataFromSetPosition() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        ByteBuffer readBuffer = ByteBuffer.allocate(4);
+        //when
+        c.position(5L);
+        int readCount = c.read(readBuffer);
+        //then
+        assertEquals(4L, readCount);
+        assertEquals("data", new String(readBuffer.array(), 
Charset.forName(UTF_8)));
+        assertEquals(testData.length, c.position());
+    }
+
+    @Test
+    public void shouldReadNoDataWhenPositionAtTheEnd() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        ByteBuffer readBuffer = ByteBuffer.allocate(testData.length);
+        //when
+        c.position(testData.length + 1);
+        int readCount = c.read(readBuffer);
+        //then
+        assertEquals(0L, readBuffer.position());
+        assertEquals(0, readCount);
+    }
+
+    @Test(expected = ClosedChannelException.class)
+    public void shouldThrowExceptionOnReadingClosedChannel() throws 
IOException {
+        //given
+        SeekableInMemoryByteChannel c = new SeekableInMemoryByteChannel();
+        //when
+        c.close();
+        c.read(ByteBuffer.allocate(1));
+    }
+
+    @Test
+    public void shouldWriteDataProperly() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new SeekableInMemoryByteChannel();
+        ByteBuffer inData = ByteBuffer.wrap(testData);
+        //when
+        int writeCount = c.write(inData);
+        //then
+        assertEquals(testData.length, writeCount);
+        assertArrayEquals(testData, Arrays.copyOf(c.array(), (int) c.size()));
+        assertEquals(testData.length, c.position());
+    }
+
+    @Test
+    public void shouldWriteDataProperlyAfterPositionSet() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        ByteBuffer inData = ByteBuffer.wrap(testData);
+        ByteBuffer expectedData = ByteBuffer.allocate(testData.length + 
5).put(testData, 0, 5).put(testData);
+        //when
+        c.position(5L);
+        int writeCount = c.write(inData);
+
+        //then
+        assertEquals(testData.length, writeCount);
+        assertArrayEquals(expectedData.array(), Arrays.copyOf(c.array(), (int) 
c.size()));
+        assertEquals(testData.length + 5, c.position());
+    }
+
+
+    @Test(expected = ClosedChannelException.class)
+    public void shouldThrowExceptionOnWritingToClosedChannel() throws 
IOException {
+        //given
+        SeekableInMemoryByteChannel c = new SeekableInMemoryByteChannel();
+        //when
+        c.close();
+        c.write(ByteBuffer.allocate(1));
+    }
+
+    @Test
+    public void shouldTruncateContentsProperly() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        //when
+        c.truncate(4);
+        //then
+        byte[] bytes = Arrays.copyOf(c.array(), (int) c.size());
+        assertEquals("Some", new String(bytes, Charset.forName(UTF_8)));
+    }
+
+    @Test
+    public void shouldSetProperPositionOnTruncate() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        //when
+        c.position(testData.length);
+        c.truncate(4L);
+        //then
+        assertEquals(4L, c.position());
+        assertEquals(4L, c.size());
+    }
+
+    @Test
+    public void shouldSetProperPosition() throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new 
SeekableInMemoryByteChannel(testData);
+        //when
+        long posAtFour = c.position(4L).position();
+        long posAtTheEnd = c.position(testData.length).position();
+        long posPastTheEnd = c.position(testData.length + 1L).position();
+        //then
+        assertEquals(4L, posAtFour);
+        assertEquals(c.size(), posAtTheEnd);
+        assertEquals(posPastTheEnd, posPastTheEnd);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void shouldThrowExceptionWhenSettingIncorrectPosition() throws 
IOException {
+        //given
+        SeekableInMemoryByteChannel c = new SeekableInMemoryByteChannel();
+        //when
+        c.position(Integer.MAX_VALUE + 1L);
+    }
+
+    @Test(expected = ClosedChannelException.class)
+    public void shouldThrowExceptionWhenSettingPositionOnClosedChannel() 
throws IOException {
+        //given
+        SeekableInMemoryByteChannel c = new SeekableInMemoryByteChannel();
+        //when
+        c.close();
+        c.position(1L);
+    }
+
+}

Reply via email to