1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00

GifCaptcha.setRepeat设置无效值的处理及GifCaptcha测试用例的补充

Merge pull request  from miko/v5-dev
This commit is contained in:
Looly 2024-12-25 09:34:29 +00:00 committed by Gitee
commit 4e30820a2a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 264 additions and 3 deletions
hutool-captcha/src
main/java/cn/hutool/captcha
test/java/cn/hutool/captcha

View File

@ -113,9 +113,7 @@ public class GifCaptcha extends AbstractCaptcha {
* @return this
*/
public GifCaptcha setRepeat(int repeat) {
if (repeat >= 0) {
this.repeat = repeat;
}
this.repeat = Math.max(repeat, 0);
return this;
}

View File

@ -0,0 +1,115 @@
package cn.hutool.captcha;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.*;
public class GifCaptchaUtilTest {
private GifCaptcha captcha;
@BeforeEach
public void setUp() {
// 初始化 GifCaptcha 类的实例
captcha = new GifCaptcha(200, 100, 4, 10); // width, height, codeCount, interfereCount
}
// 使用反射调用私有方法
private Object invokePrivateMethod(String methodName, Class<?>[] parameterTypes, Object[] parameters) throws Exception {
Method method = GifCaptcha.class.getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true); // 允许访问私有方法
return method.invoke(captcha, parameters);
}
// 测试 setQuality() 方法
@Test
public void testSetQuality() throws Exception {
captcha.setQuality(20);
// 通过反射获取 quality 字段的值并进行断言
assertEquals(20, getPrivateField("quality"), "Quality 应该设置为 20");
captcha.setQuality(0); // 设置无效值应该被设置为 1
assertEquals(1, getPrivateField("quality"), "Quality 应该设置为 1如果小于 1");
}
// 测试 setRepeat() 方法
@Test
public void testSetRepeat() throws Exception {
captcha.setRepeat(5);
// 通过反射获取 repeat 字段的值并进行断言
assertEquals(5, getPrivateField("repeat"), "Repeat 应该设置为 5");
captcha.setRepeat(-1); // 设置无效值应该保持为 0
assertEquals(0, getPrivateField("repeat"), "Repeat 应该设置为 0如果设置了负值");
}
// 测试 setColorRange() 方法
@Test
public void testSetColorRange() throws Exception {
captcha.setMinColor(100).setMaxColor(200);
// 通过反射获取 minColor maxColor 字段的值并进行断言
assertEquals(100, getPrivateField("minColor"), "Min color 应该设置为 100");
assertEquals(200, getPrivateField("maxColor"), "Max color 应该设置为 200");
}
// 测试生成验证码图像的方法 createCode()
@Test
public void testCreateCode() throws Exception {
captcha.createCode();
byte[] imageBytes = captcha.getImageBytes();
// 检查生成的图片字节是否不为 null 或空
assertNotNull(imageBytes, "生成的图片字节不应该为 null");
assertTrue(imageBytes.length > 0, "生成的图片字节不应该为空");
// 可选你也可以通过解码图片字节检查它是否是有效的 GIF 格式
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(imageBytes);
// 解码图片检查它是否为有效的 GIF假设你有库可以解码 GIF
// ImageIO.read(new ByteArrayInputStream(imageBytes)); // 可以取消注释来检查它是否是有效的 GIF
}
// 测试 graphicsImage() 方法
@Test
public void testGraphicsImage() throws Exception {
char[] chars = new char[]{'A', 'B', 'C', 'D'};
Color[] colors = new Color[]{
Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW
};
// 使用反射调用 private 方法 graphicsImage
Object result = invokePrivateMethod("graphicsImage", new Class[]{char[].class, Color[].class, char[].class, int.class}, new Object[]{chars, colors, chars, 0});
assertNotNull(result, "生成的图片不应该为 null");
assertTrue(result instanceof java.awt.image.BufferedImage, "返回的结果应该是 BufferedImage 类型");
}
// 测试 getRandomColor() 方法
@Test
public void testRandomColor() throws Exception {
// 使用反射调用 private 方法 getRandomColor
Object result = invokePrivateMethod("getRandomColor", new Class[]{int.class, int.class}, new Object[]{0, 255});
assertNotNull(result, "生成的颜色不应该为 null");
assertTrue(result instanceof Color, "返回的结果应该是 Color 类型");
Color color = (Color) result;
assertTrue(color.getRed() >= 0 && color.getRed() <= 255, "颜色的红色分量应该在 0 到 255 之间");
assertTrue(color.getGreen() >= 0 && color.getGreen() <= 255, "颜色的绿色分量应该在 0 到 255 之间");
assertTrue(color.getBlue() >= 0 && color.getBlue() <= 255, "颜色的蓝色分量应该在 0 到 255 之间");
}
// 辅助方法通过反射获取私有字段的值
private Object getPrivateField(String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field field = GifCaptcha.class.getDeclaredField(fieldName);
field.setAccessible(true); // 允许访问私有字段
return field.get(captcha);
}
}

View File

@ -0,0 +1,148 @@
package cn.hutool.captcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.img.GraphicsUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.*;
public class ShearCaptchaTest {
private ShearCaptcha captcha;
@BeforeEach
public void setUp() {
// 初始化 ShearCaptcha 实例
captcha = new ShearCaptcha(200, 100);
}
// 测试构造函数和基本功能
@Test
public void testConstructor() {
assertNotNull(captcha, "Captcha 实例应该被成功创建");
}
// 测试生成验证码图片的功能
@Test
public void testCreateImage() {
String code = "ABCD";
Image image = captcha.createImage(code);
assertNotNull(image, "验证码图片不应该为 null");
assertTrue(image instanceof BufferedImage, "生成的图片应该是 BufferedImage 类型");
// 可选进一步测试图像的内容
BufferedImage bufferedImage = (BufferedImage) image;
assertEquals(200, bufferedImage.getWidth(), "图像宽度应该为 200");
assertEquals(100, bufferedImage.getHeight(), "图像高度应该为 100");
}
// 测试绘制字符串的方法
@Test
public void testDrawString() throws Exception {
String code = "ABCD";
Method drawStringMethod = ShearCaptcha.class.getDeclaredMethod("drawString", Graphics2D.class, String.class);
drawStringMethod.setAccessible(true);
Graphics2D g2d = (Graphics2D) new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics();
drawStringMethod.invoke(captcha, g2d, code);
assertNotNull(g2d, "Graphics2D 对象不应该为 null");
assertTrue(g2d.getRenderingHints().containsKey(RenderingHints.KEY_ANTIALIASING), "应该启用抗锯齿");
}
// 测试 shear() 方法
@Test
public void testShear() throws Exception {
// 使用反射测试 shear 方法
Method shearMethod = ShearCaptcha.class.getDeclaredMethod("shear", Graphics.class, int.class, int.class, Color.class);
shearMethod.setAccessible(true);
Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics();
shearMethod.invoke(captcha, g, 200, 100, Color.WHITE);
// 假设没有明显的错误输出认为测试通过
assertNotNull(g, "Graphics 对象不应该为 null");
}
// 测试 shearX() 方法
@Test
public void testShearX() throws Exception {
// 使用反射测试 shearX 方法
Method shearXMethod = ShearCaptcha.class.getDeclaredMethod("shearX", Graphics.class, int.class, int.class, Color.class);
shearXMethod.setAccessible(true);
Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics();
shearXMethod.invoke(captcha, g, 200, 100, Color.RED);
// 假设没有明显的错误输出认为测试通过
assertNotNull(g, "Graphics 对象不应该为 null");
}
// 测试 shearY() 方法
@Test
public void testShearY() throws Exception {
// 使用反射测试 shearY 方法
Method shearYMethod = ShearCaptcha.class.getDeclaredMethod("shearY", Graphics.class, int.class, int.class, Color.class);
shearYMethod.setAccessible(true);
Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics();
shearYMethod.invoke(captcha, g, 200, 100, Color.BLUE);
// 假设没有明显的错误输出认为测试通过
assertNotNull(g, "Graphics 对象不应该为 null");
}
// 测试 drawInterfere() 方法
@Test
public void testDrawInterfere() throws Exception {
// 使用反射测试 drawInterfere 方法
Method drawInterfereMethod = ShearCaptcha.class.getDeclaredMethod("drawInterfere", Graphics.class, int.class, int.class, int.class, int.class, int.class, Color.class);
drawInterfereMethod.setAccessible(true);
Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics();
drawInterfereMethod.invoke(captcha, g, 0, 0, 200, 100, 4, Color.GREEN);
// 假设没有明显的错误输出认为测试通过
assertNotNull(g, "Graphics 对象不应该为 null");
}
// 测试验证码生成时的干扰线
@Test
public void testDrawInterfereLines() {
// 设置干扰线数量
captcha = new ShearCaptcha(200, 100, 4);
Image image = captcha.createImage("ABCD");
// 检查图像内容判断干扰线是否正确绘制
assertNotNull(image, "生成的验证码图片不应该为空");
}
// 测试验证码的尺寸
@Test
public void testCaptchaSize() {
captcha = new ShearCaptcha(300, 150);
String code = "XYZ";
Image image = captcha.createImage(code);
BufferedImage bufferedImage = (BufferedImage) image;
assertEquals(300, bufferedImage.getWidth(), "图像宽度应该为 300");
assertEquals(150, bufferedImage.getHeight(), "图像高度应该为 150");
}
// 测试生成随机验证码字符
@Test
public void testRandomGenerator() {
RandomGenerator randomGenerator = new RandomGenerator(4);
String code = randomGenerator.generate();
assertNotNull(code, "生成的验证码字符不应该为 null");
assertEquals(4, code.length(), "验证码字符长度应该为 4");
}
}