import static org.testng.Assert.assertEquals; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.io.UnsupportedEncodingException; import static java.nio.charset.StandardCharsets.UTF_8; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; /* @test * @summary Test creating a String from a bytebuffer * @run testng StringByteBufferConversions */ public class StringByteBufferConversions { private static final int offset = 5; private String helloWorld = "Hello World"; private ByteBuffer helloWorldBuffer; @BeforeMethod public void setupBuffer() { helloWorldBuffer = ByteBuffer.wrap(helloWorld.getBytes(UTF_8)); } @Test(expectedExceptions = NullPointerException.class) public void nullByteBufferWithCharsetShouldThrowException() { new String((ByteBuffer) null, UTF_8); } @Test(expectedExceptions = NullPointerException.class) public void nullByteBufferWithCharsetNameShouldThrowException() throws UnsupportedEncodingException { new String((ByteBuffer) null, "UTF-8"); } @Test(expectedExceptions = UnsupportedEncodingException.class) public void unsupportedEncodingsShouldThrowException() throws UnsupportedEncodingException { new String(ByteBuffer.allocateDirect(20), "UTF_1234"); } @Test(expectedExceptions = NullPointerException.class) public void nullCharsetShouldThrowException() { // given ByteBuffer buffer = ByteBuffer.allocateDirect(20); // when new String(buffer, (Charset) null); } @Test(expectedExceptions = NullPointerException.class) public void nullCharsetNameShouldThrowException() throws UnsupportedEncodingException { // given ByteBuffer buffer = ByteBuffer.allocateDirect(20); // when new String(buffer, (String) null); } @Test public void heapByteBuffersCanBeConvertedToStringsWithCharsets() { // when String str = new String(helloWorldBuffer, UTF_8); // then assertEquals(str, helloWorld); assertEquals(helloWorldBuffer.position(), 11); } @Test public void heapByteBuffersCanBeConvertedToStringsWithCharsetNames() throws UnsupportedEncodingException { // when String str = new String(helloWorldBuffer, "UTF-8"); // then assertEquals(str, helloWorld); assertEquals(helloWorldBuffer.position(), 11); } @Test public void directByteBuffersCanBeConvertedToStringsWithCharsets() { // given ByteBuffer buffer = getHelloWorldDirectBuffer(); // when String str = new String(buffer, UTF_8); // then assertEquals(str, helloWorld); assertEquals(buffer.position(), buffer.capacity()); } @Test public void directByteBuffersCanBeConvertedToStringsWithCharsetNames() throws UnsupportedEncodingException { // given ByteBuffer buffer = getHelloWorldDirectBuffer(); // when String str = new String(buffer, "UTF-8"); // then assertEquals(str, helloWorld); assertEquals(buffer.position(), buffer.capacity()); } private ByteBuffer getHelloWorldDirectBuffer() { byte[] bytes = helloWorld.getBytes(UTF_8); ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length) .put(bytes, 0, bytes.length); buffer.position(0); return buffer; } @Test public void chunksOfByteBuffersCanBeConvertedToStringsWithCharsets() { // given byte[] bytes = helloWorld.getBytes(UTF_8); ByteBuffer buffer = writeHelloWorldToSubsectionOfByteBuffer(bytes); // when String str = new String(buffer, UTF_8); // then assertEquals(str, helloWorld); assertEquals(buffer.position(), offset + bytes.length); } @Test public void chunksOfByteBuffersCanBeConvertedToStringsWithCharsetNames() throws UnsupportedEncodingException { // given byte[] bytes = helloWorld.getBytes(UTF_8); ByteBuffer buffer = writeHelloWorldToSubsectionOfByteBuffer(bytes); // when String str = new String(buffer, "UTF-8"); // then assertEquals(str, helloWorld); assertEquals(buffer.position(), offset + bytes.length); } private ByteBuffer writeHelloWorldToSubsectionOfByteBuffer(byte[] bytes) { 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); return buffer; } @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, (Charset) null); } @Test(expectedExceptions = NullPointerException.class) public void getBytesThrowsExceptionWithNullCharsetName() throws UnsupportedEncodingException { byte[] bytes = new byte[16]; "".getBytes(bytes, 0, (String) null); } @Test public void shouldEncodeStringsOntoByteArraysGivenCharset() { 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 shouldEncodeStringsOntoByteArraysGivenCharsetName() throws UnsupportedEncodingException { 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 shouldEncodeStringsOntoByteBufferGivenCharset() { 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 shouldEncodeStringsOntoByteBufferGivenCharsetName() throws UnsupportedEncodingException { 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 shouldEncodeStringsOntoChunkOfByteArrayGivenCharset() { 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 shouldEncodeStringsOntoChunkOfByteArrayGivenCharsetName() throws UnsupportedEncodingException { 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 shouldEncodeStringsOntoChunkOfByteBufferGivenCharset() { 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"); } @Test public void shouldEncodeStringsOntoChunkOfByteBufferGivenCharsetName() throws UnsupportedEncodingException { 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"); } }