Skip to content

Commit 719b735

Browse files
authored
Improve set tests (#20)
* Add reliable unit tests for set order * Upgrade gradle wrapper * Enable gradle configuration cache * Fix sonar findings --------- Co-authored-by: kaklakariada <[email protected]>
1 parent a4bd752 commit 719b735

File tree

9 files changed

+35
-22
lines changed

9 files changed

+35
-22
lines changed

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.configuration-cache=true

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ dependencyResolutionManagement {
1111
mavenCentral()
1212
}
1313
}
14+
15+
enableFeaturePreview "STABLE_CONFIGURATION_CACHE"

src/main/java/org/itsallcode/matcher/MismatchReporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* does not match, the overall state is 'not matching'. Additionally it builds a
1010
* description of the mismatch.
1111
*/
12-
public class MismatchReporter {
12+
public final class MismatchReporter {
1313
private final Description mismatchDescription;
1414
private boolean firstMismatch = true;
1515
private boolean matches = true;

src/main/java/org/itsallcode/matcher/auto/AutoConfigBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class AutoConfigBuilder<T> {
5555

5656
MatcherConfig<T> build() {
5757
Arrays.stream(expected.getClass().getMethods()) //
58-
.filter(this::isNotIgnored) //
58+
.filter(AutoConfigBuilder::isNotIgnored) //
5959
.filter(this::isGetterMethodName) //
60-
.filter(this::isGetterMethodSignature) //
60+
.filter(AutoConfigBuilder::isGetterMethodSignature) //
6161
.sorted(Comparator.comparing(this::hasSimpleReturnType).reversed() //
62-
.thenComparing(this::hasArrayReturnType) //
62+
.thenComparing(AutoConfigBuilder::hasArrayReturnType) //
6363
.thenComparing(Method::getName)) //
6464
.forEach(this::addConfigForGetter);
6565
return configBuilder.build();
@@ -178,11 +178,11 @@ private static <T> Matcher<T> createOptionalMatcher(final T expected) {
178178
return (Matcher<T>) OptionalMatchers.isPresentAnd(AutoMatcher.equalTo(expectedOptional.get()));
179179
}
180180

181-
private boolean isNotIgnored(final Method method) {
181+
private static boolean isNotIgnored(final Method method) {
182182
return !IGNORED_METHOD_NAMES.contains(method.getName());
183183
}
184184

185-
private boolean isGetterMethodSignature(final Method method) {
185+
private static boolean isGetterMethodSignature(final Method method) {
186186
return method.getParameterCount() == 0 //
187187
&& !method.getReturnType().equals(Void.TYPE);
188188
}
@@ -202,7 +202,7 @@ private void addConfigForGetter(final Method method) {
202202
configBuilder.addProperty(propertyName, createGetter(method), AutoMatcher::equalTo);
203203
}
204204

205-
private boolean hasArrayReturnType(final Method method) {
205+
private static boolean hasArrayReturnType(final Method method) {
206206
return method.getReturnType().isArray();
207207
}
208208

src/main/java/org/itsallcode/matcher/auto/OptionalMatchers.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
*
1919
* @author npathai, sweiler
2020
*/
21-
class OptionalMatchers {
21+
final class OptionalMatchers {
22+
// This is an utility class that must not be instantiated.
23+
private OptionalMatchers() {
24+
}
2225

2326
/**
2427
* Creates a matcher that matches when the examined {@code Optional} contains no
@@ -101,8 +104,4 @@ protected void describeMismatchSafely(final Optional<T> item, final Description
101104
}
102105
}
103106
}
104-
105-
// This is an utility class that must not be instantiated.
106-
private OptionalMatchers() {
107-
}
108107
}

src/main/java/org/itsallcode/matcher/config/MatcherConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public <P> Builder<B> addIterableProperty(final String propertyName,
120120
return addPropertyInternal(propertyName, listMatcher, propertyAccessor);
121121
}
122122

123-
private <P> Matcher<Iterable<? extends P>> createListMatcher(final Function<P, Matcher<P>> matcherBuilder,
123+
private static <P> Matcher<Iterable<? extends P>> createListMatcher(
124+
final Function<P, Matcher<P>> matcherBuilder,
124125
final Iterable<? extends P> expectedPropertyValue) {
125126
if (expectedPropertyValue == null) {
126127
return createNullIterableMatcher();
@@ -135,7 +136,7 @@ private <P> Matcher<Iterable<? extends P>> createListMatcher(final Function<P, M
135136
}
136137

137138
@SuppressWarnings({ "unchecked", "rawtypes" })
138-
private <P> Matcher<Iterable<? extends P>> createNullIterableMatcher() {
139+
private static <P> Matcher<Iterable<? extends P>> createNullIterableMatcher() {
139140
return new NullIterableMatcher();
140141
}
141142

src/test/java/org/itsallcode/matcher/auto/AutoMatcherSetTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ void testEmptySetAndAsSetOfAreEqual() {
4242
assertValuesMatch(emptySet(), Set.of());
4343
}
4444

45-
@RepeatedTest(name = "ignores set order repetition {currentRepetition} of {totalRepetitions}", value = 50)
45+
@RepeatedTest(name = "ignores set order repetition {currentRepetition} of {totalRepetitions}", value = 20)
4646
void ignoresSetOrder() {
47-
47+
final long value = System.nanoTime();
48+
final String value1 = "value" + value;
49+
final String value2 = "value2" + (value + 1);
4850
final Set<String> set = new HashSet<>();
49-
set.add("value1");
50-
set.add("value2");
51-
assertValuesMatch(Set.of("value1", "value2"), set);
51+
set.add(value1);
52+
set.add(value2);
53+
assertValuesMatch(Set.of(value1, value2), set);
54+
}
55+
56+
@Test
57+
void ignoresSetOrderForLinkedHashSet() {
58+
final Set<String> set1 = new LinkedHashSet<>(List.of("a", "b"));
59+
final Set<String> set2 = new LinkedHashSet<>(List.of("b", "a"));
60+
assertValuesMatch(set1, set2);
5261
}
5362
}

src/test/java/org/itsallcode/matcher/model/DemoModelMatcherTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ abstract class DemoModelMatcherTest extends MatcherTestBase<DemoModel> {
2626
static final String ATTR2 = "attrValue2";
2727
static final String ATTR3 = "attrValue3";
2828

29-
static String SIMPLE_MODEL_DESCRIPTION = "{id=<" + ID1 + ">, longVal=null, name=\"" + NAME1
29+
static final String SIMPLE_MODEL_DESCRIPTION = "{id=<" + ID1 + ">, longVal=null, name=\"" + NAME1
3030
+ "\", attr=null, children=null, stringArray=null}";
3131
static final String CHILD1 = "{id=<" + ID2 + ">, longVal=null, name=\"" + NAME2 + "\", attr={value=\"" + ATTR2
3232
+ "\"}, children=an empty iterable, stringArray=null}";
33-
static String COMPLEX_MODEL_DESCRIPTION = "{id=<" + ID1 + ">, longVal=null, name=\"" + NAME1 + "\", attr={value=\""
33+
static final String COMPLEX_MODEL_DESCRIPTION = "{id=<" + ID1 + ">, longVal=null, name=\"" + NAME1
34+
+ "\", attr={value=\""
3435
+ ATTR1 + "\"}, children=iterable containing [" + CHILD1 + "], stringArray=null}";
3536

3637
@Test

0 commit comments

Comments
 (0)