Skip to content

Commit

Permalink
Specify generic type nullness in spring-test
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Jan 14, 2025
1 parent 5be36ce commit e2216dd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -106,7 +106,7 @@ public WebMergedContextConfiguration(MergedContextConfiguration mergedConfig, St
* {@link #WebMergedContextConfiguration(Class, String[], Class[], Set, String[], List, String[], Set, String, ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)}
*/
@Deprecated(since = "6.1")
public WebMergedContextConfiguration(Class<?> testClass, String @Nullable [] locations, @Nullable Class<?>[] classes,
public WebMergedContextConfiguration(Class<?> testClass, String @Nullable [] locations, Class<?> @Nullable [] classes,
@Nullable Set<Class<? extends ApplicationContextInitializer<?>>> contextInitializerClasses,
String @Nullable [] activeProfiles, String @Nullable [] propertySourceLocations, String @Nullable [] propertySourceProperties,
String resourceBasePath, ContextLoader contextLoader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,20 +547,21 @@ protected EntityExchangeResult<B> getResult() {
}

@Override
public <T extends S> T isEqualTo(B expected) {
public <T extends S> T isEqualTo(@Nullable B expected) {
this.result.assertWithDiagnostics(() ->
AssertionErrors.assertEquals("Response body", expected, this.result.getResponseBody()));
return self();
}

@Override
public <T extends S> T value(Matcher<? super B> matcher) {
public <T extends S> T value(Matcher<? super @Nullable B> matcher) {
this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
return self();
}

@Override
public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher) {
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
public <T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher) {
this.result.assertWithDiagnostics(() -> {
B body = this.result.getResponseBody();
MatcherAssert.assertThat(bodyMapper.apply(body), matcher);
Expand All @@ -569,7 +570,8 @@ public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> ma
}

@Override
public <T extends S> T value(Consumer<B> consumer) {
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
public <T extends S> T value(Consumer<@Nullable B> consumer) {
this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody()));
return self();
}
Expand All @@ -592,7 +594,7 @@ public EntityExchangeResult<B> returnResult() {
}


private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<E>, ListBodySpec<E>>
private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<@Nullable E>, ListBodySpec<E>>
implements ListBodySpec<E> {

DefaultListBodySpec(EntityExchangeResult<List<E>> result) {
Expand All @@ -601,7 +603,7 @@ private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<E>, Lis

@Override
public ListBodySpec<E> hasSize(int size) {
List<E> actual = getResult().getResponseBody();
List<@Nullable E> actual = getResult().getResponseBody();
String message = "Response body does not contain " + size + " elements";
getResult().assertWithDiagnostics(() ->
AssertionErrors.assertEquals(message, size, (actual != null ? actual.size() : 0)));
Expand All @@ -610,9 +612,9 @@ public ListBodySpec<E> hasSize(int size) {

@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> contains(E... elements) {
public ListBodySpec<E> contains(@Nullable E... elements) {
List<E> expected = Arrays.asList(elements);
List<E> actual = getResult().getResponseBody();
List<@Nullable E> actual = getResult().getResponseBody();
String message = "Response body does not contain " + expected;
getResult().assertWithDiagnostics(() ->
AssertionErrors.assertTrue(message, (actual != null && actual.containsAll(expected))));
Expand All @@ -621,17 +623,17 @@ public ListBodySpec<E> contains(E... elements) {

@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> doesNotContain(E... elements) {
public ListBodySpec<E> doesNotContain(@Nullable E... elements) {
List<E> expected = Arrays.asList(elements);
List<E> actual = getResult().getResponseBody();
List<@Nullable E> actual = getResult().getResponseBody();
String message = "Response body should not have contained " + expected;
getResult().assertWithDiagnostics(() ->
AssertionErrors.assertTrue(message, (actual == null || !actual.containsAll(expected))));
return this;
}

@Override
public EntityExchangeResult<List<E>> returnResult() {
public EntityExchangeResult<List<@Nullable E>> returnResult() {
return getResult();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -918,26 +918,26 @@ interface BodySpec<B, S extends BodySpec<B, S>> {
/**
* Assert the extracted body is equal to the given value.
*/
<T extends S> T isEqualTo(B expected);
<T extends S> T isEqualTo(@Nullable B expected);

/**
* Assert the extracted body with a {@link Matcher}.
* @since 5.1
*/
<T extends S> T value(Matcher<? super B> matcher);
<T extends S> T value(Matcher<? super @Nullable B> matcher);

/**
* Transform the extracted the body with a function, for example, extracting a
* property, and assert the mapped value with a {@link Matcher}.
* @since 5.1
*/
<T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher);
<T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher);

/**
* Assert the extracted body with a {@link Consumer}.
* @since 5.1
*/
<T extends S> T value(Consumer<B> consumer);
<T extends S> T value(Consumer<@Nullable B> consumer);

/**
* Assert the exchange result with the given {@link Consumer}.
Expand All @@ -957,7 +957,7 @@ interface BodySpec<B, S extends BodySpec<B, S>> {
*
* @param <E> the body list element type
*/
interface ListBodySpec<E> extends BodySpec<List<E>, ListBodySpec<E>> {
interface ListBodySpec<E> extends BodySpec<List<@Nullable E>, ListBodySpec<E>> {

/**
* Assert the extracted list of values is of the given size.
Expand All @@ -970,14 +970,14 @@ interface ListBodySpec<E> extends BodySpec<List<E>, ListBodySpec<E>> {
* @param elements the elements to check
*/
@SuppressWarnings("unchecked")
ListBodySpec<E> contains(E... elements);
ListBodySpec<E> contains(@Nullable E... elements);

/**
* Assert the extracted list of values doesn't contain the given elements.
* @param elements the elements to check
*/
@SuppressWarnings("unchecked")
ListBodySpec<E> doesNotContain(E... elements);
ListBodySpec<E> doesNotContain(@Nullable E... elements);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -58,7 +58,7 @@ final class MockMvcFilterDecorator implements Filter {

private final Filter delegate;

private final @Nullable Function<ServletContext, FilterConfig> filterConfigInitializer;
private final @Nullable Function<@Nullable ServletContext, FilterConfig> filterConfigInitializer;

private final @Nullable EnumSet<DispatcherType> dispatcherTypes;

Expand Down Expand Up @@ -103,7 +103,7 @@ public MockMvcFilterDecorator(
this.hasPatterns = initPatterns(urlPatterns);
}

private static Function<ServletContext, FilterConfig> getFilterConfigInitializer(
private static Function<@Nullable ServletContext, FilterConfig> getFilterConfigInitializer(
Filter delegate, @Nullable String filterName, @Nullable Map<String, String> initParams) {

String className = delegate.getClass().getName();
Expand Down

0 comments on commit e2216dd

Please sign in to comment.