7
7
import org .apache .logging .log4j .test .appender .ListAppender ;
8
8
import org .junit .Before ;
9
9
import org .junit .ClassRule ;
10
+ import org .junit .Rule ;
10
11
import org .junit .Test ;
11
12
12
13
import java .util .Arrays ;
13
14
import java .util .List ;
15
+ import java .util .Objects ;
14
16
import java .util .Properties ;
15
17
import java .util .function .BiConsumer ;
18
+
16
19
import org .apache .logging .log4j .Level ;
20
+ import org .logstash .ObjectMappers ;
17
21
18
22
import static org .assertj .core .api .Assertions .*;
19
23
import static org .logstash .jackson .StreamReadConstraintsUtil .Override .*;
@@ -23,13 +27,12 @@ public class StreamReadConstraintsUtilTest {
23
27
@ ClassRule
24
28
public static final LoggerContextRule LOGGER_CONTEXT_RULE = new LoggerContextRule ("log4j2-log-stream-read-constraints.xml" );
25
29
30
+ @ Rule
31
+ public final DefaultsRestorer defaultsRestorer = new DefaultsRestorer ();
32
+
26
33
private ListAppender listAppender ;
27
34
private Logger observedLogger ;
28
35
29
- private static final int DEFAULT_MAX_STRING_LENGTH = 200_000_000 ;
30
- private static final int DEFAULT_MAX_NUMBER_LENGTH = 10_000 ;
31
- private static final int DEFAULT_MAX_NESTING_DEPTH = 1_000 ;
32
-
33
36
@ Before
34
37
public void setUpLoggingListAppender () {
35
38
int i = 1 +16 ;
@@ -39,6 +42,7 @@ public void setUpLoggingListAppender() {
39
42
40
43
@ Test
41
44
public void configuresDefaultsByDefault () {
45
+ Objects .requireNonNull (ObjectMappers .CBOR_MAPPER ); // force static init
42
46
StreamReadConstraintsUtil .fromSystemProperties ().validateIsGlobalDefault ();
43
47
}
44
48
@@ -55,8 +59,8 @@ public void configuresMaxStringLength() {
55
59
assertThat (configuredConstraints ).as ("inherited defaults" )
56
60
.returns (defaults .getMaxDocumentLength (), from (StreamReadConstraints ::getMaxDocumentLength ))
57
61
.returns (defaults .getMaxNameLength (), from (StreamReadConstraints ::getMaxNameLength ))
58
- .returns (DEFAULT_MAX_NESTING_DEPTH , from (StreamReadConstraints ::getMaxNestingDepth ))
59
- .returns (DEFAULT_MAX_NUMBER_LENGTH , from (StreamReadConstraints ::getMaxNumberLength ));
62
+ .returns (MAX_NESTING_DEPTH . defaultValue , from (StreamReadConstraints ::getMaxNestingDepth ))
63
+ .returns (MAX_NUMBER_LENGTH . defaultValue , from (StreamReadConstraints ::getMaxNumberLength ));
60
64
61
65
assertThatThrownBy (configuredUtil ::validateIsGlobalDefault ).isInstanceOf (IllegalStateException .class ).hasMessageContaining (MAX_STRING_LENGTH .propertyName );
62
66
@@ -98,8 +102,8 @@ public void configuresMaxNumberLength() {
98
102
assertThat (configuredConstraints ).as ("inherited defaults" )
99
103
.returns (defaults .getMaxDocumentLength (), from (StreamReadConstraints ::getMaxDocumentLength ))
100
104
.returns (defaults .getMaxNameLength (), from (StreamReadConstraints ::getMaxNameLength ))
101
- .returns (DEFAULT_MAX_NESTING_DEPTH , from (StreamReadConstraints ::getMaxNestingDepth ))
102
- .returns (DEFAULT_MAX_STRING_LENGTH , from (StreamReadConstraints ::getMaxStringLength ));
105
+ .returns (MAX_NESTING_DEPTH . defaultValue , from (StreamReadConstraints ::getMaxNestingDepth ))
106
+ .returns (MAX_STRING_LENGTH . defaultValue , from (StreamReadConstraints ::getMaxStringLength ));
103
107
104
108
assertThatThrownBy (configuredUtil ::validateIsGlobalDefault ).isInstanceOf (IllegalStateException .class ).hasMessageContaining (MAX_NUMBER_LENGTH .propertyName );
105
109
@@ -141,8 +145,8 @@ public void configuresMaxNestingDepth() {
141
145
assertThat (configuredConstraints ).as ("inherited defaults" )
142
146
.returns (defaults .getMaxDocumentLength (), from (StreamReadConstraints ::getMaxDocumentLength ))
143
147
.returns (defaults .getMaxNameLength (), from (StreamReadConstraints ::getMaxNameLength ))
144
- .returns (DEFAULT_MAX_STRING_LENGTH , from (StreamReadConstraints ::getMaxStringLength ))
145
- .returns (DEFAULT_MAX_NUMBER_LENGTH , from (StreamReadConstraints ::getMaxNumberLength ));
148
+ .returns (MAX_STRING_LENGTH . defaultValue , from (StreamReadConstraints ::getMaxStringLength ))
149
+ .returns (MAX_NUMBER_LENGTH . defaultValue , from (StreamReadConstraints ::getMaxNumberLength ));
146
150
147
151
assertThatThrownBy (configuredUtil ::validateIsGlobalDefault ).isInstanceOf (IllegalStateException .class ).hasMessageContaining (MAX_NESTING_DEPTH .propertyName );
148
152
@@ -187,8 +191,6 @@ public void validatesApplication() {
187
191
configuredUtil .validateIsGlobalDefault ();
188
192
});
189
193
190
- System .out .format ("OK%n" );
191
-
192
194
assertLogObserved (Level .INFO , "override `" + MAX_NESTING_DEPTH .propertyName + "` configured to `1010`" );
193
195
assertLogObserved (Level .INFO , "override `" + MAX_STRING_LENGTH .propertyName + "` configured to `1011`" );
194
196
assertLogObserved (Level .INFO , "override `" + MAX_NUMBER_LENGTH .propertyName + "` configured to `1110`" );
@@ -198,28 +200,17 @@ public void validatesApplication() {
198
200
}
199
201
200
202
@ Test
201
- public void usesJacksonDefaultsWhenNoConfig () {
202
- StreamReadConstraintsUtil util = new StreamReadConstraintsUtil (new Properties (), this .observedLogger );
203
- StreamReadConstraints constraints = util .get ();
204
-
205
- assertThat (constraints )
206
- .returns (DEFAULT_MAX_STRING_LENGTH , from (StreamReadConstraints ::getMaxStringLength ))
207
- .returns (DEFAULT_MAX_NUMBER_LENGTH , from (StreamReadConstraints ::getMaxNumberLength ))
208
- .returns (DEFAULT_MAX_NESTING_DEPTH , from (StreamReadConstraints ::getMaxNestingDepth ));
209
- }
210
-
211
- @ Test
212
- public void configOverridesDefault () {
213
- Properties props = new Properties ();
214
- props .setProperty ("logstash.jackson.stream-read-constraints.max-string-length" , "100" );
203
+ public void validatesApplicationWithDefaults () {
204
+ final Properties properties = new Properties (); // empty -- no overrides
215
205
216
- StreamReadConstraintsUtil util = new StreamReadConstraintsUtil (props , this .observedLogger );
217
- StreamReadConstraints constraints = util .get ();
206
+ fromProperties (properties , (configuredUtil , defaults ) -> {
207
+ configuredUtil .applyAsGlobalDefault ();
208
+ configuredUtil .validateIsGlobalDefault ();
218
209
219
- assertThat ( constraints )
220
- . returns ( 100 , from ( StreamReadConstraints :: getMaxStringLength ))
221
- . returns ( DEFAULT_MAX_NUMBER_LENGTH , from ( StreamReadConstraints :: getMaxNumberLength ))
222
- . returns ( DEFAULT_MAX_NESTING_DEPTH , from ( StreamReadConstraints :: getMaxNestingDepth ) );
210
+ assertLogObserved ( Level . INFO , "override `" + MAX_NESTING_DEPTH . propertyName + "` configured to `" + MAX_NESTING_DEPTH . defaultValue + "` (logstash default)" );
211
+ assertLogObserved ( Level . INFO , "override `" + MAX_STRING_LENGTH . propertyName + "` configured to `" + MAX_STRING_LENGTH . defaultValue + "` (logstash default)" );
212
+ assertLogObserved ( Level . INFO , "override `" + MAX_NUMBER_LENGTH . propertyName + "` configured to `" + MAX_NUMBER_LENGTH . defaultValue + "` (logstash default)" );
213
+ } );
223
214
}
224
215
225
216
private void assertLogObserved (final Level level , final String ... messageFragments ) {
@@ -230,13 +221,33 @@ private void assertLogObserved(final Level level, final String... messageFragmen
230
221
.anySatisfy (logEvent -> assertThat (logEvent .getMessage ().getFormattedMessage ()).contains (messageFragments ));
231
222
}
232
223
233
- private synchronized void fromProperties (final Properties properties , BiConsumer <StreamReadConstraintsUtil , StreamReadConstraints > defaultsConsumer ) {
224
+ private synchronized void fromProperties (final Properties properties ,
225
+ final BiConsumer <StreamReadConstraintsUtil , StreamReadConstraints > defaultsConsumer ) {
234
226
final StreamReadConstraints defaults = StreamReadConstraints .defaults ();
235
- try {
236
- final StreamReadConstraintsUtil util = new StreamReadConstraintsUtil (properties , this .observedLogger );
237
- defaultsConsumer .accept (util , defaults );
238
- } finally {
239
- StreamReadConstraints .overrideDefaultStreamReadConstraints (defaults );
227
+ final StreamReadConstraintsUtil util = new StreamReadConstraintsUtil (properties , this .observedLogger );
228
+
229
+ defaultsConsumer .accept (util , defaults );
230
+ }
231
+
232
+ /**
233
+ * A TestRule to snapshot the current defaults from jackson prior to test execution, and to
234
+ * ensure that snapshot is restored after test execution has completed.
235
+ */
236
+ public static class DefaultsRestorer extends org .junit .rules .ExternalResource {
237
+ private StreamReadConstraints snapshot ;
238
+
239
+ public StreamReadConstraints getSnapshot () {
240
+ return this .snapshot ;
241
+ }
242
+
243
+ @ Override
244
+ protected void before () throws Throwable {
245
+ this .snapshot = StreamReadConstraints .defaults ();
246
+ }
247
+
248
+ @ Override
249
+ protected void after () {
250
+ StreamReadConstraints .overrideDefaultStreamReadConstraints (this .snapshot );
240
251
}
241
252
}
242
253
}
0 commit comments