--- /dev/null 2014-10-19 10:21:40.720294471 +0100 +++ new/test/java/lang/String/StringByteBufferConversions.java 2014-10-26 21:08:16.940864267 +0000 @@ -0,0 +1,149 @@ + +import static org.testng.Assert.assertEquals; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import static java.nio.charset.StandardCharsets.UTF_8; + +import org.testng.annotations.Test; + +/* @test + * @summary Test creating a String from a bytebuffer + * @run testng StringByteBufferConversions + */ +public class StringByteBufferConversions { + + @Test(expectedExceptions = NullPointerException.class) + public void nullByteBufferShouldThrowException() { + // given + ByteBuffer buffer = null; + + // when + new String(buffer, UTF_8); + } + + @Test(expectedExceptions = NullPointerException.class) + public void nullCharsetShouldThrowException() { + // given + ByteBuffer buffer = ByteBuffer.allocateDirect(20); + + // when + new String(buffer, null); + } + + @Test + public void heapByteBuffersCanBeConvertedToStrings() { + // given + String expected = "Hello World"; + ByteBuffer buffer = ByteBuffer.wrap(expected.getBytes(UTF_8)); + + // when + String str = new String(buffer, UTF_8); + + // then + assertEquals(str, expected); + assertEquals(buffer.position(), 11); + } + + @Test + public void directByteBuffersCanBeConvertedToStrings() { + // given + String expected = "Hello World"; + byte[] bytes = expected.getBytes(UTF_8); + ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length) + .put(bytes, 0, bytes.length); + + buffer.position(0); + + // when + String str = new String(buffer, UTF_8); + + // then + assertEquals(str, expected); + assertEquals(buffer.position(), bytes.length); + } + + @Test + public void chunksOfByteBuffersCanBeConvertedToStrings() { + // given + String expected = "Hello World"; + byte[] bytes = expected.getBytes(UTF_8); + int offset = 5; + int length = offset + bytes.length + 5; + ByteBuffer buffer = ByteBuffer.allocateDirect(length); + buffer.position(offset); + buffer.put(bytes, 0, bytes.length); + + buffer.position(offset); + buffer.limit(offset + bytes.length); + + // when + String str = new String(buffer, UTF_8); + + // then + assertEquals(str, expected); + assertEquals(buffer.position(), offset + bytes.length); + } + + @Test(expectedExceptions = NullPointerException.class) + public void getBytesThrowsExceptionWithNullBuffer() { + "".getBytes((ByteBuffer)null, UTF_8); + } + + @Test(expectedExceptions = NullPointerException.class) + public void getBytesThrowsExceptionWithNullByteArray() { + "".getBytes((byte[])null, 0, UTF_8); + } + + @Test(expectedExceptions = NullPointerException.class) + public void getBytesThrowsExceptionWithNullCharset() { + byte[] bytes = new byte[16]; + "".getBytes(bytes, 0, null); + } + + @Test + public void shouldEncodeStringsOntoByteArrays() { + byte[] bytes = new byte[11]; + int written = "Hello World".getBytes(bytes, 0, UTF_8); + assertEquals(written, 11); + + assertEquals(new String(bytes, UTF_8), "Hello World"); + } + + @Test + public void shouldEncodeStringsOntoByteBuffer() { + ByteBuffer bytes = ByteBuffer.allocateDirect(11); + int written = "Hello World".getBytes(bytes, UTF_8); + assertEquals(written, 11); + assertEquals(bytes.position(), 11); + + bytes.position(0); + assertEquals(new String(bytes, UTF_8), "Hello World"); + } + + @Test + public void shouldEncodeStringsOntoChunkOfByteArray() { + byte[] bytes = new byte[20]; + final int offset = 5; + int written = "Hello World".getBytes(bytes, offset, UTF_8); + assertEquals(written, 11); + + assertEquals(new String(bytes, 5, 11, UTF_8), "Hello World"); + } + + @Test + public void shouldEncodeStringsOntoChunkOfByteBuffer() { + ByteBuffer bytes = ByteBuffer.allocateDirect(20); + final int offset = 5; + bytes.position(offset); + int written = "Hello World".getBytes(bytes, UTF_8); + assertEquals(written, 11); + assertEquals(bytes.position(), 11 + offset); + + bytes.position(offset); + bytes.limit(offset + 11); + assertEquals(new String(bytes, UTF_8), "Hello World"); + } + +} +