Permalink
Browse files

Add global random stuff generator for tests.

Findbugs bugs me that I should use a single Random instance instead of creating
a new instance everytime I need one ... BsonRandom for the rescue! This is a
simple wrapper around Java's Random.

 - Add BsonRandom.
 - Update tests that use Java's Random directly to use BsonRandom instead.
  • Loading branch information...
1 parent a94c000 commit 7b2f13c5807ca10109e45530b2dd53f2053b555d @kohanyirobert committed Jan 22, 2012
@@ -0,0 +1,42 @@
+package com.github.kohanyirobert.ebson;
+
+import java.util.Random;
+
+public final class BsonRandom {
+
+ private static final Random RANDOM = new Random();
+
+ private BsonRandom() {}
+
+ public static void nextBytes(byte[] bytes) {
+ RANDOM.nextBytes(bytes);
+ }
+
+ public static int nextInt() {
+ return RANDOM.nextInt();
+ }
+
+ public static int nextInt(int n) {
+ return RANDOM.nextInt(n);
+ }
+
+ public static long nextLong() {
+ return RANDOM.nextLong();
+ }
+
+ public static boolean nextBoolean() {
+ return RANDOM.nextBoolean();
+ }
+
+ public static float nextFloat() {
+ return RANDOM.nextFloat();
+ }
+
+ public static double nextDouble() {
+ return RANDOM.nextDouble();
+ }
+
+ public static double nextGaussian() {
+ return RANDOM.nextGaussian();
+ }
+}
@@ -4,23 +4,25 @@
import org.junit.Test;
-import java.util.Random;
-
public final class DefaultBinaryReaderWriterTest extends AbstractReaderWriterTest {
- private static final int BUFFER_SIZE = 1024;
- private static final Random RANDOM = new Random();
- private static final ThreadLocal<byte[]> RANDOM_BYTES = new ThreadLocal<byte[]>();
+ private static final int BINARY_LENGTH = 1024;
+ private static final ThreadLocal<byte[]> RANDOM_BINARY = new ThreadLocal<byte[]>() {
+
+ @Override
+ protected byte[] initialValue() {
+ byte[] bytes = new byte[BINARY_LENGTH];
+ BsonRandom.nextBytes(bytes);
+ return bytes;
+ }
+ };
public DefaultBinaryReaderWriterTest() {
super(DefaultReader.BINARY, DefaultWriter.BINARY);
- byte[] bytes = new byte[BUFFER_SIZE];
- RANDOM.nextBytes(bytes);
- RANDOM_BYTES.set(bytes);
}
@Test
public void primitiveBytes() {
- assertArrayEquals((byte[]) writeTo(RANDOM_BYTES.get()), (byte[]) readFrom());
+ assertArrayEquals((byte[]) writeTo(RANDOM_BINARY.get()), (byte[]) readFrom());
}
}
@@ -6,17 +6,16 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import java.util.Random;
public final class DefaultObjectIdReaderWriterTest extends AbstractReaderWriterTest {
+ private static final int OBJECT_ID_LENGTH = 12;
private static final ThreadLocal<ByteBuffer> RANDOM_OBJECT_ID = new ThreadLocal<ByteBuffer>() {
@Override
protected ByteBuffer initialValue() {
- // @checkstyle:off MagicNumber
- byte[] bytes = new byte[12];
- new Random().nextBytes(bytes);
+ byte[] bytes = new byte[OBJECT_ID_LENGTH];
+ BsonRandom.nextBytes(bytes);
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
}
};
@@ -6,17 +6,16 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import java.util.Random;
public final class DefaultTimestampReaderWriterTest extends AbstractReaderWriterTest {
+ private static final int TIMESTAMP_LENGTH = 8;
private static final ThreadLocal<ByteBuffer> RANDOM_TIMESTAMP = new ThreadLocal<ByteBuffer>() {
@Override
protected ByteBuffer initialValue() {
- // @checkstyle:off MagicNumber
- byte[] bytes = new byte[8];
- new Random().nextBytes(bytes);
+ byte[] bytes = new byte[TIMESTAMP_LENGTH];
+ BsonRandom.nextBytes(bytes);
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
}
};

0 comments on commit 7b2f13c

Please sign in to comment.