刘光辉
7 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package jnpf.bizcommon.onlyoffice.service.impl;
 
import org.junit.Test;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
 
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
 
import static org.junit.Assert.assertEquals;
 
public class DmsQianpiyeDocxPropertiesWriterTest {
    private final DmsQianpiyeDocxPropertiesWriter writer = new DmsQianpiyeDocxPropertiesWriter();
 
    @Test
    public void keepsTheLastStagedValueAndWritesEachFlowPropertyOnce() throws Exception {
        byte[] saved = writer.upsertStagedFlowVersions(docxWithCustomProperties());
        NodeList properties = customProperties(saved);
        int newCount = 0;
        int revisionCount = 0;
        String newValue = null;
        String revisionValue = null;
        for (int index = 0; index < properties.getLength(); index++) {
            Element property = (Element) properties.item(index);
            if (DmsQianpiyeDocxPropertiesWriter.NEW_FLOW_VERSION_PROPERTY.equals(property.getAttribute("name"))) {
                newCount++; newValue = property.getTextContent();
            }
            if (DmsQianpiyeDocxPropertiesWriter.EDIT_FLOW_VERSION_PROPERTY.equals(property.getAttribute("name"))) {
                revisionCount++; revisionValue = property.getTextContent();
            }
        }
        assertEquals(1, newCount);
        assertEquals(1, revisionCount);
        assertEquals("new-current", newValue);
        assertEquals("revision-current", revisionValue);
    }
 
    private byte[] docxWithCustomProperties() throws Exception {
        String custom = """
                <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
                  xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
                  <property pid="2" name="DMS_NEW_FLOW_VERSION_ID"><vt:lpwstr>new-old</vt:lpwstr></property>
                  <property pid="3" name="DMS_EDIT_FLOW_VERSION_ID"><vt:lpwstr>revision-old</vt:lpwstr></property>
                  <property pid="4" name="DMS_NEW_FLOW_VERSION_ID"><vt:lpwstr>new-current</vt:lpwstr></property>
                  <property pid="5" name="DMS_EDIT_FLOW_VERSION_ID"><vt:lpwstr>revision-current</vt:lpwstr></property>
                </Properties>
                """;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try (ZipOutputStream zip = new ZipOutputStream(output)) {
            zip.putNextEntry(new ZipEntry("docProps/custom.xml"));
            zip.write(custom.getBytes(StandardCharsets.UTF_8));
            zip.closeEntry();
        }
        return output.toByteArray();
    }
 
    private NodeList customProperties(byte[] docx) throws Exception {
        try (ZipInputStream input = new ZipInputStream(new ByteArrayInputStream(docx))) {
            ZipEntry entry;
            while ((entry = input.getNextEntry()) != null) {
                if (!DmsQianpiyeDocxPropertiesWriter.CUSTOM_PROPERTIES_PART.equals(entry.getName())) continue;
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setNamespaceAware(true);
                return factory.newDocumentBuilder().parse(input).getElementsByTagNameNS(
                        "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties", "property");
            }
        }
        throw new AssertionError("custom.xml missing");
    }
}