package jnpf.fileStorage.dms; import jnpf.exception.DataException; import org.junit.Test; 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.*; public class DmsPageWatermarkAnchorsTest { @Test public void fixesOnlyMarkedDrawingsAndPreservesOtherArchiveParts() throws Exception { byte[] xml = ("" + anchor("DMS_CUSTOM_test_0_1") + anchor("original-image") + anchor("DMS_CUSTOM_test_0_3") + "").getBytes(StandardCharsets.UTF_8); byte[] source = archive(xml); byte[] output = DmsPageWatermarkAnchors.fix(source, "DMS_CUSTOM_test", 2); javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); org.w3c.dom.Document document = factory.newDocumentBuilder().parse(new ByteArrayInputStream(part(output, "word/document.xml"))); org.w3c.dom.NodeList anchors = document.getElementsByTagNameNS("*", "anchor"); assertEquals("0", ((org.w3c.dom.Element) anchors.item(0)).getAttribute("layoutInCell")); assertEquals("1", ((org.w3c.dom.Element) anchors.item(1)).getAttribute("layoutInCell")); assertEquals("0", ((org.w3c.dom.Element) anchors.item(2)).getAttribute("layoutInCell")); assertPageAnchored((org.w3c.dom.Element) anchors.item(0)); assertPageAnchored((org.w3c.dom.Element) anchors.item(2)); org.w3c.dom.Element original = (org.w3c.dom.Element) anchors.item(1); assertEquals("column", ((org.w3c.dom.Element) original .getElementsByTagNameNS("*", "positionH").item(0)).getAttribute("relativeFrom")); assertEquals("123", original.getElementsByTagNameNS("*", "posOffset").item(0).getTextContent()); assertArrayEquals(new byte[]{1, 2, 3}, part(output, "word/media/original.png")); assertArrayEquals(xml, part(source, "word/document.xml")); assertThrows(DataException.class, () -> DmsPageWatermarkAnchors.fix(source, "DMS_CUSTOM_test", 3)); } private String anchor(String name) { return "" + "" + "123" + "456" + ""; } private void assertPageAnchored(org.w3c.dom.Element anchor) { assertEquals("1", anchor.getAttribute("locked")); assertEquals("0", anchor.getAttribute("simplePos")); org.w3c.dom.Element simple = (org.w3c.dom.Element) anchor .getElementsByTagNameNS("*", "simplePos").item(0); assertEquals("0", simple.getAttribute("x")); assertEquals("0", simple.getAttribute("y")); org.w3c.dom.Element horizontal = (org.w3c.dom.Element) anchor .getElementsByTagNameNS("*", "positionH").item(0); org.w3c.dom.Element vertical = (org.w3c.dom.Element) anchor .getElementsByTagNameNS("*", "positionV").item(0); assertEquals("page", horizontal.getAttribute("relativeFrom")); assertEquals("page", vertical.getAttribute("relativeFrom")); assertEquals("123", horizontal.getElementsByTagNameNS("*", "posOffset").item(0).getTextContent()); assertEquals("456", vertical.getElementsByTagNameNS("*", "posOffset").item(0).getTextContent()); } private byte[] archive(byte[] xml) throws Exception { ByteArrayOutputStream output = new ByteArrayOutputStream(); try (ZipOutputStream zip = new ZipOutputStream(output)) { zip.putNextEntry(new ZipEntry("word/document.xml")); zip.write(xml); zip.closeEntry(); zip.putNextEntry(new ZipEntry("word/media/original.png")); zip.write(new byte[]{1, 2, 3}); zip.closeEntry(); } return output.toByteArray(); } private byte[] part(byte[] source, String name) throws Exception { try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(source))) { ZipEntry entry; while ((entry = zip.getNextEntry()) != null) { if (name.equals(entry.getName())) return zip.readAllBytes(); } } throw new AssertionError("Missing " + name); } }