|
22 | 22 | import java.io.ByteArrayInputStream; |
23 | 23 | import java.io.IOException; |
24 | 24 | import java.io.InputStream; |
| 25 | +import java.net.URL; |
25 | 26 |
|
| 27 | +import javax.xml.XMLConstants; |
26 | 28 | import javax.xml.namespace.QName; |
| 29 | +import javax.xml.parsers.DocumentBuilder; |
| 30 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 31 | +import javax.xml.stream.XMLInputFactory; |
27 | 32 | import javax.xml.stream.XMLStreamReader; |
| 33 | +import javax.xml.transform.dom.DOMSource; |
| 34 | +import javax.xml.validation.Schema; |
| 35 | +import javax.xml.validation.SchemaFactory; |
28 | 36 |
|
| 37 | +import org.w3c.dom.Document; |
| 38 | + |
| 39 | +import com.ctc.wstx.msv.W3CSchemaFactory; |
| 40 | + |
| 41 | +import org.apache.cxf.endpoint.Endpoint; |
29 | 42 | import org.apache.cxf.interceptor.Fault; |
| 43 | +import org.apache.cxf.message.Exchange; |
| 44 | +import org.apache.cxf.message.ExchangeImpl; |
30 | 45 | import org.apache.cxf.message.Message; |
31 | 46 | import org.apache.cxf.message.MessageImpl; |
| 47 | +import org.apache.cxf.service.Service; |
| 48 | +import org.apache.cxf.service.ServiceImpl; |
| 49 | +import org.apache.cxf.service.model.MessageInfo; |
| 50 | +import org.apache.cxf.service.model.MessagePartInfo; |
| 51 | +import org.apache.cxf.service.model.ServiceInfo; |
32 | 52 | import org.apache.cxf.staxutils.StaxUtils; |
| 53 | +import org.codehaus.stax2.XMLStreamReader2; |
| 54 | +import org.codehaus.stax2.validation.XMLValidationSchema; |
33 | 55 |
|
34 | 56 | import org.junit.Test; |
35 | 57 |
|
| 58 | +import static org.junit.Assert.assertEquals; |
36 | 59 | import static org.junit.Assert.assertFalse; |
37 | 60 | import static org.junit.Assert.assertTrue; |
| 61 | +import static org.junit.Assert.fail; |
| 62 | +import static org.mockito.Mockito.mock; |
| 63 | +import static org.mockito.Mockito.when; |
38 | 64 |
|
39 | 65 | /** |
40 | 66 | * |
@@ -92,6 +118,78 @@ public void testParseNullByte() throws Exception { |
92 | 118 | ((XMLStreamReader)obj).close(); |
93 | 119 | } |
94 | 120 |
|
| 121 | + @Test |
| 122 | + public void testValid() throws Exception { |
| 123 | + testValidate("resources/schema.xsd", "resources/test-valid.xml", false); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testInValid() throws Exception { |
| 128 | + testValidate("resources/schema.xsd", "resources/test-invalid.xml", true); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + private void testValidate(String schemaPath, String xmlPath, boolean exceptionExpected) throws Exception { |
| 133 | + |
| 134 | + //create schema |
| 135 | + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 136 | + documentBuilderFactory.setNamespaceAware(true); |
| 137 | + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 138 | + URL schemaURI = getClass().getResource(schemaPath); |
| 139 | + Document wsdl = documentBuilder.parse(schemaURI.openStream()); |
| 140 | + String wsdlSystemId = schemaURI.toExternalForm(); |
| 141 | + DOMSource source = new DOMSource(wsdl); |
| 142 | + source.setSystemId(wsdlSystemId); |
| 143 | + source.setSystemId(wsdlSystemId); |
| 144 | + |
| 145 | + XMLValidationSchema schemaw3c = |
| 146 | + W3CSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA).createSchema(schemaURI); |
| 147 | + SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
| 148 | + Schema schema = schemaFactory.newSchema(schemaURI); |
| 149 | + |
| 150 | + XMLStreamDataReader reader = new XMLStreamDataReader(); |
| 151 | + reader.setSchema(schema); |
| 152 | + |
| 153 | + |
| 154 | + InputStream testIS = getClass().getResourceAsStream(xmlPath); |
| 155 | + Message msg = new MessageImpl(); |
| 156 | + Exchange exchange = new ExchangeImpl(); |
| 157 | + |
| 158 | + ServiceInfo serviceInfo = new ServiceInfo(); |
| 159 | + |
| 160 | + Endpoint endpoint = mock(Endpoint.class); |
| 161 | + when(endpoint.get(XMLValidationSchema.class.getName())).thenReturn(schemaw3c); |
| 162 | + |
| 163 | + Service svc = new ServiceImpl(serviceInfo); |
| 164 | + |
| 165 | + exchange.put(Service.class, svc); |
| 166 | + exchange.put(Endpoint.class, endpoint); |
| 167 | + |
| 168 | + msg.setExchange(exchange); |
| 169 | + msg.setContent(InputStream.class, testIS); |
| 170 | + reader.setProperty(Message.class.getName(), msg); |
| 171 | + |
| 172 | + XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); |
| 173 | + XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlFactory.createXMLStreamReader(testIS, "utf-8"); |
| 174 | + |
| 175 | + MessageInfo messageInfo = new MessageInfo(null, |
| 176 | + MessageInfo.Type.INPUT, |
| 177 | + new QName("http://www.test.org/services", |
| 178 | + "NullTestOperationRequest")); |
| 179 | + MessagePartInfo messagePartInfo = new MessagePartInfo(new QName( |
| 180 | + "http://www.test.org/services", "NullTestOperationRequest"), messageInfo); |
| 181 | + messagePartInfo.setElement(true); |
| 182 | + boolean exceptionCaught = false; |
| 183 | + try { |
| 184 | + reader.read(messagePartInfo, xmlStreamReader); |
| 185 | + } catch (Fault fault) { |
| 186 | + exceptionCaught = true; |
| 187 | + } catch (Exception exc) { |
| 188 | + fail(exc.getMessage()); |
| 189 | + } |
| 190 | + assertEquals(exceptionExpected, exceptionCaught); |
| 191 | + } |
| 192 | + |
95 | 193 | private static class TestInputStream extends ByteArrayInputStream { |
96 | 194 | private boolean closed; |
97 | 195 |
|
|
0 commit comments