20
20
import java .text .DateFormat ;
21
21
import java .text .SimpleDateFormat ;
22
22
import java .util .List ;
23
+ import java .util .stream .Stream ;
23
24
24
25
import org .assertj .core .api .Assertions ;
25
26
import org .assertj .core .presentation .Representation ;
26
27
27
28
/**
28
- * Provider for all the configuration settings / parameters within AssertJ.
29
- * <p>
30
- * All the configuration possibilities are registered via an SPI.
29
+ * All configuration settings for AssertJ Core.
31
30
*
32
31
* @since 3.13.0
33
32
*/
@@ -44,6 +43,15 @@ public class Configuration {
44
43
public static final boolean BARE_NAME_PROPERTY_EXTRACTION_ENABLED = true ;
45
44
public static final boolean LENIENT_DATE_PARSING = false ;
46
45
46
+ private boolean comparingPrivateFields = ALLOW_COMPARING_PRIVATE_FIELDS ;
47
+ private boolean extractingPrivateFields = ALLOW_EXTRACTING_PRIVATE_FIELDS ;
48
+ private boolean bareNamePropertyExtraction = BARE_NAME_PROPERTY_EXTRACTION_ENABLED ;
49
+ private boolean removeAssertJRelatedElementsFromStackTrace = REMOVE_ASSERTJ_RELATED_ELEMENTS_FROM_STACK_TRACE ;
50
+ private boolean lenientDateParsing = LENIENT_DATE_PARSING ;
51
+ private List <DateFormat > additionalDateFormats = emptyList ();
52
+ private int maxLengthForSingleLineDescription = MAX_LENGTH_FOR_SINGLE_LINE_DESCRIPTION ;
53
+ private int maxElementsForPrinting = MAX_ELEMENTS_FOR_PRINTING ;
54
+
47
55
/**
48
56
* @return the default {@link Representation} that is used within AssertJ.
49
57
*/
@@ -60,88 +68,209 @@ boolean hasCustomRepresentation() {
60
68
* <p>
61
69
* See {@link Assertions#setAllowComparingPrivateFields(boolean)} for a detailed description.
62
70
*
63
- * @return whether private fields comparison is enabled. Default is {@value #ALLOW_COMPARING_PRIVATE_FIELDS}.
71
+ * @return whether private fields comparison is enabled.
64
72
*/
65
73
public boolean comparingPrivateFieldsEnabled () {
66
- return ALLOW_COMPARING_PRIVATE_FIELDS ;
74
+ return comparingPrivateFields ;
75
+ }
76
+
77
+ /**
78
+ * Sets whether private fields comparison is enabled.
79
+ * <p>
80
+ * See {@link Assertions#setAllowComparingPrivateFields(boolean)} for a detailed description.
81
+ * <p>
82
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
83
+ *
84
+ * @param comparingPrivateFields whether private fields comparison is enabled.
85
+ */
86
+ public void setComparingPrivateFields (boolean comparingPrivateFields ) {
87
+ this .comparingPrivateFields = comparingPrivateFields ;
67
88
}
68
89
69
90
/**
70
91
* Returns whether private fields comparison is enabled. Default is {@value #ALLOW_EXTRACTING_PRIVATE_FIELDS}.
71
92
* <p>
72
93
* See {@link Assertions#setAllowExtractingPrivateFields(boolean)} for a detailed description.
73
94
*
74
- * @return whether private fields comparison is enabled. Default is {@value #ALLOW_EXTRACTING_PRIVATE_FIELDS}.
75
- *
95
+ * @return whether private fields comparison is enabled.
76
96
*/
77
97
public boolean extractingPrivateFieldsEnabled () {
78
- return ALLOW_EXTRACTING_PRIVATE_FIELDS ;
98
+ return extractingPrivateFields ;
99
+ }
100
+
101
+ /**
102
+ * Sets whether private fields comparison is enabled.
103
+ * <p>
104
+ * See {@link Assertions#setAllowExtractingPrivateFields(boolean)} for a detailed description.
105
+ * <p>
106
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
107
+ *
108
+ * @param extractingPrivateFields whether private fields comparison is enabled.
109
+ */
110
+ public void setExtractingPrivateFields (boolean extractingPrivateFields ) {
111
+ this .extractingPrivateFields = extractingPrivateFields ;
79
112
}
80
113
81
114
/**
82
115
* Returns whether the extractor considers bare-named property methods like {@code String name()}.
116
+ * Default is {@value #BARE_NAME_PROPERTY_EXTRACTION_ENABLED}.
83
117
* <p>
84
118
* See {@link Assertions#setExtractBareNamePropertyMethods(boolean)} for a detailed description.
85
119
*
86
- * @return Whether the extractor considers bare-named property methods like {@code String name()}. Default is {@value #BARE_NAME_PROPERTY_EXTRACTION_ENABLED }.
120
+ * @return whether the extractor considers bare-named property methods like {@code String name()}.
87
121
*/
88
122
public boolean bareNamePropertyExtractionEnabled () {
89
- return true ;
123
+ return bareNamePropertyExtraction ;
124
+ }
125
+
126
+ /**
127
+ * Sest whether the extractor considers bare-named property methods like {@code String name()}.
128
+ * <p>
129
+ * See {@link Assertions#setExtractBareNamePropertyMethods(boolean)} for a detailed description.
130
+ * <p>
131
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
132
+ *
133
+ * @param bareNamePropertyExtraction whether the extractor considers bare-named property methods.
134
+ */
135
+ public void setBareNamePropertyExtraction (boolean bareNamePropertyExtraction ) {
136
+ this .bareNamePropertyExtraction = bareNamePropertyExtraction ;
90
137
}
91
138
92
139
/**
93
140
* Returns whether AssertJ related elements are removed from assertion errors stack trace.
141
+ * Default is {@value #REMOVE_ASSERTJ_RELATED_ELEMENTS_FROM_STACK_TRACE}.
94
142
* <p>
95
143
* See {@link Assertions#setRemoveAssertJRelatedElementsFromStackTrace(boolean)} for a detailed description.
96
144
*
97
- * @return whether AssertJ related elements are removed from assertion errors stack trace. Default is {@value #REMOVE_ASSERTJ_RELATED_ELEMENTS_FROM_STACK_TRACE}.
145
+ * @return whether AssertJ related elements are removed from assertion errors stack trace.
98
146
*/
99
147
public boolean removeAssertJRelatedElementsFromStackTraceEnabled () {
100
- return REMOVE_ASSERTJ_RELATED_ELEMENTS_FROM_STACK_TRACE ;
148
+ return removeAssertJRelatedElementsFromStackTrace ;
149
+ }
150
+
151
+ /**
152
+ * Returns whether AssertJ related elements are removed from assertion errors stack trace.
153
+ * <p>
154
+ * See {@link Assertions#setRemoveAssertJRelatedElementsFromStackTrace(boolean)} for a detailed description.
155
+ * <p>
156
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
157
+ *
158
+ * @param removeAssertJRelatedElementsFromStackTrace whether AssertJ related elements are removed from assertion errors stack trace.
159
+ */
160
+ public void setRemoveAssertJRelatedElementsFromStackTrace (boolean removeAssertJRelatedElementsFromStackTrace ) {
161
+ this .removeAssertJRelatedElementsFromStackTrace = removeAssertJRelatedElementsFromStackTrace ;
101
162
}
102
163
103
164
/**
104
165
* Returns whether AssertJ will use lenient parsing mode for default date formats.
166
+ * Default is {@value #LENIENT_DATE_PARSING}.
105
167
* <p>
106
168
* See {@link Assertions#setLenientDateParsing(boolean)} for a detailed description.
107
169
*
108
- * @return whether AssertJ will use lenient parsing mode for default date formats. Default is {@value #LENIENT_DATE_PARSING}.
170
+ * @return whether AssertJ will use lenient parsing mode for default date formats.
109
171
*/
110
172
public boolean lenientDateParsingEnabled () {
111
- return LENIENT_DATE_PARSING ;
173
+ return lenientDateParsing ;
112
174
}
113
175
114
176
/**
115
- * Returns the additional date formats AssertJ will use in date assertions.
177
+ * Returns whether AssertJ will use lenient parsing mode for default date formats.
178
+ * <p>
179
+ * See {@link Assertions#setLenientDateParsing(boolean)} for a detailed description.
180
+ * <p>
181
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
182
+ *
183
+ * @param lenientDateParsing whether AssertJ will use lenient parsing mode for default date formats.
184
+ */
185
+ public void setLenientDateParsing (boolean lenientDateParsing ) {
186
+ this .lenientDateParsing = lenientDateParsing ;
187
+ }
188
+
189
+ /**
190
+ * AssertJ uses defaults date formats in date assertions, this property let's you register additional ones (default there are no addtional date formats).
116
191
* <p>
117
192
* See {@link Assertions#registerCustomDateFormat(java.text.DateFormat)} for a detailed description.
118
193
*
119
- * @return the additional date formats AssertJ will use in date assertions. Default is none .
194
+ * @return the date formats AssertJ will use in date assertions in addition the default ones .
120
195
*/
121
196
public List <DateFormat > additionalDateFormats () {
122
- return emptyList ();
197
+ return additionalDateFormats ;
198
+ }
199
+
200
+ /**
201
+ * Returns the additional date formats AssertJ will use in date assertions.
202
+ * <p>
203
+ * See {@link Assertions#registerCustomDateFormat(java.text.DateFormat)} for a detailed description.
204
+ * <p>
205
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
206
+ *
207
+ * @param additionalDateFormats the date formats AssertJ will use in date assertions in addition the default ones.
208
+ */
209
+ public void setAdditionalDateFormats (List <DateFormat > additionalDateFormats ) {
210
+ this .additionalDateFormats = additionalDateFormats ;
211
+ }
212
+
213
+ /**
214
+ * Add the given date formats AssertJ will use in date assertions.
215
+ * <p>
216
+ * See {@link Assertions#registerCustomDateFormat(java.text.DateFormat)} for a detailed description.
217
+ * <p>
218
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
219
+ *
220
+ * @param additionalDateFormats the date formats AssertJ will use in date assertions in addition the default ones.
221
+ */
222
+ public void addAdditionalDateFormats (DateFormat ... additionalDateFormats ) {
223
+ Stream .of (additionalDateFormats ).forEach (this .additionalDateFormats ::add );
123
224
}
124
225
125
226
/**
126
227
* Returns the maximum length for an iterable/array to be displayed on one line.
228
+ * Default is {@value #MAX_LENGTH_FOR_SINGLE_LINE_DESCRIPTION}.
127
229
* <p>
128
230
* See {@link Assertions#setMaxLengthForSingleLineDescription(int)} for a detailed description.
129
231
*
130
- * @return the maximum length for an iterable/array to be displayed on one line. Default is {@value #MAX_LENGTH_FOR_SINGLE_LINE_DESCRIPTION}.
232
+ * @return the maximum length for an iterable/array to be displayed on one line.
131
233
*/
132
234
public int maxLengthForSingleLineDescription () {
133
- return MAX_LENGTH_FOR_SINGLE_LINE_DESCRIPTION ;
235
+ return maxLengthForSingleLineDescription ;
236
+ }
237
+
238
+ /**
239
+ * Sets the maximum length for an iterable/array to be displayed on one line.
240
+ * <p>
241
+ * See {@link Assertions#setMaxLengthForSingleLineDescription(int)} for a detailed description.
242
+ * <p>
243
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
244
+ *
245
+ * @param maxLengthForSingleLineDescription the maximum length for an iterable/array to be displayed on one line.
246
+ */
247
+ public void setMaxLengthForSingleLineDescription (int maxLengthForSingleLineDescription ) {
248
+ this .maxLengthForSingleLineDescription = maxLengthForSingleLineDescription ;
134
249
}
135
250
136
251
/**
137
252
* Returns the maximum length for an iterable/array to be displayed on one line.
253
+ * Default is {@value #MAX_ELEMENTS_FOR_PRINTING}.
138
254
* <p>
139
255
* See {@link Assertions#setMaxLengthForSingleLineDescription(int)} for a detailed description.
140
256
*
141
- * @return the maximum length for an iterable/array to be displayed on one line. Default is {@value #MAX_ELEMENTS_FOR_PRINTING}.
257
+ * @return the maximum length for an iterable/array to be displayed on one line.
142
258
*/
143
259
public int maxElementsForPrinting () {
144
- return MAX_ELEMENTS_FOR_PRINTING ;
260
+ return maxElementsForPrinting ;
261
+ }
262
+
263
+ /**
264
+ * Returns the maximum length for an iterable/array to be displayed on one line.
265
+ * <p>
266
+ * See {@link Assertions#setMaxLengthForSingleLineDescription(int)} for a detailed description.
267
+ * <p>
268
+ * Note that this change will only be effective once {@link #apply()} or {@link #applyAndDisplay()} is called.
269
+ *
270
+ * @param maxElementsForPrinting the maximum length for an iterable/array to be displayed on one line.
271
+ */
272
+ public void setMaxElementsForPrinting (int maxElementsForPrinting ) {
273
+ this .maxElementsForPrinting = maxElementsForPrinting ;
145
274
}
146
275
147
276
/**
@@ -159,8 +288,17 @@ public void apply() {
159
288
additionalDateFormats ().forEach (Assertions ::registerCustomDateFormat );
160
289
}
161
290
291
+ /**
292
+ * Applies this configuration to AssertJ and prints it.
293
+ */
294
+ public void applyAndDisplay () {
295
+ apply ();
296
+ System .out .println (describe ());
297
+ }
298
+
162
299
public String describe () {
163
- return format ("- representation .................................. = %s%n" +
300
+ return format ("Applying configuration %s%n" +
301
+ "- representation .................................. = %s%n" +
164
302
"- comparingPrivateFieldsEnabled ................... = %s%n" +
165
303
"- extractingPrivateFieldsEnabled .................. = %s%n" +
166
304
"- bareNamePropertyExtractionEnabled ............... = %s%n" +
@@ -169,6 +307,7 @@ public String describe() {
169
307
"- maxLengthForSingleLineDescription ............... = %s%n" +
170
308
"- maxElementsForPrinting .......................... = %s%n" +
171
309
"- removeAssertJRelatedElementsFromStackTraceEnabled = %s%n" ,
310
+ getClass ().getName (),
172
311
representation (),
173
312
comparingPrivateFieldsEnabled (),
174
313
extractingPrivateFieldsEnabled (),
0 commit comments