Skip to content

Commit adc5477

Browse files
Ease changing Configuration properties.
Add a method to apply and display the applied Configuration
1 parent e7c6465 commit adc5477

File tree

3 files changed

+161
-26
lines changed

3 files changed

+161
-26
lines changed

src/main/java/org/assertj/core/configuration/Configuration.java

Lines changed: 161 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import java.text.DateFormat;
2121
import java.text.SimpleDateFormat;
2222
import java.util.List;
23+
import java.util.stream.Stream;
2324

2425
import org.assertj.core.api.Assertions;
2526
import org.assertj.core.presentation.Representation;
2627

2728
/**
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.
3130
*
3231
* @since 3.13.0
3332
*/
@@ -44,6 +43,15 @@ public class Configuration {
4443
public static final boolean BARE_NAME_PROPERTY_EXTRACTION_ENABLED = true;
4544
public static final boolean LENIENT_DATE_PARSING = false;
4645

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+
4755
/**
4856
* @return the default {@link Representation} that is used within AssertJ.
4957
*/
@@ -60,88 +68,209 @@ boolean hasCustomRepresentation() {
6068
* <p>
6169
* See {@link Assertions#setAllowComparingPrivateFields(boolean)} for a detailed description.
6270
*
63-
* @return whether private fields comparison is enabled. Default is {@value #ALLOW_COMPARING_PRIVATE_FIELDS}.
71+
* @return whether private fields comparison is enabled.
6472
*/
6573
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;
6788
}
6889

6990
/**
7091
* Returns whether private fields comparison is enabled. Default is {@value #ALLOW_EXTRACTING_PRIVATE_FIELDS}.
7192
* <p>
7293
* See {@link Assertions#setAllowExtractingPrivateFields(boolean)} for a detailed description.
7394
*
74-
* @return whether private fields comparison is enabled. Default is {@value #ALLOW_EXTRACTING_PRIVATE_FIELDS}.
75-
*
95+
* @return whether private fields comparison is enabled.
7696
*/
7797
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;
79112
}
80113

81114
/**
82115
* Returns whether the extractor considers bare-named property methods like {@code String name()}.
116+
* Default is {@value #BARE_NAME_PROPERTY_EXTRACTION_ENABLED}.
83117
* <p>
84118
* See {@link Assertions#setExtractBareNamePropertyMethods(boolean)} for a detailed description.
85119
*
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()}.
87121
*/
88122
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;
90137
}
91138

92139
/**
93140
* Returns whether AssertJ related elements are removed from assertion errors stack trace.
141+
* Default is {@value #REMOVE_ASSERTJ_RELATED_ELEMENTS_FROM_STACK_TRACE}.
94142
* <p>
95143
* See {@link Assertions#setRemoveAssertJRelatedElementsFromStackTrace(boolean)} for a detailed description.
96144
*
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.
98146
*/
99147
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;
101162
}
102163

103164
/**
104165
* Returns whether AssertJ will use lenient parsing mode for default date formats.
166+
* Default is {@value #LENIENT_DATE_PARSING}.
105167
* <p>
106168
* See {@link Assertions#setLenientDateParsing(boolean)} for a detailed description.
107169
*
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.
109171
*/
110172
public boolean lenientDateParsingEnabled() {
111-
return LENIENT_DATE_PARSING;
173+
return lenientDateParsing;
112174
}
113175

114176
/**
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).
116191
* <p>
117192
* See {@link Assertions#registerCustomDateFormat(java.text.DateFormat)} for a detailed description.
118193
*
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.
120195
*/
121196
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);
123224
}
124225

125226
/**
126227
* Returns the maximum length for an iterable/array to be displayed on one line.
228+
* Default is {@value #MAX_LENGTH_FOR_SINGLE_LINE_DESCRIPTION}.
127229
* <p>
128230
* See {@link Assertions#setMaxLengthForSingleLineDescription(int)} for a detailed description.
129231
*
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.
131233
*/
132234
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;
134249
}
135250

136251
/**
137252
* Returns the maximum length for an iterable/array to be displayed on one line.
253+
* Default is {@value #MAX_ELEMENTS_FOR_PRINTING}.
138254
* <p>
139255
* See {@link Assertions#setMaxLengthForSingleLineDescription(int)} for a detailed description.
140256
*
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.
142258
*/
143259
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;
145274
}
146275

147276
/**
@@ -159,8 +288,17 @@ public void apply() {
159288
additionalDateFormats().forEach(Assertions::registerCustomDateFormat);
160289
}
161290

291+
/**
292+
* Applies this configuration to AssertJ and prints it.
293+
*/
294+
public void applyAndDisplay() {
295+
apply();
296+
System.out.println(describe());
297+
}
298+
162299
public String describe() {
163-
return format("- representation .................................. = %s%n" +
300+
return format("Applying configuration %s%n" +
301+
"- representation .................................. = %s%n" +
164302
"- comparingPrivateFieldsEnabled ................... = %s%n" +
165303
"- extractingPrivateFieldsEnabled .................. = %s%n" +
166304
"- bareNamePropertyExtractionEnabled ............... = %s%n" +
@@ -169,6 +307,7 @@ public String describe() {
169307
"- maxLengthForSingleLineDescription ............... = %s%n" +
170308
"- maxElementsForPrinting .......................... = %s%n" +
171309
"- removeAssertJRelatedElementsFromStackTraceEnabled = %s%n",
310+
getClass().getName(),
172311
representation(),
173312
comparingPrivateFieldsEnabled(),
174313
extractingPrivateFieldsEnabled(),

src/main/java/org/assertj/core/configuration/ConfigurationProvider.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
public final class ConfigurationProvider {
3131

32-
private static final String NEWLINE = format("%n");
3332
public static final ConfigurationProvider CONFIGURATION_PROVIDER = new ConfigurationProvider();
3433
private final Representation representation;
3534
private final Configuration configuration;
@@ -43,8 +42,6 @@ private ConfigurationProvider() {
4342
configuration = Services.get(Configuration.class, DEFAULT_CONFIGURATION);
4443
if (configuration != DEFAULT_CONFIGURATION) {
4544
configuration.apply();
46-
System.out.println(NEWLINE + "AssertJ has found and applied the following registered configuration: " + configuration);
47-
System.out.println(configuration.describe());
4845
}
4946
}
5047

src/test/java/org/assertj/core/configuration/Configuration_describe_Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public void should_give_a_human_readable_description() throws Exception {
2626
Configuration configuration = new NonDefaultConfiguration();
2727
// WHEN
2828
String description = configuration.describe();
29-
System.out.println(description);
3029
// THEN
3130
assertThat(description).isEqualTo(format("- representation .................................. = BinaryRepresentation%n" +
3231
"- comparingPrivateFieldsEnabled ................... = false%n" +

0 commit comments

Comments
 (0)