/** * @return A random long pulled from the full 64-bit random search space (as opposed to the 48 bits of randomness you get from * {@link java.util.Random#nextLong()}). */ public static long generate64BitRandomLong() { byte[] random8Bytes = new byte[8]; random.nextBytes(random8Bytes); return convertBytesToLong(random8Bytes); }
@Test public void convertBytesToLong_should_work_correctly_for_known_value() { // given: byte[] that maps to known long 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); // when: convertBytesToLong() is called long returnVal = TraceAndSpanIdGenerator.convertBytesToLong(longAsByteArray); // then: the return value is what we expect 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"); }