Class Stringifiers

java.lang.Object
org.apache.juneau.junit.bct.Stringifiers

public class Stringifiers extends Object
Collection of standard stringifier implementations for the Bean-Centric Testing framework.

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:
Usage Example:

// Register stringifiers using builder var converter = BasicBeanConverter.builder() .defaultSettings() .addStringifier(Date.class, Stringifiers.dateStringifier()) .addStringifier(File.class, Stringifiers.fileStringifier()) .build();

Resource Handling:

Warning: Some stringifiers consume or close their input resources:

  • InputStream: Stream is consumed and closed during stringification
  • Reader: Reader is consumed and closed during stringification
  • File: 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
See Also:
  • Method Details

    • mapEntryStringifier

      Returns a stringifier for Map.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 for Map.Entry objects
      See Also:
    • calendarStringifier

      Returns a stringifier for GregorianCalendar 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 for GregorianCalendar objects
      See Also:
    • dateStringifier

      public static Stringifier<Date> dateStringifier()
      Returns a stringifier for Date 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 for Date objects
      See Also:
    • inputStreamStringifier

      Returns a stringifier for InputStream 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 for InputStream objects
      See Also:
    • byteArrayStringifier

      public static Stringifier<byte[]> 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 for Reader 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 for Reader objects
      See Also:
    • fileStringifier

      public static Stringifier<File> fileStringifier()
      Returns a stringifier for File 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 for File objects
      See Also:
    • enumStringifier

      public static Stringifier<Enum> enumStringifier()
      Returns a stringifier for Enum 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 for Enum objects
      See Also:
    • classStringifier

      Returns a stringifier for Class 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 for Class objects
      See Also:
    • constructorStringifier

      Returns a stringifier for Constructor 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 for Constructor objects
      See Also:
    • methodStringifier

      Returns a stringifier for Method 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 for Method objects
      See Also:
    • listStringifier

      public static Stringifier<List> listStringifier()
      Returns a stringifier for List 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 for List objects
      See Also:
    • mapStringifier

      public static Stringifier<Map> mapStringifier()
      Returns a stringifier for Map 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 for Map objects
      See Also: