17
17
package org .apache .logging .log4j .message ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .mockito .ArgumentMatchers .eq ;
21
+ import static org .mockito .Mockito .verify ;
20
22
21
23
import java .util .ArrayList ;
22
24
import java .util .Arrays ;
23
25
import java .util .Collections ;
24
26
import java .util .List ;
25
27
import org .apache .logging .log4j .message .ParameterFormatter .MessagePatternAnalysis ;
28
+ import org .apache .logging .log4j .status .StatusLogger ;
29
+ import org .apache .logging .log4j .test .junit .UsingStatusLoggerMock ;
26
30
import org .junit .jupiter .api .Test ;
27
31
import org .junit .jupiter .params .ParameterizedTest ;
28
32
import org .junit .jupiter .params .provider .CsvSource ;
29
33
import org .junit .jupiter .params .provider .MethodSource ;
34
+ import org .mockito .ArgumentCaptor ;
30
35
31
36
/**
32
37
* Tests {@link ParameterFormatter}.
33
38
*/
34
- public class ParameterFormatterTest {
39
+ @ UsingStatusLoggerMock
40
+ class ParameterFormatterTest {
35
41
36
42
@ ParameterizedTest
37
43
@ CsvSource ({
@@ -49,7 +55,7 @@ public class ParameterFormatterTest {
49
55
"4,0:2:4:10,false,{}{}{}a{]b{}" ,
50
56
"5,0:2:4:7:10,false,{}{}{}a{}b{}"
51
57
})
52
- public void test_pattern_analysis (
58
+ void test_pattern_analysis (
53
59
final int placeholderCount ,
54
60
final String placeholderCharIndicesString ,
55
61
final boolean escapedPlaceholderFound ,
@@ -65,9 +71,24 @@ public void test_pattern_analysis(
65
71
}
66
72
}
67
73
74
+ @ ParameterizedTest
75
+ @ CsvSource ({"1,foo {}" , "2,bar {}{}" })
76
+ void insufficient_args_should_not_throw_an_exception (final int placeholderCount , final String pattern ) {
77
+ final int argCount = placeholderCount - 1 ;
78
+ final String formattedPattern = ParameterFormatter .format (pattern , new Object [argCount ], argCount );
79
+ assertThat (formattedPattern ).isEqualTo (pattern );
80
+ final ArgumentCaptor <Throwable > errorCaptor = ArgumentCaptor .forClass (Throwable .class );
81
+ verify (StatusLogger .getLogger ()).error (eq ("parameter formatting failure" ), errorCaptor .capture ());
82
+ final Throwable error = errorCaptor .getValue ();
83
+ assertThat (error )
84
+ .hasMessage (
85
+ "found %d argument placeholders, but provided %d for pattern `%s`" ,
86
+ placeholderCount , argCount , pattern );
87
+ }
88
+
68
89
@ ParameterizedTest
69
90
@ MethodSource ("messageFormattingTestCases" )
70
- void assertMessageFormatting (
91
+ void test_message_formatting (
71
92
final String pattern , final Object [] args , final int argCount , final String expectedFormattedMessage ) {
72
93
MessagePatternAnalysis analysis = ParameterFormatter .analyzePattern (pattern , -1 );
73
94
final StringBuilder buffer = new StringBuilder ();
0 commit comments