|
26 | 26 |
|
27 | 27 | import java.io.File;
|
28 | 28 | import java.io.FileInputStream;
|
| 29 | +import java.io.Reader; |
| 30 | +import java.io.StringReader; |
29 | 31 | import javax.xml.XMLConstants;
|
30 | 32 | import javax.xml.parsers.SAXParserFactory;
|
31 | 33 | import javax.xml.stream.XMLInputFactory;
|
32 | 34 | import javax.xml.stream.XMLStreamReader;
|
33 | 35 | import javax.xml.stream.events.XMLEvent;
|
34 | 36 | import javax.xml.transform.Source;
|
35 | 37 | import javax.xml.transform.sax.SAXSource;
|
| 38 | +import javax.xml.transform.stax.StAXSource; |
36 | 39 | import javax.xml.transform.stream.StreamSource;
|
37 | 40 | import javax.xml.validation.Schema;
|
38 | 41 | import javax.xml.validation.SchemaFactory;
|
|
48 | 51 |
|
49 | 52 | /*
|
50 | 53 | * @test
|
51 |
| - * @bug 8220818 8176447 |
| 54 | + * @bug 8220818 8176447 8349516 |
52 | 55 | * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
|
53 | 56 | * @run testng/othervm validation.ValidationTest
|
54 | 57 | * @summary Runs validations with schemas and sources
|
@@ -139,6 +142,55 @@ public void startElement(String uri, String localName, String qName, Attributes
|
139 | 142 |
|
140 | 143 | }
|
141 | 144 |
|
| 145 | + /** |
| 146 | + * Verifies the bug fix for 8349516, which adds a guard against empty text. |
| 147 | + * Prior to the fix, calling {@link XMLStreamReader#getTextCharacters() XMLStreamReader#getTextCharacters()} |
| 148 | + * with {@code length = 0} resulted in an {@code IndexOutOfBoundsException}. |
| 149 | + * |
| 150 | + * This test ensures that the fix prevents such an exception. |
| 151 | + * |
| 152 | + * @throws Exception if the test fails due to unexpected issues, such as errors |
| 153 | + * in creating the schema or reader, or validation errors other than the |
| 154 | + * {@code IndexOutOfBoundsException}. |
| 155 | + */ |
| 156 | + @Test |
| 157 | + public void testValidationWithStAX() throws Exception { |
| 158 | + String schema = """ |
| 159 | + <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" |
| 160 | + targetNamespace="http://xxxx.com/schema/test" |
| 161 | + attributeFormDefault="unqualified" |
| 162 | + elementFormDefault="qualified" |
| 163 | + > |
| 164 | +
|
| 165 | + <xs:element name="test"> |
| 166 | + <xs:complexType> |
| 167 | + <xs:choice> |
| 168 | + <xs:element name="tag" type="xs:string" /> |
| 169 | + </xs:choice> |
| 170 | + </xs:complexType> |
| 171 | + </xs:element> |
| 172 | +
|
| 173 | + </xs:schema> |
| 174 | + """; |
| 175 | + |
| 176 | + String xml = """ |
| 177 | + <test xmlns="http://xxxx.com/schema/test"> |
| 178 | + <tag><![CDATA[]]></tag> |
| 179 | + </test> |
| 180 | + """; |
| 181 | + |
| 182 | + Reader schemaReader = new StringReader(schema); |
| 183 | + Reader xmlReader = new StringReader(xml); |
| 184 | + |
| 185 | + Source source = new StreamSource(schemaReader); |
| 186 | + |
| 187 | + Validator validator = |
| 188 | + SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source).newValidator(); |
| 189 | + |
| 190 | + XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(xmlReader); |
| 191 | + validator.validate(new StAXSource(xmlStreamReader)); |
| 192 | + } |
| 193 | + |
142 | 194 | private static String getTargetNamespace(String xsdFile) throws Exception {
|
143 | 195 | XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile));
|
144 | 196 | while (reader.hasNext()) {
|
|
0 commit comments