package org.collectionspace.services.id.part.test;
+import java.util.HashSet;
+
import org.collectionspace.services.id.part.IDPart;
import org.collectionspace.services.id.part.RandomNumberIDPart;
import org.collectionspace.services.id.part.JavaRandomNumberIDPartAlgorithm;
import org.testng.Assert;
-import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class RandomNumberIDPartTest {
IDPart part;
- String firstID;
- String secondID;
- String thirdID;
- @BeforeSuite
- public void setUp() {
- part = new RandomNumberIDPart();
- firstID = part.newID();
- secondID = part.newID();
- thirdID = part.newID();
- }
+ // Repetition factor for generating sufficiently
+ // large numbers of sample pseudorandom numbers.
+ final int REPETITIONS = 5;
@Test
- public void newIDGeneratesNonRepeatingIDs() {
- Assert.assertTrue(firstID.compareTo(secondID) != 0);
- Assert.assertTrue(firstID.compareTo(thirdID) != 0);
- Assert.assertTrue(secondID.compareTo(thirdID) != 0);
+ public void newIDGeneratesSufficientVarietyOfIDs() {
+ String id;
+ part = new RandomNumberIDPart(1000,0);
+ int idsGenerated = 100;
+ HashSet<String> ids = new HashSet<String>();
+ for (int i=0; i < idsGenerated; i++) {
+ id = part.newID();
+ ids.add(id); // Adds only elements not already present.
+ }
+ // A sufficiently high percentage of the pseudorandom numbers
+ // generated must be unique, to confirm apparent randomness.
+ double percentMustBeUnique = 0.9;
+ Assert.assertTrue(ids.size() >= (idsGenerated * percentMustBeUnique));
}
- @Test
- public void isValid() {
- Assert.assertTrue(part.getValidator().isValid(firstID));
- Assert.assertTrue(part.getValidator().isValid(secondID));
- Assert.assertTrue(part.getValidator().isValid(thirdID));
- }
-
- @Test(dependsOnMethods = {"newIDGeneratesNonRepeatingIDs"})
- public void newIDGeneratesNonRepeatingIDsWithSuppliedValues() {
- part = new RandomNumberIDPart(200, 100);
- firstID = part.newID();
- secondID = part.newID();
- thirdID = part.newID();
- Assert.assertTrue(firstID.compareTo(secondID) != 0);
- Assert.assertTrue(firstID.compareTo(thirdID) != 0);
- Assert.assertTrue(secondID.compareTo(thirdID) != 0);
- }
+ // @TODO Consider another test to look at some measure of
+ // even distribution of generated pseudorandom numbers
+ // across a midpoint, or across some bands (e.g. in quartiles).
@Test
- public void highMinAndMaxValues() {
- int minValue = Integer.MAX_VALUE - 1000;
+ public void IDsWithinBoundsOfHighMinAndMaxValues() {
+ int minValue = Integer.MAX_VALUE - 10;
int maxValue = Integer.MAX_VALUE - 2;
String id;
part = new RandomNumberIDPart(maxValue, minValue);
- for (int i=0; i < 20; i++) {
+ // Generate a sufficient number of values that
+ // there is a high probability of generating an
+ // out of bounds value, if any.
+ for (int i=0; i < (((maxValue - minValue) + 1) * REPETITIONS); i++) {
id = part.newID();
Assert.assertTrue(Integer.parseInt(id) >= minValue);
+ Assert.assertTrue(Integer.parseInt(id) <= maxValue);
}
}
// With only maximum value specified.
int maxValue = 20;
String id;
- // Generate a sufficient number of values that
- // there is a high probability of realizing an
- // error condition, if any.
part = new RandomNumberIDPart(maxValue);
- for (int i=0; i < (maxValue * 5); i++) {
+ for (int i=0; i < (maxValue * REPETITIONS); i++) {
id = part.newID();
Assert.assertTrue(Integer.parseInt(id) <= maxValue);
}
// With minimum value also specified.
int minValue = 5;
part = new RandomNumberIDPart(maxValue, minValue);
- for (int i=0; i < ((maxValue - minValue) * 5); i++) {
+ for (int i=0; i < (((maxValue - minValue) + 1) * REPETITIONS); i++) {
id = part.newID();
Assert.assertTrue(Integer.parseInt(id) <= maxValue);
}
int maxValue = 20;
String id;
part = new RandomNumberIDPart(maxValue, minValue);
- for (int i=0; i <= ((maxValue - minValue) * 5); i++) {
+ for (int i=0; i <= (((maxValue - minValue) + 1) * REPETITIONS); i++) {
id = part.newID();
Assert.assertTrue(Integer.parseInt(id) >= minValue);
}
public void maxValueTooHigh() {
part = new RandomNumberIDPart(Integer.MAX_VALUE);
}
+
+ @Test
+ public void isValid() {
+ part = new RandomNumberIDPart();
+ Assert.assertTrue(part.getValidator().isValid(part.newID()));
+ }
}