chore: initialize repository with gitignore
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
plugins {
|
||||
id 'application'
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation libs.junit.jupiter
|
||||
|
||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||
|
||||
implementation libs.guava
|
||||
}
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
mainClass = 'login.LoginValidator'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package login;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LoginValidator {
|
||||
|
||||
public static List<String> validate(String account, String password) {
|
||||
List<String> messages = new ArrayList<>();
|
||||
|
||||
boolean accountValid = isAccountValid(account);
|
||||
boolean passwordValid = isPasswordValid(password);
|
||||
|
||||
if (accountValid && passwordValid) {
|
||||
messages.add("输入合法");
|
||||
} else {
|
||||
if (!accountValid) {
|
||||
messages.add("账号不合法");
|
||||
}
|
||||
if (!passwordValid) {
|
||||
messages.add("密码不合法");
|
||||
}
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* 账号合法:6-10位自然数(纯数字,不含负号/小数点,长度6~10)
|
||||
*/
|
||||
static boolean isAccountValid(String account) {
|
||||
if (account == null) return false;
|
||||
int len = account.length();
|
||||
if (len < 6 || len > 10) return false;
|
||||
for (char c : account.toCharArray()) {
|
||||
if (c < '0' || c > '9') return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码合法:恰好8位字符串(非null,长度==8)
|
||||
*/
|
||||
static boolean isPasswordValid(String password) {
|
||||
if (password == null) return false;
|
||||
return password.length() == 8;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length != 2) {
|
||||
System.out.println("用法: java login.LoginValidator <账号> <密码>");
|
||||
return;
|
||||
}
|
||||
List<String> result = validate(args[0], args[1]);
|
||||
result.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package login;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.List;
|
||||
|
||||
class LoginValidatorTest {
|
||||
|
||||
// ========================================================================
|
||||
// 因果图分析
|
||||
// ========================================================================
|
||||
//
|
||||
// 原因 (Causes):
|
||||
// C1: 账号长度 >= 6
|
||||
// C2: 账号长度 <= 10
|
||||
// C3: 账号为纯数字(自然数)
|
||||
// C4: 密码长度 = 8
|
||||
//
|
||||
// 中间节点:
|
||||
// M1 = C1 ∧ C2 ∧ C3 (账号合法)
|
||||
//
|
||||
// 结果 (Effects):
|
||||
// E1: 显示"输入合法" ← M1 ∧ C4
|
||||
// E2: 显示"账号不合法" ← ¬M1
|
||||
// E3: 显示"密码不合法" ← ¬C4
|
||||
//
|
||||
// ========================================================================
|
||||
// 判定表简化后的 4 条规则 → 测试用例
|
||||
// ========================================================================
|
||||
//
|
||||
// | 规则 | 账号合法(M1) | 密码合法(C4) | 预期结果 |
|
||||
// |------|-------------|-------------|------------------------|
|
||||
// | R1 | T | T | 输入合法 |
|
||||
// | R2 | T | F | 密码不合法 |
|
||||
// | R3 | F | T | 账号不合法 |
|
||||
// | R4 | F | F | 账号不合法 + 密码不合法 |
|
||||
//
|
||||
|
||||
// --- 规则 R1: 账号合法 ∧ 密码合法 → 输入合法 ---
|
||||
|
||||
@Test
|
||||
@DisplayName("R1: 账号合法(6位数字) + 密码合法(8位) → 输入合法")
|
||||
void testR1_validAccountAndPassword() {
|
||||
List<String> result = LoginValidator.validate("123456", "abcd1234");
|
||||
assertEquals(List.of("输入合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R1 变体: 账号10位数字 + 密码8位 → 输入合法")
|
||||
void testR1_account10Digits() {
|
||||
List<String> result = LoginValidator.validate("1234567890", "pass$12#");
|
||||
assertEquals(List.of("输入合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R1 变体: 账号8位数字(中间值) + 密码8位 → 输入合法")
|
||||
void testR1_accountMiddleLength() {
|
||||
List<String> result = LoginValidator.validate("12345678", "Pa55w0rd");
|
||||
assertEquals(List.of("输入合法"), result);
|
||||
}
|
||||
|
||||
// --- 规则 R2: 账号合法 ∧ 密码不合法 → 密码不合法 ---
|
||||
|
||||
@Test
|
||||
@DisplayName("R2: 账号合法 + 密码过短(7位) → 密码不合法")
|
||||
void testR2_passwordTooShort() {
|
||||
List<String> result = LoginValidator.validate("123456", "abcd123");
|
||||
assertEquals(List.of("密码不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R2 变体: 账号合法 + 密码过长(9位) → 密码不合法")
|
||||
void testR2_passwordTooLong() {
|
||||
List<String> result = LoginValidator.validate("123456", "abcd12345");
|
||||
assertEquals(List.of("密码不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R2 变体: 账号合法 + 密码为空 → 密码不合法")
|
||||
void testR2_passwordEmpty() {
|
||||
List<String> result = LoginValidator.validate("123456", "");
|
||||
assertEquals(List.of("密码不合法"), result);
|
||||
}
|
||||
|
||||
// --- 规则 R3: 账号不合法 ∧ 密码合法 → 账号不合法 ---
|
||||
|
||||
@Test
|
||||
@DisplayName("R3a: 账号过短(5位) + 密码合法 → 账号不合法 [C1为假]")
|
||||
void testR3_accountTooShort() {
|
||||
List<String> result = LoginValidator.validate("12345", "abcd1234");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R3b: 账号过长(11位) + 密码合法 → 账号不合法 [C2为假]")
|
||||
void testR3_accountTooLong() {
|
||||
List<String> result = LoginValidator.validate("12345678901", "abcd1234");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R3c: 账号含非数字字符 + 密码合法 → 账号不合法 [C3为假]")
|
||||
void testR3_accountNonNumeric() {
|
||||
List<String> result = LoginValidator.validate("12345a", "abcd1234");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R3d: 账号为空 + 密码合法 → 账号不合法")
|
||||
void testR3_accountEmpty() {
|
||||
List<String> result = LoginValidator.validate("", "abcd1234");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
// --- 规则 R4: 账号不合法 ∧ 密码不合法 → 账号不合法 + 密码不合法 ---
|
||||
|
||||
@Test
|
||||
@DisplayName("R4a: 账号过短 + 密码过短 → 账号不合法 + 密码不合法")
|
||||
void testR4_bothInvalid_shortShort() {
|
||||
List<String> result = LoginValidator.validate("123", "abc");
|
||||
assertEquals(List.of("账号不合法", "密码不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R4b: 账号过长 + 密码过长 → 账号不合法 + 密码不合法")
|
||||
void testR4_bothInvalid_longLong() {
|
||||
List<String> result = LoginValidator.validate("12345678901", "abcde12345");
|
||||
assertEquals(List.of("账号不合法", "密码不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("R4c: 账号含字母 + 密码为空 → 账号不合法 + 密码不合法")
|
||||
void testR4_bothInvalid_nonNumericEmpty() {
|
||||
List<String> result = LoginValidator.validate("abcdef", "");
|
||||
assertEquals(List.of("账号不合法", "密码不合法"), result);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// 补充: 覆盖各个原因条件的边界情况
|
||||
// ========================================================================
|
||||
|
||||
@Test
|
||||
@DisplayName("边界: 账号恰好6位(下界) + 密码8位")
|
||||
void testBoundary_account6() {
|
||||
List<String> result = LoginValidator.validate("100000", "12345678");
|
||||
assertEquals(List.of("输入合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("边界: 账号恰好10位(上界) + 密码8位")
|
||||
void testBoundary_account10() {
|
||||
List<String> result = LoginValidator.validate("1000000000", "12345678");
|
||||
assertEquals(List.of("输入合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("边界: 账号5位(下界-1) + 密码8位")
|
||||
void testBoundary_account5() {
|
||||
List<String> result = LoginValidator.validate("10000", "12345678");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("边界: 账号11位(上界+1) + 密码8位")
|
||||
void testBoundary_account11() {
|
||||
List<String> result = LoginValidator.validate("10000000001", "12345678");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("边界: 密码恰好8位")
|
||||
void testBoundary_password8() {
|
||||
List<String> result = LoginValidator.validate("123456", "12345678");
|
||||
assertEquals(List.of("输入合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("边界: 密码7位(8-1)")
|
||||
void testBoundary_password7() {
|
||||
List<String> result = LoginValidator.validate("123456", "1234567");
|
||||
assertEquals(List.of("密码不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("边界: 密码9位(8+1)")
|
||||
void testBoundary_password9() {
|
||||
List<String> result = LoginValidator.validate("123456", "123456789");
|
||||
assertEquals(List.of("密码不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("C3: 账号含空格")
|
||||
void testAccountWithSpace() {
|
||||
List<String> result = LoginValidator.validate("123 56", "12345678");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("C3: 账号含负号")
|
||||
void testAccountWithMinus() {
|
||||
List<String> result = LoginValidator.validate("-12345", "12345678");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("C3: 账号含小数点")
|
||||
void testAccountWithDot() {
|
||||
List<String> result = LoginValidator.validate("123.56", "12345678");
|
||||
assertEquals(List.of("账号不合法"), result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user