16
16
17
17
package org .springframework .boot .logging .structured ;
18
18
19
+ import java .io .IOException ;
20
+
19
21
import org .junit .jupiter .api .Test ;
22
+ import org .junit .jupiter .params .ParameterizedTest ;
23
+ import org .junit .jupiter .params .provider .ValueSource ;
20
24
21
25
import org .springframework .aot .hint .MemberCategory ;
22
26
import org .springframework .aot .hint .RuntimeHints ;
26
30
import org .springframework .beans .factory .aot .BeanFactoryInitializationAotContribution ;
27
31
import org .springframework .beans .factory .aot .BeanFactoryInitializationAotProcessor ;
28
32
import org .springframework .boot .json .JsonWriter .Members ;
33
+ import org .springframework .boot .logging .StackTracePrinter ;
29
34
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
30
35
import org .springframework .core .env .ConfigurableEnvironment ;
31
36
import org .springframework .mock .env .MockEnvironment ;
32
37
33
38
import static org .assertj .core .api .Assertions .assertThat ;
34
39
35
40
/**
36
- * Tests for
37
- * {@link StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor}.
41
+ * Tests for {@link StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor}.
38
42
*
39
43
* @author Dmytro Nosan
40
44
*/
41
- class StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorTests {
45
+ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests {
42
46
43
47
@ Test
44
- void structuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorIsRegistered () {
48
+ void structuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorIsRegistered () {
45
49
assertThat (AotServices .factories ().load (BeanFactoryInitializationAotProcessor .class ))
46
- .anyMatch (StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor .class ::isInstance );
50
+ .anyMatch (StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor .class ::isInstance );
47
51
}
48
52
49
53
@ Test
50
- void shouldRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints () {
54
+ void shouldRegisterRuntimeHintsWhenCustomizerIsPresent () {
51
55
MockEnvironment environment = new MockEnvironment ();
52
56
environment .setProperty ("logging.structured.json.customizer" , TestCustomizer .class .getName ());
53
57
@@ -63,14 +67,40 @@ void shouldRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints() {
63
67
}
64
68
65
69
@ Test
66
- void shouldNotRegisterStructuredLoggingJsonMembersCustomizerRuntimeHintsWhenPropertiesAreNotSet () {
70
+ void shouldRegisterRuntimeHintsWhenCustomStackTracePrinterIsPresent () {
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 ) {
67
88
MockEnvironment environment = new MockEnvironment ();
89
+ environment .setProperty ("logging.structured.json.stacktrace.printer" , printer );
90
+
68
91
BeanFactoryInitializationAotContribution contribution = getContribution (environment );
69
92
assertThat (contribution ).isNull ();
70
93
}
71
94
72
95
@ 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 () {
74
104
MockEnvironment environment = new MockEnvironment ();
75
105
environment .setProperty ("logging.structured.json.exclude" , "something" );
76
106
BeanFactoryInitializationAotContribution contribution = getContribution (environment );
@@ -81,7 +111,7 @@ private BeanFactoryInitializationAotContribution getContribution(ConfigurableEnv
81
111
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ()) {
82
112
context .setEnvironment (environment );
83
113
context .refresh ();
84
- return new StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor ()
114
+ return new StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor ()
85
115
.processAheadOfTime (context .getBeanFactory ());
86
116
}
87
117
}
@@ -100,4 +130,13 @@ public void customize(Members<String> members) {
100
130
101
131
}
102
132
133
+ static class TestStackTracePrinter implements StackTracePrinter {
134
+
135
+ @ Override
136
+ public void printStackTrace (Throwable throwable , Appendable out ) throws IOException {
137
+
138
+ }
139
+
140
+ }
141
+
103
142
}
0 commit comments