Class Stringifiers
This class provides built-in string conversion strategies that handle common Java types
and objects. These stringifiers are automatically registered when using
BasicBeanConverter.Builder.defaultSettings()
.
Purpose:
Stringifiers convert objects to human-readable string representations for use in BCT assertions and test output. They provide consistent, meaningful string formats across different object types while supporting customization for specific testing needs.
Built-in Stringifiers:
mapEntryStringifier()
- ConvertsMap.Entry
to"key=value" formatcalendarStringifier()
- ConvertsGregorianCalendar
to ISO-8601 formatdateStringifier()
- ConvertsDate
to ISO instant formatinputStreamStringifier()
- ConvertsInputStream
content to hex stringsbyteArrayStringifier()
- Converts byte arrays to hex stringsreaderStringifier()
- ConvertsReader
content to stringsfileStringifier()
- ConvertsFile
content to stringsenumStringifier()
- ConvertsEnum
values to name formatclassStringifier()
- ConvertsClass
objects to name formatconstructorStringifier()
- ConvertsConstructor
to signature formatmethodStringifier()
- ConvertsMethod
to signature formatlistStringifier()
- ConvertsList
to bracket-delimited formatmapStringifier()
- ConvertsMap
to brace-delimited format
Usage Example:
Resource Handling:
Warning: Some stringifiers consume or close their input resources:
InputStream
: Stream is consumed and closed during stringificationReader
: Reader is consumed and closed during stringificationFile
: File content is read completely during stringification
Custom Stringifier Development:
When creating custom stringifiers, follow these patterns:
- Null Safety: Handle
null inputs gracefully - Resource Management: Properly close resources after use
- Exception Handling: Convert exceptions to meaningful error messages
- Performance: Consider string building efficiency for complex objects
- Readability: Ensure output is useful for debugging and assertions
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringifier<byte[]>
Returns a stringifier for byte arrays that converts them to hex strings.static Stringifier<GregorianCalendar>
Returns a stringifier forGregorianCalendar
objects that formats them as ISO-8601 strings.static Stringifier<Class>
Returns a stringifier forClass
objects that formats them according to configured settings.static Stringifier<Constructor>
Returns a stringifier forConstructor
objects that formats them as readable signatures.static Stringifier<Date>
Returns a stringifier forDate
objects that formats them as ISO instant strings.static Stringifier<Enum>
Returns a stringifier forEnum
objects that converts them to name format.static Stringifier<File>
Returns a stringifier forFile
objects that converts file content to strings.static Stringifier<InputStream>
Returns a stringifier forInputStream
objects that converts content to hex strings.static Stringifier<List>
Returns a stringifier forList
objects that formats them with configurable delimiters.static Stringifier<Map.Entry>
Returns a stringifier forMap.Entry
objects that formats them as"key=value" .static Stringifier<Map>
Returns a stringifier forMap
objects that formats them with configurable delimiters.static Stringifier<Method>
Returns a stringifier forMethod
objects that formats them as readable signatures.static Stringifier<Reader>
Returns a stringifier forReader
objects that converts content to strings.
-
Method Details
-
mapEntryStringifier
Returns a stringifier forMap.Entry
objects that formats them as"key=value" .This stringifier creates a human-readable representation of map entries by converting both the key and value to strings and joining them with the configured entry separator.
Behavior:
- Format: Uses the pattern
"{key}{separator}{value}" - Separator: Uses the
mapEntrySeparator
setting (default:"=" ) - Recursive conversion: Both key and value are converted using the same converter
Usage Examples:
// Test map entry stringification var entry = Map.entry ("name" ,"John" );assertBean (entry ,"<self>" ,"name=John" );// Test with custom separator var converter = BasicBeanConverter.builder () .defaultSettings() .addSetting(SETTING_mapEntrySeparator ,": " ) .build();assertBean (entry ,"<self>" ,"name: John" );- Returns:
- A
Stringifier
forMap.Entry
objects - See Also:
- Format: Uses the pattern
-
calendarStringifier
Returns a stringifier forGregorianCalendar
objects that formats them as ISO-8601 strings.This stringifier converts calendar objects to standardized ISO-8601 timestamp format, which provides consistent, sortable, and internationally recognized date representations.
Behavior:
- Format: Uses the
calendarFormat
setting (default:DateTimeFormatter.ISO_INSTANT
) - Timezone: Respects the calendar's timezone information
- Precision: Includes full precision available in the calendar
Usage Examples:
// Test calendar stringification var calendar =new GregorianCalendar(2023 , Calendar.JANUARY ,15 );assertMatchesGlob ("2023-01-*" ,calendar );// Test with custom format var converter = BasicBeanConverter.builder () .defaultSettings() .addSetting(SETTING_calendarFormat , DateTimeFormatter.ISO_LOCAL_DATE ) .build();- Returns:
- A
Stringifier
forGregorianCalendar
objects - See Also:
- Format: Uses the
-
dateStringifier
Returns a stringifier forDate
objects that formats them as ISO instant strings.This stringifier converts Date objects to ISO-8601 instant format, providing standardized timestamp representations suitable for logging and comparison.
Behavior:
- Format: ISO-8601 instant format (e.g.,
"2023-01-15T10:30:00Z" ) - Timezone: Always represents time in UTC (Z timezone)
- Precision: Millisecond precision as available in Date objects
Usage Examples:
// Test date stringification var date =new Date(1673780400000L );// 2023-01-15T10:00:00Z assertBean (date ,"<self>" ,"2023-01-15T10:00:00Z" );// Test in object property var event =new Event().setTimestamp(date );assertBean (event ,"timestamp" ,"2023-01-15T10:00:00Z" );- Returns:
- A
Stringifier
forDate
objects - See Also:
- Format: ISO-8601 instant format (e.g.,
-
inputStreamStringifier
Returns a stringifier forInputStream
objects that converts content to hex strings.Warning: This stringifier consumes and closes the input stream during conversion. After stringification, the stream cannot be used again.
Behavior:
- Content reading: Reads all available bytes from the stream
- Hex conversion: Converts bytes to uppercase hexadecimal representation
- Resource management: Automatically closes the stream after reading
Usage Examples:
// Test with byte content var stream =new ByteArrayInputStream(new byte []{0x48 ,0x65 ,0x6C ,0x6C ,0x6F });assertBean (stream ,"<self>" ,"48656C6C6F" );// "Hello" in hex // Test empty stream var empty =new ByteArrayInputStream(new byte [0 ]);assertBean (empty ,"<self>" ,"" );Important Notes:
- One-time use: The stream is consumed and closed during conversion
- Memory usage: All content is loaded into memory for conversion
- Exception handling: IO exceptions are wrapped in RuntimeException
- Returns:
- A
Stringifier
forInputStream
objects - See Also:
-
byteArrayStringifier
Returns a stringifier for byte arrays that converts them to hex strings.This stringifier provides a consistent way to represent binary data as readable hexadecimal strings, useful for testing and debugging binary content.
Behavior:
- Hex format: Each byte is represented as two uppercase hex digits
- No separators: Bytes are concatenated without spaces or delimiters
- Empty arrays: Returns empty string for zero-length arrays
Usage Examples:
// Test byte array stringification byte []data = {0x48 ,0x65 ,0x6C ,0x6C ,0x6F };assertBean (data ,"<self>" ,"48656C6C6F" );// "Hello" in hex // Test with zeros and high values byte []mixed = {0x00 ,0xFF ,0x7F };assertBean (mixed ,"<self>" ,"00FF7F" );- Returns:
- A
Stringifier
for byte arrays
-
readerStringifier
Returns a stringifier forReader
objects that converts content to strings.Warning: This stringifier consumes and closes the reader during conversion. After stringification, the reader cannot be used again.
Behavior:
- Content reading: Reads all available characters from the reader
- String conversion: Converts characters directly to string format
- Resource management: Automatically closes the reader after reading
Usage Examples:
// Test with string content var reader =new StringReader("Hello World" );assertBean (reader ,"<self>" ,"Hello World" );// Test with file reader var fileReader = Files.newBufferedReader (path);assertMatchesGlob ("*expected content*" ,fileReader );Important Notes:
- One-time use: The reader is consumed and closed during conversion
- Memory usage: All content is loaded into memory for conversion
- Exception handling: IO exceptions are wrapped in RuntimeException
- Returns:
- A
Stringifier
forReader
objects - See Also:
-
fileStringifier
Returns a stringifier forFile
objects that converts file content to strings.This stringifier reads the entire file content and returns it as a string, making it useful for testing file-based operations and content verification.
Behavior:
- Content reading: Reads the entire file content into memory
- Encoding: Uses the default platform encoding for text files
- Resource management: Properly closes file resources after reading
Usage Examples:
// Test file content var configFile =new File("config.properties" );assertMatchesGlob ("*database.url=*" ,configFile );// Test empty file var emptyFile =new File("empty.txt" );assertBean (emptyFile ,"<self>" ,"" );Important Notes:
- Memory usage: Large files will consume significant memory
- File existence: Non-existent files will cause exceptions
- Binary files: May produce unexpected results with binary content
- Exception handling: IO exceptions are wrapped in RuntimeException
- Returns:
- A
Stringifier
forFile
objects - See Also:
-
enumStringifier
Returns a stringifier forEnum
objects that converts them to name format.This stringifier provides a consistent way to represent enum values as their declared constant names, which is typically the most useful format for testing.
Behavior:
- Name format: Uses
Enum.name()
method for string representation - Case preservation: Maintains the exact case as declared in enum
- All enum types: Works with any enum implementation
Usage Examples:
// Test enum stringification assertBean (Color.RED ,"<self>" ,"RED" );assertBean (Status.IN_PROGRESS ,"<self>" ,"IN_PROGRESS" );// Test in object property var task =new Task().setStatus(Status.COMPLETED );assertBean (task ,"status" ,"COMPLETED" );Alternative Formats:
If you need different enum string representations (like
Enum.toString()
or custom formatting), register a custom stringifier for specific enum types.- Returns:
- A
Stringifier
forEnum
objects - See Also:
- Name format: Uses
-
classStringifier
Returns a stringifier forClass
objects that formats them according to configured settings.This stringifier provides flexible class name formatting, supporting different levels of detail from simple names to fully qualified class names.
Behavior:
- Format options: Controlled by
classNameFormat
setting - Simple format: Class simple name (default)
- Canonical format: Fully qualified canonical name
- Full format: Complete class name including package
Usage Examples:
// Test with default simple format assertBean (String.class ,"<self>" ,"String" );assertBean (ArrayList.class ,"<self>" ,"ArrayList" );// Test with canonical format var converter = BasicBeanConverter.builder () .defaultSettings() .addSetting(SETTING_classNameFormat ,"canonical" ) .build();assertBean (String.class ,"<self>" ,"java.lang.String" );- Returns:
- A
Stringifier
forClass
objects - See Also:
- Format options: Controlled by
-
constructorStringifier
Returns a stringifier forConstructor
objects that formats them as readable signatures.This stringifier creates human-readable constructor signatures including the declaring class name and parameter types, useful for reflection-based testing.
Behavior:
- Format:
"{ClassName}({paramType1},{paramType2},...)" - Class names: Uses the configured class name format
- Parameter types: Includes all parameter types in declaration order
Usage Examples:
// Test constructor stringification var constructor = String.class .getConstructor(char [].class );assertBean (constructor ,"<self>" ,"String(char[])" );// Test no-arg constructor var defaultConstructor = ArrayList.class .getConstructor();assertBean (defaultConstructor ,"<self>" ,"ArrayList()" );- Returns:
- A
Stringifier
forConstructor
objects - See Also:
- Format:
-
methodStringifier
Returns a stringifier forMethod
objects that formats them as readable signatures.This stringifier creates human-readable method signatures including the method name and parameter types, useful for reflection-based testing and debugging.
Behavior:
- Format:
"{methodName}({paramType1},{paramType2},...)" - Method name: Uses the declared method name
- Parameter types: Includes all parameter types in declaration order
Usage Examples:
// Test method stringification var method = String.class .getMethod("substring" ,int .class ,int .class );assertBean (method ,"<self>" ,"substring(int,int)" );// Test no-arg method var toString = Object.class .getMethod("toString" );assertBean (toString ,"<self>" ,"toString()" );- Returns:
- A
Stringifier
forMethod
objects - See Also:
- Format:
-
listStringifier
Returns a stringifier forList
objects that formats them with configurable delimiters.This stringifier converts lists to bracket-delimited strings with customizable separators and prefixes/suffixes, providing consistent list representation across tests.
Behavior:
- Format:
"{prefix}{element1}{separator}{element2}...{suffix}" - Separator: Uses
fieldSeparator
setting (default:"," ) - Prefix: Uses
collectionPrefix
setting (default:"[" ) - Suffix: Uses
collectionSuffix
setting (default:"]" ) - Recursive: Elements are converted using the same converter
Usage Examples:
// Test list stringification var list = List.of ("apple" ,"banana" ,"cherry" );assertBean (list ,"<self>" ,"[apple,banana,cherry]" );// Test with custom formatting var converter = BasicBeanConverter.builder () .defaultSettings() .addSetting(SETTING_fieldSeparator ,"; " ) .addSetting(SETTING_collectionPrefix ,"(" ) .addSetting(SETTING_collectionSuffix ,")" ) .build();assertBean (list ,"<self>" ,"(apple; banana; cherry)" );- Returns:
- A
Stringifier
forList
objects - See Also:
- Format:
-
mapStringifier
Returns a stringifier forMap
objects that formats them with configurable delimiters.This stringifier converts maps to brace-delimited strings by first converting the map to a list of entries and then stringifying each entry, providing consistent map representation across tests.
Behavior:
- Format:
"{prefix}{entry1}{separator}{entry2}...{suffix}" - Separator: Uses
fieldSeparator
setting (default:"," ) - Prefix: Uses
mapPrefix
setting (default:"{" ) - Suffix: Uses
mapSuffix
setting (default:"}" ) - Entry format: Each entry uses the map entry stringifier
Usage Examples:
// Test map stringification var map = Map.of ("name" ,"John" ,"age" ,25 );assertMatchesGlob ("{*name=John*age=25*}" ,map );// Test empty map var emptyMap = Map.of ();assertBean (emptyMap ,"<self>" ,"{}" );Order Considerations:
The order of entries in the string depends on the map implementation's iteration order. Use order-independent assertions (like
assertMatchesGlob
) for maps where order is not guaranteed.- Returns:
- A
Stringifier
forMap
objects - See Also:
- Format:
-