liujie
2026-09-15 5a95ec36565d7ecc1a5ecb261f26752c90dec120
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.estate.expert.admin.stat.report.builder;
 
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Test;
 
import java.io.ByteArrayInputStream;
import java.util.Base64;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
 
class ChengduHengtaiSystemReportBuilderTest {
 
    private static final int TEMPLATE_PICTURE_ROW_HEIGHT = 3308;
 
    private static final byte[] ONE_PIXEL_PNG = Base64.getDecoder().decode(
            "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=");
 
    @Test
    void shouldCompactPictureRowWhenNoPictureExists() throws Exception {
        try (var document = createPictureTable()) {
            ChengduHengtaiSystemReportBuilder.compactEmptyPictureRows(document);
 
            assertEquals(240, document.getTables().get(0).getRow(1).getHeight());
        }
    }
 
    @Test
    void shouldKeepTemplateHeightWhenPictureExists() throws Exception {
        try (var document = createPictureTable()) {
            var pictureCell = document.getTables().get(0).getRow(1).getCell(0);
            pictureCell.getParagraphs().get(0).createRun().addPicture(
                    new ByteArrayInputStream(ONE_PIXEL_PNG),
                    Document.PICTURE_TYPE_PNG,
                    "pixel.png",
                    Units.pixelToEMU(1),
                    Units.pixelToEMU(1));
 
            ChengduHengtaiSystemReportBuilder.compactEmptyPictureRows(document);
 
            assertEquals(TEMPLATE_PICTURE_ROW_HEIGHT, document.getTables().get(0).getRow(1).getHeight());
        }
    }
 
    private XWPFDocument createPictureTable() {
        var document = new XWPFDocument();
        var table = document.createTable(2, 1);
        table.getRow(0).getCell(0).setText("问题图片");
        table.getRow(1).setHeight(TEMPLATE_PICTURE_ROW_HEIGHT);
        return document;
    }
}