1 
   2 import static org.testng.Assert.assertEquals;
   3 
   4 import java.nio.ByteBuffer;
   5 import java.nio.charset.Charset;
   6 import static java.nio.charset.StandardCharsets.UTF_8;
   7 
   8 import org.testng.annotations.Test;
   9 
  10 /* @test
  11  * @summary Test creating a String from a bytebuffer
  12  * @run testng StringByteBufferConversions 
  13  */
  14 public class StringByteBufferConversions {
  15 
  16     @Test(expectedExceptions = NullPointerException.class)
  17     public void nullByteBufferShouldThrowException() {
  18         // given
  19         ByteBuffer buffer = null;
  20 
  21         // when
  22         new String(buffer, UTF_8);
  23     }
  24 
  25     @Test(expectedExceptions = NullPointerException.class)
  26     public void nullCharsetShouldThrowException() {
  27         // given
  28         ByteBuffer buffer = ByteBuffer.allocateDirect(20);
  29 
  30         // when
  31         new String(buffer, null);
  32     }
  33     
  34     @Test
  35     public void heapByteBuffersCanBeConvertedToStrings() {
  36         // given
  37         String expected = "Hello World";
  38         ByteBuffer buffer = ByteBuffer.wrap(expected.getBytes(UTF_8));
  39 
  40         // when
  41         String str = new String(buffer, UTF_8);
  42 
  43         // then
  44         assertEquals(str, expected);
  45     }
  46     
  47     @Test
  48     public void directByteBuffersCanBeConvertedToStrings() {
  49         // given
  50         String expected = "Hello World";
  51         byte[] bytes = expected.getBytes(UTF_8);
  52         ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length)
  53                                       .put(bytes, 0, bytes.length);
  54 
  55         buffer.position(0);
  56 
  57         // when
  58         String str = new String(buffer, UTF_8);
  59 
  60         // then
  61         assertEquals(str, expected);
  62     }
  63     
  64     @Test
  65     public void chunksOfByteBuffersCanBeConvertedToStrings() {
  66         // given
  67         String expected = "Hello World";
  68         byte[] bytes = expected.getBytes(UTF_8);
  69         int offset = 5;
  70         int length = offset + bytes.length + 5;
  71         ByteBuffer buffer = ByteBuffer.allocateDirect(length);
  72         buffer.position(offset);
  73         buffer.put(bytes, 0, bytes.length);
  74         
  75         buffer.position(offset);
  76         buffer.limit(offset + bytes.length);
  77 
  78         // when
  79         String str = new String(buffer, UTF_8);
  80 
  81         // then
  82         assertEquals(str, expected);
  83     }
  84     
  85     @Test(expectedExceptions = NullPointerException.class)
  86     public void getBytesThrowsExceptionWithNullBuffer() {
  87         "".getBytes((ByteBuffer)null, UTF_8);
  88     }
  89 
  90     @Test(expectedExceptions = NullPointerException.class)
  91     public void getBytesThrowsExceptionWithNullByteArray() {
  92         "".getBytes((byte[])null, 0, UTF_8);
  93     }
  94     
  95     @Test(expectedExceptions = NullPointerException.class)
  96     public void getBytesThrowsExceptionWithNullCharset() {
  97         byte[] bytes = new byte[16];
  98         "".getBytes(bytes, 0, null);
  99     }
 100 
 101     @Test
 102     public void shouldEncodeStringsOntoByteArrays() {
 103         byte[] bytes = new byte[11];
 104         int written = "Hello World".getBytes(bytes, 0, UTF_8);
 105         assertEquals(written, 11);
 106 
 107         assertEquals(new String(bytes, UTF_8), "Hello World");
 108     }
 109 
 110     @Test
 111     public void shouldEncodeStringsOntoByteBuffer() {
 112         ByteBuffer bytes = ByteBuffer.allocateDirect(11);
 113         int written = "Hello World".getBytes(bytes, UTF_8);
 114         assertEquals(written, 11);
 115 
 116         bytes.position(0);
 117         assertEquals(new String(bytes, UTF_8), "Hello World");
 118     }
 119 
 120     @Test
 121     public void shouldEncodeStringsOntoChunkOfByteArray() {
 122         byte[] bytes = new byte[20];
 123         final int offset = 5;
 124         int written = "Hello World".getBytes(bytes, offset, UTF_8);
 125         assertEquals(written, 11);
 126 
 127         assertEquals(new String(bytes, 5, 11, UTF_8), "Hello World");
 128     }
 129 
 130     @Test
 131     public void shouldEncodeStringsOntoChunkOfByteBuffer() {
 132         ByteBuffer bytes = ByteBuffer.allocateDirect(20);
 133         final int offset = 5;
 134         bytes.position(offset);
 135         int written = "Hello World".getBytes(bytes, UTF_8);
 136         assertEquals(written, 11);
 137 
 138         bytes.position(offset);
 139         bytes.limit(offset + 11);
 140         assertEquals(new String(bytes, UTF_8), "Hello World");
 141     }
 142 
 143 }
 144