Class TestCaptchaGenerator
java.lang.Object
cloud.opencode.base.captcha.generator.TestCaptchaGenerator
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionTestCaptchaGenerator(String fixedAnswer) Constructs a TestCaptchaGenerator with the given fixed answer. -
Method Summary
Modifier and TypeMethodDescriptiongenerate(CaptchaConfig config) Generates a CAPTCHA with the fixed answer and minimal image data.Returns the fixed answer configured for this generator.getType()Gets the supported CAPTCHA type.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface CaptchaGenerator
generate
-
Constructor Details
-
TestCaptchaGenerator
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
Generates a CAPTCHA with the fixed answer and minimal image data. 使用固定答案和最小图像数据生成验证码。- Specified by:
generatein interfaceCaptchaGenerator- Parameters:
config- the configuration (expiration time is respected) | 配置(遵循过期时间)- Returns:
- the generated CAPTCHA with fixed answer | 具有固定答案的生成验证码
- Throws:
NullPointerException- if config is null | 如果 config 为 null
-
getType
Gets the supported CAPTCHA type. 获取支持的验证码类型。- Specified by:
getTypein interfaceCaptchaGenerator- Returns:
CaptchaType.ALPHANUMERIC| 字母数字类型
-
getFixedAnswer
Returns the fixed answer configured for this generator. 返回此生成器配置的固定答案。- Returns:
- the fixed answer | 固定答案
-