From 6000dd8a3c4ca3f156d5de2ebf67ab5185850627 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 2 Jul 2023 16:01:51 -0400 Subject: [PATCH] Try to eliminate random failures which did happen on GH with a 100 limit --- .../commons/lang3/RandomStringUtilsTest.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java index 2ec4092e2..cf02875c1 100644 --- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java @@ -37,10 +37,12 @@ import java.util.Random; import org.junit.jupiter.api.Test; /** - * Unit tests {@link org.apache.commons.lang3.RandomStringUtils}. + * Tests {@link org.apache.commons.lang3.RandomStringUtils}. */ public class RandomStringUtilsTest extends AbstractLangTest { + private static final int LOOP_COUNT = 1_000; + @Test public void testConstructor() { assertNotNull(new RandomStringUtils()); @@ -195,7 +197,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { public void testRandomAlphaNumeric() { final char[] testChars = {'a', 'z', 'A', 'Z', '0', '9'}; final boolean[] found = {false, false, false, false, false, false}; - for (int i = 0; i < 100; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String randString = RandomStringUtils.randomAlphanumeric(10); for (int j = 0; j < testChars.length; j++) { if (randString.indexOf(testChars[j]) > 0) { @@ -218,7 +220,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { public void testRandomNumeric() { final char[] testChars = {'0', '9'}; final boolean[] found = {false, false}; - for (int i = 0; i < 100; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String randString = RandomStringUtils.randomNumeric(10); for (int j = 0; j < testChars.length; j++) { if (randString.indexOf(testChars[j]) > 0) { @@ -241,7 +243,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { public void testRandomAlphabetic() { final char[] testChars = {'a', 'z', 'A', 'Z'}; final boolean[] found = {false, false, false, false}; - for (int i = 0; i < 100; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String randString = RandomStringUtils.randomAlphabetic(10); for (int j = 0; j < testChars.length; j++) { if (randString.indexOf(testChars[j]) > 0) { @@ -265,7 +267,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { final char[] testChars = {(char) 32, (char) 126}; final boolean[] found = {false, false}; // Test failures have been observed on GitHub builds with a 100 limit. - for (int i = 0; i < 1_000; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String randString = RandomStringUtils.randomAscii(10); for (int j = 0; j < testChars.length; j++) { if (randString.indexOf(testChars[j]) > 0) { @@ -288,7 +290,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String s = RandomStringUtils.randomAscii(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s.matches(pattern), s); @@ -313,7 +315,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String s = RandomStringUtils.randomAlphabetic(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s.matches(pattern), s); @@ -338,7 +340,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String s = RandomStringUtils.randomAlphanumeric(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s.matches(pattern), s); @@ -363,7 +365,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String s = RandomStringUtils.randomGraph(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s.matches(pattern), s); @@ -388,7 +390,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String s = RandomStringUtils.randomNumeric(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s.matches(pattern), s); @@ -413,7 +415,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < LOOP_COUNT; i++) { final String s = RandomStringUtils.randomPrint(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s.matches(pattern), s); @@ -433,7 +435,7 @@ public class RandomStringUtilsTest extends AbstractLangTest { /** * Test homogeneity of random strings generated -- * i.e., test that characters show up with expected frequencies - * in generated strings. Will fail randomly about 1 in 1000 times. + * in generated strings. Will fail randomly about 1 in LOOP_COUNT times. * Repeated failures indicate a problem. */ @Test @@ -443,7 +445,8 @@ public class RandomStringUtilsTest extends AbstractLangTest { String gen = ""; final int[] counts = {0, 0, 0}; final int[] expected = {200, 200, 200}; - for (int i = 0; i< 100; i++) { + // More likely to fail for 1000? + for (int i = 0; i < 100; i++) { gen = RandomStringUtils.random(6, chars); for (int j = 0; j < 6; j++) { switch (gen.charAt(j)) {