More test cases and trivial fixes

This commit is contained in:
Trustin Lee 2008-09-04 11:53:44 +00:00
parent 1d1e88397e
commit d9a37093e2
12 changed files with 80 additions and 7 deletions

View File

@ -314,7 +314,13 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
}
public ChannelBuffer copy(int index, int length) {
ByteBuffer src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
ByteBuffer src;
try {
src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
}
ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length);
dst.put(src);
dst.clear();

View File

@ -216,12 +216,8 @@ public class DynamicChannelBuffer extends AbstractChannelBuffer {
public ChannelBuffer copy(int index, int length) {
DynamicChannelBuffer copiedBuffer = new DynamicChannelBuffer(endianness, Math.max(length, 64));
if (readable()) {
copiedBuffer.buffer = buffer.copy(readerIndex(), readableBytes());
copiedBuffer.setIndex(0, readableBytes());
} else {
copiedBuffer.buffer = ChannelBuffers.EMPTY_BUFFER;
}
copiedBuffer.buffer = buffer.copy(index, length);
copiedBuffer.setIndex(0, length);
return copiedBuffer;
}

View File

@ -108,6 +108,7 @@ public class SlicedChannelBuffer extends AbstractChannelBuffer implements Wrappe
}
public ChannelBuffer copy(int index, int length) {
checkIndex(index, length);
return buffer.copy(index + adjustment, length);
}

View File

@ -102,6 +102,7 @@ public class TruncatedChannelBuffer extends AbstractChannelBuffer implements Wra
}
public ChannelBuffer copy(int index, int length) {
checkIndex(index, length);
return buffer.copy(index, length);
}

View File

@ -249,6 +249,26 @@ public abstract class AbstractChannelBufferTest {
buffer.getBytes(-1, ByteBuffer.allocate(0));
}
@Test(expected=IndexOutOfBoundsException.class)
public void copyBoundaryCheck1() {
buffer.copy(-1, 0);
}
@Test(expected=IndexOutOfBoundsException.class)
public void copyBoundaryCheck2() {
buffer.copy(0, buffer.capacity() + 1);
}
@Test(expected=IndexOutOfBoundsException.class)
public void copyBoundaryCheck3() {
buffer.copy(buffer.capacity() + 1, 0);
}
@Test(expected=IndexOutOfBoundsException.class)
public void copyBoundaryCheck4() {
buffer.copy(buffer.capacity(), 1);
}
@Test(expected=IndexOutOfBoundsException.class)
public void setIndexBoundaryCheck1() {
buffer.setIndex(-1, CAPACITY);

View File

@ -24,6 +24,8 @@ package org.jboss.netty.buffer;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -46,4 +48,9 @@ public class BigEndianHeapChannelBufferTest extends AbstractChannelBufferTest {
protected ChannelBuffer[] components() {
return new ChannelBuffer[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new BigEndianHeapChannelBuffer(null);
}
}

View File

@ -24,6 +24,8 @@ package org.jboss.netty.buffer;
import java.nio.ByteBuffer;
import org.junit.Test;
/**
*
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -45,4 +47,9 @@ public class ByteBufferBackedHeapChannelBufferTest extends AbstractChannelBuffer
protected ChannelBuffer[] components() {
return new ChannelBuffer[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new ByteBufferBackedChannelBuffer(null);
}
}

View File

@ -24,6 +24,8 @@ package org.jboss.netty.buffer;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -46,4 +48,9 @@ public class DuplicateChannelBufferTest extends AbstractChannelBufferTest {
protected ChannelBuffer[] components() {
return new ChannelBuffer[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new DuplicatedChannelBuffer(null);
}
}

View File

@ -24,6 +24,8 @@ package org.jboss.netty.buffer;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -57,4 +59,9 @@ public class DynamicChannelBufferTest extends AbstractChannelBufferTest {
protected ChannelBuffer[] components() {
return new ChannelBuffer[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new DynamicChannelBuffer(null, 0);
}
}

View File

@ -26,6 +26,8 @@ import static org.junit.Assert.*;
import java.nio.ByteOrder;
import org.junit.Test;
/**
*
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -48,4 +50,9 @@ public class LittleEndianHeapChannelBufferTest extends AbstractChannelBufferTest
protected ChannelBuffer[] components() {
return new ChannelBuffer[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new LittleEndianHeapChannelBuffer(null);
}
}

View File

@ -26,6 +26,8 @@ import static org.junit.Assert.*;
import java.util.Random;
import org.junit.Test;
/**
*
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -50,4 +52,9 @@ public class SlicedChannelBufferTest extends AbstractChannelBufferTest {
protected ChannelBuffer[] components() {
return new ChannelBuffer[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new SlicedChannelBuffer(null, 0, 0);
}
}

View File

@ -24,6 +24,8 @@ package org.jboss.netty.buffer;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author The Netty Project (netty-dev@lists.jboss.org)
@ -47,4 +49,9 @@ public class TruncatedChannelBufferTest extends AbstractChannelBufferTest {
protected ChannelBuffer[] components() {
return new ChannelBuffer[] { buffer };
}
@Test(expected = NullPointerException.class)
public void shouldNotAllowNullInConstructor() {
new TruncatedChannelBuffer(null, 0);
}
}