|
| 1 | +package cn.hutool.captcha; |
| 2 | +import cn.hutool.captcha.ShearCaptcha; |
| 3 | +import cn.hutool.captcha.generator.RandomGenerator; |
| 4 | +import cn.hutool.core.img.GraphicsUtil; |
| 5 | +import cn.hutool.core.img.ImgUtil; |
| 6 | +import cn.hutool.core.util.RandomUtil; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.awt.*; |
| 11 | +import java.awt.image.BufferedImage; |
| 12 | +import java.lang.reflect.Method; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +public class ShearCaptchaTest { |
| 17 | + |
| 18 | + private ShearCaptcha captcha; |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + public void setUp() { |
| 22 | + // 初始化 ShearCaptcha 实例 |
| 23 | + captcha = new ShearCaptcha(200, 100); |
| 24 | + } |
| 25 | + |
| 26 | + // 测试构造函数和基本功能 |
| 27 | + @Test |
| 28 | + public void testConstructor() { |
| 29 | + assertNotNull(captcha, "Captcha 实例应该被成功创建"); |
| 30 | + } |
| 31 | + |
| 32 | + // 测试生成验证码图片的功能 |
| 33 | + @Test |
| 34 | + public void testCreateImage() { |
| 35 | + String code = "ABCD"; |
| 36 | + Image image = captcha.createImage(code); |
| 37 | + assertNotNull(image, "验证码图片不应该为 null"); |
| 38 | + assertTrue(image instanceof BufferedImage, "生成的图片应该是 BufferedImage 类型"); |
| 39 | + |
| 40 | + // 可选:进一步测试图像的内容 |
| 41 | + BufferedImage bufferedImage = (BufferedImage) image; |
| 42 | + assertEquals(200, bufferedImage.getWidth(), "图像宽度应该为 200"); |
| 43 | + assertEquals(100, bufferedImage.getHeight(), "图像高度应该为 100"); |
| 44 | + } |
| 45 | + |
| 46 | + // 测试绘制字符串的方法 |
| 47 | + @Test |
| 48 | + public void testDrawString() throws Exception { |
| 49 | + String code = "ABCD"; |
| 50 | + Method drawStringMethod = ShearCaptcha.class.getDeclaredMethod("drawString", Graphics2D.class, String.class); |
| 51 | + drawStringMethod.setAccessible(true); |
| 52 | + |
| 53 | + Graphics2D g2d = (Graphics2D) new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics(); |
| 54 | + drawStringMethod.invoke(captcha, g2d, code); |
| 55 | + |
| 56 | + assertNotNull(g2d, "Graphics2D 对象不应该为 null"); |
| 57 | + assertTrue(g2d.getRenderingHints().containsKey(RenderingHints.KEY_ANTIALIASING), "应该启用抗锯齿"); |
| 58 | + } |
| 59 | + |
| 60 | + // 测试 shear() 方法 |
| 61 | + @Test |
| 62 | + public void testShear() throws Exception { |
| 63 | + // 使用反射测试 shear 方法 |
| 64 | + Method shearMethod = ShearCaptcha.class.getDeclaredMethod("shear", Graphics.class, int.class, int.class, Color.class); |
| 65 | + shearMethod.setAccessible(true); |
| 66 | + |
| 67 | + Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics(); |
| 68 | + shearMethod.invoke(captcha, g, 200, 100, Color.WHITE); |
| 69 | + |
| 70 | + // 假设没有明显的错误输出,认为测试通过 |
| 71 | + assertNotNull(g, "Graphics 对象不应该为 null"); |
| 72 | + } |
| 73 | + |
| 74 | + // 测试 shearX() 方法 |
| 75 | + @Test |
| 76 | + public void testShearX() throws Exception { |
| 77 | + // 使用反射测试 shearX 方法 |
| 78 | + Method shearXMethod = ShearCaptcha.class.getDeclaredMethod("shearX", Graphics.class, int.class, int.class, Color.class); |
| 79 | + shearXMethod.setAccessible(true); |
| 80 | + |
| 81 | + Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics(); |
| 82 | + shearXMethod.invoke(captcha, g, 200, 100, Color.RED); |
| 83 | + |
| 84 | + // 假设没有明显的错误输出,认为测试通过 |
| 85 | + assertNotNull(g, "Graphics 对象不应该为 null"); |
| 86 | + } |
| 87 | + |
| 88 | + // 测试 shearY() 方法 |
| 89 | + @Test |
| 90 | + public void testShearY() throws Exception { |
| 91 | + // 使用反射测试 shearY 方法 |
| 92 | + Method shearYMethod = ShearCaptcha.class.getDeclaredMethod("shearY", Graphics.class, int.class, int.class, Color.class); |
| 93 | + shearYMethod.setAccessible(true); |
| 94 | + |
| 95 | + Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics(); |
| 96 | + shearYMethod.invoke(captcha, g, 200, 100, Color.BLUE); |
| 97 | + |
| 98 | + // 假设没有明显的错误输出,认为测试通过 |
| 99 | + assertNotNull(g, "Graphics 对象不应该为 null"); |
| 100 | + } |
| 101 | + |
| 102 | + // 测试 drawInterfere() 方法 |
| 103 | + @Test |
| 104 | + public void testDrawInterfere() throws Exception { |
| 105 | + // 使用反射测试 drawInterfere 方法 |
| 106 | + Method drawInterfereMethod = ShearCaptcha.class.getDeclaredMethod("drawInterfere", Graphics.class, int.class, int.class, int.class, int.class, int.class, Color.class); |
| 107 | + drawInterfereMethod.setAccessible(true); |
| 108 | + |
| 109 | + Graphics g = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB).getGraphics(); |
| 110 | + drawInterfereMethod.invoke(captcha, g, 0, 0, 200, 100, 4, Color.GREEN); |
| 111 | + |
| 112 | + // 假设没有明显的错误输出,认为测试通过 |
| 113 | + assertNotNull(g, "Graphics 对象不应该为 null"); |
| 114 | + } |
| 115 | + |
| 116 | + // 测试验证码生成时的干扰线 |
| 117 | + @Test |
| 118 | + public void testDrawInterfereLines() { |
| 119 | + // 设置干扰线数量 |
| 120 | + captcha = new ShearCaptcha(200, 100, 4); |
| 121 | + Image image = captcha.createImage("ABCD"); |
| 122 | + |
| 123 | + // 检查图像内容,判断干扰线是否正确绘制 |
| 124 | + assertNotNull(image, "生成的验证码图片不应该为空"); |
| 125 | + } |
| 126 | + |
| 127 | + // 测试验证码的尺寸 |
| 128 | + @Test |
| 129 | + public void testCaptchaSize() { |
| 130 | + captcha = new ShearCaptcha(300, 150); |
| 131 | + |
| 132 | + String code = "XYZ"; |
| 133 | + Image image = captcha.createImage(code); |
| 134 | + |
| 135 | + BufferedImage bufferedImage = (BufferedImage) image; |
| 136 | + assertEquals(300, bufferedImage.getWidth(), "图像宽度应该为 300"); |
| 137 | + assertEquals(150, bufferedImage.getHeight(), "图像高度应该为 150"); |
| 138 | + } |
| 139 | + |
| 140 | + // 测试生成随机验证码字符 |
| 141 | + @Test |
| 142 | + public void testRandomGenerator() { |
| 143 | + RandomGenerator randomGenerator = new RandomGenerator(4); |
| 144 | + String code = randomGenerator.generate(); |
| 145 | + assertNotNull(code, "生成的验证码字符不应该为 null"); |
| 146 | + assertEquals(4, code.length(), "验证码字符长度应该为 4"); |
| 147 | + } |
| 148 | +} |
0 commit comments