From 55b6a1da47f0cc179ccfba48cd3b63a51fe3dbec Mon Sep 17 00:00:00 2001 From: s_zhangziang Date: Tue, 25 Jun 2024 09:54:53 +0800 Subject: [PATCH] Add unit test --- .../detailed/ExtraNameSpaceTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/java/xmlobject/detailed/ExtraNameSpaceTest.java diff --git a/src/test/java/xmlobject/detailed/ExtraNameSpaceTest.java b/src/test/java/xmlobject/detailed/ExtraNameSpaceTest.java new file mode 100644 index 000000000..9eedb4311 --- /dev/null +++ b/src/test/java/xmlobject/detailed/ExtraNameSpaceTest.java @@ -0,0 +1,71 @@ +/* Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package xmlobject.detailed; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import org.apache.xmlbeans.XmlObject; +import org.apache.xmlbeans.XmlOptions; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.junit.jupiter.api.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ExtraNameSpaceTest { + // Test whether xmlobject can write extraNamespaces + @Test + public void testSaveExtraNamespaces() throws Exception { + // Creating and setting up a namespace + Map extraNamespaces = new HashMap<>(); + extraNamespaces.put("uof", "http://www.nits.org.cn/uof3/zh-cn/2021/uof"); + extraNamespaces.put("图", "http://www.nits.org.cn/uof3/zh-cn/2021/graph"); + extraNamespaces.put("字", "http://www.nits.org.cn/uof3/zh-cn/2021/wordproc"); + extraNamespaces.put("表", "http://www.nits.org.cn/uof3/zh-cn/2021/spreadsheet"); + extraNamespaces.put("演", "http://www.nits.org.cn/uof3/zh-cn/2021/presentation"); + + XmlOptions xmlOptions = new XmlOptions(); + xmlOptions.setSaveExtraNamespaces(extraNamespaces); + + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/uof", extraNamespaces.get("uof")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/graph", extraNamespaces.get("图")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/wordproc", extraNamespaces.get("字")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/spreadsheet", extraNamespaces.get("表")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/presentation", extraNamespaces.get("演")); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + XmlObject doc = XmlObject.Factory.parse(""); + + doc.save(outputStream, xmlOptions); + String outputXml = outputStream.toString(StandardCharsets.UTF_8); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(new java.io.ByteArrayInputStream(outputXml.getBytes(StandardCharsets.UTF_8))); + Element root = document.getDocumentElement(); + + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/uof", root.getAttribute("xmlns:uof")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/graph", root.getAttribute("xmlns:图")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/wordproc", root.getAttribute("xmlns:字")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/spreadsheet", root.getAttribute("xmlns:表")); + assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/presentation", root.getAttribute("xmlns:演")); + } +}