Class ThreadLocalPropertiesManager

java.lang.Object
com.shaft.properties.internal.ThreadLocalPropertiesManager

public final class ThreadLocalPropertiesManager extends Object
Manages per-thread property overrides for SHAFT properties.

When a property is set via the SHAFT.Properties API, it is stored in a thread-local map so that each test thread has its own isolated configuration. This prevents cross-thread contamination during parallel test execution.

Call clear() after each test class lifecycle completes to reset per-thread overrides and prevent stale state when thread pools reuse threads.

  • Method Details

    • setProperty

      public static void setProperty(String key, String value)
      Sets a property override for the current thread only. Does not affect other threads or global system properties.
      Parameters:
      key - the property key
      value - the property value
    • getProperty

      public static String getProperty(String key)
      Returns the effective value for the given property key by checking the current thread's overrides first and falling back to the corresponding system property. This is the single-key equivalent of getEffectiveProperties() and should be used in place of System.getProperty(key) wherever SHAFT configuration values are read, so that per-thread overrides are honoured.
      Parameters:
      key - the property key
      Returns:
      the thread-local value if set, otherwise the system property value, or null if neither is set
    • getOverrides

      public static Properties getOverrides()
      Returns the live thread-local overrides map for the current thread. This map is passed to ConfigFactory.create() as the highest-priority property source so that any overrides set by the current thread take precedence over system properties and file-based properties.
      Returns:
      the current thread's property overrides
    • getEffectiveProperties

      public static Properties getEffectiveProperties()
      Returns a merged view of system properties and thread-local overrides for the current thread. Thread-local overrides take precedence over system properties.

      This method should be used wherever code previously called System.getProperties() to look up SHAFT configuration values, so that per-thread overrides set via the SHAFT.Properties API are visible.

      Returns:
      a new Properties instance containing all system properties with thread-local overrides applied on top
    • clear

      public static void clear()
      Clears all thread-local property overrides for the current thread. Should be called at the start of each new test class lifecycle (before @BeforeClass runs) to prevent stale overrides from a previously executed test class on the same pooled thread.