TraceAndSpanIdGenerator { protected static long convertBytesToLong(byte[] byteArray) { if (byteArray.length != 8) throw new IllegalArgumentException("byteArray must be 8 bytes in length"); long longVal = 0; for (int i=0; i<8; i++) longVal = (longVal << 8) | (byteArray[i] & 0xff); return longVal; } private  TraceAndSpanIdGenerator(); static String generateId(); static long generate64BitRandomLong(); static String longToUnsignedLowerHexString(long primitiveLong); static long unsignedLowerHexStringToLong(String hexString); static long unsignedLowerHexStringToLong(String hexString, int index);  }
@Test public void convertBytesToLong_should_work_correctly_for_known_value() { final long EXPECTED_LONG_VALUE = 4242424242L; ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / 8); buffer.putLong(EXPECTED_LONG_VALUE); byte[] longAsByteArray = buffer.array(); assertThat(longAsByteArray.length).isEqualTo(8); long returnVal = TraceAndSpanIdGenerator.convertBytesToLong(longAsByteArray); assertThat(returnVal).isEqualTo(EXPECTED_LONG_VALUE); }
@Test(expected = IllegalArgumentException.class) public void convertBytesToLong_should_explode_if_byte_array_is_less_than_8_bytes() { byte[] badByteArray = new byte[]{0, 0, 0, 0, 0, 0, 1}; assertThat(badByteArray.length).isLessThan(8); TraceAndSpanIdGenerator.convertBytesToLong(badByteArray); fail("Expected IllegalArgumentException but none was thrown"); }
@Test(expected = IllegalArgumentException.class) public void convertBytesToLong_should_explode_if_byte_array_is_more_than_8_bytes() { byte[] badByteArray = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1}; assertThat(badByteArray.length).isGreaterThan(8); TraceAndSpanIdGenerator.convertBytesToLong(badByteArray); fail("Expected IllegalArgumentException but none was thrown"); }