提交货源流程优化
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package com.nanri.aiimage.modules.similarasin.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ExcelCellImageWriterTest {
|
||||
|
||||
@Test
|
||||
void patchedCellImageValueMetadataUsesZeroBasedVmIndexes() throws Exception {
|
||||
Path dir = Files.createTempDirectory("excel-cell-image-test-");
|
||||
Path xlsx = dir.resolve("result.xlsx");
|
||||
writeMinimalWorkbook(xlsx);
|
||||
|
||||
ExcelCellImageWriter.Session session = ExcelCellImageWriter.createSession();
|
||||
session.registerImage(1, 9, new byte[]{1, 2, 3});
|
||||
session.registerImage(1, 10, new byte[]{4, 5, 6});
|
||||
|
||||
ExcelCellImageWriter.patchXlsxFile(xlsx.toFile(), session);
|
||||
|
||||
try (ZipFile zip = new ZipFile(xlsx.toFile())) {
|
||||
String sheet = read(zip, "xl/worksheets/sheet1.xml");
|
||||
assertTrue(sheet.contains("<c r=\"J2\" t=\"e\" vm=\"0\"><v>#VALUE!</v></c>"));
|
||||
assertTrue(sheet.contains("<c r=\"K2\" t=\"e\" vm=\"1\"><v>#VALUE!</v></c>"));
|
||||
|
||||
String metadata = read(zip, "xl/metadata.xml");
|
||||
assertTrue(metadata.contains("<xlrd:rvb i=\"0\"/>"));
|
||||
assertTrue(metadata.contains("<xlrd:rvb i=\"1\"/>"));
|
||||
assertEquals(2, maxVm(sheet) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private static int maxVm(String sheet) {
|
||||
Matcher matcher = Pattern.compile("vm=\"(\\d+)\"").matcher(sheet);
|
||||
int max = -1;
|
||||
while (matcher.find()) {
|
||||
max = Math.max(max, Integer.parseInt(matcher.group(1)));
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
private static String read(ZipFile zip, String name) throws Exception {
|
||||
return new String(zip.getInputStream(zip.getEntry(name)).readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static void writeMinimalWorkbook(Path xlsx) throws Exception {
|
||||
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(xlsx))) {
|
||||
write(out, "[Content_Types].xml", """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
||||
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
||||
<Default Extension="xml" ContentType="application/xml"/>
|
||||
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>
|
||||
<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>
|
||||
</Types>
|
||||
""");
|
||||
write(out, "_rels/.rels", """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
|
||||
</Relationships>
|
||||
""");
|
||||
write(out, "xl/workbook.xml", """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
|
||||
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
||||
<sheets><sheet name="Sheet1" r:id="rId1" sheetId="1"/></sheets>
|
||||
</workbook>
|
||||
""");
|
||||
write(out, "xl/_rels/workbook.xml.rels", """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
|
||||
</Relationships>
|
||||
""");
|
||||
write(out, "xl/worksheets/sheet1.xml", """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
||||
<sheetData>
|
||||
<row r="2">
|
||||
<c r="J2" t="inlineStr"><is><t>main</t></is></c>
|
||||
<c r="K2" t="inlineStr"><is><t>puzzle</t></is></c>
|
||||
</row>
|
||||
</sheetData>
|
||||
</worksheet>
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
private static void write(ZipOutputStream out, String name, String content) throws Exception {
|
||||
out.putNextEntry(new ZipEntry(name));
|
||||
out.write(content.getBytes(StandardCharsets.UTF_8));
|
||||
out.closeEntry();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user