Polish RuntimeHintsRegistrar

Add RuntimeHints suffix to classes
Make classes package-private
Use ReflectionUtilsPredicates#serialization

Closes gh-2111
Closes gh-2112
This commit is contained in:
Marcus Da Coregio
2022-07-13 10:30:46 -03:00
parent 374ea4a432
commit 65b994cad1
11 changed files with 40 additions and 112 deletions

View File

@@ -44,7 +44,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
*
* @author Marcus Da Coregio
*/
public class CommonSessionSecurityHints implements RuntimeHintsRegistrar {
class CommonSessionSecurityRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {

View File

@@ -26,7 +26,7 @@ import org.springframework.util.ClassUtils;
*
* @author Marcus Da Coregio
*/
public class WebSessionSecurityHints implements RuntimeHintsRegistrar {
class WebSessionSecurityRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {

View File

@@ -34,7 +34,7 @@ import org.springframework.util.ClassUtils;
*
* @author Marcus Da Coregio
*/
public class HttpSessionSecurityHints implements RuntimeHintsRegistrar {
class HttpSessionSecurityRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {

View File

@@ -1,4 +1,4 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.session.aot.hint.CommonSessionSecurityHints,\
org.springframework.session.aot.hint.servlet.HttpSessionSecurityHints,\
org.springframework.session.aot.hint.server.WebSessionSecurityHints
org.springframework.session.aot.hint.CommonSessionSecurityRuntimeHints,\
org.springframework.session.aot.hint.servlet.HttpSessionSecurityRuntimeHints,\
org.springframework.session.aot.hint.server.WebSessionSecurityRuntimeHints

View File

@@ -27,6 +27,7 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AccountExpiredException;
@@ -47,28 +48,28 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link CommonSessionSecurityHints}
* Tests for {@link CommonSessionSecurityRuntimeHints}
*
* @author Marcus Da Coregio
*/
class CommonSessionSecurityHintsTests {
class CommonSessionSecurityRuntimeHintsTests {
private final RuntimeHints hints = new RuntimeHints();
private final CommonSessionSecurityHints commonSessionSecurityHints = new CommonSessionSecurityHints();
private final CommonSessionSecurityRuntimeHints commonSessionSecurityRuntimeHints = new CommonSessionSecurityRuntimeHints();
@ParameterizedTest
@MethodSource("getSerializationHintTypes")
void coreTypesHasHints(TypeReference typeReference) {
this.commonSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(new SerializationHintsPredicates().onType(typeReference)).accepts(this.hints);
this.commonSessionSecurityRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.serialization().onType(typeReference)).accepts(this.hints);
}
@Test
void aotFactoriesContainsRegistrar() {
boolean match = SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
.load(RuntimeHintsRegistrar.class).stream()
.anyMatch((registrar) -> registrar instanceof CommonSessionSecurityHints);
.anyMatch((registrar) -> registrar instanceof CommonSessionSecurityRuntimeHints);
assertThat(match).isTrue();
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright 2014-2022 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.aot.hint;
import java.util.function.Predicate;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsPredicates;
import org.springframework.aot.hint.SerializationHints;
import org.springframework.aot.hint.TypeReference;
import org.springframework.util.Assert;
/**
* Generator of {@link SerializationHints} predicates, testing whether the given hints
* match the expected behavior for serialization.
*
* @author Marcus Da Coregio
* @see RuntimeHintsPredicates
*/
public class SerializationHintsPredicates {
/**
* Return a predicate that checks whether a serialization hint is registered for the
* given type.
* @param typeReference the type
* @return the {@link RuntimeHints} predicate
*/
public TypeHintPredicate onType(TypeReference typeReference) {
Assert.notNull(typeReference, "'typeReference' should not be null");
return new TypeHintPredicate(typeReference);
}
/**
* Return a predicate that checks whether a serialization hint is registered for the
* given type.
* @param type the type
* @return the {@link RuntimeHints} predicate
*/
public TypeHintPredicate onType(Class<?> type) {
Assert.notNull(type, "'type' should not be null");
return new TypeHintPredicate(TypeReference.of(type));
}
public static class TypeHintPredicate implements Predicate<RuntimeHints> {
private final TypeReference type;
TypeHintPredicate(TypeReference type) {
this.type = type;
}
@Override
public boolean test(RuntimeHints hints) {
return hints.serialization().javaSerialization().anyMatch((hint) -> hint.getType().equals(this.type));
}
}
}

View File

@@ -21,9 +21,9 @@ import org.mockito.MockedStatic;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.security.web.server.csrf.DefaultCsrfToken;
import org.springframework.session.aot.hint.SerializationHintsPredicates;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,20 +32,20 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockStatic;
/**
* Tests for {@link WebSessionSecurityHints}
* Tests for {@link WebSessionSecurityRuntimeHints}
*
* @author Marcus Da Coregio
*/
class WebSessionSecurityHintsTests {
class WebSessionSecurityRuntimeHintsTests {
private final RuntimeHints hints = new RuntimeHints();
private final WebSessionSecurityHints webSessionSecurityHints = new WebSessionSecurityHints();
private final WebSessionSecurityRuntimeHints webSessionSecurityRuntimeHints = new WebSessionSecurityRuntimeHints();
@Test
void defaultCsrfTokenHasHints() {
this.webSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(new SerializationHintsPredicates().onType(DefaultCsrfToken.class)).accepts(this.hints);
this.webSessionSecurityRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.serialization().onType(DefaultCsrfToken.class)).accepts(this.hints);
}
@Test
@@ -53,7 +53,7 @@ class WebSessionSecurityHintsTests {
try (MockedStatic<ClassUtils> classUtilsMock = mockStatic(ClassUtils.class)) {
classUtilsMock.when(() -> ClassUtils.isPresent(eq("org.springframework.web.server.WebSession"), any()))
.thenReturn(false);
this.webSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
this.webSessionSecurityRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(this.hints.serialization().javaSerialization()).isEmpty();
}
}
@@ -65,7 +65,7 @@ class WebSessionSecurityHintsTests {
.when(() -> ClassUtils
.isPresent(eq("org.springframework.security.web.server.csrf.DefaultCsrfToken"), any()))
.thenReturn(false);
this.webSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
this.webSessionSecurityRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(this.hints.serialization().javaSerialization()).isEmpty();
}
}
@@ -74,7 +74,7 @@ class WebSessionSecurityHintsTests {
void aotFactoriesContainsRegistrar() {
boolean match = SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
.load(RuntimeHintsRegistrar.class).stream()
.anyMatch((registrar) -> registrar instanceof WebSessionSecurityHints);
.anyMatch((registrar) -> registrar instanceof WebSessionSecurityRuntimeHints);
assertThat(match).isTrue();
}

View File

@@ -28,12 +28,12 @@ import org.mockito.MockedStatic;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.csrf.DefaultCsrfToken;
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
import org.springframework.security.web.savedrequest.SavedCookie;
import org.springframework.session.aot.hint.SerializationHintsPredicates;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -42,21 +42,21 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockStatic;
/**
* Tests for {@link HttpSessionSecurityHints}
* Tests for {@link HttpSessionSecurityRuntimeHints}
*
* @author Marcus Da Coregio
*/
class HttpSessionSecurityHintsTests {
class HttpSessionSecurityRuntimeHintsTests {
private final RuntimeHints hints = new RuntimeHints();
private final HttpSessionSecurityHints httpSessionSecurityHints = new HttpSessionSecurityHints();
private final HttpSessionSecurityRuntimeHints httpSessionSecurityRuntimeHints = new HttpSessionSecurityRuntimeHints();
@ParameterizedTest
@MethodSource("getSerializationHintTypes")
void httpSessionHasHints(TypeReference typeReference) {
this.httpSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(new SerializationHintsPredicates().onType(typeReference)).accepts(this.hints);
this.httpSessionSecurityRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.serialization().onType(typeReference)).accepts(this.hints);
}
@Test
@@ -64,7 +64,7 @@ class HttpSessionSecurityHintsTests {
try (MockedStatic<ClassUtils> classUtilsMock = mockStatic(ClassUtils.class)) {
classUtilsMock.when(() -> ClassUtils.isPresent(eq("jakarta.servlet.http.HttpSession"), any()))
.thenReturn(false);
this.httpSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
this.httpSessionSecurityRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(this.hints.serialization().javaSerialization()).isEmpty();
}
}
@@ -75,7 +75,7 @@ class HttpSessionSecurityHintsTests {
classUtilsMock.when(
() -> ClassUtils.isPresent(eq("org.springframework.security.web.csrf.DefaultCsrfToken"), any()))
.thenReturn(false);
this.httpSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader());
this.httpSessionSecurityRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(this.hints.serialization().javaSerialization()).isEmpty();
}
}
@@ -84,7 +84,7 @@ class HttpSessionSecurityHintsTests {
void aotFactoriesContainsRegistrar() {
boolean match = SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
.load(RuntimeHintsRegistrar.class).stream()
.anyMatch((registrar) -> registrar instanceof HttpSessionSecurityHints);
.anyMatch((registrar) -> registrar instanceof HttpSessionSecurityRuntimeHints);
assertThat(match).isTrue();
}

View File

@@ -26,7 +26,7 @@ import org.springframework.aot.hint.TypeReference;
*
* @author Marcus Da Coregio
*/
public class SessionJdbcSecurityHints implements RuntimeHintsRegistrar {
class SessionJdbcRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {

View File

@@ -1,2 +1,2 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.session.jdbc.aot.hint.SessionJdbcSecurityHints
org.springframework.session.jdbc.aot.hint.SessionJdbcRuntimeHints

View File

@@ -19,42 +19,42 @@ package org.springframework.session.jdbc.aot.hint;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsPredicates;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.core.io.support.SpringFactoriesLoader;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SessionJdbcSecurityHints}
* Tests for {@link SessionJdbcRuntimeHints}
*
* @author Marcus Da Coregio
*/
class SessionJdbcSecurityHintsTests {
class SessionJdbcRuntimeHintsTests {
private final RuntimeHints hints = new RuntimeHints();
private final SessionJdbcSecurityHints sessionJdbcSecurityHints = new SessionJdbcSecurityHints();
private final SessionJdbcRuntimeHints sessionJdbcRuntimeHints = new SessionJdbcRuntimeHints();
@Test
void aotFactoriesContainsRegistrar() {
boolean match = SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
.load(RuntimeHintsRegistrar.class).stream()
.anyMatch((registrar) -> registrar instanceof SessionJdbcSecurityHints);
.anyMatch((registrar) -> registrar instanceof SessionJdbcRuntimeHints);
assertThat(match).isTrue();
}
@Test
void jdbcSchemasHasHints() {
this.sessionJdbcSecurityHints.registerHints(this.hints, getClass().getClassLoader());
this.sessionJdbcRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.resource().forResource("org/springframework/session/jdbc/schema.sql"))
.accepts(this.hints);
}
@Test
void dataSourceHasHints() {
this.sessionJdbcSecurityHints.registerHints(this.hints, getClass().getClassLoader());
this.sessionJdbcRuntimeHints.registerHints(this.hints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection().onType(TypeReference.of("javax.sql.DataSource")))
.accepts(this.hints);
}