Skip to content

Commit 7094ee3

Browse files
Add runtime hints for annotations using @WithSecurityContext
Closes gh-12215
1 parent 063f06e commit 7094ee3

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

etc/checkstyle/checkstyle.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<property name="avoidStaticImportExcludes" value="org.springframework.security.web.csrf.CsrfTokenAssert.*" />
2121
<property name="avoidStaticImportExcludes" value="org.springframework.security.web.util.matcher.AntPathRequestMatcher.*" />
2222
<property name="avoidStaticImportExcludes" value="org.springframework.security.web.util.matcher.RegexRequestMatcher.*" />
23+
<property name="avoidStaticImportExcludes" value="org.springframework.core.annotation.MergedAnnotations.SearchStrategy.*" />
2324
</module>
2425
<module name="com.puppycrawl.tools.checkstyle.TreeWalker">
2526
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 java.util.Arrays;
20+
21+
import org.springframework.aot.hint.MemberCategory;
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.core.annotation.MergedAnnotation;
24+
import org.springframework.core.annotation.MergedAnnotations;
25+
import org.springframework.security.test.context.support.WithSecurityContext;
26+
import org.springframework.test.context.aot.TestRuntimeHintsRegistrar;
27+
28+
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.SUPERCLASS;
29+
30+
/**
31+
* {@link TestRuntimeHintsRegistrar} implementation that register runtime hints for
32+
* {@link WithSecurityContext#factory()} classes.
33+
*
34+
* @author Marcus da Coregio
35+
* @since 6.0
36+
*/
37+
class WithSecurityContextTestRuntimeHints implements TestRuntimeHintsRegistrar {
38+
39+
@Override
40+
public void registerHints(RuntimeHints hints, Class<?> testClass, ClassLoader classLoader) {
41+
Arrays.stream(testClass.getDeclaredMethods())
42+
.map((method) -> MergedAnnotations.from(method, SUPERCLASS).get(WithSecurityContext.class))
43+
.filter(MergedAnnotation::isPresent)
44+
.map((withSecurityContext) -> withSecurityContext.getClass("factory"))
45+
.forEach((factory) -> registerDeclaredConstructors(hints, factory));
46+
}
47+
48+
private void registerDeclaredConstructors(RuntimeHints hints, Class<?> factory) {
49+
hints.reflection().registerType(factory, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
50+
}
51+
52+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.test.context.aot.TestRuntimeHintsRegistrar=\
2+
org.springframework.security.test.aot.hint.WithSecurityContextTestRuntimeHints
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.aot.hint.MemberCategory;
26+
import org.springframework.aot.hint.RuntimeHints;
27+
import org.springframework.aot.hint.TypeReference;
28+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
29+
import org.springframework.security.core.context.SecurityContext;
30+
import org.springframework.security.core.context.SecurityContextHolder;
31+
import org.springframework.security.test.context.support.WithAnonymousUser;
32+
import org.springframework.security.test.context.support.WithMockUser;
33+
import org.springframework.security.test.context.support.WithSecurityContext;
34+
import org.springframework.security.test.context.support.WithSecurityContextFactory;
35+
import org.springframework.security.test.context.support.WithUserDetails;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
39+
/**
40+
* Tests for {@link WithSecurityContextTestRuntimeHints}.
41+
*/
42+
class WithSecurityContextTestRuntimeHintsTests {
43+
44+
private final RuntimeHints hints = new RuntimeHints();
45+
46+
private final WithSecurityContextTestRuntimeHints registrar = new WithSecurityContextTestRuntimeHints();
47+
48+
@BeforeEach
49+
void setup() {
50+
this.registrar.registerHints(this.hints, WithSecurityContextTestRuntimeHintsTests.class,
51+
WithSecurityContextTestRuntimeHintsTests.class.getClassLoader());
52+
}
53+
54+
@Test
55+
@WithMockUser
56+
void withMockUserHasHints() {
57+
assertThat(RuntimeHintsPredicates.reflection()
58+
.onType(TypeReference
59+
.of("org.springframework.security.test.context.support.WithMockUserSecurityContextFactory"))
60+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
61+
}
62+
63+
@Test
64+
@WithAnonymousUser
65+
void withAnonymousUserHasHints() {
66+
assertThat(RuntimeHintsPredicates.reflection()
67+
.onType(TypeReference.of(
68+
"org.springframework.security.test.context.support.WithAnonymousUserSecurityContextFactory"))
69+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
70+
}
71+
72+
@Test
73+
@WithUserDetails
74+
void withUserDetailsHasHints() {
75+
assertThat(RuntimeHintsPredicates.reflection()
76+
.onType(TypeReference
77+
.of("org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory"))
78+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
79+
}
80+
81+
@Test
82+
@WithMockTestUser
83+
void withMockTestUserHasHints() {
84+
assertThat(RuntimeHintsPredicates.reflection().onType(WithMockTestUserSecurityContextFactory.class)
85+
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
86+
}
87+
88+
@Retention(RetentionPolicy.RUNTIME)
89+
@WithSecurityContext(factory = WithMockTestUserSecurityContextFactory.class)
90+
@interface WithMockTestUser {
91+
92+
}
93+
94+
static class WithMockTestUserSecurityContextFactory implements WithSecurityContextFactory<WithMockTestUser> {
95+
96+
@Override
97+
public SecurityContext createSecurityContext(WithMockTestUser annotation) {
98+
return SecurityContextHolder.createEmptyContext();
99+
}
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)