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.
95 lines
3.7 KiB
95 lines
3.7 KiB
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class TestMain {
|
|
public static void main(String[] args) {
|
|
System.out.println("===== 测试 ArticleRepository =====");
|
|
testRepository();
|
|
|
|
System.out.println("\n===== 测试 AnalyzeCommand =====");
|
|
testAnalyzeCommand();
|
|
|
|
System.out.println("\n===== 测试防御性编程 =====");
|
|
testDefensiveProgramming();
|
|
}
|
|
|
|
private static void testRepository() {
|
|
ArticleRepository repo = new ArticleRepository();
|
|
|
|
Article article1 = new Article("Java编程入门", "https://example.com/java");
|
|
Article article2 = new Article("Python数据分析", "https://example.com/python");
|
|
Article article3 = new Article("JavaScript高级", "https://example.com/js");
|
|
|
|
// 测试添加单个文章
|
|
repo.add(article1);
|
|
repo.add(article2);
|
|
System.out.println("添加2篇文章后数量: " + repo.count());
|
|
|
|
// 测试添加多个文章
|
|
repo.addAll(Arrays.asList(article3));
|
|
System.out.println("批量添加后数量: " + repo.count());
|
|
|
|
// 测试查找
|
|
Article found = repo.findByTitle("Java编程入门");
|
|
System.out.println("查找结果: " + (found != null ? found.getTitle() : "未找到"));
|
|
|
|
// 测试获取所有文章(不可变视图)
|
|
List<Article> all = repo.getAll();
|
|
System.out.println("获取文章列表大小: " + all.size());
|
|
}
|
|
|
|
private static void testAnalyzeCommand() {
|
|
// 创建示例文章
|
|
List<Article> articles = new ArrayList<>();
|
|
articles.add(new Article("Java编程入门教程", "https://example.com/java"));
|
|
articles.add(new Article("Python数据分析实战", "https://example.com/python"));
|
|
articles.add(new Article("JavaScript高级编程", "https://example.com/js"));
|
|
articles.add(new Article("Spring Boot指南", "https://example.com/spring"));
|
|
articles.add(new Article("Docker容器化部署", "https://example.com/docker"));
|
|
|
|
// 创建分析器列表
|
|
List<ArticleAnalyzer> analyzers = new ArrayList<>();
|
|
analyzers.add(new ArticleCountAnalyzer());
|
|
analyzers.add(new TitleLengthAnalyzer());
|
|
|
|
// 创建并执行分析命令
|
|
AnalyzeCommand command = new AnalyzeCommand(articles, analyzers);
|
|
command.execute();
|
|
}
|
|
|
|
private static void testDefensiveProgramming() {
|
|
ArticleRepository repo = new ArticleRepository();
|
|
|
|
try {
|
|
repo.add(null);
|
|
System.out.println("ERROR: 应该抛出异常!");
|
|
} catch (NullPointerException e) {
|
|
System.out.println("正确: 成功捕获 null 文章异常");
|
|
}
|
|
|
|
try {
|
|
repo.addAll(null);
|
|
System.out.println("ERROR: 应该抛出异常!");
|
|
} catch (NullPointerException e) {
|
|
System.out.println("正确: 成功捕获 null 集合异常");
|
|
}
|
|
|
|
try {
|
|
List<Article> listWithNull = new ArrayList<>();
|
|
listWithNull.add(new Article("Test", "https://test.com"));
|
|
listWithNull.add(null);
|
|
repo.addAll(listWithNull);
|
|
System.out.println("ERROR: 应该抛出异常!");
|
|
} catch (NullPointerException e) {
|
|
System.out.println("正确: 成功捕获集合中包含 null 异常");
|
|
}
|
|
|
|
try {
|
|
new Article(null, "https://test.com");
|
|
System.out.println("ERROR: 应该抛出异常!");
|
|
} catch (IllegalArgumentException e) {
|
|
System.out.println("正确: 成功捕获空标题异常");
|
|
}
|
|
}
|
|
}
|