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 = """ new-old revision-old new-current revision-current """; 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"); } }