diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/util/XMLFileReader.java b/tools/cldr-code/src/main/java/org/unicode/cldr/util/XMLFileReader.java index f52f0c5551b..8ac2b6e0510 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/util/XMLFileReader.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/util/XMLFileReader.java @@ -191,7 +191,7 @@ public static void read( } } - private static final XMLReader createXMLReader( + public static final XMLReader createXMLReader( int handlers, boolean validating, AllHandler allHandler) throws SAXNotRecognizedException, SAXNotSupportedException { XMLReader xmlReader = createXMLReader(validating); diff --git a/tools/cldr-code/src/test/java/org/unicode/cldr/util/TestDTDInsertingReaderFactory.java b/tools/cldr-code/src/test/java/org/unicode/cldr/util/TestDTDInsertingReaderFactory.java new file mode 100644 index 00000000000..f906cfa9dbc --- /dev/null +++ b/tools/cldr-code/src/test/java/org/unicode/cldr/util/TestDTDInsertingReaderFactory.java @@ -0,0 +1,106 @@ +package org.unicode.cldr.util; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.unicode.cldr.util.XMLFileReader.LoggingHandler; +import org.unicode.cldr.util.XMLFileReader.SimpleHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class TestDTDInsertingReaderFactory { + private static final int COUNT = 10; // increase this for perf testing + private static final String COMMON_MT = "common/main/mt.xml"; + private static final String KEYBOARDS_MT = "keyboards/3.0/mt.xml"; + + @Test + void TestProcessPathValues() { + for (int i = 0; i < COUNT; i++) { + XMLFileReader.processPathValues(COMMON_MT, true, new SimpleHandler()); + } + } + + @Test + void TestReadJar() throws IOException { + for (int i = 0; i < COUNT; i++) { + new XMLFileReader() + .setHandler(new XMLFileReader.SimpleHandler()) + .readCLDRResource("dl_iso_table_a1.xml", -1, false); + } + } + + @Test + void TestReadCommon() throws FileNotFoundException, IOException { + for (int i = 0; i < COUNT; i++) { + new XMLFileReader() + .setHandler(new XMLFileReader.SimpleHandler()) + .read(COMMON_MT, -1, true); + } + } + + @Test + void TestReadKeyboard() throws FileNotFoundException, IOException { + for (int i = 0; i < COUNT; i++) { + new XMLFileReader() + .setHandler(new XMLFileReader.SimpleHandler()) + .read(KEYBOARDS_MT, -1, true); + } + } + + @Test + void TestReadKeyboardByte() throws IOException, SAXException { + // verify that reading via InputStream (byte) works as well + try (InputStream fis = new FileInputStream(KEYBOARDS_MT); ) { + InputSource is = new InputSource(fis); + is.setSystemId(KEYBOARDS_MT); + is = DTDInsertingReaderFactory.wrap(is); + XMLFileReader.createXMLReader(-1, true, new LoggingHandler()).parse(is); + } + } + + @Test + void TestReadKeyboardChar() throws IOException, SAXException { + // verify that reading via Reader (char) works as well + try (InputStream fis = new FileInputStream(KEYBOARDS_MT); + InputStreamReader isr = new InputStreamReader(fis); ) { + InputSource is = new InputSource(isr); + is.setSystemId(KEYBOARDS_MT); + is = DTDInsertingReaderFactory.wrap(is); + XMLFileReader.createXMLReader(-1, true, new LoggingHandler()).parse(is); + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + public void TestBytePerf(boolean wrapped) throws IOException, SAXException { + for (int i = 0; i < COUNT; i++) { + // mimic XMLFileHandler.read() here, but with wrapping enabled/disabled + try (InputStream fis = new FileInputStream(COMMON_MT); ) { + InputSource is = new InputSource(fis); + is.setSystemId(COMMON_MT); + if (wrapped) is = DTDInsertingReaderFactory.wrap(is); + XMLFileReader.createXMLReader(-1, true, new LoggingHandler()).parse(is); + } + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + public void TestCharPerf(boolean wrapped) throws IOException, SAXException { + for (int i = 0; i < COUNT; i++) { + // mimic XMLFileHandler.read() here, but with wrapping enabled/disabled + try (InputStream fis = new FileInputStream(COMMON_MT); + InputStreamReader isr = new InputStreamReader(fis); ) { + InputSource is = new InputSource(isr); + is.setSystemId(COMMON_MT); + if (wrapped) is = DTDInsertingReaderFactory.wrap(is); + XMLFileReader.createXMLReader(-1, true, new LoggingHandler()).parse(is); + } + } + } +}