You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by rb...@apache.org on 2013/07/01 14:35:56 UTC

[5/5] git commit: improved coverage in IoBuffer, various improvements

improved coverage in IoBuffer, various improvements


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/fffe4b56
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/fffe4b56
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/fffe4b56

Branch: refs/heads/trunk
Commit: fffe4b5658907eabfc3d6682cadc8173f3ac8489
Parents: 8884baf
Author: Raphaël P. Barazzutti <rb...@apache.org>
Authored: Sun Jun 30 22:34:56 2013 +0200
Committer: Raphaël P. Barazzutti <rb...@apache.org>
Committed: Mon Jul 1 08:28:34 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/mina/codec/IoBuffer.java    |  46 ++++-
 .../org/apache/mina/codec/IoBufferTest.java     | 169 ++++++++++++++++++-
 2 files changed, 199 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/fffe4b56/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
----------------------------------------------------------------------
diff --git a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
index 081da51..9b659c8 100644
--- a/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
+++ b/codec/src/main/java/org/apache/mina/codec/IoBuffer.java
@@ -168,6 +168,13 @@ public final class IoBuffer {
         return new InputStream() {
 
             @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                int toRead = Math.min(remaining(), len);
+                get(b, off, toRead);
+                return toRead;
+            }
+
+            @Override
             public int read() throws IOException {
                 return hasRemaining() ? get() & 0xff : -1;
             }
@@ -275,11 +282,17 @@ public final class IoBuffer {
             return false;
         }
         int p = this.position();
-        for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
-            if (this.get(i) != that.get(j)) {
+        int q = that.position();
+        while (this.hasRemaining() && that.hasRemaining()) {
+            if (this.get() != that.get()) {
+                this.position(p);
+                that.position(q);
                 return false;
             }
+
         }
+        this.position(p);
+        that.position(q);
         return true;
     }
 
@@ -697,10 +710,6 @@ public final class IoBuffer {
      * @see ByteBuffer#putChar(char)
      */
     public IoBuffer putChar(char value) {
-        if (remaining() < 2) {
-            throw new BufferUnderflowException();
-        }
-
         putShort((short) value);
         return this;
     }
@@ -888,8 +897,22 @@ public final class IoBuffer {
         return out;
     }
 
+    @Override
+    public String toString() {
+        StringBuffer sb = new StringBuffer();
+        sb.append(getClass().getName());
+        sb.append("[pos=");
+        sb.append(position());
+        sb.append(" lim=");
+        sb.append(limit());
+        sb.append(" cap=");
+        sb.append(capacity());
+        sb.append("]");
+        return sb.toString();
+    }
+
     private void updatePosition() {
-        while (!position.getNode().getBuffer().hasRemaining()) {
+        while (!position.getNode().getBuffer().hasRemaining() && position.getNode().hasNext()) {
             position.setNode(position.getNode().getNext());
             position.getNode().getBuffer().rewind();
         }
@@ -970,7 +993,14 @@ public final class IoBuffer {
 
         @Override
         public String toString() {
-            return "Pointer [node=" + node + ", position=" + position + "]";
+            StringBuffer sb = new StringBuffer();
+            sb.append(getClass().getName());
+            sb.append("[node=");
+            sb.append(getNode());
+            sb.append(", pos=");
+            sb.append(getPosition());            
+            sb.append("]");
+            return sb.toString();           
         }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina/blob/fffe4b56/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
----------------------------------------------------------------------
diff --git a/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java b/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
index 3c92f66..831c5ce 100644
--- a/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
+++ b/codec/src/test/java/org/apache/mina/codec/IoBufferTest.java
@@ -18,17 +18,22 @@
  */
 package org.apache.mina.codec;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
 import java.util.Arrays;
 
+import org.junit.Assert;
 import org.junit.Test;
 
 /**
@@ -564,7 +569,7 @@ public class IoBufferTest {
         // Move forward a bit
         ioBuffer.get();
         ioBuffer.get();
-        
+
         ioBuffer.limit(3);
 
         // Clear
@@ -818,6 +823,23 @@ public class IoBufferTest {
             ioBuffer.rewind();
             assertEquals(12345, ioBuffer.getShort());
             assertEquals(-23456, ioBuffer.getShort());
+
+            ioBuffer.putShort(1, (short) 12345);
+            assertEquals((short) 12345, ioBuffer.getShort(1));
+
+            try {
+                ioBuffer.putShort(3, (short) 1);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+
+            try {
+                ioBuffer.getShort(3);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
         }
     }
 
@@ -834,6 +856,23 @@ public class IoBufferTest {
             ioBuffer.rewind();
             assertEquals(123456, ioBuffer.getInt());
             assertEquals(-23456789, ioBuffer.getInt());
+
+            ioBuffer.putInt(2, 1234567890);
+            assertEquals(1234567890, ioBuffer.getInt(2));
+
+            try {
+                ioBuffer.putInt(5, 1);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+
+            try {
+                ioBuffer.getInt(5);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
         }
     }
 
@@ -851,6 +890,61 @@ public class IoBufferTest {
             ioBuffer.rewind();
             assertEquals(123456789012l, ioBuffer.getLong());
             assertEquals(-23456789023l, ioBuffer.getLong());
+
+            ioBuffer.putLong(4, 1234567890);
+            assertEquals(1234567890, ioBuffer.getLong(4));
+
+            try {
+                ioBuffer.putLong(9, 1);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+
+            try {
+                ioBuffer.getLong(9);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
+        }
+    }
+
+    @Test
+    public void testFloat() {
+        for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
+            ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putFloat(-0.68f).rewind();
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
+            assertEquals(5, ioBuffer.capacity());
+            ioBuffer.extend(3);
+            ioBuffer.position(4);
+            assertEquals(8, ioBuffer.capacity());
+            ioBuffer.putFloat(3.14f);
+            ioBuffer.rewind();
+            assertEquals(-0.68f, ioBuffer.getFloat(), 0.001f);
+            assertEquals(3.14f, ioBuffer.getFloat(), 0.001f);
+            ioBuffer.putFloat(2, -12.34f);
+            assertEquals(-12.34f, ioBuffer.getFloat(2), 0.001f);
+        }
+    }
+
+    @Test
+    public void testDouble() {
+        for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
+            ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(9).order(bo).putDouble(Math.PI).rewind();
+            IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
+            assertEquals(9, ioBuffer.capacity());
+            ioBuffer.extend(7);
+
+            ioBuffer.position(8);
+            assertEquals(16, ioBuffer.capacity());
+            ioBuffer.putDouble(-Math.E);
+            ioBuffer.rewind();
+            assertEquals(Math.PI, ioBuffer.getDouble(), 1E-10);
+            assertEquals(-Math.E, ioBuffer.getDouble(), 1E-10);
+
+            ioBuffer.putDouble(4, 12.34);
+            assertEquals(12.34, ioBuffer.getDouble(4), 1E-10);
         }
     }
 
@@ -859,18 +953,35 @@ public class IoBufferTest {
         for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
             ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putChar('ë').rewind();
             IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
-            
+
             assertEquals(3, ioBuffer.capacity());
-            
+
             ioBuffer.extend(1);
             ioBuffer.order(bo);
             ioBuffer.position(2);
             assertEquals(4, ioBuffer.capacity());
             ioBuffer.putChar('ü');
             ioBuffer.rewind();
-            
+
             assertEquals('ë', ioBuffer.getChar());
             assertEquals('ü', ioBuffer.getChar());
+            
+            ioBuffer.putChar(1, 'ç');
+            assertEquals('ç', ioBuffer.getChar(1));
+            
+            try {
+                ioBuffer.putChar(3, 'ñ');
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here                
+            }
+
+            try {
+                ioBuffer.getChar(3);
+                fail("Not enough place on the buffer");
+            } catch (BufferUnderflowException e) {
+                // Should come here
+            }
         }
     }
 
@@ -928,7 +1039,7 @@ public class IoBufferTest {
             assertEquals(expected[i], ioBuffer.get(i));
         }
     }
-    
+
     @Test
     public void testCompact() {
         ByteBuffer bb1 = ByteBuffer.allocate(5);
@@ -950,12 +1061,54 @@ public class IoBufferTest {
         ioBuffer.limit(8);
 
         ioBuffer.compact();
-        assertEquals(ioBuffer.capacity(),ioBuffer.limit());
-        assertEquals(6,ioBuffer.position());
-        
+        assertEquals(ioBuffer.capacity(), ioBuffer.limit());
+        assertEquals(6, ioBuffer.position());
+
         byte seg[] = "234567".getBytes();
         for (int i = 0; i < 6; i++) {
             assertEquals(seg[i], ioBuffer.get(i));
         }
     }
+
+    @Test
+    public void testInputStreamGetByte() throws IOException {
+        String hw = "HelloWorld";
+        IoBuffer bb = IoBuffer.wrap(hw.getBytes());
+        InputStream is = bb.asInputStream();
+        for (int i = 0; i < 10; i++) {
+            assertEquals(i, bb.position());
+            assertEquals(hw.getBytes()[i], is.read());
+        }
+        assertEquals(-1, is.read());
+    }
+
+    @Test
+    public void testInputStreamGetByteArray() throws IOException {
+        String hw = "HelloWorld";
+        IoBuffer bb = IoBuffer.wrap(hw.getBytes());
+        InputStream is = bb.asInputStream();
+        byte array[] = new byte[15];
+
+        assertEquals(5, is.read(array, 0, 5));
+        assertEquals(5, bb.position());
+        assertEquals(5, is.read(array, 5, 10));
+        assertEquals(10, bb.position());
+
+        for (int i = 0; i < 10; i++) {
+            assertEquals(hw.getBytes()[i], array[i]);
+        }
+    }
+
+    @Test
+    public void testEquals() {
+        String h = "Hello";
+        String w = "World";
+        IoBuffer hw1b = IoBuffer.wrap((h + w).getBytes());
+        IoBuffer wh1b = IoBuffer.wrap((w + h).getBytes());
+        IoBuffer hw2b = IoBuffer.newInstance();
+        hw2b.add(ByteBuffer.wrap(h.getBytes()));
+        hw2b.add(ByteBuffer.wrap(w.getBytes()));
+        assertEquals(hw2b, hw1b);
+        Assert.assertThat(wh1b, is(not(hw1b)));
+    }
 }