Browse Source

Merge pull request #46855 from scordio

* pr/46855:
  Replace AssertJ internal APIs with alternative implementations

Closes gh-46855
pull/47304/head
Stéphane Nicoll 4 months ago
parent
commit
39dced55a4
  1. 13
      spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/ObjectContentAssert.java
  2. 47
      spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/ItemMetadataAssert.java

13
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/ObjectContentAssert.java

@ -16,20 +16,18 @@
package org.springframework.boot.test.json; package org.springframework.boot.test.json;
import java.util.Map;
import org.assertj.core.api.AbstractMapAssert; import org.assertj.core.api.AbstractMapAssert;
import org.assertj.core.api.AbstractObjectArrayAssert; import org.assertj.core.api.AbstractObjectArrayAssert;
import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.Assert; import org.assertj.core.api.Assert;
import org.assertj.core.api.Assertions; import org.assertj.core.api.InstanceOfAssertFactories;
import org.assertj.core.internal.Objects;
/** /**
* AssertJ {@link Assert} for {@link ObjectContent}. * AssertJ {@link Assert} for {@link ObjectContent}.
* *
* @param <A> the actual type * @param <A> the actual type
* @author Phillip Webb * @author Phillip Webb
* @author Stefano Cordio
* @since 1.4.0 * @since 1.4.0
*/ */
public class ObjectContentAssert<A> extends AbstractObjectAssert<ObjectContentAssert<A>, A> { public class ObjectContentAssert<A> extends AbstractObjectAssert<ObjectContentAssert<A>, A> {
@ -44,8 +42,7 @@ public class ObjectContentAssert<A> extends AbstractObjectAssert<ObjectContentAs
* @return an array assertion object * @return an array assertion object
*/ */
public AbstractObjectArrayAssert<?, Object> asArray() { public AbstractObjectArrayAssert<?, Object> asArray() {
Objects.instance().assertIsInstanceOf(this.info, this.actual, Object[].class); return asInstanceOf(InstanceOfAssertFactories.ARRAY);
return Assertions.assertThat((Object[]) this.actual);
} }
/** /**
@ -53,10 +50,8 @@ public class ObjectContentAssert<A> extends AbstractObjectAssert<ObjectContentAs
* chaining of map-specific assertions from this call. * chaining of map-specific assertions from this call.
* @return a map assertion object * @return a map assertion object
*/ */
@SuppressWarnings("unchecked")
public AbstractMapAssert<?, ?, Object, Object> asMap() { public AbstractMapAssert<?, ?, Object, Object> asMap() {
Objects.instance().assertIsInstanceOf(this.info, this.actual, Map.class); return asInstanceOf(InstanceOfAssertFactories.MAP);
return Assertions.assertThat((Map<Object, Object>) this.actual);
} }
} }

47
spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/ItemMetadataAssert.java

@ -16,9 +16,12 @@
package org.springframework.boot.configurationprocessor.test; package org.springframework.boot.configurationprocessor.test;
import java.util.function.Function;
import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AssertProvider; import org.assertj.core.api.AssertProvider;
import org.assertj.core.internal.Objects; import org.assertj.core.api.Assertions;
import org.assertj.core.api.ObjectAssert;
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation; import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
@ -28,34 +31,33 @@ import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.Ite
* AssertJ assert for {@link ItemMetadata}. * AssertJ assert for {@link ItemMetadata}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Stefano Cordio
*/ */
public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemMetadata> public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemMetadata>
implements AssertProvider<ItemMetadataAssert> { implements AssertProvider<ItemMetadataAssert> {
private static final Objects objects = Objects.instance();
public ItemMetadataAssert(ItemMetadata itemMetadata) { public ItemMetadataAssert(ItemMetadata itemMetadata) {
super(itemMetadata, ItemMetadataAssert.class); super(itemMetadata, ItemMetadataAssert.class);
objects.assertNotNull(this.info, itemMetadata); isNotNull();
} }
public ItemMetadataAssert isProperty() { public ItemMetadataAssert isProperty() {
objects.assertEqual(this.info, this.actual.isOfItemType(ItemType.PROPERTY), true); extracting((actual) -> actual.isOfItemType(ItemType.PROPERTY)).isEqualTo(true);
return this; return this;
} }
public ItemMetadataAssert isGroup() { public ItemMetadataAssert isGroup() {
objects.assertEqual(this.info, this.actual.isOfItemType(ItemType.GROUP), true); extracting((actual) -> actual.isOfItemType(ItemType.GROUP)).isEqualTo(true);
return this; return this;
} }
public ItemMetadataAssert hasName(String name) { public ItemMetadataAssert hasName(String name) {
objects.assertEqual(this.info, this.actual.getName(), name); extracting(ItemMetadata::getName).isEqualTo(name);
return this; return this;
} }
public ItemMetadataAssert hasType(String type) { public ItemMetadataAssert hasType(String type) {
objects.assertEqual(this.info, this.actual.getType(), type); extracting(ItemMetadata::getType).isEqualTo(type);
return this; return this;
} }
@ -64,7 +66,7 @@ public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemM
} }
public ItemMetadataAssert hasDescription(String description) { public ItemMetadataAssert hasDescription(String description) {
objects.assertEqual(this.info, this.actual.getDescription(), description); extracting(ItemMetadata::getDescription).isEqualTo(description);
return this; return this;
} }
@ -73,7 +75,7 @@ public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemM
} }
public ItemMetadataAssert hasSourceType(String type) { public ItemMetadataAssert hasSourceType(String type) {
objects.assertEqual(this.info, this.actual.getSourceType(), type); extracting(ItemMetadata::getSourceType).isEqualTo(type);
return this; return this;
} }
@ -82,12 +84,12 @@ public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemM
} }
public ItemMetadataAssert hasSourceMethod(String type) { public ItemMetadataAssert hasSourceMethod(String type) {
objects.assertEqual(this.info, this.actual.getSourceMethod(), type); extracting(ItemMetadata::getSourceMethod).isEqualTo(type);
return this; return this;
} }
public ItemMetadataAssert hasDefaultValue(Object defaultValue) { public ItemMetadataAssert hasDefaultValue(Object defaultValue) {
objects.assertEqual(this.info, this.actual.getDefaultValue(), defaultValue); extracting(ItemMetadata::getDefaultValue).isEqualTo(defaultValue);
return this; return this;
} }
@ -97,27 +99,28 @@ public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemM
} }
public ItemMetadataAssert isDeprecatedWithReason(String reason) { public ItemMetadataAssert isDeprecatedWithReason(String reason) {
ItemDeprecation deprecation = assertItemDeprecation(); assertItemDeprecation().extracting(ItemDeprecation::getReason).isEqualTo(reason);
objects.assertEqual(this.info, deprecation.getReason(), reason);
return this; return this;
} }
public ItemMetadataAssert isDeprecatedWithReplacement(String replacement) { public ItemMetadataAssert isDeprecatedWithReplacement(String replacement) {
ItemDeprecation deprecation = assertItemDeprecation(); assertItemDeprecation().extracting(ItemDeprecation::getReplacement).isEqualTo(replacement);
objects.assertEqual(this.info, deprecation.getReplacement(), replacement);
return this; return this;
} }
public ItemMetadataAssert isNotDeprecated() { public ItemMetadataAssert isNotDeprecated() {
objects.assertNull(this.info, this.actual.getDeprecation()); extracting(ItemMetadata::getDeprecation).isNull();
return this; return this;
} }
private ItemDeprecation assertItemDeprecation() { private ObjectAssert<ItemDeprecation> assertItemDeprecation() {
ItemDeprecation deprecation = this.actual.getDeprecation(); ObjectAssert<ItemDeprecation> itemDeprecationAssert = extracting(ItemMetadata::getDeprecation);
objects.assertNotNull(this.info, deprecation); itemDeprecationAssert.extracting(ItemDeprecation::getLevel).isNull();
objects.assertNull(this.info, deprecation.getLevel()); return itemDeprecationAssert;
return deprecation; }
private <T> ObjectAssert<T> extracting(Function<ItemMetadata, T> extractor) {
return super.extracting(extractor, Assertions::assertThat);
} }
@Override @Override

Loading…
Cancel
Save