19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
21
21
import java .math .BigDecimal ;
22
+ import java .util .List ;
23
+ import java .util .function .Supplier ;
24
+ import java .util .stream .Collectors ;
22
25
import java .util .stream .Stream ;
26
+ import org .apache .logging .log4j .Level ;
27
+ import org .apache .logging .log4j .status .StatusData ;
28
+ import org .apache .logging .log4j .test .ListStatusListener ;
23
29
import org .apache .logging .log4j .test .junit .Mutable ;
24
30
import org .apache .logging .log4j .test .junit .SerialUtil ;
31
+ import org .apache .logging .log4j .test .junit .UsingStatusListener ;
25
32
import org .junit .jupiter .api .Test ;
26
33
import org .junit .jupiter .params .ParameterizedTest ;
27
34
import org .junit .jupiter .params .provider .MethodSource ;
28
35
29
- public class ParameterizedMessageTest {
36
+ @ UsingStatusListener
37
+ class ParameterizedMessageTest {
38
+
39
+ final ListStatusListener statusListener ;
40
+
41
+ ParameterizedMessageTest (ListStatusListener statusListener ) {
42
+ this .statusListener = statusListener ;
43
+ }
30
44
31
45
@ Test
32
- public void testNoArgs () {
46
+ void testNoArgs () {
33
47
final String testMsg = "Test message {}" ;
34
48
ParameterizedMessage msg = new ParameterizedMessage (testMsg , (Object []) null );
35
49
String result = msg .getFormattedMessage ();
@@ -41,7 +55,7 @@ public void testNoArgs() {
41
55
}
42
56
43
57
@ Test
44
- public void testZeroLength () {
58
+ void testZeroLength () {
45
59
final String testMsg = "" ;
46
60
ParameterizedMessage msg = new ParameterizedMessage (testMsg , new Object [] {"arg" });
47
61
String result = msg .getFormattedMessage ();
@@ -53,7 +67,7 @@ public void testZeroLength() {
53
67
}
54
68
55
69
@ Test
56
- public void testOneCharLength () {
70
+ void testOneCharLength () {
57
71
final String testMsg = "d" ;
58
72
ParameterizedMessage msg = new ParameterizedMessage (testMsg , new Object [] {"arg" });
59
73
String result = msg .getFormattedMessage ();
@@ -65,71 +79,71 @@ public void testOneCharLength() {
65
79
}
66
80
67
81
@ Test
68
- public void testFormat3StringArgs () {
82
+ void testFormat3StringArgs () {
69
83
final String testMsg = "Test message {}{} {}" ;
70
84
final String [] args = {"a" , "b" , "c" };
71
85
final String result = ParameterizedMessage .format (testMsg , args );
72
86
assertThat (result ).isEqualTo ("Test message ab c" );
73
87
}
74
88
75
89
@ Test
76
- public void testFormatNullArgs () {
90
+ void testFormatNullArgs () {
77
91
final String testMsg = "Test message {} {} {} {} {} {}" ;
78
92
final String [] args = {"a" , null , "c" , null , null , null };
79
93
final String result = ParameterizedMessage .format (testMsg , args );
80
94
assertThat (result ).isEqualTo ("Test message a null c null null null" );
81
95
}
82
96
83
97
@ Test
84
- public void testFormatStringArgsIgnoresSuperfluousArgs () {
98
+ void testFormatStringArgsIgnoresSuperfluousArgs () {
85
99
final String testMsg = "Test message {}{} {}" ;
86
100
final String [] args = {"a" , "b" , "c" , "unnecessary" , "superfluous" };
87
101
final String result = ParameterizedMessage .format (testMsg , args );
88
102
assertThat (result ).isEqualTo ("Test message ab c" );
89
103
}
90
104
91
105
@ Test
92
- public void testFormatStringArgsWithEscape () {
106
+ void testFormatStringArgsWithEscape () {
93
107
final String testMsg = "Test message \\ {}{} {}" ;
94
108
final String [] args = {"a" , "b" , "c" };
95
109
final String result = ParameterizedMessage .format (testMsg , args );
96
110
assertThat (result ).isEqualTo ("Test message {}a b" );
97
111
}
98
112
99
113
@ Test
100
- public void testFormatStringArgsWithTrailingEscape () {
114
+ void testFormatStringArgsWithTrailingEscape () {
101
115
final String testMsg = "Test message {}{} {}\\ " ;
102
116
final String [] args = {"a" , "b" , "c" };
103
117
final String result = ParameterizedMessage .format (testMsg , args );
104
118
assertThat (result ).isEqualTo ("Test message ab c\\ " );
105
119
}
106
120
107
121
@ Test
108
- public void testFormatStringArgsWithTrailingText () {
122
+ void testFormatStringArgsWithTrailingText () {
109
123
final String testMsg = "Test message {}{} {}Text" ;
110
124
final String [] args = {"a" , "b" , "c" };
111
125
final String result = ParameterizedMessage .format (testMsg , args );
112
126
assertThat (result ).isEqualTo ("Test message ab cText" );
113
127
}
114
128
115
129
@ Test
116
- public void testFormatStringArgsWithTrailingEscapedEscape () {
130
+ void testFormatStringArgsWithTrailingEscapedEscape () {
117
131
final String testMsg = "Test message {}{} {}\\ \\ " ;
118
132
final String [] args = {"a" , "b" , "c" };
119
133
final String result = ParameterizedMessage .format (testMsg , args );
120
134
assertThat (result ).isEqualTo ("Test message ab c\\ " );
121
135
}
122
136
123
137
@ Test
124
- public void testFormatStringArgsWithEscapedEscape () {
138
+ void testFormatStringArgsWithEscapedEscape () {
125
139
final String testMsg = "Test message \\ \\ {}{} {}" ;
126
140
final String [] args = {"a" , "b" , "c" };
127
141
final String result = ParameterizedMessage .format (testMsg , args );
128
142
assertThat (result ).isEqualTo ("Test message \\ ab c" );
129
143
}
130
144
131
145
@ Test
132
- public void testSafeWithMutableParams () { // LOG4J2-763
146
+ void testSafeWithMutableParams () { // LOG4J2-763
133
147
final String testMsg = "Test message {}" ;
134
148
final Mutable param = new Mutable ().set ("abc" );
135
149
final ParameterizedMessage msg = new ParameterizedMessage (testMsg , param );
@@ -170,4 +184,51 @@ void testSerializable(final Object arg) {
170
184
assertThat (actual ).isInstanceOf (ParameterizedMessage .class );
171
185
assertThat (actual .getFormattedMessage ()).isEqualTo (expected .getFormattedMessage ());
172
186
}
187
+
188
+ static Stream <Object []> testCasesForInsufficientFormatArgs () {
189
+ return Stream .of (new Object [] {1 , "foo {}" }, new Object [] {2 , "bar {}{}" });
190
+ }
191
+
192
+ @ ParameterizedTest
193
+ @ MethodSource ("testCasesForInsufficientFormatArgs" )
194
+ void formatTo_should_fail_on_insufficient_args (final int placeholderCount , final String pattern ) {
195
+ final int argCount = placeholderCount - 1 ;
196
+ verifyFormattingFailureOnInsufficientArgs (placeholderCount , pattern , argCount , () -> {
197
+ final ParameterizedMessage message = new ParameterizedMessage (pattern , new Object [argCount ]);
198
+ final StringBuilder buffer = new StringBuilder ();
199
+ message .formatTo (buffer );
200
+ return buffer .toString ();
201
+ });
202
+ }
203
+
204
+ @ ParameterizedTest
205
+ @ MethodSource ("testCasesForInsufficientFormatArgs" )
206
+ void format_should_fail_on_insufficient_args (final int placeholderCount , final String pattern ) {
207
+ final int argCount = placeholderCount - 1 ;
208
+ verifyFormattingFailureOnInsufficientArgs (
209
+ placeholderCount , pattern , argCount , () -> ParameterizedMessage .format (pattern , new Object [argCount ]));
210
+ }
211
+
212
+ private void verifyFormattingFailureOnInsufficientArgs (
213
+ final int placeholderCount ,
214
+ final String pattern ,
215
+ final int argCount ,
216
+ final Supplier <String > formattedMessageSupplier ) {
217
+
218
+ // Verify the formatted message
219
+ final String formattedMessage = formattedMessageSupplier .get ();
220
+ assertThat (formattedMessage ).isEqualTo (pattern );
221
+
222
+ // Verify the logged failure
223
+ final List <StatusData > statusDataList = statusListener .getStatusData ().collect (Collectors .toList ());
224
+ assertThat (statusDataList ).hasSize (1 );
225
+ final StatusData statusData = statusDataList .get (0 );
226
+ assertThat (statusData .getLevel ()).isEqualTo (Level .ERROR );
227
+ assertThat (statusData .getMessage ().getFormattedMessage ()).isEqualTo ("Unable to format msg: %s" , pattern );
228
+ assertThat (statusData .getThrowable ())
229
+ .isInstanceOf (IllegalArgumentException .class )
230
+ .hasMessage (
231
+ "found %d argument placeholders, but provided %d for pattern `%s`" ,
232
+ placeholderCount , argCount , pattern );
233
+ }
173
234
}
0 commit comments