Skip to content

Commit 8986a91

Browse files
committed
When returning multiple failures, eliminate any duplicate messages.
1 parent 9904e25 commit 8986a91

File tree

8 files changed

+50
-34
lines changed

8 files changed

+50
-34
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# <img src="docs/checklist.svg" width=64 height=64 alt="checklist"> Requirements API
55

6-
[![API](https://img.shields.io/badge/api_docs-5B45D5.svg)](https://cowwoc.github.io/requirements.java/10.7/)
6+
[![API](https://img.shields.io/badge/api_docs-5B45D5.svg)](https://cowwoc.github.io/requirements.java/10.8/)
77
[![Changelog](https://img.shields.io/badge/changelog-A345D5.svg)](docs/changelog.md)
88
[![javascript, typescript](https://img.shields.io/badge/other%20languages-javascript,%20typescript-457FD5.svg)](../../../requirements.js)
99

@@ -22,7 +22,7 @@ To get started, add this Maven dependency:
2222
<dependency>
2323
<groupId>com.github.cowwoc.requirements</groupId>
2424
<artifactId>java</artifactId>
25-
<version>10.7</version>
25+
<version>10.8</version>
2626
</dependency>
2727
```
2828

@@ -152,14 +152,14 @@ This library offers the following features:
152152
Designed for discovery using your favorite IDE's auto-complete feature.
153153
The main entry points are:
154154

155-
* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#requireThat(T,java.lang.String))
155+
* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#requireThat(T,java.lang.String))
156156
for method preconditions.
157-
* [that(value, name)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#that(T,java.lang.String))
157+
* [that(value, name)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#that(T,java.lang.String))
158158
for [class invariants, method postconditions and private methods](docs/features.md#assertion-support).
159-
* [checkIf(value, name)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#checkIf(T,java.lang.String))
159+
* [checkIf(value, name)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html#checkIf(T,java.lang.String))
160160
for multiple failures and customized error handling.
161161

162-
See the [API documentation](https://cowwoc.github.io/requirements.java/10.7/) for more details.
162+
See the [API documentation](https://cowwoc.github.io/requirements.java/10.8/) for more details.
163163

164164
## Best practices
165165

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Minor updates involving cosmetic changes have been omitted from this list.
22

33
See https://github.com/cowwoc/requirements.java/commits/main for a full list.
44

5+
## Version 10.8 - 2025/01/13
6+
7+
* When returning multiple failures, eliminate any duplicate messages.
8+
59
## Version 10.7 - 2025/01/03
610

711
* Bugfix: If the string representation of the expected and actual values was equal, the class names were not

docs/features.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ String province = "Florida";
130130
List<String> provinces = Arrays.asList("Ontario", "Quebec", "Nova Scotia", "New Brunswick", "Manitoba",
131131
"British Columbia", "Prince Edward Island", "Saskatchewan", "Alberta", "Newfoundland and Labrador");
132132

133-
List<ValidationFailure> failures = checkIf(name, "name").length().isBetween(10, 30).elseGetFailures();
133+
List<ValidationFailure> failures = new ArrayList<>(checkIf(name, "name").length().isBetween(10, 30).elseGetFailures());
134134
failures.addAll(checkIf(provinces, "provinces").contains(province).elseGetFailures());
135135

136136
for (ValidationFailure failure: failures)
@@ -174,7 +174,7 @@ requireThat(nameToAge, "nameToAge").
174174
## String diff
175175

176176
When
177-
a [String comparison](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/type/component/ObjectValidatorComponent#isEqualTo(java.lang.Object))
177+
a [String comparison](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/type/component/ObjectValidatorComponent#isEqualTo(java.lang.Object))
178178
fails, the library outputs a diff of the values being compared.
179179

180180
Depending on the terminal capability, you will see a [textual](textual_diff.md) or a [colored](colored_diff.md) diff.
@@ -188,7 +188,7 @@ terminal.
188188

189189
The use of colors is disabled by default if stdin or stdout are redirected, even if ANSI colors are supported.
190190
To enable colors,
191-
invoke [GlobalConfiguration.terminalEncoding(TerminalEncoding)](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/GlobalConfiguration.html#terminalEncoding(com.github.cowwoc.requirements10.java.TerminalEncoding)).
191+
invoke [GlobalConfiguration.terminalEncoding(TerminalEncoding)](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/GlobalConfiguration.html#terminalEncoding(com.github.cowwoc.requirements10.java.TerminalEncoding)).
192192

193193
## Returning the value after validation
194194

docs/supported_libraries.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Each module uses a separate class pair for validation. For example,
2-
[DefaultJavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html)
2+
[DefaultJavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/DefaultJavaValidators.html)
33
and
4-
[JavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/JavaValidators.html)
4+
[JavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/java/JavaValidators.html)
55
validate the core Java API. Similarly,
6-
[DefaultGuavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/DefaultGuavaValidators.html)
6+
[DefaultGuavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/DefaultGuavaValidators.html)
77
and
8-
[GuavaValidators](https://cowwoc.github.io/requirements.java/10.7/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/GuavaValidators.html)
8+
[GuavaValidators](https://cowwoc.github.io/requirements.java/10.8/com.github.cowwoc.requirements.java/com/github/cowwoc/requirements10/guava/GuavaValidators.html)
99
validate the Guava API.
1010

1111
The following table lists validators for third-party libraries:

java/src/main/java/com/github/cowwoc/requirements10/java/ValidationFailures.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,7 @@ public boolean throwOnFailure()
6969
{
7070
if (failures.isEmpty())
7171
return true;
72-
Throwable throwable;
73-
if (failures.size() == 1)
74-
{
75-
ValidationFailure failure = failures.getFirst();
76-
throwable = failure.getException();
77-
}
78-
else
79-
throwable = new MultipleFailuresException(failures);
72+
Throwable throwable = getThrowable();
8073
if (cleanStackTrace)
8174
Exceptions.removeLibraryFromStackTrace(throwable);
8275
switch (throwable)
@@ -87,6 +80,19 @@ public boolean throwOnFailure()
8780
}
8881
}
8982

83+
/**
84+
* @return the failure exception
85+
*/
86+
private Throwable getThrowable()
87+
{
88+
if (failures.size() == 1)
89+
{
90+
ValidationFailure failure = failures.getFirst();
91+
return failure.getException();
92+
}
93+
return new MultipleFailuresException(failures);
94+
}
95+
9096
/**
9197
* Returns the validation failure messages.
9298
*
@@ -115,14 +121,7 @@ public Throwable getException()
115121
{
116122
if (failures.isEmpty())
117123
return null;
118-
Throwable throwable;
119-
if (failures.size() == 1)
120-
{
121-
ValidationFailure failure = failures.getFirst();
122-
throwable = failure.getException();
123-
}
124-
else
125-
throwable = new MultipleFailuresException(failures);
124+
Throwable throwable = getThrowable();
126125
if (cleanStackTrace)
127126
Exceptions.removeLibraryFromStackTrace(throwable);
128127
return throwable;

java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/AbstractValidator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ public void addFailure(String message, Throwable cause, ExceptionBuilder excepti
158158
{
159159
ValidationFailureImpl failure = new ValidationFailureImpl(configuration, message, cause,
160160
exceptionBuilder, Set.of());
161+
for (ValidationFailure existingFailure : failures)
162+
if (existingFailure.getMessage().equals(failure.getMessage()))
163+
{
164+
// Skip duplicate failure messages
165+
return;
166+
}
167+
161168
failures.add(failure);
162169
if (configuration.throwOnFailure())
163170
{
@@ -186,6 +193,13 @@ public <E extends Exception> void addFailure(String message, Throwable cause,
186193
{
187194
ValidationFailureImpl failure = new ValidationFailureImpl(configuration, message, cause, exceptionBuilder,
188195
Set.of(checkedException));
196+
for (ValidationFailure existingFailure : failures)
197+
if (existingFailure.getMessage().equals(failure.getMessage()))
198+
{
199+
// Skip duplicate failure messages
200+
return;
201+
}
202+
189203
failures.add(failure);
190204
if (configuration.throwOnFailure())
191205
{

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
<dependency>
118118
<groupId>ch.qos.logback</groupId>
119119
<artifactId>logback-classic</artifactId>
120-
<version>1.5.15</version>
120+
<version>1.5.16</version>
121121
</dependency>
122122
<dependency>
123123
<groupId>com.github.javaparser</groupId>
@@ -147,7 +147,7 @@
147147
<dependency>
148148
<groupId>org.twdata.maven</groupId>
149149
<artifactId>mojo-executor</artifactId>
150-
<version>2.4.1-m2</version>
150+
<version>2.4.1</version>
151151
</dependency>
152152
<dependency>
153153
<groupId>org.testng</groupId>
@@ -157,7 +157,7 @@
157157
<dependency>
158158
<groupId>org.assertj</groupId>
159159
<artifactId>assertj-core</artifactId>
160-
<version>3.27.1</version>
160+
<version>3.27.2</version>
161161
</dependency>
162162
</dependencies>
163163
</dependencyManagement>

test/src/test/java/com/github/cowwoc/requirements10/test/java/ComparableTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,8 @@ public void multipleFailuresNullIsComparableTo()
715715
TestValidators validators = TestValidators.of(scope);
716716

717717
Integer actual = null;
718+
// Ensure that expectedMessages does not contain the same failure message twice
718719
List<String> expectedMessages = List.of("""
719-
"actual" must be equal to 5.
720-
actual: null""", """
721720
"actual" must be equal to 5.
722721
actual: null""");
723722
try (ConfigurationUpdater configurationUpdater = validators.updateConfiguration())

0 commit comments

Comments
 (0)