Class TestFeatureManager

java.lang.Object
cloud.opencode.base.feature.testing.TestFeatureManager
All Implemented Interfaces:
AutoCloseable

public class TestFeatureManager extends Object implements AutoCloseable
Test Feature Manager 测试功能管理器

Convenience class for managing features in tests.

用于在测试中管理功能的便捷类。

Features | 主要功能:

  • Simple feature registration - 简单功能注册
  • Enable/disable all features - 启用/禁用所有功能
  • AutoCloseable for try-with-resources - 支持AutoCloseable以使用try-with-resources
  • Delegates to OpenFeature - 委托给OpenFeature

Usage Examples | 使用示例:

try (TestFeatureManager manager = new TestFeatureManager()) {
    manager.withFeature("dark-mode", true)
           .withFeature("beta-feature", false);

    assertTrue(manager.isEnabled("dark-mode"));
    assertFalse(manager.isEnabled("beta-feature"));
}

Security | 安全性:

  • Thread-safe: Yes (delegates to thread-safe OpenFeature) - 线程安全: 是(委托给线程安全的OpenFeature)
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-feature V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new test feature manager 创建新的测试功能管理器
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close and clear all features 关闭并清除所有功能
    void
    Disable a feature, registering it if it does not exist 禁用功能,如果不存在则注册
    void
    Disable all registered features 禁用所有已注册的功能
    void
    Enable a feature, registering it if it does not exist 启用功能,如果不存在则注册
    void
    Enable all registered features 启用所有已注册的功能
    Get the underlying OpenFeature delegate 获取底层OpenFeature委托
    boolean
    Check if a feature is enabled 检查功能是否启用
    boolean
    Check if a feature is enabled with context 使用上下文检查功能是否启用
    Register a feature with full definition 使用完整定义注册功能
    withFeature(String key, boolean enabled)
    Register a feature as enabled or disabled 将功能注册为启用或禁用

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TestFeatureManager

      public TestFeatureManager()
      Create a new test feature manager 创建新的测试功能管理器
  • Method Details

    • withFeature

      public TestFeatureManager withFeature(String key, boolean enabled)
      Register a feature as enabled or disabled 将功能注册为启用或禁用
      Parameters:
      key - the feature key | 功能键
      enabled - whether the feature is enabled | 功能是否启用
      Returns:
      this manager for fluent API | 此管理器以支持流式API
    • withFeature

      public TestFeatureManager withFeature(Feature feature)
      Register a feature with full definition 使用完整定义注册功能
      Parameters:
      feature - the feature | 功能
      Returns:
      this manager for fluent API | 此管理器以支持流式API
    • enable

      public void enable(String key)
      Enable a feature, registering it if it does not exist 启用功能,如果不存在则注册
      Parameters:
      key - the feature key | 功能键
    • disable

      public void disable(String key)
      Disable a feature, registering it if it does not exist 禁用功能,如果不存在则注册
      Parameters:
      key - the feature key | 功能键
    • enableAll

      public void enableAll()
      Enable all registered features 启用所有已注册的功能
    • disableAll

      public void disableAll()
      Disable all registered features 禁用所有已注册的功能
    • isEnabled

      public boolean isEnabled(String key)
      Check if a feature is enabled 检查功能是否启用
      Parameters:
      key - the feature key | 功能键
      Returns:
      true if enabled | 如果启用返回true
    • isEnabled

      public boolean isEnabled(String key, FeatureContext context)
      Check if a feature is enabled with context 使用上下文检查功能是否启用
      Parameters:
      key - the feature key | 功能键
      context - the evaluation context | 评估上下文
      Returns:
      true if enabled | 如果启用返回true
    • getDelegate

      public OpenFeature getDelegate()
      Get the underlying OpenFeature delegate 获取底层OpenFeature委托
      Returns:
      the delegate | 委托
    • close

      public void close()
      Close and clear all features 关闭并清除所有功能
      Specified by:
      close in interface AutoCloseable