diff --git a/src/main/java/com/github/javafaker/service/RandomService.java b/src/main/java/com/github/javafaker/service/RandomService.java index d53883524..2d93ac0f0 100644 --- a/src/main/java/com/github/javafaker/service/RandomService.java +++ b/src/main/java/com/github/javafaker/service/RandomService.java @@ -1,5 +1,6 @@ package com.github.javafaker.service; +import java.util.ArrayList; import java.util.Random; public class RandomService { @@ -74,4 +75,40 @@ public String hex(int length) { } return new String(hexChars); } + + public String randomString(int length, char[] characters) { + if (length <= 0) { + return ""; // Keep the existing behavior instead of throwing an error. + } + + StringBuilder randomString = new StringBuilder(); + + for (int i = 0; i < length; i++) { + char nextChar = characters[nextInt(characters.length)]; + randomString.append(nextChar); + } + + return randomString.toString(); + } + + public String randomString(int length) { + String digits = "0123456789"; + String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + String lower = upper.toLowerCase(); + String alphanumeric = upper + lower + digits; + + return randomString(length, alphanumeric.toCharArray()); + } + + public String randomString(int minLength, int maxLength) { + return randomString(nextInt(minLength, maxLength)); + } + + public String randomString(int minLength, int maxLength, char[] characters) { + return randomString(nextInt(minLength, maxLength), characters); + } + + public String randomString() { + return randomString(nextInt(1, 255)); + } } diff --git a/src/test/java/com/github/javafaker/service/RandomServiceTest.java b/src/test/java/com/github/javafaker/service/RandomServiceTest.java index 1e18de1cf..d1932e5e1 100644 --- a/src/test/java/com/github/javafaker/service/RandomServiceTest.java +++ b/src/test/java/com/github/javafaker/service/RandomServiceTest.java @@ -72,4 +72,37 @@ public void testHex() { public void testDefaultHex() { assertThat(randomService.hex(), matchesRegularExpression("^[0-9A-F]{8}$")); } + + @Test + public void testRandomString() { + assertThat(randomService.randomString(8), matchesRegularExpression("^[a-zA-Z0-9]{8}$")); + } + + @Test + public void testRandomStringRandomLength() { + String randomString = randomService.randomString(); + + assertThat(randomString, matchesRegularExpression("^[a-zA-Z0-9]{1,255}$")); + } + + @Test + public void testRandomStringWithMinMaxLength() { + String randomString = randomService.randomString(10, 30); + + assertThat(randomString, matchesRegularExpression("^[a-zA-Z0-9]{10,30}$")); + } + + @Test + public void testRandomStringWithMinMaxLengthAndSpecificCharacters() { + String randomString = randomService.randomString(10, 30, "abc123".toCharArray()); + + assertThat(randomString, matchesRegularExpression("^[abc123]{10,30}$")); + } + + @Test + public void testRandomStringWithLengthAndSpecificCharacters() { + String randomString = randomService.randomString(35, "abc123".toCharArray()); + + assertThat(randomString, matchesRegularExpression("^[abc123]{35}$")); + } }