Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 7b2f13c5807ca10109e45530b2dd53f2053b555d 1 parent a94c000
Kohányi Róbert authored
42  src/test/java/com/github/kohanyirobert/ebson/BsonRandom.java
... ...
@@ -0,0 +1,42 @@
  1
+package com.github.kohanyirobert.ebson;
  2
+
  3
+import java.util.Random;
  4
+
  5
+public final class BsonRandom {
  6
+
  7
+  private static final Random RANDOM = new Random();
  8
+
  9
+  private BsonRandom() {}
  10
+
  11
+  public static void nextBytes(byte[] bytes) {
  12
+    RANDOM.nextBytes(bytes);
  13
+  }
  14
+
  15
+  public static int nextInt() {
  16
+    return RANDOM.nextInt();
  17
+  }
  18
+
  19
+  public static int nextInt(int n) {
  20
+    return RANDOM.nextInt(n);
  21
+  }
  22
+
  23
+  public static long nextLong() {
  24
+    return RANDOM.nextLong();
  25
+  }
  26
+
  27
+  public static boolean nextBoolean() {
  28
+    return RANDOM.nextBoolean();
  29
+  }
  30
+
  31
+  public static float nextFloat() {
  32
+    return RANDOM.nextFloat();
  33
+  }
  34
+
  35
+  public static double nextDouble() {
  36
+    return RANDOM.nextDouble();
  37
+  }
  38
+
  39
+  public static double nextGaussian() {
  40
+    return RANDOM.nextGaussian();
  41
+  }
  42
+}
20  src/test/java/com/github/kohanyirobert/ebson/DefaultBinaryReaderWriterTest.java
@@ -4,23 +4,25 @@
4 4
 
5 5
 import org.junit.Test;
6 6
 
7  
-import java.util.Random;
8  
-
9 7
 public final class DefaultBinaryReaderWriterTest extends AbstractReaderWriterTest {
10 8
 
11  
-  private static final int BUFFER_SIZE = 1024;
12  
-  private static final Random RANDOM = new Random();
13  
-  private static final ThreadLocal<byte[]> RANDOM_BYTES = new ThreadLocal<byte[]>();
  9
+  private static final int BINARY_LENGTH = 1024;
  10
+  private static final ThreadLocal<byte[]> RANDOM_BINARY = new ThreadLocal<byte[]>() {
  11
+
  12
+    @Override
  13
+    protected byte[] initialValue() {
  14
+      byte[] bytes = new byte[BINARY_LENGTH];
  15
+      BsonRandom.nextBytes(bytes);
  16
+      return bytes;
  17
+    }
  18
+  };
14 19
 
15 20
   public DefaultBinaryReaderWriterTest() {
16 21
     super(DefaultReader.BINARY, DefaultWriter.BINARY);
17  
-    byte[] bytes = new byte[BUFFER_SIZE];
18  
-    RANDOM.nextBytes(bytes);
19  
-    RANDOM_BYTES.set(bytes);
20 22
   }
21 23
 
22 24
   @Test
23 25
   public void primitiveBytes() {
24  
-    assertArrayEquals((byte[]) writeTo(RANDOM_BYTES.get()), (byte[]) readFrom());
  26
+    assertArrayEquals((byte[]) writeTo(RANDOM_BINARY.get()), (byte[]) readFrom());
25 27
   }
26 28
 }
7  src/test/java/com/github/kohanyirobert/ebson/DefaultObjectIdReaderWriterTest.java
@@ -6,17 +6,16 @@
6 6
 
7 7
 import java.nio.ByteBuffer;
8 8
 import java.nio.ByteOrder;
9  
-import java.util.Random;
10 9
 
11 10
 public final class DefaultObjectIdReaderWriterTest extends AbstractReaderWriterTest {
12 11
 
  12
+  private static final int OBJECT_ID_LENGTH = 12;
13 13
   private static final ThreadLocal<ByteBuffer> RANDOM_OBJECT_ID = new ThreadLocal<ByteBuffer>() {
14 14
 
15 15
     @Override
16 16
     protected ByteBuffer initialValue() {
17  
-      // @checkstyle:off MagicNumber
18  
-      byte[] bytes = new byte[12];
19  
-      new Random().nextBytes(bytes);
  17
+      byte[] bytes = new byte[OBJECT_ID_LENGTH];
  18
+      BsonRandom.nextBytes(bytes);
20 19
       return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
21 20
     }
22 21
   };
7  src/test/java/com/github/kohanyirobert/ebson/DefaultTimestampReaderWriterTest.java
@@ -6,17 +6,16 @@
6 6
 
7 7
 import java.nio.ByteBuffer;
8 8
 import java.nio.ByteOrder;
9  
-import java.util.Random;
10 9
 
11 10
 public final class DefaultTimestampReaderWriterTest extends AbstractReaderWriterTest {
12 11
 
  12
+  private static final int TIMESTAMP_LENGTH = 8;
13 13
   private static final ThreadLocal<ByteBuffer> RANDOM_TIMESTAMP = new ThreadLocal<ByteBuffer>() {
14 14
 
15 15
     @Override
16 16
     protected ByteBuffer initialValue() {
17  
-      // @checkstyle:off MagicNumber
18  
-      byte[] bytes = new byte[8];
19  
-      new Random().nextBytes(bytes);
  17
+      byte[] bytes = new byte[TIMESTAMP_LENGTH];
  18
+      BsonRandom.nextBytes(bytes);
20 19
       return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
21 20
     }
22 21
   };

0 notes on commit 7b2f13c

Please sign in to comment.
Something went wrong with that request. Please try again.