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         assertEquals(buffer.position(), 11);
  46     }
  47     
  48     @Test
  49     public void directByteBuffersCanBeConvertedToStrings() {
  50         // given
  51         String expected = "Hello World";
  52         byte[] bytes = expected.getBytes(UTF_8);
  53         ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length)
  54                                       .put(bytes, 0, bytes.length);
  55 
  56         buffer.position(0);
  57 
  58         // when
  59         String str = new String(buffer, UTF_8);
  60 
  61         // then
  62         assertEquals(str, expected);
  63         assertEquals(buffer.position(), bytes.length);
  64     }
  65     
  66     @Test
  67     public void chunksOfByteBuffersCanBeConvertedToStrings() {
  68         // given
  69         String expected = "Hello World";
  70         byte[] bytes = expected.getBytes(UTF_8);
  71         int offset = 5;
  72         int length = offset + bytes.length + 5;
  73         ByteBuffer buffer = ByteBuffer.allocateDirect(length);
  74         buffer.position(offset);
  75         buffer.put(bytes, 0, bytes.length);
  76         
  77         buffer.position(offset);
  78         buffer.limit(offset + bytes.length);
  79 
  80         // when
  81         String str = new String(buffer, UTF_8);
  82 
  83         // then
  84         assertEquals(str, expected);
  85         assertEquals(buffer.position(), offset + bytes.length);
  86     }
  87     
  88     @Test(expectedExceptions = NullPointerException.class)
  89     public void getBytesThrowsExceptionWithNullBuffer() {
  90         "".getBytes((ByteBuffer)null, UTF_8);
  91     }
  92 
  93     @Test(expectedExceptions = NullPointerException.class)
  94     public void getBytesThrowsExceptionWithNullByteArray() {
  95         "".getBytes((byte[])null, 0, UTF_8);
  96     }
  97     
  98     @Test(expectedExceptions = NullPointerException.class)
  99     public void getBytesThrowsExceptionWithNullCharset() {
 100         byte[] bytes = new byte[16];
 101         "".getBytes(bytes, 0, null);
 102     }
 103 
 104     @Test
 105     public void shouldEncodeStringsOntoByteArrays() {
 106         byte[] bytes = new byte[11];
 107         int written = "Hello World".getBytes(bytes, 0, UTF_8);
 108         assertEquals(written, 11);
 109 
 110         assertEquals(new String(bytes, UTF_8), "Hello World");
 111     }
 112 
 113     @Test
 114     public void shouldEncodeStringsOntoByteBuffer() {
 115         ByteBuffer bytes = ByteBuffer.allocateDirect(11);
 116         int written = "Hello World".getBytes(bytes, UTF_8);
 117         assertEquals(written, 11);
 118         assertEquals(bytes.position(), 11);
 119 
 120         bytes.position(0);
 121         assertEquals(new String(bytes, UTF_8), "Hello World");
 122     }
 123 
 124     @Test
 125     public void shouldEncodeStringsOntoChunkOfByteArray() {
 126         byte[] bytes = new byte[20];
 127         final int offset = 5;
 128         int written = "Hello World".getBytes(bytes, offset, UTF_8);
 129         assertEquals(written, 11);
 130 
 131         assertEquals(new String(bytes, 5, 11, UTF_8), "Hello World");
 132     }
 133 
 134     @Test
 135     public void shouldEncodeStringsOntoChunkOfByteBuffer() {
 136         ByteBuffer bytes = ByteBuffer.allocateDirect(20);
 137         final int offset = 5;
 138         bytes.position(offset);
 139         int written = "Hello World".getBytes(bytes, UTF_8);
 140         assertEquals(written, 11);
 141         assertEquals(bytes.position(), 11 + offset);
 142 
 143         bytes.position(offset);
 144         bytes.limit(offset + 11);
 145         assertEquals(new String(bytes, UTF_8), "Hello World");
 146     }
 147 
 148 }
 149