Skip to content

Commit 8cb22d5

Browse files
committedDec 4, 2024
feat(#277): write the first ugly test
1 parent 672d8c6 commit 8cb22d5

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
 

‎src/test/java/com/jcabi/xml/XMLDocumentTest.java

+50
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
package com.jcabi.xml;
3131

3232
import com.google.common.collect.Iterables;
33+
import com.jcabi.log.Logger;
3334
import com.jcabi.matchers.XhtmlMatchers;
3435
import java.io.ByteArrayInputStream;
3536
import java.io.File;
3637
import java.io.IOException;
38+
import java.nio.charset.StandardCharsets;
3739
import java.nio.file.Files;
3840
import java.security.SecureRandom;
3941
import java.util.Arrays;
@@ -47,6 +49,8 @@
4749
import java.util.concurrent.Executors;
4850
import java.util.concurrent.TimeUnit;
4951
import java.util.concurrent.atomic.AtomicInteger;
52+
import javax.xml.parsers.DocumentBuilderFactory;
53+
import javax.xml.parsers.ParserConfigurationException;
5054
import org.apache.commons.lang3.RandomStringUtils;
5155
import org.apache.commons.lang3.StringUtils;
5256
import org.cactoos.io.ResourceOf;
@@ -61,6 +65,8 @@
6165
import org.w3c.dom.Document;
6266
import org.w3c.dom.Element;
6367
import org.w3c.dom.Node;
68+
import org.xml.sax.InputSource;
69+
import org.xml.sax.SAXException;
6470
import org.xml.sax.SAXParseException;
6571

6672
/**
@@ -670,4 +676,48 @@ void validatesMultipleXmlsInThreads() throws Exception {
670676
service.shutdownNow();
671677
}
672678

679+
680+
@Test
681+
void createsManyXmlDocuments() throws ParserConfigurationException, IOException, SAXException {
682+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
683+
String xml = StringUtils.join(
684+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
685+
"<payment><id>333</id>",
686+
"<date>1-Jan-2013</date>",
687+
"<debit>test-1</debit>",
688+
"<credit>test-2</credit>",
689+
"</payment>"
690+
);
691+
final long startSimple = System.nanoTime();
692+
final String expected = "payment";
693+
for (int i = 0; i < 10_000; ++i) {
694+
final Document parse = factory.newDocumentBuilder()
695+
.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
696+
final String actual = parse.getFirstChild().getNodeName();
697+
MatcherAssert.assertThat(
698+
actual,
699+
Matchers.equalTo(expected)
700+
);
701+
}
702+
final long endSimple = System.nanoTime();
703+
System.out.println(
704+
"Default approach to create XML timing: " + (endSimple - startSimple) / 1_000_000 + " ms");
705+
Logger.info(this, "Time: %[ms]s", (endSimple - startSimple) / 1000);
706+
final long start = System.nanoTime();
707+
for (int i = 0; i < 10_000; ++i) {
708+
;
709+
final String actual = new XMLDocument(
710+
new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)))
711+
.node()
712+
.getFirstChild().getNodeName();
713+
MatcherAssert.assertThat(
714+
actual,
715+
Matchers.equalTo(expected)
716+
);
717+
}
718+
final long end = System.nanoTime();
719+
System.out.println(
720+
"jcabi-xml approach to create XML timing: " + (end - start) / 1_000_000 + " ms"
721+
);
722+
}
673723
}

0 commit comments

Comments
 (0)