]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3349b9ecb8470995bfb0ca86618f7dabc2c0ce27
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.id.part.test;
2
3 import java.util.HashSet;
4
5 import org.collectionspace.services.id.part.IDPart;
6 import org.collectionspace.services.id.part.RandomNumberIDPart;
7 import org.collectionspace.services.id.part.JavaRandomNumberIDPartAlgorithm;
8
9 import org.testng.Assert;
10 import org.testng.annotations.Test;
11
12 public class RandomNumberIDPartTest {
13
14     IDPart part;
15
16     // Repetition factor for generating sufficiently
17     // large numbers of sample pseudorandom numbers.
18     final int REPETITIONS = 5;
19
20     @Test
21     public void newIDGeneratesSufficientVarietyOfIDs() {
22         String id;
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++) {
27             id = part.newID();
28             ids.add(id); // Adds only elements not already present.
29         }
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));
34     }
35
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).
39
40     @Test
41     public void IDsWithinBoundsOfHighMinAndMaxValues() {
42         int minValue = Integer.MAX_VALUE - 10;
43         int maxValue = Integer.MAX_VALUE - 2;
44         String id;
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++) {
50             id = part.newID();
51             Assert.assertTrue(Integer.parseInt(id) >= minValue);
52             Assert.assertTrue(Integer.parseInt(id) <= maxValue);
53         }
54     }
55
56     @Test
57     public void newIDsLessThanOrEqualToSuppliedMaxValue() {
58
59         // With only maximum value specified.
60         int maxValue = 20;
61         String id;
62         part = new RandomNumberIDPart(maxValue);
63         for (int i=0; i < (maxValue * REPETITIONS); i++) {
64             id = part.newID();
65             Assert.assertTrue(Integer.parseInt(id) <= maxValue);
66         }
67
68         // With minimum value also specified.
69         int minValue = 5;
70         part = new RandomNumberIDPart(maxValue, minValue);
71         for (int i=0; i < (((maxValue - minValue) + 1) * REPETITIONS); i++) {
72             id = part.newID();
73             Assert.assertTrue(Integer.parseInt(id) <= maxValue);
74         }
75     }
76
77     @Test(dependsOnMethods = {"newIDsLessThanOrEqualToSuppliedMaxValue"})
78     public void newIDsHigherThanOrEqualToSuppliedMinValue() {
79         int minValue = 5;
80         int maxValue = 20;
81         String id;
82         part = new RandomNumberIDPart(maxValue, minValue);
83         for (int i=0; i <= (((maxValue - minValue) + 1) * REPETITIONS); i++) {
84             id = part.newID();
85             Assert.assertTrue(Integer.parseInt(id) >= minValue);
86         }
87     }
88
89     @Test
90     public void defaultMaxValue() {
91         part = new RandomNumberIDPart(
92             JavaRandomNumberIDPartAlgorithm.DEFAULT_MAX_VALUE);
93         part.newID();
94     }
95
96      @Test(dependsOnMethods = {"defaultMaxValue"})
97     public void defaultMinValue() {
98         part = new RandomNumberIDPart(
99             JavaRandomNumberIDPartAlgorithm.DEFAULT_MAX_VALUE,
100             JavaRandomNumberIDPartAlgorithm.DEFAULT_MIN_VALUE);
101         part.newID();
102     }
103
104     @Test(expectedExceptions = IllegalArgumentException.class)
105     public void maxValueTooHigh() {
106         int maxValue = Integer.MAX_VALUE; // Value too high
107         part = new RandomNumberIDPart(maxValue);
108     }
109
110     @Test(expectedExceptions = IllegalArgumentException.class)
111     public void minValueTooLow() {
112         int maxValue = 10;
113         int minValue = -1; // Value too low
114         part = new RandomNumberIDPart(maxValue, minValue);
115     }
116
117     @Test
118     public void isValid() {
119         part = new RandomNumberIDPart();
120         Assert.assertTrue(part.getValidator().isValid(part.newID()));
121     }
122 }