Class TestCaptchaGenerator

java.lang.Object
cloud.opencode.base.captcha.generator.TestCaptchaGenerator
All Implemented Interfaces:
CaptchaGenerator

public final class TestCaptchaGenerator extends Object implements CaptchaGenerator
Test Captcha Generator - Generates CAPTCHAs with a predictable fixed answer for unit testing 测试验证码生成器 - 生成具有可预测固定答案的验证码,用于单元测试

This generator produces CAPTCHAs with a known, fixed answer and minimal image data, making it ideal for unit testing validation logic without requiring actual image rendering.

此生成器生成具有已知固定答案和最小图像数据的验证码,非常适合在不需要实际图像渲染的情况下 对验证逻辑进行单元测试。

Features | 主要功能:

  • Deterministic answer for repeatable tests - 确定性答案用于可重复测试
  • Minimal image data (empty byte array) to avoid rendering overhead - 最小图像数据(空字节数组)避免渲染开销
  • Respects config expiration time - 遵循配置的过期时间
  • Metadata includes test marker for easy identification - 元数据包含测试标记便于识别

Usage Examples | 使用示例:

CaptchaGenerator generator = new TestCaptchaGenerator("ABC123");
Captcha captcha = generator.generate(CaptchaConfig.defaults());
assert "ABC123".equals(captcha.answer());

Performance | 性能特性:

  • Time complexity: O(1) - 时间复杂度: O(1)
  • Space complexity: O(1) - 空间复杂度: O(1)

Security | 安全性:

  • Thread-safe: Yes (immutable state) - 线程安全: 是(不可变状态)
  • Null-safe: No (fixedAnswer must not be null) - 空值安全: 否(fixedAnswer 不能为 null)
Since:
JDK 25, opencode-base-captcha V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • TestCaptchaGenerator

      public TestCaptchaGenerator(String fixedAnswer)
      Constructs a TestCaptchaGenerator with the given fixed answer. 使用给定的固定答案构造测试验证码生成器。
      Parameters:
      fixedAnswer - the fixed answer for all generated CAPTCHAs | 所有生成验证码的固定答案
      Throws:
      NullPointerException - if fixedAnswer is null | 如果 fixedAnswer 为 null
  • Method Details

    • generate

      public Captcha generate(CaptchaConfig config)
      Generates a CAPTCHA with the fixed answer and minimal image data. 使用固定答案和最小图像数据生成验证码。
      Specified by:
      generate in interface CaptchaGenerator
      Parameters:
      config - the configuration (expiration time is respected) | 配置(遵循过期时间)
      Returns:
      the generated CAPTCHA with fixed answer | 具有固定答案的生成验证码
      Throws:
      NullPointerException - if config is null | 如果 config 为 null
    • getType

      public CaptchaType getType()
      Gets the supported CAPTCHA type. 获取支持的验证码类型。
      Specified by:
      getType in interface CaptchaGenerator
      Returns:
      CaptchaType.ALPHANUMERIC | 字母数字类型
    • getFixedAnswer

      public String getFixedAnswer()
      Returns the fixed answer configured for this generator. 返回此生成器配置的固定答案。
      Returns:
      the fixed answer | 固定答案