Skip to content

Commit

Permalink
Replaces assertAll with assertThat().satisfies
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 20, 2024
1 parent 77ebac8 commit 27385a5
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package nl.jqno.equalsverifier.internal.util;

// CHECKSTYLE OFF: IllegalImport

import static nl.jqno.equalsverifier.internal.testhelpers.Util.coverThePrivateConstructor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.*;

import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
import org.junit.jupiter.api.Test;
Expand All @@ -23,60 +18,66 @@ void coverTheConstructor() {

@Test
void validateFieldTypeMatches_shouldFailOnWrongType() {
assertAll(() -> {
ExpectedException
.when(() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", HashSet.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type List", "but are HashSet");
}, () -> {
ExpectedException
.when(() -> Validations.validateFieldTypeMatches(TestContainer.class, "objectField", int.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type Object", "but are int");
}, () -> {
ExpectedException
.when(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "charsField", Character.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type CharSequence", "but are Character");
});
assertThat(true)
.satisfies(
b -> ExpectedException
.when(
() -> Validations
.validateFieldTypeMatches(TestContainer.class, "listField", HashSet.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type List", "but are HashSet"),
b -> ExpectedException
.when(
() -> Validations
.validateFieldTypeMatches(TestContainer.class, "objectField", int.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type Object", "but are int"),
b -> ExpectedException
.when(
() -> Validations
.validateFieldTypeMatches(TestContainer.class, "charsField", Character.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type CharSequence", "but are Character"));
}

@Test
void validateFieldTypeMatches_shouldAllowSubTypes() {
assertAll(
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", ArrayList.class))
.as("Should allow ArrayList as a List")
.doesNotThrowAnyException(),
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", ArrayList.class))
.as("Should allow ArrayList as a List")
.doesNotThrowAnyException(),
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "objectField", Integer.class))
.as("Should allow Integer as an Object")
.doesNotThrowAnyException(),
() -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "charsField", String.class))
.as("Should allow String as a CharSequence")
.doesNotThrowAnyException());
assertThat(true)
.satisfies(
b -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", ArrayList.class))
.as("Should allow ArrayList as a List")
.doesNotThrowAnyException(),
b -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", ArrayList.class))
.as("Should allow ArrayList as a List")
.doesNotThrowAnyException(),
b -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "objectField", Integer.class))
.as("Should allow Integer as an Object")
.doesNotThrowAnyException(),
b -> assertThatCode(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "charsField", String.class))
.as("Should allow String as a CharSequence")
.doesNotThrowAnyException());
}

@Test
void validateFieldTypeMatches_shouldFailOnSuperTypes() {
assertAll(() -> {
ExpectedException
.when(
() -> Validations.validateFieldTypeMatches(TestContainer.class, "listField", Collection.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type List", "but are Collection");
}, () -> {
ExpectedException
.when(() -> Validations.validateFieldTypeMatches(TestContainer.class, "charsField", Object.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type CharSequence", "but are Object");
});
assertThat(true)
.satisfies(
b -> ExpectedException
.when(
() -> Validations
.validateFieldTypeMatches(TestContainer.class, "listField", Collection.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type List", "but are Collection"),
b -> ExpectedException
.when(
() -> Validations
.validateFieldTypeMatches(TestContainer.class, "charsField", Object.class))
.assertThrows(IllegalStateException.class)
.assertMessageContains("should be of type CharSequence", "but are Object"));
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
package nl.jqno.equalsverifier.verify_release.jar.helper;

// CHECKSTYLE OFF: IllegalImport

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.junit.jupiter.api.function.Executable;

public class JarAsserter {

Expand Down Expand Up @@ -49,72 +39,61 @@ public void assertAbsenceOfEmbeddedDepedencies() {

public void assertPresenceOf(String... filenames) {
var entries = reader.getEntries();
assertForAll(
entries::contains,
fn -> "Expected presence of: " + fn + ", but it was absent.\nFilename: " + reader.getFilename(),
filenames);
assertThat(filenames).allMatch(entries::contains, "present in " + reader.getFilename());
}

public void assertAbsenceOf(String... fileNames) {
var entries = reader.getEntries();
assertForAll(
fn -> !entries.contains(fn),
fn -> "Expected absence of: " + fn + ", but it was present.\nFilename: " + reader.getFilename(),
fileNames);
}

private void assertForAll(Predicate<String> assertion, Function<String, String> message, String... filenames) {
Stream<Executable> assertions =
Arrays.stream(filenames).map(fn -> () -> assertThat(assertion.test(fn)).as(message.apply(fn)).isTrue());
assertAll(assertions);
assertThat(fileNames).allMatch(fn -> !entries.contains(fn), "absent from " + reader.getFilename());
}

public void assertContentOfManifest(String implementationTitle) {
var filename = "/META-INF/MANIFEST.MF";
var manifest = new String(reader.getContentOf(filename));
assertAll(
() -> assertContains("Automatic-Module-Name: nl.jqno.equalsverifier", manifest, filename),
() -> assertContains("Implementation-Title: " + implementationTitle, manifest, filename),
() -> assertContains("Implementation-Version: ", manifest, filename),
() -> assertContains("Multi-Release: true", manifest, filename),
() -> assertContains("Website: https://www.jqno.nl/equalsverifier", manifest, filename));
assertThat(manifest)
.satisfies(
m -> assertContains("Automatic-Module-Name: nl.jqno.equalsverifier", m, filename),
m -> assertContains("Implementation-Title: " + implementationTitle, m, filename),
m -> assertContains("Implementation-Version: ", m, filename),
m -> assertContains("Multi-Release: true", m, filename),
m -> assertContains("Website: https://www.jqno.nl/equalsverifier", m, filename));
}

private void assertContains(String needle, String haystack, String innerFilename) {
assertThat(haystack.contains(needle)).as("Expected to find '" + needle + "' in " + innerFilename).isTrue();
assertThat(haystack).as("Expected to find '" + needle + "' in " + innerFilename).contains(needle);
}

public void assertVersionsOfClassFiles() {
// See https://javaalmanac.io/bytecode/versions/
assertAll(
() -> assertVersionOfClassFile(52, EV + "/EqualsVerifier.class"),
() -> assertVersionOfClassFile(
55,
"/META-INF/versions/11" + EV + "/internal/versionspecific/ModuleHelper.class"),
() -> assertVersionOfClassFile(
60,
"/META-INF/versions/16" + EV + "/internal/versionspecific/RecordsHelper.class"),
() -> assertVersionOfClassFile(
61,
"/META-INF/versions/17" + EV + "/internal/versionspecific/SealedTypesHelper.class"),
() -> assertVersionOfClassFile(
65,
"/META-INF/versions/21" + EV + "/internal/versionspecific/SequencedCollectionsHelper.class"));
assertThat(true)
.satisfies(
b -> assertVersionOfClassFile(52, EV + "/EqualsVerifier.class"),
b -> assertVersionOfClassFile(
55,
"/META-INF/versions/11" + EV + "/internal/versionspecific/ModuleHelper.class"),
b -> assertVersionOfClassFile(
60,
"/META-INF/versions/16" + EV + "/internal/versionspecific/RecordsHelper.class"),
b -> assertVersionOfClassFile(
61,
"/META-INF/versions/17" + EV + "/internal/versionspecific/SealedTypesHelper.class"),
b -> assertVersionOfClassFile(
65,
"/META-INF/versions/21" + EV + "/internal/versionspecific/SequencedCollectionsHelper.class"));
}

public void assertVersionsOfEmbeddedClassFiles() {
assertAll(
() -> assertVersionOfClassFile(49, EV + "/internal/lib/bytebuddy/ByteBuddy.class"),
() -> assertVersionOfClassFile(52, EV + "/internal/lib/objenesis/Objenesis.class"));
assertThat(true)
.satisfies(
b -> assertVersionOfClassFile(49, EV + "/internal/lib/bytebuddy/ByteBuddy.class"),
b -> assertVersionOfClassFile(52, EV + "/internal/lib/objenesis/Objenesis.class"));
}

private void assertVersionOfClassFile(int expectedVersion, String innerFilename) {
var classFile = reader.getContentOf(innerFilename);
var actualVersion = classFile[7];
assertThat(actualVersion)
.as(
"Expected " + innerFilename + " to have version " + expectedVersion + ", but it has version "
+ actualVersion)
.isEqualTo((byte) expectedVersion);
var description = "Expected " + innerFilename + " to have version " + expectedVersion + ", but it has version "
+ actualVersion;
assertThat(actualVersion).as(description).isEqualTo((byte) expectedVersion);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nl.jqno.equalsverifier.verify_release.pom;

// CHECKSTYLE OFF: IllegalImport

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.assertj.core.api.Assertions.assertThat;

import nl.jqno.equalsverifier.verify_release.pom.helper.PomAsserter;
import nl.jqno.equalsverifier.verify_release.pom.helper.PomReader;
Expand Down Expand Up @@ -32,13 +30,14 @@ void artifactId() {

@Test
void dependencies() {
assertAll(
() -> pom.assertNumberOfDependencies(6),
() -> pom.assertDependencyIsOptional(1),
() -> pom.assertDependencyIsOptional(2),
() -> pom.assertDependencyIsOptional(3),
() -> pom.assertDependencyIsOptional(4),
() -> pom.assertDependency(5, "org.objenesis", "objenesis"),
() -> pom.assertDependency(6, "net.bytebuddy", "byte-buddy"));
assertThat(pom)
.satisfies(
p -> p.assertNumberOfDependencies(6),
p -> p.assertDependencyIsOptional(1),
p -> p.assertDependencyIsOptional(2),
p -> p.assertDependencyIsOptional(3),
p -> p.assertDependencyIsOptional(4),
p -> p.assertDependency(5, "org.objenesis", "objenesis"),
p -> p.assertDependency(6, "net.bytebuddy", "byte-buddy"));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package nl.jqno.equalsverifier.verify_release.pom.helper;

// CHECKSTYLE OFF: IllegalImport

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.assertj.core.api.Assertions.assertThat;

public class PomAsserter {

Expand All @@ -13,19 +11,20 @@ public PomAsserter(PomReader reader) {
}

public void assertCommonProperties() {
assertAll(
() -> reader.assertNode("/project/groupId", "nl.jqno.equalsverifier"),
() -> reader.assertNode("/project/url", "https://www.jqno.nl/equalsverifier"),
() -> reader.assertNode("/project/inceptionYear", "2009"),
() -> reader.assertNode("/project/licenses/license[1]/name", "Apache License, Version 2.0"),
() -> reader.assertNode("/project/developers/developer[1]/name", "Jan Ouwens"),
() -> reader
.assertNode(
"/project/mailingLists/mailingList[1]/archive",
"https://groups.google.com/group/equalsverifier"),
() -> reader.assertNode("/project/scm/url", "https://github.com/jqno/equalsverifier"),
() -> reader.assertNode("/project/issueManagement/url", "https://github.com/jqno/equalsverifier/issues"),
() -> reader.assertNode("/project/ciManagement/url", "https://github.com/jqno/equalsverifier/actions"));
assertThat(reader)
.satisfies(
r -> r.assertNode("/project/groupId", "nl.jqno.equalsverifier"),
r -> r.assertNode("/project/url", "https://www.jqno.nl/equalsverifier"),
r -> r.assertNode("/project/inceptionYear", "2009"),
r -> r.assertNode("/project/licenses/license[1]/name", "Apache License, Version 2.0"),
r -> r.assertNode("/project/developers/developer[1]/name", "Jan Ouwens"),
r -> r
.assertNode(
"/project/mailingLists/mailingList[1]/archive",
"https://groups.google.com/group/equalsverifier"),
r -> r.assertNode("/project/scm/url", "https://github.com/jqno/equalsverifier"),
r -> r.assertNode("/project/issueManagement/url", "https://github.com/jqno/equalsverifier/issues"),
r -> r.assertNode("/project/ciManagement/url", "https://github.com/jqno/equalsverifier/actions"));
}

public void assertArtifactId(String artifactId) {
Expand All @@ -38,16 +37,18 @@ public void assertNumberOfDependencies(int n) {

public void assertDependency(int idx, String groupId, String artifactId) {
var prefix = "/project/dependencies/dependency[" + idx + "]";
assertAll(
() -> reader.assertNode(prefix + "/groupId", groupId),
() -> reader.assertNode(prefix + "/artifactId", artifactId),
() -> reader.assertNode(prefix + "/scope", "compile"));
assertThat(reader)
.satisfies(
r -> r.assertNode(prefix + "/groupId", groupId),
r -> r.assertNode(prefix + "/artifactId", artifactId),
r -> r.assertNode(prefix + "/scope", "compile"));
}

public void assertDependencyIsOptional(int idx) {
var prefix = "/project/dependencies/dependency[" + idx + "]";
assertAll(
() -> reader.assertNode(prefix + "/optional", "true"),
() -> reader.assertNode(prefix + "/scope", "provided"));
assertThat(reader)
.satisfies(
r -> r.assertNode(prefix + "/optional", "true"),
r -> r.assertNode(prefix + "/scope", "provided"));
}
}

0 comments on commit 27385a5

Please sign in to comment.