Commit 257c8d2d authored by wujipeng's avatar wujipeng
Browse files

优化

parent 0562fc83
...@@ -9,11 +9,14 @@ import java.io.ByteArrayInputStream; ...@@ -9,11 +9,14 @@ import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** 直接验证RTF正文,防止测试只检查文件外壳而漏掉Word/WPS打开后内容为空的问题。 */ /** 验证原版式RTF模板的结构和可见正文,防止只检查文件外壳而遗漏空白导出。 */
class B2TemplateInspectionTest { class B2TemplateInspectionTest {
@Test @Test
void generatedDocumentShouldContainProfileValuesAndWordTableStructure() throws Exception { void generatedDocumentShouldContainProfileValuesAndWordTableStructure() throws Exception {
...@@ -36,27 +39,34 @@ class B2TemplateInspectionTest { ...@@ -36,27 +39,34 @@ class B2TemplateInspectionTest {
byte[] bytes = new StudentCertificationAdminServiceImpl().createDocument(profile); byte[] bytes = new StudentCertificationAdminServiceImpl().createDocument(profile);
String rtf = new String(bytes, StandardCharsets.US_ASCII); String rtf = new String(bytes, StandardCharsets.US_ASCII);
// RTF内容全部以ASCII控制字表达,测试可稳定检查中文对应的Unicode转义值。 // 原模板固定为33行复杂合并表格;行数变化意味着字段坐标需要重新人工确认。
assertTrue(rtf.startsWith("{\\rtf1")); assertTrue(rtf.startsWith("{\\rtf1"));
assertTrue(rtf.contains("\\trowd")); assertTrue(rtf.contains("\\trowd"));
assertEquals(33, countMatches(rtf, Pattern.compile("\\\\row(?![a-zA-Z])")));
assertTrue(rtf.contains(toRtfUnicode("张三"))); assertTrue(rtf.contains(toRtfUnicode("张三")));
assertTrue(rtf.contains("310101199506011234"));
assertTrue(rtf.contains("13800138000")); assertTrue(rtf.contains("13800138000"));
assertTrue(rtf.contains(toRtfUnicode("企业培训")));
assertTrue(rtf.contains(toRtfUnicode("污水处理工"))); assertTrue(rtf.contains(toRtfUnicode("污水处理工")));
assertTrue(rtf.contains(toRtfUnicode("[√]企业培训"))); assertTrue(rtf.contains(toRtfUnicode("示例水务公司")));
assertTrue(rtf.contains("CERT-001"));
assertFalse(rtf.contains("WordDocument")); assertFalse(rtf.contains("WordDocument"));
// 使用JDK自带RTF解析器执行一次真实回读,证明输出不是“控制字存在但文档不可解析”。 // 使用JDK自带RTF解析器真实回读;去除单元格间空白后,18位身份证必须完整可见。
DefaultStyledDocument parsedDocument = new DefaultStyledDocument(); DefaultStyledDocument parsedDocument = new DefaultStyledDocument();
new RTFEditorKit().read(new ByteArrayInputStream(bytes), parsedDocument, 0); new RTFEditorKit().read(new ByteArrayInputStream(bytes), parsedDocument, 0);
String visibleText = parsedDocument.getText(0, parsedDocument.getLength()); String visibleText = parsedDocument.getText(0, parsedDocument.getLength());
String compactText = visibleText.replaceAll("\\s+", "");
assertTrue(visibleText.contains("张三")); assertTrue(visibleText.contains("张三"));
assertTrue(visibleText.contains("310101199506011234")); assertTrue(compactText.contains("310101199506011234"));
assertTrue(visibleText.contains("企业培训"));
assertTrue(visibleText.contains("污水处理工")); assertTrue(visibleText.contains("污水处理工"));
} }
private int countMatches(String source, Pattern pattern) {
int count = 0;
Matcher matcher = pattern.matcher(source);
while (matcher.find()) count++;
return count;
}
/** 与生产代码相同地把中文转换为RTF Unicode控制字,避免测试依赖本机字符集。 */ /** 与生产代码相同地把中文转换为RTF Unicode控制字,避免测试依赖本机字符集。 */
private String toRtfUnicode(String value) { private String toRtfUnicode(String value) {
StringBuilder escaped = new StringBuilder(); StringBuilder escaped = new StringBuilder();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment