1616
1717package org .springframework .boot .logging .structured ;
1818
19+ import java .io .IOException ;
20+
1921import org .junit .jupiter .api .Test ;
22+ import org .junit .jupiter .params .ParameterizedTest ;
23+ import org .junit .jupiter .params .provider .ValueSource ;
2024
2125import org .springframework .aot .hint .MemberCategory ;
2226import org .springframework .aot .hint .RuntimeHints ;
2630import org .springframework .beans .factory .aot .BeanFactoryInitializationAotContribution ;
2731import org .springframework .beans .factory .aot .BeanFactoryInitializationAotProcessor ;
2832import org .springframework .boot .json .JsonWriter .Members ;
33+ import org .springframework .boot .logging .StackTracePrinter ;
2934import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
3035import org .springframework .core .env .ConfigurableEnvironment ;
3136import org .springframework .mock .env .MockEnvironment ;
3237
3338import static org .assertj .core .api .Assertions .assertThat ;
3439
3540/**
36- * Tests for
37- * {@link StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor}.
41+ * Tests for {@link StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor}.
3842 *
3943 * @author Dmytro Nosan
4044 */
41- class StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorTests {
45+ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests {
4246
4347 @ Test
44- void structuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorIsRegistered () {
48+ void structuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorIsRegistered () {
4549 assertThat (AotServices .factories ().load (BeanFactoryInitializationAotProcessor .class ))
46- .anyMatch (StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor .class ::isInstance );
50+ .anyMatch (StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor .class ::isInstance );
4751 }
4852
4953 @ Test
50- void shouldRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints () {
54+ void shouldRegisterRuntimeHintsWhenCustomizerIsPresent () {
5155 MockEnvironment environment = new MockEnvironment ();
5256 environment .setProperty ("logging.structured.json.customizer" , TestCustomizer .class .getName ());
5357
@@ -63,14 +67,40 @@ void shouldRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints() {
6367 }
6468
6569 @ Test
66- void shouldNotRegisterStructuredLoggingJsonMembersCustomizerRuntimeHintsWhenPropertiesAreNotSet () {
70+ void shouldRegisterRuntimeHintsWhenStackTracePrinterIsPresent () {
71+ MockEnvironment environment = new MockEnvironment ();
72+ environment .setProperty ("logging.structured.json.stacktrace.printer" , TestStackTracePrinter .class .getName ());
73+
74+ BeanFactoryInitializationAotContribution contribution = getContribution (environment );
75+ assertThat (contribution ).isNotNull ();
76+
77+ RuntimeHints hints = getRuntimeHints (contribution );
78+ assertThat (RuntimeHintsPredicates .reflection ()
79+ .onType (TestStackTracePrinter .class )
80+ .withMemberCategories (MemberCategory .INVOKE_DECLARED_CONSTRUCTORS ,
81+ MemberCategory .INVOKE_PUBLIC_CONSTRUCTORS ))
82+ .accepts (hints );
83+ }
84+
85+ @ ParameterizedTest
86+ @ ValueSource (strings = { "logging-system" , "standard" })
87+ void shouldNotRegisterRuntimeHintsWhenStackTracePrinterIsNotCustomImplementation (String printer ) {
6788 MockEnvironment environment = new MockEnvironment ();
89+ environment .setProperty ("logging.structured.json.stacktrace.printer" , printer );
90+
6891 BeanFactoryInitializationAotContribution contribution = getContribution (environment );
6992 assertThat (contribution ).isNull ();
7093 }
7194
7295 @ Test
73- void shouldNotRegisterStructuredLoggingJsonMembersCustomizerRuntimeHintsWhenCustomizerIsNotSet () {
96+ void shouldNotRegisterRuntimeHintsWhenPropertiesAreNotSet () {
97+ MockEnvironment environment = new MockEnvironment ();
98+ BeanFactoryInitializationAotContribution contribution = getContribution (environment );
99+ assertThat (contribution ).isNull ();
100+ }
101+
102+ @ Test
103+ void shouldNotRegisterRuntimeHintsWhenCustomizerAndPrinterAreNotSet () {
74104 MockEnvironment environment = new MockEnvironment ();
75105 environment .setProperty ("logging.structured.json.exclude" , "something" );
76106 BeanFactoryInitializationAotContribution contribution = getContribution (environment );
@@ -81,7 +111,7 @@ private BeanFactoryInitializationAotContribution getContribution(ConfigurableEnv
81111 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ()) {
82112 context .setEnvironment (environment );
83113 context .refresh ();
84- return new StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor ()
114+ return new StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor ()
85115 .processAheadOfTime (context .getBeanFactory ());
86116 }
87117 }
@@ -100,4 +130,13 @@ public void customize(Members<String> members) {
100130
101131 }
102132
133+ static class TestStackTracePrinter implements StackTracePrinter {
134+
135+ @ Override
136+ public void printStackTrace (Throwable throwable , Appendable out ) throws IOException {
137+
138+ }
139+
140+ }
141+
103142}
0 commit comments