Skip to content

Commit

Permalink
Improve set tests (#20)
Browse files Browse the repository at this point in the history
* Add reliable unit tests for set order

* Upgrade gradle wrapper

* Enable gradle configuration cache

* Fix sonar findings

---------

Co-authored-by: kaklakariada <[email protected]>
  • Loading branch information
kaklakariada and kaklakariada authored Nov 15, 2024
1 parent a4bd752 commit 719b735
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.configuration-cache=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ dependencyResolutionManagement {
mavenCentral()
}
}

enableFeaturePreview "STABLE_CONFIGURATION_CACHE"
2 changes: 1 addition & 1 deletion src/main/java/org/itsallcode/matcher/MismatchReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* does not match, the overall state is 'not matching'. Additionally it builds a
* description of the mismatch.
*/
public class MismatchReporter {
public final class MismatchReporter {
private final Description mismatchDescription;
private boolean firstMismatch = true;
private boolean matches = true;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/itsallcode/matcher/auto/AutoConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ class AutoConfigBuilder<T> {

MatcherConfig<T> build() {
Arrays.stream(expected.getClass().getMethods()) //
.filter(this::isNotIgnored) //
.filter(AutoConfigBuilder::isNotIgnored) //
.filter(this::isGetterMethodName) //
.filter(this::isGetterMethodSignature) //
.filter(AutoConfigBuilder::isGetterMethodSignature) //
.sorted(Comparator.comparing(this::hasSimpleReturnType).reversed() //
.thenComparing(this::hasArrayReturnType) //
.thenComparing(AutoConfigBuilder::hasArrayReturnType) //
.thenComparing(Method::getName)) //
.forEach(this::addConfigForGetter);
return configBuilder.build();
Expand Down Expand Up @@ -178,11 +178,11 @@ private static <T> Matcher<T> createOptionalMatcher(final T expected) {
return (Matcher<T>) OptionalMatchers.isPresentAnd(AutoMatcher.equalTo(expectedOptional.get()));
}

private boolean isNotIgnored(final Method method) {
private static boolean isNotIgnored(final Method method) {
return !IGNORED_METHOD_NAMES.contains(method.getName());
}

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

private boolean hasArrayReturnType(final Method method) {
private static boolean hasArrayReturnType(final Method method) {
return method.getReturnType().isArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*
* @author npathai, sweiler
*/
class OptionalMatchers {
final class OptionalMatchers {
// This is an utility class that must not be instantiated.
private OptionalMatchers() {
}

/**
* Creates a matcher that matches when the examined {@code Optional} contains no
Expand Down Expand Up @@ -101,8 +104,4 @@ protected void describeMismatchSafely(final Optional<T> item, final Description
}
}
}

// This is an utility class that must not be instantiated.
private OptionalMatchers() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public <P> Builder<B> addIterableProperty(final String propertyName,
return addPropertyInternal(propertyName, listMatcher, propertyAccessor);
}

private <P> Matcher<Iterable<? extends P>> createListMatcher(final Function<P, Matcher<P>> matcherBuilder,
private static <P> Matcher<Iterable<? extends P>> createListMatcher(
final Function<P, Matcher<P>> matcherBuilder,
final Iterable<? extends P> expectedPropertyValue) {
if (expectedPropertyValue == null) {
return createNullIterableMatcher();
Expand All @@ -135,7 +136,7 @@ private <P> Matcher<Iterable<? extends P>> createListMatcher(final Function<P, M
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private <P> Matcher<Iterable<? extends P>> createNullIterableMatcher() {
private static <P> Matcher<Iterable<? extends P>> createNullIterableMatcher() {
return new NullIterableMatcher();
}

Expand Down
19 changes: 14 additions & 5 deletions src/test/java/org/itsallcode/matcher/auto/AutoMatcherSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ void testEmptySetAndAsSetOfAreEqual() {
assertValuesMatch(emptySet(), Set.of());
}

@RepeatedTest(name = "ignores set order repetition {currentRepetition} of {totalRepetitions}", value = 50)
@RepeatedTest(name = "ignores set order repetition {currentRepetition} of {totalRepetitions}", value = 20)
void ignoresSetOrder() {

final long value = System.nanoTime();
final String value1 = "value" + value;
final String value2 = "value2" + (value + 1);
final Set<String> set = new HashSet<>();
set.add("value1");
set.add("value2");
assertValuesMatch(Set.of("value1", "value2"), set);
set.add(value1);
set.add(value2);
assertValuesMatch(Set.of(value1, value2), set);
}

@Test
void ignoresSetOrderForLinkedHashSet() {
final Set<String> set1 = new LinkedHashSet<>(List.of("a", "b"));
final Set<String> set2 = new LinkedHashSet<>(List.of("b", "a"));
assertValuesMatch(set1, set2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ abstract class DemoModelMatcherTest extends MatcherTestBase<DemoModel> {
static final String ATTR2 = "attrValue2";
static final String ATTR3 = "attrValue3";

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

@Test
Expand Down

0 comments on commit 719b735

Please sign in to comment.