test/jaxp/javax/xml/jaxp/unittest/validation/ValidationTest.java
changeset 57545 550a1a6ca596
parent 55688 0e1bc587472c
child 58679 9c3209ff7550
equal deleted inserted replaced
57544:99d2dd7b84a8 57545:550a1a6ca596
    23 
    23 
    24 package validation;
    24 package validation;
    25 
    25 
    26 
    26 
    27 import java.io.File;
    27 import java.io.File;
    28 import java.net.URL;
    28 import java.io.FileInputStream;
    29 
       
    30 import javax.xml.XMLConstants;
    29 import javax.xml.XMLConstants;
       
    30 import javax.xml.parsers.SAXParserFactory;
       
    31 import javax.xml.stream.XMLInputFactory;
       
    32 import javax.xml.stream.XMLStreamReader;
       
    33 import javax.xml.stream.events.XMLEvent;
       
    34 import javax.xml.transform.Source;
       
    35 import javax.xml.transform.sax.SAXSource;
    31 import javax.xml.transform.stream.StreamSource;
    36 import javax.xml.transform.stream.StreamSource;
    32 import javax.xml.validation.Schema;
    37 import javax.xml.validation.Schema;
    33 import javax.xml.validation.SchemaFactory;
    38 import javax.xml.validation.SchemaFactory;
    34 import javax.xml.validation.Validator;
    39 import javax.xml.validation.Validator;
    35 import org.testng.annotations.DataProvider;
    40 import org.testng.annotations.DataProvider;
    36 
       
    37 import org.testng.annotations.Listeners;
    41 import org.testng.annotations.Listeners;
    38 import org.testng.annotations.Test;
    42 import org.testng.annotations.Test;
       
    43 import org.xml.sax.Attributes;
       
    44 import org.xml.sax.InputSource;
    39 import org.xml.sax.SAXException;
    45 import org.xml.sax.SAXException;
    40 import org.xml.sax.SAXParseException;
    46 import org.xml.sax.SAXParseException;
       
    47 import org.xml.sax.XMLFilter;
       
    48 import org.xml.sax.helpers.XMLFilterImpl;
    41 
    49 
    42 /*
    50 /*
    43  * @test
    51  * @test
    44  * @bug 8220818 8176447
    52  * @bug 8220818 8176447
    45  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
    53  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
   104     @Test(dataProvider = "uniqueness", expectedExceptions = SAXException.class)
   112     @Test(dataProvider = "uniqueness", expectedExceptions = SAXException.class)
   105     public void testUnique(String xsd, String xml) throws Exception {
   113     public void testUnique(String xsd, String xml) throws Exception {
   106         validate(xsd, xml);
   114         validate(xsd, xml);
   107     }
   115     }
   108 
   116 
       
   117     /**
       
   118      * @bug 8068376
       
   119      * Verifies that validation performs normally with externally provided string
       
   120      * parameters.
       
   121      * @throws Exception if the test fails
       
   122      */
       
   123     @Test
       
   124     public void testJDK8068376() throws Exception {
       
   125 
       
   126         String xsdFile = getClass().getResource(FILE_PATH + "JDK8068376.xsd").getFile();
       
   127         String xmlFile = getClass().getResource(FILE_PATH + "JDK8068376.xml").getFile();
       
   128         String targetNamespace = getTargetNamespace(xsdFile);
       
   129 
       
   130         XMLFilter namespaceFilter = new XMLFilterImpl(SAXParserFactory.newDefaultNSInstance().newSAXParser().getXMLReader()) {
       
   131         @Override
       
   132         public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
       
   133             uri = targetNamespace; // overwriting the uri with our own choice
       
   134             super.startElement(uri, localName, qName, atts);
       
   135         }
       
   136         };
       
   137 
       
   138         Source xmlSource = new SAXSource(namespaceFilter, new InputSource(xmlFile));
       
   139         Source schemaSource = new StreamSource(xsdFile);
       
   140         validate(schemaSource, xmlSource);
       
   141 
       
   142     }
       
   143 
       
   144     private static String getTargetNamespace(String xsdFile) throws Exception {
       
   145         XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(xsdFile));
       
   146         while (reader.hasNext()) {
       
   147             int event = reader.next();
       
   148 
       
   149             // Get the root element's "targetNamespace" attribute
       
   150             if (event == XMLEvent.START_ELEMENT) {
       
   151                 // validation fails before patch
       
   152                 String value = reader.getAttributeValue(null, "targetNamespace"); // fails validation
       
   153                 // validation passes due to a reference comparison in the original code
       
   154                 // String value = "mynamespace";
       
   155                 return value;
       
   156             }
       
   157         }
       
   158         return null;
       
   159     }
       
   160 
   109     private void validate(String xsd, String xml) throws Exception {
   161     private void validate(String xsd, String xml) throws Exception {
   110         final SchemaFactory schemaFactory = SchemaFactory.newInstance(
   162         final SchemaFactory schemaFactory = SchemaFactory.newInstance(
   111                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
   163                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
   112         final Schema schema = schemaFactory.newSchema(
   164         final Schema schema = schemaFactory.newSchema(
   113                 new File(getClass().getResource(FILE_PATH + xsd).getFile()));
   165                 new File(getClass().getResource(FILE_PATH + xsd).getFile()));
   114         final Validator validator = schema.newValidator();
   166         final Validator validator = schema.newValidator();
   115         validator.validate(new StreamSource(
   167         validator.validate(new StreamSource(
   116                 new File(getClass().getResource(FILE_PATH + xml).getFile())));
   168                 new File(getClass().getResource(FILE_PATH + xml).getFile())));
   117     }
   169     }
   118 
   170 
       
   171     private void validate(Source xsd, Source xml) throws Exception {
       
   172         final SchemaFactory schemaFactory = SchemaFactory.newInstance(
       
   173                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
       
   174         final Schema schema = schemaFactory.newSchema(xsd);
       
   175         final Validator validator = schema.newValidator();
       
   176         validator.validate(xml);
       
   177     }
       
   178 
   119 }
   179 }