public class PDFTextExtractionTest {
private PDFTextExtraction textExtractionService;
private File testFile;
@BeforeEach
public void setUp() {
// 初始化服务实现类
textExtractionService = new PDFTextExtractionImpl();
// 设置测试文件路径
try {
testFile = ResourceUtils.getFile("classpath:legal_data/PDF/test01.pdf");
System.out.println("文件路径: " + testFile.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void testExtractSelectableText() throws IOException {
// 提取可选择文本
String text = textExtractionService.extractSelectableText(testFile);
// 断言文本不为空
assertNotNull(text, "Selectable text should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Selectable text should not be empty");
// 输出提取的文本
System.out.println("Selectable Text:\n" + text);
}
@Test
public void testExtractImageText() throws IOException, TesseractException {
// 提取图像中的文本
String text = textExtractionService.extractImageText(testFile);
// 断言文本不为空
assertNotNull(text, "Image text should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Image text should not be empty");
// 输出提取的文本
System.out.println("Image Text:\n" + text);
}
@Test
public void testExtractFullText() throws IOException, TesseractException {
// 提取完整文本
String text = textExtractionService.extractFullText(testFile);
// 断言文本不为空
assertNotNull(text, "Full text should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Full text should not be empty");
// 输出提取的文本
System.out.println("Full Text (Selectable + Image):\n" + text);
}
@Test
public void testExtractImageTextM() throws IOException, TesseractException, InterruptedException, ExecutionException {
try {
// 多线程提取图像中的文本
String text = textExtractionService.extractImageTextM(testFile);
// 断言文本不为空
assertNotNull(text, "Image text (multi-threaded) should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Image text (multi-threaded) should not be empty");
// 输出提取的文本
System.out.println("Image Text (multi-threaded):\n" + text);
} catch (RuntimeException e) {
// 处理异常情况
System.err.println("Failed to process extractImageTextM: " + e.getMessage());
e.printStackTrace();
}
}
@Test
public void testExtractImageTextWithoutWhitespace() throws IOException, TesseractException {
// 提取图像中的文本并去除空白
String text = textExtractionService.extractImageTextWithoutWhitespace(testFile);
// 断言文本不为空
assertNotNull(text, "Image text without whitespace should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Image text without whitespace should not be empty");
// 输出提取的文本
System.out.println("Image Text without Whitespace:\n" + text);
}
}
1. 初始化服务实现类
@BeforeEach
public void setUp() {
// 初始化服务实现类
textExtractionService = new PDFTextExtractionImpl();
// 设置测试文件路径
try {
testFile = ResourceUtils.getFile("classpath:legal_data/PDF/test01.pdf");
System.out.println("文件路径: " + testFile.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
2. 测试提取可选择文本
@Test
public void testExtractSelectableText() throws IOException {
// 提取可选择文本
String text = textExtractionService.extractSelectableText(testFile);
// 断言文本不为空
assertNotNull(text, "Selectable text should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Selectable text should not be empty");
// 输出提取的文本
System.out.println("Selectable Text:\n" + text);
}
3. 测试提取图像中的文本
@Test
public void testExtractImageText() throws IOException, TesseractException {
// 提取图像中的文本
String text = textExtractionService.extractImageText(testFile);
// 断言文本不为空
assertNotNull(text, "Image text should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Image text should not be empty");
// 输出提取的文本
System.out.println("Image Text:\n" + text);
}
4. 测试提取完整文本
@Test
public void testExtractFullText() throws IOException, TesseractException {
// 提取完整文本
String text = textExtractionService.extractFullText(testFile);
// 断言文本不为空
assertNotNull(text, "Full text should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Full text should not be empty");
// 输出提取的文本
System.out.println("Full Text (Selectable + Image):\n" + text);
}
5. 测试多线程提取图像中的文本
@Test
public void testExtractImageTextM() throws IOException, TesseractException, InterruptedException, ExecutionException {
try {
// 多线程提取图像中的文本
String text = textExtractionService.extractImageTextM(testFile);
// 断言文本不为空
assertNotNull(text, "Image text (multi-threaded) should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Image text (multi-threaded) should not be empty");
// 输出提取的文本
System.out.println("Image Text (multi-threaded):\n" + text);
} catch (RuntimeException e) {
// 处理异常情况
System.err.println("Failed to process extractImageTextM: " + e.getMessage());
e.printStackTrace();
}
}
```
6. 测试提取图像中的文本并去除空白
@Test
public void testExtractImageTextWithoutWhitespace() throws IOException, TesseractException {
// 提取图像中的文本并去除空白
String text = textExtractionService.extractImageTextWithoutWhitespace(testFile);
// 断言文本不为空
assertNotNull(text, "Image text without whitespace should not be null");
// 断言文本长度大于0
assertTrue(text.length() > 0, "Image text without whitespace should not be empty");
// 输出提取的文本
System.out.println("Image Text without Whitespace:\n" + text);
}