Skip to content

Commit 8185a04

Browse files
authored
chore(tests): add pattern-based string matching tests and update method expectations (#107)
Signed-off-by: Erik Olsson <olsson.erik1993@gmail.com>
1 parent 245070c commit 8185a04

6 files changed

Lines changed: 122 additions & 11 deletions

File tree

lib/src/main/java/io/github/mangila/ensure4j/Ensure.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Collection;
55
import java.util.Map;
66
import java.util.function.Supplier;
7+
import java.util.regex.Pattern;
78
import org.intellij.lang.annotations.RegExp;
89
import org.jetbrains.annotations.Contract;
910

@@ -1671,6 +1672,62 @@ public static String matches(
16711672
return EnsureStringOps.matches(string, regex, exceptionSupplier);
16721673
}
16731674

1675+
/**
1676+
* Ensures that the provided string matches the specified pattern.
1677+
*
1678+
* @param string the string to check
1679+
* @param pattern the pattern to match against
1680+
* @return the provided string if it matches the pattern
1681+
* @throws EnsureException if the string is {@code null} or does not match the pattern, with the
1682+
* message {@code "string must match pattern %s"}
1683+
* @see #matches(String, Pattern, String)
1684+
* @see #matches(String, Pattern, Supplier)
1685+
*/
1686+
@Contract("null, _ -> fail; !null, _ -> param1")
1687+
public static String matches(String string, Pattern pattern) {
1688+
return Ensure.matches(
1689+
string,
1690+
pattern,
1691+
EnsureStringOps.STRING_MUST_MATCH_REGEX_FORMAT.formatted(
1692+
pattern == null ? "null" : pattern.pattern()));
1693+
}
1694+
1695+
/**
1696+
* Ensures that the provided string matches the specified pattern.
1697+
*
1698+
* @param string the string to check
1699+
* @param pattern the pattern to match against
1700+
* @param exceptionMessage the message to include in the exception if validation fails
1701+
* @return the provided string if it matches the pattern
1702+
* @throws EnsureException if the string is {@code null} or does not match the pattern, with the
1703+
* provided message
1704+
* @see #matches(String, Pattern)
1705+
* @see #matches(String, Pattern, Supplier)
1706+
*/
1707+
@Contract("null, _, _ -> fail; !null, _, _ -> param1")
1708+
public static String matches(String string, Pattern pattern, String exceptionMessage) {
1709+
return Ensure.matches(string, pattern, () -> EnsureException.from(exceptionMessage));
1710+
}
1711+
1712+
/**
1713+
* Ensures that the provided string matches the specified pattern.
1714+
*
1715+
* @param string the string to check
1716+
* @param pattern the pattern to match against
1717+
* @param exceptionSupplier the supplier that provides the exception to be thrown if validation
1718+
* fails
1719+
* @return the provided string if it matches the pattern
1720+
* @throws RuntimeException if the string is {@code null} or does not match the pattern; the
1721+
* thrown exception is provided by {@code exceptionSupplier}
1722+
* @see #matches(String, Pattern)
1723+
* @see #matches(String, Pattern, String)
1724+
*/
1725+
@Contract("null, _, _ -> fail; !null, _, _ -> param1")
1726+
public static String matches(
1727+
String string, Pattern pattern, Supplier<? extends RuntimeException> exceptionSupplier) {
1728+
return EnsurePatternOps.matches(string, pattern, exceptionSupplier);
1729+
}
1730+
16741731
/**
16751732
* Ensures that the provided string matches the alphanumeric pattern (including spaces).
16761733
*

lib/src/main/java/io/github/mangila/ensure4j/EnsurePatternOps.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class EnsurePatternOps {
1414
static final String STRING_MUST_MATCH_EMAIL_MESSAGE = "string must match email pattern";
1515
static final String STRING_MUST_MATCH_ALPHANUMERIC_MESSAGE =
1616
"string must match alphanumeric pattern";
17+
static final String PATTERN_MUST_NOT_BE_NULL_MESSAGE = "pattern must not be null";
1718

1819
/**
1920
* While RFC 5322 is notoriously complex, this is a highly reliable, performant pattern for 99.9%
@@ -29,6 +30,17 @@ private EnsurePatternOps() {
2930
throw new AssertionError("No Ensure4j for you!");
3031
}
3132

33+
static String matches(
34+
String string, Pattern pattern, Supplier<? extends RuntimeException> exceptionSupplier) {
35+
if (pattern == null) {
36+
throw EnsureException.from(PATTERN_MUST_NOT_BE_NULL_MESSAGE);
37+
}
38+
if (!EnsureUtils.matches(string, pattern)) {
39+
throw getSupplierOrThrow(exceptionSupplier);
40+
}
41+
return string;
42+
}
43+
3244
/**
3345
* Ensures that the provided string matches the alphanumeric pattern (including spaces).
3446
*
@@ -41,7 +53,7 @@ private EnsurePatternOps() {
4153
*/
4254
static String matchesAlphanumeric(
4355
String string, Supplier<? extends RuntimeException> exceptionSupplier) {
44-
if (!EnsureUtils.isAlphanumeric(string)) {
56+
if (!EnsureUtils.matches(string, ALPHANUMERIC_PATTERN)) {
4557
throw getSupplierOrThrow(exceptionSupplier);
4658
}
4759

@@ -60,7 +72,7 @@ static String matchesAlphanumeric(
6072
*/
6173
static String matchesEmail(
6274
String string, Supplier<? extends RuntimeException> exceptionSupplier) {
63-
if (!EnsureUtils.isEmail(string)) {
75+
if (!EnsureUtils.matches(string, EMAIL_PATTERN)) {
6476
throw getSupplierOrThrow(exceptionSupplier);
6577
}
6678

lib/src/main/java/io/github/mangila/ensure4j/EnsureUtils.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Collection;
66
import java.util.Map;
77
import java.util.function.Supplier;
8+
import java.util.regex.Pattern;
89
import org.intellij.lang.annotations.RegExp;
910

1011
final class EnsureUtils {
@@ -104,11 +105,7 @@ static boolean isNegativeWithZero(long value) {
104105
return value <= 0;
105106
}
106107

107-
static boolean isAlphanumeric(String string) {
108-
return string != null && EnsurePatternOps.ALPHANUMERIC_PATTERN.matcher(string).matches();
109-
}
110-
111-
public static boolean isEmail(String string) {
112-
return string != null && EnsurePatternOps.EMAIL_PATTERN.matcher(string).matches();
108+
static boolean matches(String string, Pattern pattern) {
109+
return string != null && pattern.matcher(string).matches();
113110
}
114111
}

lib/src/test/java/io/github/mangila/ensure4j/EnsureArchitectureTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void shouldVerifyEnsure() {
7474
@Override
7575
public void check(JavaClass item, ConditionEvents events) {
7676
final long count = getMethodCount(item);
77-
final long expectedMethodCount = 102;
77+
final long expectedMethodCount = 105;
7878
assertThat(count)
7979
.as(
8080
"Expected methods: %s - %s"

lib/src/test/java/io/github/mangila/ensure4j/EnsurePatternOpsTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatThrownBy;
55

6+
import java.util.regex.Pattern;
67
import org.junit.jupiter.api.DisplayName;
78
import org.junit.jupiter.api.Test;
89

@@ -15,7 +16,51 @@ public Class<EnsurePatternOps> clazz() {
1516

1617
@Override
1718
public long expectedMethodCount() {
18-
return 2;
19+
return 3;
20+
}
21+
22+
@Test
23+
@DisplayName("matches should return string when it matches pattern")
24+
void matchesShouldReturnStringWhenItMatchesPattern() {
25+
String value = "test";
26+
Pattern pattern = Pattern.compile("^t.*t$");
27+
assertThat(Ensure.matches(value, pattern)).isEqualTo(value);
28+
}
29+
30+
@Test
31+
@DisplayName("matches should throw exception when it does not match pattern")
32+
void matchesShouldThrowExceptionWhenItDoesNotMatchPattern() {
33+
Pattern pattern = Pattern.compile("^abc$");
34+
assertThatThrownBy(() -> Ensure.matches("test", pattern))
35+
.isInstanceOf(EnsureException.class)
36+
.hasMessage("string must match regex: ^abc$");
37+
}
38+
39+
@Test
40+
@DisplayName("matches should throw exception with custom message")
41+
void matchesShouldThrowExceptionWithCustomMessage() {
42+
Pattern pattern = Pattern.compile("^abc$");
43+
assertThatThrownBy(() -> Ensure.matches("test", pattern, "custom message"))
44+
.isInstanceOf(EnsureException.class)
45+
.hasMessage("custom message");
46+
}
47+
48+
@Test
49+
@DisplayName("matches should throw custom exception")
50+
void matchesShouldThrowCustomException() {
51+
Pattern pattern = Pattern.compile("^abc$");
52+
assertThatThrownBy(
53+
() -> Ensure.matches("test", pattern, () -> new IllegalArgumentException("custom")))
54+
.isInstanceOf(IllegalArgumentException.class)
55+
.hasMessage("custom");
56+
}
57+
58+
@Test
59+
@DisplayName("matches should throw exception when pattern is null")
60+
void matchesShouldThrowExceptionWhenPatternIsNull() {
61+
assertThatThrownBy(() -> Ensure.matches("test", (Pattern) null))
62+
.isInstanceOf(EnsureException.class)
63+
.hasMessage("pattern must not be null");
1964
}
2065

2166
@Test

lib/src/test/java/io/github/mangila/ensure4j/EnsureStringOpsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ void matchesShouldThrowCustomException() {
252252
@Test
253253
@DisplayName("matches should throw exception when regex is null")
254254
void matchesShouldThrowExceptionWhenRegexIsNull() {
255-
assertThatThrownBy(() -> Ensure.matches("test", null))
255+
assertThatThrownBy(() -> Ensure.matches("test", (String) null))
256256
.isInstanceOf(EnsureException.class)
257257
.hasMessage("regex must not be null");
258258
}

0 commit comments

Comments
 (0)