Merge pull request #22261 from izeye

* pr/22261:
  Replace ReflectionUtils with AssertJ's extracting()

Closes gh-22261
pull/22266/head
Stephane Nicoll 4 years ago
commit 456cb2da51

@ -16,10 +16,9 @@
package org.springframework.boot.autoconfigure.security.servlet; package org.springframework.boot.autoconfigure.security.servlet;
import java.util.EnumSet;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -44,7 +43,6 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension; import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
import org.springframework.security.web.FilterChainProxy; import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -143,11 +141,9 @@ class SecurityAutoConfigurationTests {
.run((context) -> { .run((context) -> {
DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration",
DelegatingFilterProxyRegistrationBean.class); DelegatingFilterProxyRegistrationBean.class);
@SuppressWarnings("unchecked") assertThat(bean)
EnumSet<DispatcherType> dispatcherTypes = (EnumSet<DispatcherType>) ReflectionTestUtils .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class))
.getField(bean, "dispatcherTypes"); .containsOnly(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST);
assertThat(dispatcherTypes).containsOnly(DispatcherType.ASYNC, DispatcherType.ERROR,
DispatcherType.REQUEST);
}); });
} }
@ -157,10 +153,9 @@ class SecurityAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(SecurityFilterAutoConfiguration.class)).run((context) -> { .withConfiguration(AutoConfigurations.of(SecurityFilterAutoConfiguration.class)).run((context) -> {
DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration",
DelegatingFilterProxyRegistrationBean.class); DelegatingFilterProxyRegistrationBean.class);
@SuppressWarnings("unchecked") assertThat(bean)
EnumSet<DispatcherType> dispatcherTypes = (EnumSet<DispatcherType>) ReflectionTestUtils .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class))
.getField(bean, "dispatcherTypes"); .containsOnly(DispatcherType.INCLUDE, DispatcherType.ERROR);
assertThat(dispatcherTypes).containsOnly(DispatcherType.INCLUDE, DispatcherType.ERROR);
}); });
} }
@ -170,10 +165,9 @@ class SecurityAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(SecurityFilterAutoConfiguration.class)).run((context) -> { .withConfiguration(AutoConfigurations.of(SecurityFilterAutoConfiguration.class)).run((context) -> {
DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration",
DelegatingFilterProxyRegistrationBean.class); DelegatingFilterProxyRegistrationBean.class);
@SuppressWarnings("unchecked") assertThat(bean)
EnumSet<DispatcherType> dispatcherTypes = (EnumSet<DispatcherType>) ReflectionTestUtils .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class))
.getField(bean, "dispatcherTypes"); .isEmpty();
assertThat(dispatcherTypes).isEmpty();
}); });
} }

@ -17,10 +17,10 @@
package org.springframework.boot.autoconfigure.session; package org.springframework.boot.autoconfigure.session;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
@ -41,7 +41,6 @@ import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver; import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
import org.springframework.session.web.http.HttpSessionIdResolver; import org.springframework.session.web.http.HttpSessionIdResolver;
import org.springframework.session.web.http.SessionRepositoryFilter; import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@ -112,13 +111,13 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest
.run((context) -> assertThat(context.getBean(SessionProperties.class).getTimeout()).hasSeconds(3)); .run((context) -> assertThat(context.getBean(SessionProperties.class).getTimeout()).hasSeconds(3));
} }
@SuppressWarnings("unchecked")
@Test @Test
void filterIsRegisteredWithAsyncErrorAndRequestDispatcherTypes() { void filterIsRegisteredWithAsyncErrorAndRequestDispatcherTypes() {
this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class).run((context) -> {
FilterRegistrationBean<?> registration = context.getBean(FilterRegistrationBean.class); FilterRegistrationBean<?> registration = context.getBean(FilterRegistrationBean.class);
assertThat(registration.getFilter()).isSameAs(context.getBean(SessionRepositoryFilter.class)); assertThat(registration.getFilter()).isSameAs(context.getBean(SessionRepositoryFilter.class));
assertThat((EnumSet<DispatcherType>) ReflectionTestUtils.getField(registration, "dispatcherTypes")) assertThat(registration)
.extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class))
.containsOnly(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST); .containsOnly(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST);
}); });
} }
@ -132,25 +131,25 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest
}); });
} }
@SuppressWarnings("unchecked")
@Test @Test
void filterDispatcherTypesCanBeCustomized() { void filterDispatcherTypesCanBeCustomized() {
this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class) this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class)
.withPropertyValues("spring.session.servlet.filter-dispatcher-types=error, request").run((context) -> { .withPropertyValues("spring.session.servlet.filter-dispatcher-types=error, request").run((context) -> {
FilterRegistrationBean<?> registration = context.getBean(FilterRegistrationBean.class); FilterRegistrationBean<?> registration = context.getBean(FilterRegistrationBean.class);
assertThat((EnumSet<DispatcherType>) ReflectionTestUtils.getField(registration, "dispatcherTypes")) assertThat(registration)
.extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class))
.containsOnly(DispatcherType.ERROR, DispatcherType.REQUEST); .containsOnly(DispatcherType.ERROR, DispatcherType.REQUEST);
}); });
} }
@SuppressWarnings("unchecked")
@Test @Test
void emptyFilterDispatcherTypesDoNotThrowException() { void emptyFilterDispatcherTypesDoNotThrowException() {
this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class) this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class)
.withPropertyValues("spring.session.servlet.filter-dispatcher-types=").run((context) -> { .withPropertyValues("spring.session.servlet.filter-dispatcher-types=").run((context) -> {
FilterRegistrationBean<?> registration = context.getBean(FilterRegistrationBean.class); FilterRegistrationBean<?> registration = context.getBean(FilterRegistrationBean.class);
Object dispatcherTypes = ReflectionTestUtils.getField(registration, "dispatcherTypes"); assertThat(registration)
assertThat((EnumSet<DispatcherType>) dispatcherTypes).isEmpty(); .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class))
.isEmpty();
}); });
} }
@ -177,11 +176,8 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest
this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class) this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class)
.withPropertyValues("server.port=0").run((context) -> { .withPropertyValues("server.port=0").run((context) -> {
SessionRepositoryFilter<?> filter = context.getBean(SessionRepositoryFilter.class); SessionRepositoryFilter<?> filter = context.getBean(SessionRepositoryFilter.class);
CookieHttpSessionIdResolver sessionIdResolver = (CookieHttpSessionIdResolver) ReflectionTestUtils assertThat(filter).extracting("httpSessionIdResolver").extracting("cookieSerializer")
.getField(filter, "httpSessionIdResolver"); .isSameAs(context.getBean(DefaultCookieSerializer.class));
DefaultCookieSerializer cookieSerializer = (DefaultCookieSerializer) ReflectionTestUtils
.getField(sessionIdResolver, "cookieSerializer");
assertThat(cookieSerializer).isSameAs(context.getBean(DefaultCookieSerializer.class));
}); });
} }

Loading…
Cancel
Save