1 package org.collectionspace.services.id.part.test;
3 import java.util.HashSet;
5 import org.collectionspace.services.id.part.IDPart;
6 import org.collectionspace.services.id.part.RandomNumberIDPart;
7 import org.collectionspace.services.id.part.JavaRandomNumberIDPartAlgorithm;
9 import org.testng.Assert;
10 import org.testng.annotations.Test;
12 public class RandomNumberIDPartTest {
16 // Repetition factor for generating sufficiently
17 // large numbers of sample pseudorandom numbers.
18 final int REPETITIONS = 5;
21 public void newIDGeneratesSufficientVarietyOfIDs() {
23 part = new RandomNumberIDPart(1000,0);
24 int idsGenerated = 100;
25 HashSet<String> ids = new HashSet<String>();
26 for (int i=0; i < idsGenerated; i++) {
28 ids.add(id); // Adds only elements not already present.
30 // A sufficiently high percentage of the pseudorandom numbers
31 // generated must be unique, to confirm apparent randomness.
32 double percentMustBeUnique = 0.9;
33 Assert.assertTrue(ids.size() >= (idsGenerated * percentMustBeUnique));
36 // @TODO Consider another test to look at some measure of
37 // even distribution of generated pseudorandom numbers
38 // across a midpoint, or across some bands (e.g. in quartiles).
41 public void IDsWithinBoundsOfHighMinAndMaxValues() {
42 int minValue = Integer.MAX_VALUE - 10;
43 int maxValue = Integer.MAX_VALUE - 2;
45 part = new RandomNumberIDPart(maxValue, minValue);
46 // Generate a sufficient number of values that
47 // there is a high probability of generating an
48 // out of bounds value, if any.
49 for (int i=0; i < (((maxValue - minValue) + 1) * REPETITIONS); i++) {
51 Assert.assertTrue(Integer.parseInt(id) >= minValue);
52 Assert.assertTrue(Integer.parseInt(id) <= maxValue);
57 public void newIDsLessThanOrEqualToSuppliedMaxValue() {
59 // With only maximum value specified.
62 part = new RandomNumberIDPart(maxValue);
63 for (int i=0; i < (maxValue * REPETITIONS); i++) {
65 Assert.assertTrue(Integer.parseInt(id) <= maxValue);
68 // With minimum value also specified.
70 part = new RandomNumberIDPart(maxValue, minValue);
71 for (int i=0; i < (((maxValue - minValue) + 1) * REPETITIONS); i++) {
73 Assert.assertTrue(Integer.parseInt(id) <= maxValue);
77 @Test(dependsOnMethods = {"newIDsLessThanOrEqualToSuppliedMaxValue"})
78 public void newIDsHigherThanOrEqualToSuppliedMinValue() {
82 part = new RandomNumberIDPart(maxValue, minValue);
83 for (int i=0; i <= (((maxValue - minValue) + 1) * REPETITIONS); i++) {
85 Assert.assertTrue(Integer.parseInt(id) >= minValue);
90 public void defaultMaxValue() {
91 part = new RandomNumberIDPart(
92 JavaRandomNumberIDPartAlgorithm.DEFAULT_MAX_VALUE);
96 @Test(dependsOnMethods = {"defaultMaxValue"})
97 public void defaultMinValue() {
98 part = new RandomNumberIDPart(
99 JavaRandomNumberIDPartAlgorithm.DEFAULT_MAX_VALUE,
100 JavaRandomNumberIDPartAlgorithm.DEFAULT_MIN_VALUE);
104 @Test(expectedExceptions = IllegalArgumentException.class)
105 public void maxValueTooHigh() {
106 int maxValue = Integer.MAX_VALUE; // Value too high
107 part = new RandomNumberIDPart(maxValue);
110 @Test(expectedExceptions = IllegalArgumentException.class)
111 public void minValueTooLow() {
113 int minValue = -1; // Value too low
114 part = new RandomNumberIDPart(maxValue, minValue);
118 public void isValid() {
119 part = new RandomNumberIDPart();
120 Assert.assertTrue(part.getValidator().isValid(part.newID()));