From 49e445c2b2f66e5f21dc818f2673709dd18f20d0 Mon Sep 17 00:00:00 2001 From: ssw Date: Fri, 12 Jun 2026 11:37:17 +0800 Subject: [PATCH] =?UTF-8?q?v15=20W1+=20FormNegativeDemoTest:=2017=20form?= =?UTF-8?q?=20=E8=B4=9F=E9=9D=A2=E7=94=A8=E4=BE=8B=20(L1-1~1-4=20+=20L2-1~?= =?UTF-8?q?2-6=20+=20SQL=20=E6=B3=A8=E5=85=A5=20+=20=E5=85=A8=E8=A7=92?= =?UTF-8?q?=E7=A9=BA=E6=A0=BC=20+=20=E5=AF=B9=E7=85=A7=E7=BB=84)=20(396=20?= =?UTF-8?q?total=20tests)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validation/FormNegativeDemoTest.java | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 ssw-dacli/src/test/java/ssw/dacli/validation/FormNegativeDemoTest.java diff --git a/ssw-dacli/src/test/java/ssw/dacli/validation/FormNegativeDemoTest.java b/ssw-dacli/src/test/java/ssw/dacli/validation/FormNegativeDemoTest.java new file mode 100644 index 0000000..bbfc22e --- /dev/null +++ b/ssw-dacli/src/test/java/ssw/dacli/validation/FormNegativeDemoTest.java @@ -0,0 +1,159 @@ +package ssw.dacli.validation; + +import org.junit.jupiter.api.Test; +import ssw.dacli.validation.form.FormConfig; +import ssw.dacli.validation.form.FormConfigValidator; +import ssw.dacli.validation.form.FormField; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * v15 W1+ — FormConfigValidator 负面数据断言测试. + * + *

17 个 form 负面/正面用例, 验证校验器真拒违规, 不会 false positive. + */ +public class FormNegativeDemoTest { + + private static final FormConfigValidator V = new FormConfigValidator(); + + @Test + public void testL1_1_nameMissing_rejected() { + FormConfig cfg = new FormConfig("", "asset", "cat", 2, List.of( + new FormField("f_id", "input", "主键", 1))); + List errs = V.validate(cfg); + assertTrue(errs.stream().anyMatch(e -> e.code().equals("form-L1-1")), + "L1-1 name 缺失应被拒, 实际: " + errs); + } + + @Test + public void testL1_1_nameDash_rejected() { + FormConfig cfg = new FormConfig("user-info", "asset", "cat", 2, List.of( + new FormField("f_id", "input", "主键", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L1-1")), + "L1-1 name 含连字符应被拒"); + } + + @Test + public void testL1_2_enCodeMissing_rejected() { + FormConfig cfg = new FormConfig("资产", "", "cat", 2, List.of( + new FormField("f_id", "input", "主键", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L1-2")), + "L1-2 enCode 缺失应被拒"); + } + + @Test + public void testL1_2_enCodeUnderscore_rejected() { + FormConfig cfg = new FormConfig("资产", "asset_x", "cat", 2, List.of( + new FormField("f_id", "input", "主键", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L1-2")), + "L1-2 enCode 含下划线应被拒 (formValidate 严格)"); + } + + @Test + public void testL1_2_enCodeChinese_rejected() { + FormConfig cfg = new FormConfig("资产", "资产编码", "cat", 2, List.of( + new FormField("f_id", "input", "主键", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L1-2")), + "L1-2 enCode 含中文应被拒"); + } + + @Test + public void testL1_3_categoryMissing_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "", 2, List.of( + new FormField("f_id", "input", "主键", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L1-3")), + "L1-3 category 缺失应被拒"); + } + + @Test + public void testL1_4_webTypeOutOfRange_rejected() { + for (int wt : new int[]{-1, 0, 5, 99, Integer.MAX_VALUE}) { + FormConfig cfg = new FormConfig("资产", "asset", "cat", wt, List.of( + new FormField("f_id", "input", "主键", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L1-4")), + "L1-4 webType=" + wt + " 应被拒"); + } + } + + @Test + public void testL2_1_noFields_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "cat", 2, List.of()); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L2-1")), + "L2-1 空字段应被拒"); + } + + @Test + public void testL2_2_modelNameEmpty_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "cat", 2, List.of( + new FormField("", "input", "PK", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L2-2")), + "L2-2 modelName 空应被拒"); + } + + @Test + public void testL2_3_modelNameNumberStart_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "cat", 2, List.of( + new FormField("1bad", "input", "PK", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L2-3")), + "L2-3 数字开头应被拒"); + } + + @Test + public void testL2_4_modelNameDuplicated_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "cat", 2, List.of( + new FormField("f_id", "input", "PK", 1), + new FormField("f_id", "input", "重复", 0))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L2-4")), + "L2-4 字段名重复应被拒"); + } + + @Test + public void testL2_5_jnpfKeyEmpty_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "cat", 2, List.of( + new FormField("f_id", "", "PK", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L2-5")), + "L2-5 jnpfKey 空应被拒"); + } + + @Test + public void testL2_6_jnpfKeyNotRegistered_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "cat", 2, List.of( + new FormField("f_id", "totallyUnregisteredJnpfKey", "PK", 1))); + assertTrue(V.validate(cfg).stream().anyMatch(e -> e.code().equals("form-L2-6")), + "L2-6 jnpfKey 未注册应被拒"); + } + + @Test + public void testSqlInjection_rejected() { + FormConfig cfg = new FormConfig("资产", "asset", "cat", 2, List.of( + new FormField("f_name; DROP TABLE users;--", "input", "X", 1))); + assertTrue(!V.validate(cfg).isEmpty(), "SQL 注入应被拒 (至少 1 条规则)"); + } + + @Test + public void testWhitespace_modelName_rejected() { + FormConfig cfg = new FormConfig(" ", "asset", "cat", 2, List.of( + new FormField("f_id", "input", "PK", 1))); + assertTrue(!V.validate(cfg).isEmpty(), "空格 name 应被拒"); + } + + @Test + public void testFullWidthSpace_rejected() { + FormConfig cfg = new FormConfig("资产 名称", "asset", "cat", 2, List.of( + new FormField("f_id", "input", "PK", 1))); + assertTrue(!V.validate(cfg).isEmpty(), "全角空格应被拒"); + } + + @Test + public void testValidForm_noFalsePositive() { + // 对照组: 真实合法 form 应通过, 不能误报 + FormConfig cfg = new FormConfig("学生信息表", "studentInfo", "cat-uuid", 2, List.of( + new FormField("f_name", "input", "姓名", 0), + new FormField("f_age", "inputNumber", "年龄", 0), + new FormField("f_id", "input", "主键", 1))); + List errs = V.validate(cfg); + assertTrue(errs.isEmpty(), "真实合法 form 不应被拒, 实际: " + errs); + } +}