|
| 1 | +package org.java_websocket.util; |
| 2 | + |
| 3 | +import org.java_websocket.util.ByteBufferUtils; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.nio.ByteBuffer; |
| 7 | + |
| 8 | +import static org.junit.Assert.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * JUnit Test for the new ByteBufferUtils class |
| 12 | + */ |
| 13 | +public class ByteBufferUtilsTest { |
| 14 | + |
| 15 | + private static byte[] smallArray = { 0, -1, -2, -3, -4 }; |
| 16 | + private static byte[] bigArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testEmptyByteBufferCapacity() { |
| 20 | + ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer(); |
| 21 | + assertEquals( "capacity must be 0", byteBuffer.capacity(), 0 ); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testEmptyByteBufferLimit() { |
| 26 | + ByteBuffer byteBuffer = ByteBufferUtils.getEmptyByteBuffer(); |
| 27 | + assertEquals( "limit must be 0", byteBuffer.limit(), 0 ); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testEmptyByteBufferNewObject() { |
| 32 | + ByteBuffer byteBuffer0 = ByteBufferUtils.getEmptyByteBuffer(); |
| 33 | + ByteBuffer byteBuffer1 = ByteBufferUtils.getEmptyByteBuffer(); |
| 34 | + assertTrue( "Allocated new object", byteBuffer0 != byteBuffer1 ); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testTransferByteBufferSmallToEmpty() { |
| 39 | + ByteBuffer small = ByteBuffer.wrap( smallArray ); |
| 40 | + ByteBuffer empty = ByteBufferUtils.getEmptyByteBuffer(); |
| 41 | + ByteBufferUtils.transferByteBuffer( small, empty ); |
| 42 | + assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray ); |
| 43 | + assertEquals( "capacity of the empty bytebuffer should still be 0", empty.capacity(), 0 ); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testTransferByteBufferSmallToBig() { |
| 48 | + ByteBuffer small = ByteBuffer.wrap( smallArray ); |
| 49 | + ByteBuffer big = ByteBuffer.wrap( bigArray ); |
| 50 | + ByteBufferUtils.transferByteBuffer( small, big ); |
| 51 | + assertArrayEquals( "Small bytebuffer should not change", small.array(), smallArray ); |
| 52 | + assertEquals( big.get( 0 ), smallArray[0] ); |
| 53 | + assertEquals( big.get( 1 ), smallArray[1] ); |
| 54 | + assertEquals( big.get( 2 ), smallArray[2] ); |
| 55 | + assertEquals( big.get( 3 ), smallArray[3] ); |
| 56 | + assertEquals( big.get( 4 ), smallArray[4] ); |
| 57 | + assertEquals( big.get( 5 ), bigArray[5] ); |
| 58 | + assertEquals( big.get( 6 ), bigArray[6] ); |
| 59 | + assertEquals( big.get( 7 ), bigArray[7] ); |
| 60 | + assertEquals( big.get( 8 ), bigArray[8] ); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testTransferByteBufferBigToSmall() { |
| 65 | + ByteBuffer small = ByteBuffer.wrap( smallArray ); |
| 66 | + ByteBuffer big = ByteBuffer.wrap( bigArray ); |
| 67 | + ByteBufferUtils.transferByteBuffer( big, small ); |
| 68 | + assert ( small.get( 0 ) == bigArray[0] ); |
| 69 | + assert ( small.get( 1 ) == bigArray[1] ); |
| 70 | + assert ( small.get( 2 ) == bigArray[2] ); |
| 71 | + assert ( small.get( 3 ) == bigArray[3] ); |
| 72 | + assert ( small.get( 4 ) == bigArray[4] ); |
| 73 | + assert ( big.array() == bigArray ); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testTransferByteBufferCheckNull() { |
| 78 | + ByteBuffer source = ByteBufferUtils.getEmptyByteBuffer(); |
| 79 | + ByteBuffer dest = ByteBufferUtils.getEmptyByteBuffer(); |
| 80 | + ByteBufferUtils.transferByteBuffer( source, null ); |
| 81 | + } |
| 82 | +} |
0 commit comments