Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add randomString method to RandomService #780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/com/github/javafaker/service/RandomService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.javafaker.service;

import java.util.ArrayList;
import java.util.Random;

public class RandomService {
Expand Down Expand Up @@ -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));
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/github/javafaker/service/RandomServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}$"));
}
}