From 77419346011466427d129d42127b26d00f167f4a Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 27 Aug 2008 07:17:41 +0000 Subject: [PATCH] More test cases.. --- .../buffer/ChannelBufferInputStream.java | 12 +- .../jboss/netty/buffer/ChannelBuffers.java | 124 ++++++++---- .../netty/buffer/CompositeChannelBuffer.java | 14 +- .../netty/buffer/ChannelBufferStreamTest.java | 182 ++++++++++++++++++ .../netty/buffer/ChannelBuffersTest.java | 152 +++++++++++---- 5 files changed, 401 insertions(+), 83 deletions(-) create mode 100644 src/test/java/org/jboss/netty/buffer/ChannelBufferStreamTest.java diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBufferInputStream.java b/src/main/java/org/jboss/netty/buffer/ChannelBufferInputStream.java index 10ec5d4570..00eba2c980 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBufferInputStream.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBufferInputStream.java @@ -75,6 +75,9 @@ public class ChannelBufferInputStream extends InputStream implements DataInput { if (buffer == null) { throw new NullPointerException("buffer"); } + if (length < 0) { + throw new IllegalArgumentException("length: " + length); + } if (length > buffer.readableBytes()) { throw new IndexOutOfBoundsException(); } @@ -142,11 +145,8 @@ public class ChannelBufferInputStream extends InputStream implements DataInput { } public boolean readBoolean() throws IOException { - int b = read(); - if (b < 0) { - throw new EOFException(); - } - return b != 0; + checkAvailable(1); + return read() != 0; } public byte readByte() throws IOException { @@ -226,7 +226,7 @@ public class ChannelBufferInputStream extends InputStream implements DataInput { public int skipBytes(int n) throws IOException { int nBytes = Math.min(available(), n); - buffer.skipBytes(n); + buffer.skipBytes(nBytes); return nBytes; } diff --git a/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java b/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java index 1542c73b4e..50031bf5de 100644 --- a/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java +++ b/src/main/java/org/jboss/netty/buffer/ChannelBuffers.java @@ -129,12 +129,15 @@ public class ChannelBuffers { * {@code writerIndex} are {@code 0}. */ public static ChannelBuffer buffer(ByteOrder endianness, int capacity) { - if (capacity == 0) { - return EMPTY_BUFFER; - } if (endianness == BIG_ENDIAN) { + if (capacity == 0) { + return EMPTY_BUFFER; + } return new BigEndianHeapChannelBuffer(capacity); } else if (endianness == LITTLE_ENDIAN) { + if (capacity == 0) { + return EMPTY_BUFFER; + } return new LittleEndianHeapChannelBuffer(capacity); } else { throw new NullPointerException("endianness"); @@ -156,9 +159,13 @@ public class ChannelBuffers { * {@code writerIndex} are {@code 0}. */ public static ChannelBuffer directBuffer(ByteOrder endianness, int capacity) { + if (endianness == null) { + throw new NullPointerException("endianness"); + } if (capacity == 0) { return EMPTY_BUFFER; } + ChannelBuffer buffer = new ByteBufferBackedChannelBuffer( ByteBuffer.allocateDirect(capacity).order(endianness)); buffer.clear(); @@ -209,12 +216,15 @@ public class ChannelBuffers { * content will be visible to the returned buffer. */ public static ChannelBuffer wrappedBuffer(ByteOrder endianness, byte[] array) { - if (array.length == 0) { - return EMPTY_BUFFER; - } if (endianness == BIG_ENDIAN) { + if (array.length == 0) { + return EMPTY_BUFFER; + } return new BigEndianHeapChannelBuffer(array); } else if (endianness == LITTLE_ENDIAN) { + if (array.length == 0) { + return EMPTY_BUFFER; + } return new LittleEndianHeapChannelBuffer(array); } else { throw new NullPointerException("endianness"); @@ -236,17 +246,25 @@ public class ChannelBuffers { * the specified array's content will be visible to the returned buffer. */ public static ChannelBuffer wrappedBuffer(ByteOrder endianness, byte[] array, int offset, int length) { - if (length == 0) { - return EMPTY_BUFFER; + if (endianness == null) { + throw new NullPointerException("endianness"); } if (offset == 0) { if (length == array.length) { return wrappedBuffer(endianness, array); } else { - return new TruncatedChannelBuffer(wrappedBuffer(endianness, array), length); + if (length == 0) { + return EMPTY_BUFFER; + } else { + return new TruncatedChannelBuffer(wrappedBuffer(endianness, array), length); + } } } else { - return new SlicedChannelBuffer(wrappedBuffer(endianness, array), offset, length); + if (length == 0) { + return EMPTY_BUFFER; + } else { + return new SlicedChannelBuffer(wrappedBuffer(endianness, array), offset, length); + } } } @@ -298,15 +316,21 @@ public class ChannelBuffers { public static ChannelBuffer wrappedBuffer(ByteOrder endianness, byte[]... arrays) { switch (arrays.length) { case 0: - return EMPTY_BUFFER; + break; case 1: - return wrappedBuffer(endianness, arrays[0]); + if (arrays[0].length != 0) { + return wrappedBuffer(endianness, arrays[0]); + } + break; + default: + ChannelBuffer[] wrappedBuffers = new ChannelBuffer[arrays.length]; + for (int i = 0; i < arrays.length; i ++) { + wrappedBuffers[i] = wrappedBuffer(endianness, arrays[i]); + } + return wrappedBuffer(wrappedBuffers); } - ChannelBuffer[] wrappedBuffers = new ChannelBuffer[arrays.length]; - for (int i = 0; i < arrays.length; i ++) { - wrappedBuffers[i] = wrappedBuffer(endianness, arrays[i]); - } - return wrappedBuffer(wrappedBuffers); + + return EMPTY_BUFFER; } /** @@ -321,12 +345,20 @@ public class ChannelBuffers { public static ChannelBuffer wrappedBuffer(ChannelBuffer... buffers) { switch (buffers.length) { case 0: - return EMPTY_BUFFER; + break; case 1: - return wrappedBuffer(buffers[0]); + if (buffers[0].readable()) { + return wrappedBuffer(buffers[0]); + } + break; default: - return new CompositeChannelBuffer(buffers); + for (ChannelBuffer b: buffers) { + if (b.capacity() != 0) { + return new CompositeChannelBuffer(buffers); + } + } } + return EMPTY_BUFFER; } /** @@ -341,15 +373,21 @@ public class ChannelBuffers { public static ChannelBuffer wrappedBuffer(ByteBuffer... buffers) { switch (buffers.length) { case 0: - return EMPTY_BUFFER; + break; case 1: - return wrappedBuffer(buffers[0]); + if (buffers[0].hasRemaining()) { + return wrappedBuffer(buffers[0]); + } + break; + default: + ChannelBuffer[] wrappedBuffers = new ChannelBuffer[buffers.length]; + for (int i = 0; i < buffers.length; i ++) { + wrappedBuffers[i] = wrappedBuffer(buffers[i]); + } + return wrappedBuffer(wrappedBuffers); } - ChannelBuffer[] wrappedBuffers = new ChannelBuffer[buffers.length]; - for (int i = 0; i < buffers.length; i ++) { - wrappedBuffers[i] = wrappedBuffer(buffers[i]); - } - return wrappedBuffer(wrappedBuffers); + + return EMPTY_BUFFER; } /** @@ -368,12 +406,15 @@ public class ChannelBuffers { * {@code array.length} respectively. */ public static ChannelBuffer copiedBuffer(ByteOrder endianness, byte[] array) { - if (array.length == 0) { - return EMPTY_BUFFER; - } if (endianness == BIG_ENDIAN) { + if (array.length == 0) { + return EMPTY_BUFFER; + } return new BigEndianHeapChannelBuffer(array.clone()); } else if (endianness == LITTLE_ENDIAN) { + if (array.length == 0) { + return EMPTY_BUFFER; + } return new LittleEndianHeapChannelBuffer(array.clone()); } else { throw new NullPointerException("endianness"); @@ -397,6 +438,9 @@ public class ChannelBuffers { * the specified {@code length} respectively. */ public static ChannelBuffer copiedBuffer(ByteOrder endianness, byte[] array, int offset, int length) { + if (endianness == null) { + throw new NullPointerException("endianness"); + } if (length == 0) { return EMPTY_BUFFER; } @@ -433,7 +477,11 @@ public class ChannelBuffers { * respectively. */ public static ChannelBuffer copiedBuffer(ChannelBuffer buffer) { - return buffer.copy(); + if (buffer.readable()) { + return buffer.copy(); + } else { + return EMPTY_BUFFER; + } } /** @@ -457,7 +505,11 @@ public class ChannelBuffers { case 0: return EMPTY_BUFFER; case 1: - return copiedBuffer(endianness, arrays[0]); + if (arrays[0].length == 0) { + return EMPTY_BUFFER; + } else { + return copiedBuffer(endianness, arrays[0]); + } } // Merge the specified arrays into one array. @@ -470,6 +522,10 @@ public class ChannelBuffers { length += a.length; } + if (length == 0) { + return EMPTY_BUFFER; + } + byte[] mergedArray = new byte[length]; for (int i = 0, j = 0; i < arrays.length; i ++) { byte[] a = arrays[i]; @@ -500,7 +556,7 @@ public class ChannelBuffers { ChannelBuffer[] copiedBuffers = new ChannelBuffer[buffers.length]; for (int i = 0; i < buffers.length; i ++) { - copiedBuffers[i] = buffers[i].copy(); + copiedBuffers[i] = copiedBuffer(buffers[i]); } return wrappedBuffer(copiedBuffers); } @@ -525,7 +581,7 @@ public class ChannelBuffers { ChannelBuffer[] copiedBuffers = new ChannelBuffer[buffers.length]; for (int i = 0; i < buffers.length; i ++) { - copiedBuffers[i] = wrappedBuffer(buffers[i]).copy(); + copiedBuffers[i] = copiedBuffer(buffers[i]); } return wrappedBuffer(copiedBuffers); } diff --git a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java index 928daa423f..d6f6aa184e 100644 --- a/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/CompositeChannelBuffer.java @@ -55,10 +55,20 @@ public class CompositeChannelBuffer extends AbstractChannelBuffer { throw new IllegalArgumentException("buffers should not be empty."); } - ByteOrder expectedEndianness = buffers[0].order(); + ByteOrder expectedEndianness = null; + for (ChannelBuffer buffer : buffers) { + if (buffer.capacity() != 0) { + expectedEndianness = buffer.order(); + } + } + + if (expectedEndianness == null) { + throw new IllegalArgumentException("buffers have only empty buffers."); + } + slices = new ChannelBuffer[buffers.length]; for (int i = 0; i < buffers.length; i ++) { - if (buffers[i].order() != expectedEndianness) { + if (buffers[i].capacity() != 0 && buffers[i].order() != expectedEndianness) { throw new IllegalArgumentException( "All buffers must have the same endianness."); } diff --git a/src/test/java/org/jboss/netty/buffer/ChannelBufferStreamTest.java b/src/test/java/org/jboss/netty/buffer/ChannelBufferStreamTest.java new file mode 100644 index 0000000000..db04b531b6 --- /dev/null +++ b/src/test/java/org/jboss/netty/buffer/ChannelBufferStreamTest.java @@ -0,0 +1,182 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.buffer; + +import static org.junit.Assert.*; + +import java.io.EOFException; + +import org.junit.Test; + + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev$, $Date$ + * + */ +public class ChannelBufferStreamTest { + + @Test + public void testAll() throws Exception { + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); + + try { + new ChannelBufferOutputStream(null); + fail(); + } catch (NullPointerException e) { + // Expected + } + + ChannelBufferOutputStream out = new ChannelBufferOutputStream(buf); + assertSame(buf, out.buffer()); + out.writeBoolean(true); + out.writeBoolean(false); + out.writeByte(42); + out.writeByte(224); + out.writeBytes("Hello, World!"); + out.writeChars("Hello, World"); + out.writeChar('!'); + out.writeDouble(42.0); + out.writeFloat(42.0f); + out.writeInt(42); + out.writeLong(42); + out.writeShort(42); + out.writeShort(49152); + out.writeUTF("Hello, World!"); + out.writeBytes("The first line\r\r\n"); + out.write(new byte[0]); + out.write(new byte[] { 1, 2, 3, 4 }); + out.write(new byte[] { 1, 3, 3, 4 }, 0, 0); + out.close(); + + try { + new ChannelBufferInputStream(null); + fail(); + } catch (NullPointerException e) { + // Expected + } + + try { + new ChannelBufferInputStream(null, 0); + fail(); + } catch (NullPointerException e) { + // Expected + } + + try { + new ChannelBufferInputStream(buf, -1); + } catch (IllegalArgumentException e) { + // Expected + } + + try { + new ChannelBufferInputStream(buf, buf.capacity() + 1); + } catch (IndexOutOfBoundsException e) { + // Expected + } + + ChannelBufferInputStream in = new ChannelBufferInputStream(buf); + + assertTrue(in.markSupported()); + in.mark(Integer.MAX_VALUE); + + assertEquals(buf.writerIndex(), in.skip(Long.MAX_VALUE)); + assertFalse(buf.readable()); + + in.reset(); + assertEquals(0, buf.readerIndex()); + + assertEquals(4, in.skip(4)); + assertEquals(4, buf.readerIndex()); + in.reset(); + + assertTrue(in.readBoolean()); + assertFalse(in.readBoolean()); + assertEquals(42, in.readByte()); + assertEquals(224, in.readUnsignedByte()); + + byte[] tmp = new byte[13]; + in.readFully(tmp); + assertEquals("Hello, World!", new String(tmp, "ISO-8859-1")); + + assertEquals('H', in.readChar()); + assertEquals('e', in.readChar()); + assertEquals('l', in.readChar()); + assertEquals('l', in.readChar()); + assertEquals('o', in.readChar()); + assertEquals(',', in.readChar()); + assertEquals(' ', in.readChar()); + assertEquals('W', in.readChar()); + assertEquals('o', in.readChar()); + assertEquals('r', in.readChar()); + assertEquals('l', in.readChar()); + assertEquals('d', in.readChar()); + assertEquals('!', in.readChar()); + + assertEquals(42.0, in.readDouble(), 0.0); + assertEquals(42.0f, in.readFloat(), 0.0); + assertEquals(42, in.readInt()); + assertEquals(42, in.readLong()); + assertEquals(42, in.readShort()); + assertEquals(49152, in.readUnsignedShort()); + + assertEquals("Hello, World!", in.readUTF()); + assertEquals("The first line", in.readLine()); + + assertEquals(4, in.read(tmp)); + assertEquals(1, tmp[0]); + assertEquals(2, tmp[1]); + assertEquals(3, tmp[2]); + assertEquals(4, tmp[3]); + + assertEquals(-1, in.read()); + assertEquals(-1, in.read(tmp)); + + try { + in.readByte(); + fail(); + } catch (EOFException e) { + // Expected + } + + try { + in.readFully(tmp, 0, -1); + fail(); + } catch (IndexOutOfBoundsException e) { + // Expected + } + + try { + in.readFully(tmp); + fail(); + } catch (EOFException e) { + // Expected + } + + in.close(); + + assertEquals(buf.readerIndex(), in.readBytes()); + } +} diff --git a/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java b/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java index fcaaf10477..e657b40556 100644 --- a/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java +++ b/src/test/java/org/jboss/netty/buffer/ChannelBuffersTest.java @@ -22,8 +22,10 @@ */ package org.jboss.netty.buffer; +import static org.jboss.netty.buffer.ChannelBuffers.*; import static org.junit.Assert.*; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -43,13 +45,13 @@ public class ChannelBuffersTest { @Test public void testCompositeWrappedBuffer() { - ChannelBuffer header = ChannelBuffers.dynamicBuffer(12); - ChannelBuffer payload = ChannelBuffers.dynamicBuffer(512); + ChannelBuffer header = dynamicBuffer(12); + ChannelBuffer payload = dynamicBuffer(512); header.writeBytes(new byte[12]); payload.writeBytes(new byte[512]); - ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(header, payload); + ChannelBuffer buffer = wrappedBuffer(header, payload); assertTrue(header.readableBytes() == 12); assertTrue(payload.readableBytes() == 512); @@ -74,7 +76,7 @@ public class ChannelBuffersTest { for (Entry e: map.entrySet()) { assertEquals( e.getValue().intValue(), - ChannelBuffers.hashCode(ChannelBuffers.wrappedBuffer(e.getKey()))); + ChannelBuffers.hashCode(wrappedBuffer(e.getKey()))); } } @@ -83,81 +85,149 @@ public class ChannelBuffersTest { ChannelBuffer a, b; // Different length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2 }); + a = wrappedBuffer(new byte[] { 1 }); + b = wrappedBuffer(new byte[] { 1, 2 }); assertFalse(ChannelBuffers.equals(a, b)); // Same content, same firstIndex, short length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3 }); + a = wrappedBuffer(new byte[] { 1, 2, 3 }); + b = wrappedBuffer(new byte[] { 1, 2, 3 }); assertTrue(ChannelBuffers.equals(a, b)); // Same content, different firstIndex, short length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3); + a = wrappedBuffer(new byte[] { 1, 2, 3 }); + b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4 }, 1, 3); assertTrue(ChannelBuffers.equals(a, b)); // Different content, same firstIndex, short length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 4 }); + a = wrappedBuffer(new byte[] { 1, 2, 3 }); + b = wrappedBuffer(new byte[] { 1, 2, 4 }); assertFalse(ChannelBuffers.equals(a, b)); // Different content, different firstIndex, short length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3); + a = wrappedBuffer(new byte[] { 1, 2, 3 }); + b = wrappedBuffer(new byte[] { 0, 1, 2, 4, 5 }, 1, 3); assertFalse(ChannelBuffers.equals(a, b)); // Same content, same firstIndex, long length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); assertTrue(ChannelBuffers.equals(a, b)); // Same content, different firstIndex, long length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10); + a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10); assertTrue(ChannelBuffers.equals(a, b)); // Different content, same firstIndex, long length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 6, 7, 8, 5, 9, 10 }); + a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + b = wrappedBuffer(new byte[] { 1, 2, 3, 4, 6, 7, 8, 5, 9, 10 }); assertFalse(ChannelBuffers.equals(a, b)); // Different content, different firstIndex, long length. - a = ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); - b = ChannelBuffers.wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10); + a = wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + b = wrappedBuffer(new byte[] { 0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11 }, 1, 10); assertFalse(ChannelBuffers.equals(a, b)); } @Test public void testCompare() { List expected = new ArrayList(); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2, 3 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 })); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2, 3, 4 }, 1, 1)); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4 }, 2, 2)); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 1, 10)); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 2, 12)); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2, 3, 4, 5 }, 2, 1)); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5 }, 3, 2)); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 2, 10)); - expected.add(ChannelBuffers.wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, 3, 12)); + expected.add(wrappedBuffer(new byte[] { 1 })); + expected.add(wrappedBuffer(new byte[] { 1, 2 })); + expected.add(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })); + expected.add(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); + expected.add(wrappedBuffer(new byte[] { 2 })); + expected.add(wrappedBuffer(new byte[] { 2, 3 })); + expected.add(wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 })); + expected.add(wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 })); + expected.add(wrappedBuffer(new byte[] { 2, 3, 4 }, 1, 1)); + expected.add(wrappedBuffer(new byte[] { 1, 2, 3, 4 }, 2, 2)); + expected.add(wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 1, 10)); + expected.add(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 2, 12)); + expected.add(wrappedBuffer(new byte[] { 2, 3, 4, 5 }, 2, 1)); + expected.add(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5 }, 3, 2)); + expected.add(wrappedBuffer(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, 2, 10)); + expected.add(wrappedBuffer(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, 3, 12)); for (int i = 0; i < expected.size(); i ++) { for (int j = 0; j < expected.size(); j ++) { if (i == j) { - assertEquals(0, ChannelBuffers.compare(expected.get(i), expected.get(j))); + assertEquals(0, compare(expected.get(i), expected.get(j))); } else if (i < j) { - assertTrue(ChannelBuffers.compare(expected.get(i), expected.get(j)) < 0); + assertTrue(compare(expected.get(i), expected.get(j)) < 0); } else { - assertTrue(ChannelBuffers.compare(expected.get(i), expected.get(j)) > 0); + assertTrue(compare(expected.get(i), expected.get(j)) > 0); } } } } + + @Test + public void shouldReturnEmptyBufferWhenLengthIsZero() { + assertSame(EMPTY_BUFFER, buffer(0)); + assertSame(EMPTY_BUFFER, buffer(LITTLE_ENDIAN, 0)); + assertSame(EMPTY_BUFFER, directBuffer(0)); + + assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[0])); + assertSame(EMPTY_BUFFER, wrappedBuffer(LITTLE_ENDIAN, new byte[0])); + assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[8], 0, 0)); + assertSame(EMPTY_BUFFER, wrappedBuffer(LITTLE_ENDIAN, new byte[8], 0, 0)); + assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[8], 8, 0)); + assertSame(EMPTY_BUFFER, wrappedBuffer(LITTLE_ENDIAN, new byte[8], 8, 0)); + assertSame(EMPTY_BUFFER, wrappedBuffer(ByteBuffer.allocateDirect(0))); + assertSame(EMPTY_BUFFER, wrappedBuffer(EMPTY_BUFFER)); + assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[0][])); + assertSame(EMPTY_BUFFER, wrappedBuffer(new byte[][] { new byte[0] })); + assertSame(EMPTY_BUFFER, wrappedBuffer(new ByteBuffer[0])); + assertSame(EMPTY_BUFFER, wrappedBuffer(new ByteBuffer[] { ByteBuffer.allocate(0) })); + assertSame(EMPTY_BUFFER, wrappedBuffer(new ByteBuffer[] { ByteBuffer.allocate(0), ByteBuffer.allocate(0) })); + assertSame(EMPTY_BUFFER, wrappedBuffer(new ChannelBuffer[0])); + assertSame(EMPTY_BUFFER, wrappedBuffer(new ChannelBuffer[] { buffer(0) })); + assertSame(EMPTY_BUFFER, wrappedBuffer(new ChannelBuffer[] { buffer(0), buffer(0) })); + + assertSame(EMPTY_BUFFER, copiedBuffer(new byte[0])); + assertSame(EMPTY_BUFFER, copiedBuffer(LITTLE_ENDIAN, new byte[0])); + assertSame(EMPTY_BUFFER, copiedBuffer(new byte[8], 0, 0)); + assertSame(EMPTY_BUFFER, copiedBuffer(LITTLE_ENDIAN, new byte[8], 0, 0)); + assertSame(EMPTY_BUFFER, copiedBuffer(new byte[8], 8, 0)); + assertSame(EMPTY_BUFFER, copiedBuffer(LITTLE_ENDIAN, new byte[8], 8, 0)); + assertSame(EMPTY_BUFFER, copiedBuffer(ByteBuffer.allocateDirect(0))); + assertSame(EMPTY_BUFFER, copiedBuffer(EMPTY_BUFFER)); + assertSame(EMPTY_BUFFER, copiedBuffer(new byte[0][])); + assertSame(EMPTY_BUFFER, copiedBuffer(new byte[][] { new byte[0] })); + assertSame(EMPTY_BUFFER, copiedBuffer(new ByteBuffer[0])); + assertSame(EMPTY_BUFFER, copiedBuffer(new ByteBuffer[] { ByteBuffer.allocate(0) })); + assertSame(EMPTY_BUFFER, copiedBuffer(new ByteBuffer[] { ByteBuffer.allocate(0), ByteBuffer.allocate(0) })); + assertSame(EMPTY_BUFFER, copiedBuffer(new ChannelBuffer[0])); + assertSame(EMPTY_BUFFER, copiedBuffer(new ChannelBuffer[] { buffer(0) })); + assertSame(EMPTY_BUFFER, copiedBuffer(new ChannelBuffer[] { buffer(0), buffer(0) })); + } + + @Test(expected = NullPointerException.class) + public void shouldDisallowNullEndian1() { + buffer(null, 0); + } + + @Test(expected = NullPointerException.class) + public void shouldDisallowNullEndian2() { + directBuffer(null, 0); + } + + @Test(expected = NullPointerException.class) + public void shouldDisallowNullEndian3() { + wrappedBuffer(null, new byte[0]); + } + + @Test(expected = NullPointerException.class) + public void shouldDisallowNullEndian4() { + wrappedBuffer(null, new byte[0], 0, 0); + } + + @Test + public void shouldAllowEmptyBufferToCreateCompositeBuffer() { + wrappedBuffer( + EMPTY_BUFFER, + buffer(LITTLE_ENDIAN, 16)); + } }