-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent duplicated random BSNs in one test run
Fixes #61
- Loading branch information
Showing
3 changed files
with
122 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package nl.hsac.fitnesse.symbols; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
|
||
public class RandomBsnTest { | ||
@Test | ||
public void ensureNoDuplicates() { | ||
Set<String> generatedBsns = new HashSet<>(); | ||
for (int i = 0; i < 10000; i++) { | ||
String bsn = new RandomBsn().toTarget(null, null); | ||
assertFalse("Duplicated value at loop: " + i, generatedBsns.contains(bsn)); | ||
generatedBsns.add(bsn); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,63 @@ | ||
package nl.hsac.fitnesse.util; | ||
|
||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Tests BsnUtil. | ||
*/ | ||
public class BsnUtilTest { | ||
private final BsnUtil generator = new BsnUtil(); | ||
|
||
@After | ||
public void resetGenerator() { | ||
generator.resetExcludedBsns(); | ||
} | ||
|
||
/** | ||
* Tests basic generation. | ||
*/ | ||
@Test | ||
public void testGenerate() { | ||
for (int i = 0; i < 100; i++) { | ||
for (int i = 0; i < 1000; i++) { | ||
String result = generator.generateBsn(); | ||
assertEquals("Got: " + result, 9, result.length()); | ||
assertTrue("Got: " + result, generator.testBsn(result)); | ||
} | ||
} | ||
|
||
@Test | ||
public void ensureNoDuplicates() { | ||
Set<String> generatedBsns = new HashSet<>(); | ||
for (int i = 0; i < 100_000; i++) { | ||
String result = generator.generateBsn(); | ||
assertFalse("Duplicated value at loop: " + i, generatedBsns.contains(result)); | ||
generatedBsns.add(result); | ||
} | ||
|
||
assertEquals(generatedBsns, generator.getBsnsToExclude()); | ||
} | ||
|
||
@Test | ||
public void resetClears() { | ||
generator.generateBsn(); | ||
assertFalse(generator.getBsnsToExclude().isEmpty()); | ||
|
||
generator.resetExcludedBsns(); | ||
assertTrue(generator.getBsnsToExclude().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testAddBsnToExclude() { | ||
assertFalse(generator.getBsnsToExclude().contains("abc")); | ||
generator.addBsnToExclude("abc"); | ||
assertTrue(generator.getBsnsToExclude().contains("abc")); | ||
} | ||
} |