From 603a8e66cad10a35b0a5e7ba020a15f90ccc17a2 Mon Sep 17 00:00:00 2001 From: AKurakin Date: Wed, 10 Jul 2024 18:22:42 +0300 Subject: [PATCH] =?UTF-8?q?backend-api=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D1=8E=D0=BD=D0=B8=D1=82-=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82(=D1=8B)=20(bug=20at=204b1a834883dce0ae)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clearing-parent/backend-api/pom.xml | 4 - .../clearing/backendapi/meta/DataXmlTest.java | 91 ++++++++++++++++--- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/clearing-parent/backend-api/pom.xml b/clearing-parent/backend-api/pom.xml index add972c06..6120a417b 100644 --- a/clearing-parent/backend-api/pom.xml +++ b/clearing-parent/backend-api/pom.xml @@ -111,10 +111,6 @@ org.mockito mockito-core - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml test diff --git a/clearing-parent/backend-api/src/test/java/ru/spcex/clearing/backendapi/meta/DataXmlTest.java b/clearing-parent/backend-api/src/test/java/ru/spcex/clearing/backendapi/meta/DataXmlTest.java index bb299c3de..93bfd5325 100644 --- a/clearing-parent/backend-api/src/test/java/ru/spcex/clearing/backendapi/meta/DataXmlTest.java +++ b/clearing-parent/backend-api/src/test/java/ru/spcex/clearing/backendapi/meta/DataXmlTest.java @@ -1,15 +1,20 @@ package ru.spcex.clearing.backendapi.meta; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.*; -import java.io.File; -import java.io.IOException; +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.*; import java.util.*; /** @@ -22,7 +27,7 @@ public class DataXmlTest { private Logger log = LoggerFactory.getLogger(getClass()); @Test - public void testMetaXml() throws IOException { + public void testMetaXml() throws Exception { verifyXML(new File("src/main/resources/meta/meta.xml")); } @@ -46,12 +51,73 @@ public class DataXmlTest { verifyXML(new File("src/main/resources/meta/data_register_code.xml")); } - protected void verifyXML(File xmlFile) throws IOException { - XmlMapper xmlObjectMapper = new XmlMapper(); - xmlObjectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); - xmlObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + Map parseXml(File xmlFile) throws ParserConfigurationException, IOException, SAXException { // Это юнит-тест с внутренними данными, парсинг DTD выключать не обязательно. - Map root = xmlObjectMapper.readValue(xmlFile, Map.class); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setFeature("http://xml.org/sax/features/external-general-entities", false); + factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + + DocumentBuilder builder = factory.newDocumentBuilder(); + Map out = new HashMap<>(); + try (FileInputStream is = new FileInputStream(xmlFile)) { + Document doc = builder.parse(new InputSource(is)); + out.put(doc.getNodeName(), xmlNodeToMap(doc)); + } + return out; + } + + /** + * Аналог TransformerFactory.newInstance().newTransformer() to Map + * + * @param node w3c.Node для разбора + * @return Map или String + */ + private Object xmlNodeToMap(Node node) { + if (node == null) + return null; + Map out = new LinkedHashMap<>(); + int n = 0; + if (node.hasAttributes()) { + NamedNodeMap attributes = node.getAttributes(); + int attrCount = attributes.getLength(); + for (int i = 0; i < attrCount; i++) { + Node attribute = attributes.item(i); + String value = attribute.getNodeValue(); + if (value != null) + out.put((n++) + "attr " + attribute.getNodeName(), value); + } + } + String value = node.getNodeValue(); + if (node.hasChildNodes()) { + NodeList childs = node.getChildNodes(); + for (int i = 0; i < childs.getLength(); i++) { + Node child = childs.item(i); + if (child != null && child.getNodeType() != Node.COMMENT_NODE) { + out.put((n++) + " " + child.getNodeName(), xmlNodeToMap(child)); + } + } + } + if (out.isEmpty()) { + return value; + } else { + if (value != null) { + out.put("value ", value); + } + } + + return out; + } + + protected void verifyXML(File xmlFile) throws IOException { + Map root = null; + try { + root = parseXml(xmlFile); + } catch (ParserConfigurationException | SAXException e) { + throw new IOException("Can not parse XML " + xmlFile, e); + } Assertions.assertNotNull(root, "Empty XML in file " + xmlFile); String errors = verifyNode(xmlFile.getName(), root); @@ -119,7 +185,7 @@ public class DataXmlTest { if (text != null) { for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); - boolean isWhitespace = Character.isWhitespace(c); + boolean isWhitespace = Character.isWhitespace(c) || c == '=' || c == '='; // || c=='_' boolean isCyrillic = Arrays.binarySearch(CYRILLICS, c) >= 0; boolean isLatin = Arrays.binarySearch(LATINS, c) >= 0; assert !(isCyrillic && isLatin); @@ -155,6 +221,7 @@ public class DataXmlTest { Assertions.assertTrue(verifyText("XML")); Assertions.assertTrue(verifyText("Same sample apple")); Assertions.assertTrue(verifyText("По tExT кРАям")); + Assertions.assertTrue(verifyText("company id=\"5\" fullName=\"Центральный Банк Российской Федерации\" ")); Assertions.assertFalse(verifyText("Пользоватьель userтекст обычный.")); Assertions.assertFalse(verifyText("ХML"));