From: Aron Roberts Date: Sat, 21 Nov 2009 01:45:29 +0000 (+0000) Subject: CSPACE-234,CSPACE-364: Ongoing work on revising interfaces for ID Parts, still in... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=6a48a2c769a0b822c63c35aeff7add68cce9fd7f;p=tmp%2Fjakarta-migration.git CSPACE-234,CSPACE-364: Ongoing work on revising interfaces for ID Parts, still in progress. Minor improvements to test cases for one part type. --- diff --git a/services/id/service/src/test/java/org/collectionspace/services/id/part/test/RandomNumberIDPartTest.java b/services/id/service/src/test/java/org/collectionspace/services/id/part/test/RandomNumberIDPartTest.java index 478c85a55..bfb3f204b 100644 --- a/services/id/service/src/test/java/org/collectionspace/services/id/part/test/RandomNumberIDPartTest.java +++ b/services/id/service/src/test/java/org/collectionspace/services/id/part/test/RandomNumberIDPartTest.java @@ -1,62 +1,55 @@ 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 ids = new HashSet(); + 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); } } @@ -66,11 +59,8 @@ public class RandomNumberIDPartTest { // 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); } @@ -78,7 +68,7 @@ public class RandomNumberIDPartTest { // 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); } @@ -90,7 +80,7 @@ public class RandomNumberIDPartTest { 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); } @@ -107,4 +97,10 @@ public class RandomNumberIDPartTest { public void maxValueTooHigh() { part = new RandomNumberIDPart(Integer.MAX_VALUE); } + + @Test + public void isValid() { + part = new RandomNumberIDPart(); + Assert.assertTrue(part.getValidator().isValid(part.newID())); + } }