• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xmlunit / xmlunit / cd160610-9b67-4752-a2c4-e3a0d82d991e

21 Apr 2025 11:55AM UTC coverage: 91.756% (-0.02%) from 91.78%
cd160610-9b67-4752-a2c4-e3a0d82d991e

push

circleci

web-flow
Merge pull request #289 from xmlunit/circleci-project-setup

CircleCI project setup

3996 of 4698 branches covered (85.06%)

11754 of 12810 relevant lines covered (91.76%)

2.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.45
/xmlunit-matchers/src/main/java/org/xmlunit/matchers/ValidationMatcher.java
1
/*
2
  This file is licensed to You under the Apache License, Version 2.0
3
  (the "License"); you may not use this file except in compliance with
4
  the License.  You may obtain a copy of the License at
5

6
  http://www.apache.org/licenses/LICENSE-2.0
7

8
  Unless required by applicable law or agreed to in writing, software
9
  distributed under the License is distributed on an "AS IS" BASIS,
10
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
  See the License for the specific language governing permissions and
12
  limitations under the License.
13
*/
14
package org.xmlunit.matchers;
15

16
import static org.xmlunit.util.Linqy.any;
17
import static org.xmlunit.util.Linqy.asList;
18
import static org.xmlunit.util.Linqy.map;
19

20
import org.xmlunit.builder.Input;
21
import org.xmlunit.util.IsNullPredicate;
22
import org.xmlunit.util.Mapper;
23
import org.xmlunit.util.Predicate;
24
import org.xmlunit.validation.JAXPValidator;
25
import org.xmlunit.validation.Languages;
26
import org.xmlunit.validation.ValidationProblem;
27
import org.xmlunit.validation.ValidationResult;
28
import org.hamcrest.BaseMatcher;
29
import org.hamcrest.Description;
30
import org.hamcrest.Factory;
31

32
import javax.xml.transform.Source;
33
import javax.xml.validation.Schema;
34
import java.util.Arrays;
35

36
/**
37
 * Hamcrest Matcher for XML Validation against W3C XML Schema using
38
 * {@link JAXPValidator}.
39
 */
40
public class ValidationMatcher extends BaseMatcher {
41

42
    private final Source[] schemaSource;
43
    private final Schema schema;
44
    private Source instance;
45
    private ValidationResult result;
46

47
    /**
48
     * @param schemaSource schema source document(s)
49
     */
50
    public ValidationMatcher(Object... schemaSource) {
1✔
51
        if (schemaSource == null) {
1✔
52
            throw new IllegalArgumentException("schemaSource must not be null");
1✔
53
        }
54
        Iterable<Object> schemaSourceList = Arrays.asList(schemaSource);
1✔
55
        if (any(schemaSourceList, new IsNullPredicate())) {
1!
56
            throw new IllegalArgumentException("schemaSource must not contain null values");
×
57
        }
58
        this.schemaSource = asList(map(schemaSourceList,
1✔
59
                                       new Mapper<Object, Source>() {
1✔
60
                                           @Override
61
                                           public Source apply(Object source) {
62
                                               return Input.from(source).build();
1✔
63
                                           }
64
                                       })
65
                                   ).toArray(new Source[schemaSource.length]);
1✔
66
        schema = null;
1✔
67
    }
1✔
68

69
    /**
70
     * @param schema schema source document
71
     * @since XMLUnit 2.3.0
72
     */
73
    public ValidationMatcher(Schema schema) {
1✔
74
        if (schema == null) {
1✔
75
            throw new IllegalArgumentException("schema must not be null");
1✔
76
        }
77
        this.schemaSource = new Source[0];
1✔
78
        this.schema = schema;
1✔
79
    }
1✔
80

81
    @Override
82
    public boolean matches(Object instance) {
83
        this.instance = Input.from(instance).build();
1✔
84
        JAXPValidator v = new JAXPValidator(Languages.W3C_XML_SCHEMA_NS_URI);
1✔
85
        if (schema != null) {
1✔
86
            v.setSchema(schema);
1✔
87
        } else if (schemaSource.length > 0) {
1✔
88
            v.setSchemaSources(schemaSource);
1✔
89
        }
90
        this.result = v.validateInstance(this.instance);
1✔
91
        return this.result.isValid();
1✔
92
    }
93

94
    @Override
95
    public void describeTo(Description description) {
96
        description.appendText(" that ")
1✔
97
            .appendValue(instance != null && instance.getSystemId() != null
1!
98
                         ? instance.getSystemId() : "instance");
1✔
99
        if (schema == null && any(Arrays.asList(schemaSource), new HasSystemIdPredicate())) {
1!
100
            description.appendText(" validates against ");
1✔
101
            boolean first = true;
1✔
102
            for (Source schema : Arrays.asList(schemaSource)) {
1✔
103
                if (!first) {
1!
104
                    description.appendValue(", ");
×
105
                }
106
                first = false;
1✔
107
                description.appendValue(schema.getSystemId() != null
1!
108
                                        ? schema.getSystemId()
1✔
109
                                        : "schema without systemId");
×
110
            }
1✔
111
        } else {
1✔
112
            description.appendText(" validates");
1✔
113
        }
114
    }
1✔
115

116
    @Override
117
    public void describeMismatch(final Object item, final Description mismatchDescription) {
118
        if (this.result != null && this.result.getProblems() != null) {
1!
119
            mismatchDescription.appendText(" got validation errors: ");
1✔
120
            for (ValidationProblem problem : this.result.getProblems()) {
1✔
121
                mismatchDescription.appendText(problem.toString());
1✔
122
            }
1✔
123
        } else {
124
            mismatchDescription.appendText(" got unexpected error!");
×
125
        }
126
    }
1✔
127

128
    /**
129
     * Creates a matcher that validates the XML under test.
130
     * @param schemaSource schema source document
131
     * @return matcher
132
     */
133
    @Factory
134
    public static ValidationMatcher valid(final Object schemaSource) {
135
        return new ValidationMatcher(schemaSource);
1✔
136
    }
137

138
    /**
139
     * Creates a matcher that validates the XML under test.
140
     * @param schema schema source document
141
     * @return matcher
142
     * @since XMLUnit 2.3.0
143
     */
144
    @Factory
145
    public static ValidationMatcher valid(final Schema schema) {
146
        return new ValidationMatcher(schema);
1✔
147
    }
148

149
    private static class HasSystemIdPredicate implements Predicate<Source> {
150
        @Override
151
        public boolean test(Source s) {
152
            return s.getSystemId() != null;
1!
153
        }
154
    }
155
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc