Skip to content

Commit 177e11f

Browse files
Add WebTestUtils test runtime hints
Closes gh-12216
1 parent 7094ee3 commit 177e11f

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.test.aot.hint;
18+
19+
import org.springframework.aot.hint.MemberCategory;
20+
import org.springframework.aot.hint.RuntimeHints;
21+
import org.springframework.security.web.FilterChainProxy;
22+
import org.springframework.security.web.context.SecurityContextHolderFilter;
23+
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
24+
import org.springframework.security.web.csrf.CsrfFilter;
25+
import org.springframework.test.context.aot.TestRuntimeHintsRegistrar;
26+
27+
/**
28+
* {@link TestRuntimeHintsRegistrar} implementation that register runtime hints for
29+
* {@link org.springframework.security.test.web.support.WebTestUtils}.
30+
*
31+
* @author Marcus da Coregio
32+
* @since 6.0
33+
*/
34+
class WebTestUtilsTestRuntimeHints implements TestRuntimeHintsRegistrar {
35+
36+
@Override
37+
public void registerHints(RuntimeHints hints, Class<?> testClass, ClassLoader classLoader) {
38+
registerFilterChainProxyHints(hints);
39+
registerSecurityContextRepositoryHints(hints);
40+
registerCsrfTokenRepositoryHints(hints);
41+
}
42+
43+
private void registerFilterChainProxyHints(RuntimeHints hints) {
44+
hints.reflection().registerType(FilterChainProxy.class, MemberCategory.INVOKE_DECLARED_METHODS);
45+
}
46+
47+
private void registerCsrfTokenRepositoryHints(RuntimeHints hints) {
48+
hints.reflection().registerType(CsrfFilter.class, MemberCategory.DECLARED_FIELDS);
49+
}
50+
51+
private void registerSecurityContextRepositoryHints(RuntimeHints hints) {
52+
hints.reflection().registerType(SecurityContextPersistenceFilter.class, MemberCategory.DECLARED_FIELDS);
53+
hints.reflection().registerType(SecurityContextHolderFilter.class, MemberCategory.DECLARED_FIELDS);
54+
}
55+
56+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.springframework.test.context.aot.TestRuntimeHintsRegistrar=\
2-
org.springframework.security.test.aot.hint.WithSecurityContextTestRuntimeHints
2+
org.springframework.security.test.aot.hint.WithSecurityContextTestRuntimeHints,\
3+
org.springframework.security.test.aot.hint.WebTestUtilsTestRuntimeHints
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.test.aot.hint;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.aot.hint.MemberCategory;
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
25+
import org.springframework.core.io.support.SpringFactoriesLoader;
26+
import org.springframework.security.web.FilterChainProxy;
27+
import org.springframework.security.web.context.SecurityContextHolderFilter;
28+
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
29+
import org.springframework.security.web.csrf.CsrfFilter;
30+
import org.springframework.test.context.aot.TestRuntimeHintsRegistrar;
31+
import org.springframework.util.ClassUtils;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Tests for {@link WebTestUtilsTestRuntimeHints}.
37+
*
38+
* @author Marcus da Coregio
39+
*/
40+
class WebTestUtilsTestRuntimeHintsTests {
41+
42+
private final RuntimeHints hints = new RuntimeHints();
43+
44+
@BeforeEach
45+
void setup() {
46+
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories").load(TestRuntimeHintsRegistrar.class)
47+
.forEach((registrar) -> registrar.registerHints(this.hints, WebTestUtilsTestRuntimeHintsTests.class,
48+
ClassUtils.getDefaultClassLoader()));
49+
}
50+
51+
@Test
52+
void filterChainProxyHasHints() {
53+
assertThat(RuntimeHintsPredicates.reflection().onType(FilterChainProxy.class)
54+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
55+
}
56+
57+
@Test
58+
void csrfFilterHasHints() {
59+
assertThat(RuntimeHintsPredicates.reflection().onType(CsrfFilter.class)
60+
.withMemberCategories(MemberCategory.DECLARED_FIELDS)).accepts(this.hints);
61+
}
62+
63+
@Test
64+
void securityContextPersistenceFilterHasHints() {
65+
assertThat(RuntimeHintsPredicates.reflection().onType(SecurityContextPersistenceFilter.class)
66+
.withMemberCategories(MemberCategory.DECLARED_FIELDS)).accepts(this.hints);
67+
}
68+
69+
@Test
70+
void securityContextHolderFilterHasHints() {
71+
assertThat(RuntimeHintsPredicates.reflection().onType(SecurityContextHolderFilter.class)
72+
.withMemberCategories(MemberCategory.DECLARED_FIELDS)).accepts(this.hints);
73+
}
74+
75+
}

0 commit comments

Comments
 (0)