backend-api исправил юнит-тест(ы) (bug at 4b1a834883)
This commit is contained in:
parent
cd3488c5ec
commit
603a8e66ca
2 changed files with 79 additions and 16 deletions
|
|
@ -111,10 +111,6 @@
|
|||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
|||
|
|
@ -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<String, Object> parseXml(File xmlFile) throws ParserConfigurationException, IOException, SAXException {
|
||||
// Это юнит-тест с внутренними данными, парсинг DTD выключать не обязательно.
|
||||
Map<String, Object> 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<String, Object> 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, Object> или String
|
||||
*/
|
||||
private Object xmlNodeToMap(Node node) {
|
||||
if (node == null)
|
||||
return null;
|
||||
Map<String, Object> 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<String, Object> 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"));
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue