diff --git a/src/main/java/net/datafaker/providers/base/Company.java b/src/main/java/net/datafaker/providers/base/Company.java index c04ea4ebf..1c5cc9686 100644 --- a/src/main/java/net/datafaker/providers/base/Company.java +++ b/src/main/java/net/datafaker/providers/base/Company.java @@ -82,25 +82,22 @@ public String logo() { } public String url() { - return String.join(".", - "www", - FakerIDN.toASCII(domainName()), - domainSuffix() - ); + return "www." + + FakerIDN.toASCII(domainName()) + "." + + domainSuffix(); } private String domainName() { - String value = name().toLowerCase(faker.getContext().getLocale()); - StringBuilder sb = new StringBuilder(value.length()); - for (int i = 0; i < value.length(); i++) { - char c = value.charAt(i); + final char[] res = name().toLowerCase(faker.getContext().getLocale()).toCharArray(); + int offset = 0; + for (int i = 0; i < res.length; i++) { + final char c = res[i]; switch (c) { - case '.', ',', '\'', ' ', ']' -> { - } - default -> sb.append(c); + case '.', ',', '\'', ' ', ']' -> offset++; + default -> res[i - offset] = res[i]; } } - return sb.toString(); + return String.valueOf(res, 0, res.length - offset); } private String domainSuffix() { diff --git a/src/test/java/net/datafaker/providers/base/CompanyTest.java b/src/test/java/net/datafaker/providers/base/CompanyTest.java index 78decc303..4e22b406b 100644 --- a/src/test/java/net/datafaker/providers/base/CompanyTest.java +++ b/src/test/java/net/datafaker/providers/base/CompanyTest.java @@ -7,9 +7,12 @@ import java.util.List; import java.util.Collection; +import java.util.regex.Pattern; class CompanyTest extends BaseFakerTest { + public static final Pattern URL_PATTERN = Pattern.compile("(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])"); + public static final Pattern PHRASE_PATTERN = Pattern.compile("(\\w+[ /-]?){1,9}"); private final Company company = faker.company(); @Test @@ -31,12 +34,12 @@ void testBuzzword() { @Test void testCatchPhrase() { - assertThat(company.catchPhrase()).matches("(\\w+[ /-]?){1,9}"); + assertThat(company.catchPhrase()).matches(PHRASE_PATTERN); } @Test void testBs() { - assertThat(company.bs()).matches("(\\w+[ /-]?){1,9}"); + assertThat(company.bs()).matches(PHRASE_PATTERN); } @Test @@ -46,7 +49,6 @@ void testLogo() { @RepeatedTest(100) void testUrl() { - String regexp = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])"; - assertThat(company.url()).matches(regexp); + assertThat(company.url()).matches(URL_PATTERN); } }