Migrate Hamcrest assertions to AssertJ
Migrate all existing `assertThat(..., Matcher)` assertions to AssertJ and add checkstyle rules to ensure they don't return. See gh-23022
This commit is contained in:
@@ -60,10 +60,10 @@ import org.springframework.context.index.test.TestCompiler;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.springframework.context.index.processor.Metadata.hasComponent;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tests for {@link CandidateComponentsIndexer}.
|
||||
@@ -87,13 +87,13 @@ public class CandidateComponentsIndexerTests {
|
||||
@Test
|
||||
public void noCandidate() {
|
||||
CandidateComponentsMetadata metadata = compile(SampleNone.class);
|
||||
assertThat(metadata.getItems(), hasSize(0));
|
||||
assertThat(metadata.getItems()).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noAnnotation() {
|
||||
CandidateComponentsMetadata metadata = compile(CandidateComponentsIndexerTests.class);
|
||||
assertThat(metadata.getItems(), hasSize(0));
|
||||
assertThat(metadata.getItems()).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -171,7 +171,7 @@ public class CandidateComponentsIndexerTests {
|
||||
public void packageInfo() {
|
||||
CandidateComponentsMetadata metadata = compile(
|
||||
"org/springframework/context/index/sample/jpa/package-info");
|
||||
assertThat(metadata, hasComponent(
|
||||
assertThat(metadata).has(Metadata.of(
|
||||
"org.springframework.context.index.sample.jpa", "package-info"));
|
||||
}
|
||||
|
||||
@@ -211,33 +211,33 @@ public class CandidateComponentsIndexerTests {
|
||||
// Validate nested type structure
|
||||
String nestedType = "org.springframework.context.index.sample.SampleEmbedded.Another$AnotherPublicCandidate";
|
||||
Class<?> type = ClassUtils.forName(nestedType, getClass().getClassLoader());
|
||||
assertThat(type, sameInstance(SampleEmbedded.Another.AnotherPublicCandidate.class));
|
||||
assertThat(type).isSameAs(SampleEmbedded.Another.AnotherPublicCandidate.class);
|
||||
|
||||
CandidateComponentsMetadata metadata = compile(SampleEmbedded.class);
|
||||
assertThat(metadata, hasComponent(
|
||||
assertThat(metadata).has(Metadata.of(
|
||||
SampleEmbedded.PublicCandidate.class, Component.class));
|
||||
assertThat(metadata, hasComponent(nestedType, Component.class.getName()));
|
||||
assertThat(metadata.getItems(), hasSize(2));
|
||||
assertThat(metadata).has(Metadata.of(nestedType, Component.class.getName()));
|
||||
assertThat(metadata.getItems()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void embeddedNonStaticCandidateAreIgnored() {
|
||||
CandidateComponentsMetadata metadata = compile(SampleNonStaticEmbedded.class);
|
||||
assertThat(metadata.getItems(), hasSize(0));
|
||||
assertThat(metadata.getItems()).hasSize(0);
|
||||
}
|
||||
|
||||
private void testComponent(Class<?>... classes) {
|
||||
CandidateComponentsMetadata metadata = compile(classes);
|
||||
for (Class<?> c : classes) {
|
||||
assertThat(metadata, hasComponent(c, Component.class));
|
||||
assertThat(metadata).has(Metadata.of(c, Component.class));
|
||||
}
|
||||
assertThat(metadata.getItems(), hasSize(classes.length));
|
||||
assertThat(metadata.getItems()).hasSize(classes.length);
|
||||
}
|
||||
|
||||
private void testSingleComponent(Class<?> target, Class<?>... stereotypes) {
|
||||
CandidateComponentsMetadata metadata = compile(target);
|
||||
assertThat(metadata, hasComponent(target, stereotypes));
|
||||
assertThat(metadata.getItems(), hasSize(1));
|
||||
assertThat(metadata).has(Metadata.of(target, stereotypes));
|
||||
assertThat(metadata.getItems()).hasSize(1);
|
||||
}
|
||||
|
||||
private CandidateComponentsMetadata compile(Class<?>... types) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,84 +20,32 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.assertj.core.api.Condition;
|
||||
|
||||
/**
|
||||
* Hamcrest {@link org.hamcrest.Matcher Matcher} to help test {@link CandidateComponentsMetadata}.
|
||||
* AssertJ {@link Condition} to help test {@link CandidateComponentsMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class Metadata {
|
||||
class Metadata {
|
||||
|
||||
public static ItemMetadataMatcher hasComponent(Class<?> type, Class<?>... stereotypes) {
|
||||
return new ItemMetadataMatcher(type.getName(), stereotypes);
|
||||
public static Condition<CandidateComponentsMetadata> of(Class<?> type, Class<?>... stereotypes) {
|
||||
return of(type.getName(), Arrays.stream(stereotypes).map(Class::getName).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static ItemMetadataMatcher hasComponent(String type, String... stereotypes) {
|
||||
return new ItemMetadataMatcher(type, stereotypes);
|
||||
public static Condition<CandidateComponentsMetadata> of(String type, String... stereotypes) {
|
||||
return of(type, Arrays.asList(stereotypes));
|
||||
}
|
||||
|
||||
|
||||
private static class ItemMetadataMatcher extends BaseMatcher<CandidateComponentsMetadata> {
|
||||
|
||||
private final String type;
|
||||
|
||||
private final List<String> stereotypes;
|
||||
|
||||
private ItemMetadataMatcher(String type, List<String> stereotypes) {
|
||||
this.type = type;
|
||||
this.stereotypes = stereotypes;
|
||||
}
|
||||
|
||||
public ItemMetadataMatcher(String type, String... stereotypes) {
|
||||
this(type, Arrays.asList(stereotypes));
|
||||
}
|
||||
|
||||
public ItemMetadataMatcher(String type, Class<?>... stereotypes) {
|
||||
this(type, Arrays.stream(stereotypes)
|
||||
.map(Class::getName).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object value) {
|
||||
if (!(value instanceof CandidateComponentsMetadata)) {
|
||||
return false;
|
||||
}
|
||||
ItemMetadata itemMetadata = getFirstItemWithType((CandidateComponentsMetadata) value, this.type);
|
||||
if (itemMetadata == null) {
|
||||
return false;
|
||||
}
|
||||
if (this.type != null && !this.type.equals(itemMetadata.getType())) {
|
||||
return false;
|
||||
}
|
||||
if (this.stereotypes != null) {
|
||||
for (String stereotype : this.stereotypes) {
|
||||
if (!itemMetadata.getStereotypes().contains(stereotype)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (this.stereotypes.size() != itemMetadata.getStereotypes().size()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private ItemMetadata getFirstItemWithType(CandidateComponentsMetadata metadata, String type) {
|
||||
for (ItemMetadata item : metadata.getItems()) {
|
||||
if (item.getType().equals(type)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("Candidates with type ").appendValue(this.type);
|
||||
description.appendText(" and stereotypes ").appendValue(this.stereotypes);
|
||||
}
|
||||
public static Condition<CandidateComponentsMetadata> of(String type,
|
||||
List<String> stereotypes) {
|
||||
return new Condition<>(metadata -> {
|
||||
ItemMetadata itemMetadata = metadata.getItems().stream()
|
||||
.filter(item -> item.getType().equals(type))
|
||||
.findFirst().orElse(null);
|
||||
return itemMetadata != null && itemMetadata.getStereotypes().size() == stereotypes.size()
|
||||
&& itemMetadata.getStereotypes().containsAll(stereotypes);
|
||||
}, "Candidates with type %s and stereotypes %s", type, stereotypes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,9 +24,9 @@ import java.util.HashSet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.springframework.context.index.processor.Metadata.hasComponent;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tests for {@link PropertiesMarshaller}.
|
||||
@@ -45,9 +45,9 @@ public class PropertiesMarshallerTests {
|
||||
PropertiesMarshaller.write(metadata, outputStream);
|
||||
CandidateComponentsMetadata readMetadata = PropertiesMarshaller.read(
|
||||
new ByteArrayInputStream(outputStream.toByteArray()));
|
||||
assertThat(readMetadata, hasComponent("com.foo", "first", "second"));
|
||||
assertThat(readMetadata, hasComponent("com.bar", "first"));
|
||||
assertThat(readMetadata.getItems(), hasSize(2));
|
||||
assertThat(readMetadata).has(Metadata.of("com.foo", "first", "second"));
|
||||
assertThat(readMetadata).has(Metadata.of("com.bar", "first"));
|
||||
assertThat(readMetadata.getItems()).hasSize(2);
|
||||
}
|
||||
|
||||
private static ItemMetadata createItem(String type, String... stereotypes) {
|
||||
|
||||
Reference in New Issue
Block a user