You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.7 KiB

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
public class Test {
// 暗色主题常量(修改一处常量示例)
public static final String BACKGROUND_COLOR = "#1E1E1E"; // 深灰(暗色)
public static final String TEXT_COLOR = "#FFFFFF"; // 白色
// 命令别名映射
private static final Map<String, String> ALIASES = new HashMap<>();
static {
ALIASES.put("c", "crawl");
ALIASES.put("h", "help");
}
// URL 格式验证正则
private static final Pattern URL_PATTERN =
Pattern.compile("^(https?://)?[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)+(/[a-zA-Z0-9-./]*)*$");
public static boolean isValidUrl(String url) {
if (url == null || url.isBlank()) {
return false;
}
return URL_PATTERN.matcher(url).matches();
}
public static String resolveAlias(String inputCommand) {
String[] parts = inputCommand.split(" ", 2);
String command = parts[0];
String args = parts.length > 1 ? parts[1] : "";
String realCommand = ALIASES.getOrDefault(command, command);
return args.isEmpty() ? realCommand : realCommand + " " + args;
}
public static void main(String[] args) {
// 1. 测试 Article 类
Article article = new Article(
"Java MVC 入门",
"这是一篇关于MVC模式的文章...",
"张三",
LocalDate.now()
);
System.out.println("=== 测试 Article 类 ===");
System.out.println(article);
// 2. 测试 HistoryCommand
HistoryCommand history = new HistoryCommand();
history.addCommand("c https://example.com");
history.addCommand("help");
history.addCommand("exit");
System.out.println("\n=== 测试 HistoryCommand ===");
history.printHistory();
// 3. 测试命令别名
System.out.println("\n=== 测试命令别名 ===");
System.out.println("输入:c https://test.com → 解析后:" + resolveAlias("c https://test.com"));
System.out.println("输入:h → 解析后:" + resolveAlias("h"));
// 4. 测试 URL 验证
System.out.println("\n=== 测试 URL 验证 ===");
System.out.println("https://example.com → " + (isValidUrl("https://example.com") ? "有效" : "无效"));
System.out.println("invalid-url → " + (isValidUrl("invalid-url") ? "有效" : "无效"));
// 5. 测试暗色主题常量
System.out.println("\n=== 测试暗色主题常量 ===");
System.out.println("背景色:" + BACKGROUND_COLOR);
System.out.println("文字色:" + TEXT_COLOR);
}
}