Class BctConfiguration

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

public class BctConfiguration extends Object
Configuration utility for Bean-Centric Testing (BCT) framework.

This class provides thread-local configuration settings for BCT assertions, allowing test-specific customization of behavior such as map sorting, collection sorting, and bean converter selection.

Configuration is managed using thread-local storage, ensuring that parallel test execution doesn't interfere with each other's settings. Settings are typically configured via the @BctConfig annotation, but can also be set programmatically.

Configuration Properties:
  • BCT_SORT_MAPS: Enable sorting of maps in assertions
  • BCT_SORT_COLLECTIONS: Enable sorting of collections in assertions
  • Bean Converter: Custom bean converter instance for property access and conversion
Usage via Annotation:

@BctConfig(sortMaps=TriState.TRUE, beanConverter=MyConverter.class) class MyTest { @Test @BctConfig(sortCollections=TriState.TRUE) void testSomething() { // sortMaps=true (from class), sortCollections=true (from method), // beanConverter=MyConverter (from class) } }

Usage via Programmatic API:

@BeforeEach void setUp() { BctConfiguration.set(BctConfiguration.BCT_SORT_MAPS, true); BctConfiguration.set(new MyCustomConverter()); } @AfterEach void tearDown() { BctConfiguration.clear(); }

Thread Safety:

All methods in this class are thread-safe. Configuration is stored in thread-local storage, ensuring that each test thread has its own isolated configuration that doesn't interfere with other concurrently running tests.

See Also:
  • Field Details

    • BCT_SORT_MAPS

      public static final String BCT_SORT_MAPS
      Configuration property name for enabling map sorting in BCT assertions.

      When set to true, maps will be sorted by key before comparison in assertions. This ensures consistent ordering regardless of the map's internal ordering.

      Example:

      BctConfiguration.set(BctConfiguration.BCT_SORT_MAPS, true);

      See Also:
    • BCT_SORT_COLLECTIONS

      public static final String BCT_SORT_COLLECTIONS
      Configuration property name for enabling collection sorting in BCT assertions.

      When set to true, collections will be sorted before comparison in assertions. This ensures consistent ordering regardless of the collection's internal ordering.

      Example:

      BctConfiguration.set(BctConfiguration.BCT_SORT_COLLECTIONS, true);

      See Also:
  • Constructor Details

  • Method Details

    • set

      public static void set(String name, Object value)
      Sets a thread-local configuration value.

      The value is stored in thread-local storage and will only affect the current thread. This is the recommended way to set configuration for individual tests.

      Example:

      BctConfiguration.set(BctConfiguration.BCT_SORT_MAPS, true);

      Parameters:
      name - The configuration property name (e.g., BCT_SORT_MAPS).
      value - The value to set.
      See Also:
    • setGlobal

      public static void setGlobal(String name, Object value)
      Sets a global configuration value.

      Global values apply to all threads and persist until explicitly cleared. Use with caution in multi-threaded test environments.

      Example:

      BctConfiguration.setGlobal(BctConfiguration.BCT_SORT_MAPS, true);

      Parameters:
      name - The configuration property name (e.g., BCT_SORT_MAPS).
      value - The value to set.
      See Also:
    • get

      public static StringSetting get(String name)
      Gets a configuration setting by name.

      Returns the setting object which can be used to check if a value is set and retrieve it. Thread-local values take precedence over global values.

      Parameters:
      name - The configuration property name (e.g., BCT_SORT_MAPS).
      Returns:
      The setting object, or null if not set.
      See Also:
    • get

      public static <T> T get(String name, T def)
      Gets a configuration value by name with a default value.

      Returns the configured value if set, otherwise returns the provided default value. Thread-local values take precedence over global values.

      Example:

      boolean sortMaps = BctConfiguration.get(BctConfiguration.BCT_SORT_MAPS, false);

      Type Parameters:
      T - The type of the value.
      Parameters:
      name - The configuration property name (e.g., BCT_SORT_MAPS).
      def - The default value to return if not set.
      Returns:
      The configured value, or the default if not set.
      See Also:
    • clear

      public static void clear()
      Clears all thread-local configuration settings.

      This method removes all thread-local settings including:

      This is typically called in test teardown methods (e.g., @AfterEach) to ensure a clean state for subsequent tests. The BctConfigExtension automatically calls this method after each test.

      Example:

      @AfterEach void tearDown() { BctConfiguration.clear(); }

      See Also:
    • clearGlobal

      public static void clearGlobal()
      Clears all global configuration settings.

      This method removes all global settings. Use with caution as this affects all threads.

      See Also:
    • set

      public static void set(BeanConverter converter)
      Sets a custom bean converter for the current thread.

      This method allows you to override the default converter (BasicBeanConverter.DEFAULT) for all assertions in the current test method. The converter will be used by all assertion methods in the current thread.

      This is particularly useful when you need custom property access, stringification, or conversion logic for specific tests. The converter can be configured via the @BctConfig annotation or set programmatically.

      Usage Example:

      @BeforeEach void setUp() { var customConverter = BasicBeanConverter.builder() .defaultSettings() .addStringifier(LocalDate.class, date -> date.format(DateTimeFormatter.ISO_LOCAL_DATE)) .build(); BctConfiguration.set(customConverter); } @Test void testWithCustomConverter() { // All assertions use the custom converter assertBean(event, "date", "2023-12-01"); } @AfterEach void tearDown() { BctConfiguration.clear(); // Also clears converter override }

      Thread Safety:

      This method is thread-safe and uses thread-local storage. Each test method running in parallel will have its own converter instance, preventing cross-thread interference.

      Parameters:
      converter - The bean converter to use for the current thread. Must not be null.
      Throws:
      IllegalArgumentException - If converter is null.
      See Also: