1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.test.support.matcher;
18
19 import java.io.IOException;
20 import java.util.Arrays;
21
22 import org.springframework.core.io.Resource;
23 import org.springframework.util.Assert;
24 import org.springframework.util.ObjectUtils;
25 import org.springframework.ws.WebServiceMessage;
26 import org.springframework.xml.validation.XmlValidator;
27 import org.springframework.xml.validation.XmlValidatorFactory;
28
29 import org.xml.sax.SAXParseException;
30
31 import static org.springframework.ws.test.support.AssertionErrors.fail;
32
33
34
35
36
37
38
39
40 public class SchemaValidatingMatcher implements WebServiceMessageMatcher {
41
42 private final XmlValidator xmlValidator;
43
44
45
46
47
48
49
50
51 public SchemaValidatingMatcher(Resource schema, Resource... furtherSchemas) throws IOException {
52 Assert.notNull(schema, "'schema' must not be null");
53 Resource[] joinedSchemas = new Resource[furtherSchemas.length + 1];
54 joinedSchemas[0] = schema;
55 System.arraycopy(furtherSchemas, 0, joinedSchemas, 1, furtherSchemas.length);
56 xmlValidator = XmlValidatorFactory.createValidator(joinedSchemas, XmlValidatorFactory.SCHEMA_W3C_XML);
57
58 }
59
60 public void match(WebServiceMessage message) throws IOException, AssertionError {
61 SAXParseException[] exceptions = xmlValidator.validate(message.getPayloadSource());
62 if (!ObjectUtils.isEmpty(exceptions)) {
63 fail("XML is not valid: " + Arrays.toString(exceptions), "Payload", message.getPayloadSource());
64 }
65 }
66 }