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); + } +}