@GwtCompatible public final class Config extends Object
As documented in the Check
class, the library can only be configured
via a config file on the classpath. The file must be named
"better-checks-config".properties
(without the quotes, of
course!) and must be located in the classpath root. The config file is loaded
via ResourceBundle
s, so localization is supported. However, it is
questionable to display exception messages directly to the user, so this
might not be commonly used.
Here is what a config file might look like:
cleanStackTraces = false defaultArgumentName = le argument ARG_POSITIVE.format = %s should -(not )-be positive ARG_NEGATIVE.format = %s should -(not )-be negativeThe
getConfig()
method provides read access to the config singleton.
To assert that the your config file has been loaded, you may use the
isLoadedFromConfigFile()
method.
For more information, refer to the documentation of the
isCleanStackTracesEnabled()
, getDefaultArgumentName()
and
getMessageFormat(MessageType, boolean)
methods.
Modifier and Type | Field and Description |
---|---|
static String |
CLEAN_STACK_TRACES_ENABLED_KEY
The key for disabling stack trace cleaning in the config file:
"cleanStackTraces" (without quotes). |
static String |
CONFIG_BASE_NAME
The base name of the config file for this library:
"better-checks-config" |
static String |
DEFAULT_ARG_NAME_KEY
The key for setting the default argument name in the config file:
"defaultArgumentName" (without quotes). |
static boolean |
DEFAULT_CLEAN_STRACK_TRACES_ENABLED
The default setting for for stack trace cleaning, which is true .
|
static String |
DEFAULT_DEFAULT_ARG_NAME
The default setting for the default argument name, which is "the argument" .
|
Modifier and Type | Method and Description |
---|---|
static Config |
getConfig()
Returns the Better Checks config singleton.
|
String |
getDefaultArgumentName()
Returns the default argument name used in exception messages.
|
String |
getMessageFormat(MessageType type,
boolean inverted)
Returns the exception message format for the given
MessageType . |
boolean |
isCleanStackTracesEnabled()
Indicates whether stack trace cleaning is enabled.
|
boolean |
isLoadedFromConfigFile()
Indicates whether a valid config file was loaded.
|
@GwtIncompatible(value="Configuration not supported with GWT") public static final String CONFIG_BASE_NAME
"better-checks-config"
@GwtIncompatible(value="Configuration not supported with GWT") public static final String CLEAN_STACK_TRACES_ENABLED_KEY
"cleanStackTraces"
(without quotes).isCleanStackTracesEnabled()
,
Constant Field Values@GwtIncompatible(value="Configuration not supported with GWT") public static final String DEFAULT_ARG_NAME_KEY
"defaultArgumentName"
(without quotes).getDefaultArgumentName()
,
Constant Field Valuespublic static final String DEFAULT_DEFAULT_ARG_NAME
getDefaultArgumentName()
,
Constant Field Valuespublic static final boolean DEFAULT_CLEAN_STRACK_TRACES_ENABLED
isCleanStackTracesEnabled()
,
Constant Field Valuespublic static Config getConfig()
Config
public boolean isLoadedFromConfigFile()
Note that the config file may define as many and as few properties as desired. The rest of the properties are set to the default value.
true
iff a config file was loaded.public boolean isCleanStackTracesEnabled()
Check
for more information.
The default value is true. To
override this, set the
"cleanStackTraces"
property in the
config file.
true
if stack trace cleaning is enabled.public String getDefaultArgumentName()
BaseCheck.named(String)
modifier.
The default value (meaning the default default name) is
"the argument". To override this, set the
"defaultArgumentName"
property in the config file.
public String getMessageFormat(MessageType type, boolean inverted)
MessageType
.
The formats are format strings defined by the Formatter
class.
Besides the message type, a second parameter affects the exception message format: Whether or not the check was inverted. The messages for inverted checks are also inverted, typically with an additional or missing "not".
To combine two such message formats, the library internally makes use of
a "meta" format that encompasses both versions. This meta format is
simple: message parts that should only be used in the positive
(non-inverted) version are enclosed in +(
and
)+
. Analogically, parts for the negative (inverted) version
are enclosed in -(
and )-
. Here's an example:
%s must +(not )+be null-( (value: %s))-
The positive format here is %s must not be null
and the
negative version is %s must be null (value: %s)
. In both
cases the first %s
will be replaced with the argument name
and in the negative version, the second %s
will be replaced
with the actual value of the argument.
The default meta format for each message type is given by the
MessageType.getDefaultFormat()
method. To override a specific
format, add a property in the config file with a key named
MESSAGE_TYPE.format
(where
MESSAGE_TYPE
is the string representation of one of
the MessageType
enum values) and a value containing the meta
format for that type.
type
- The message typeinverted
- If true
, the message format is inverted to
reflect an inverted check.Formatter
format stringCopyright © 2012–2017 Michael Faes. All rights reserved.