From: <bo...@us...> - 2013-04-25 16:22:28
|
Revision: 534 http://sourceforge.net/p/xmlunit/code/534 Author: bodewig Date: 2013-04-25 16:22:25 +0000 (Thu, 25 Apr 2013) Log Message: ----------- hamcrest matcher for schema validation by Robert Reimann Added Paths: ----------- trunk/xmlunit/src/main/java-hamcrest/net/sf/xmlunit/matcher/ trunk/xmlunit/src/main/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcher.java trunk/xmlunit/src/tests/java-hamcrest/net/sf/xmlunit/matcher/ trunk/xmlunit/src/tests/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcherTest.java Added: trunk/xmlunit/src/main/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcher.java =================================================================== --- trunk/xmlunit/src/main/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcher.java (rev 0) +++ trunk/xmlunit/src/main/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcher.java 2013-04-25 16:22:25 UTC (rev 534) @@ -0,0 +1,81 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit.matcher; + +import net.sf.xmlunit.validation.JAXPValidator; +import net.sf.xmlunit.validation.Languages; +import net.sf.xmlunit.validation.ValidationProblem; +import net.sf.xmlunit.validation.ValidationResult; +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.TypeSafeMatcher; + +import javax.xml.transform.Source; +import java.util.Arrays; + +/** + * Hamcrest Matcher for XML Validation. + */ +public class ValidationMatcher extends TypeSafeMatcher<Source> { + + private final Source schemaSource[]; + private Source instance; + private ValidationResult result; + + public ValidationMatcher(Source... schemaSource) { + this.schemaSource = schemaSource; + } + + @Override + public boolean matchesSafely(Source instance) { + this.instance = instance; + JAXPValidator v = new JAXPValidator(Languages.W3C_XML_SCHEMA_NS_URI); + if (schemaSource.length <= 1) { + v.setSchemaSource(schemaSource[0]); + } else { + v.setSchemaSources(schemaSource); + } + this.result = v.validateInstance(instance); + return this.result.isValid(); + } + + @Override + public void describeTo(Description description) { + description.appendText(" that ") + .appendValue(instance.getSystemId()) + .appendText(" validates against "); + for (Source schema : Arrays.asList(schemaSource)) { + description.appendValue(schema.getSystemId()); + } + } + + @Override + protected void describeMismatchSafely(final Source instance, final Description mismatchDescription) { + if (this.result != null && this.result.getProblems() != null) { + mismatchDescription.appendText(" got validation errors: "); + for (ValidationProblem problem : this.result.getProblems()) { + mismatchDescription.appendText(problem.toString()); + } + } else { + mismatchDescription.appendText(" got unexpected error!"); + } + } + + @Factory + public static ValidationMatcher valid(final Source schemaSource) { + return new ValidationMatcher(schemaSource); + } + + +} Property changes on: trunk/xmlunit/src/main/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcher.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/xmlunit/src/tests/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcherTest.java =================================================================== --- trunk/xmlunit/src/tests/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcherTest.java (rev 0) +++ trunk/xmlunit/src/tests/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcherTest.java 2013-04-25 16:22:25 UTC (rev 534) @@ -0,0 +1,43 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit.matcher; + +import org.junit.Test; + +import javax.xml.transform.stream.StreamSource; +import java.io.File; + +import static net.sf.xmlunit.matcher.ValidationMatcher.valid; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; + +/** + * Tests for ValidationMatcher. + */ +public class ValidationMatcherTest { + + @Test + public void shouldSuccessfullyValidateInstance() { + assertThat(new StreamSource(new File("src/tests/resources/BookXsdGenerated.xml")), + is(valid(new StreamSource(new File("src/tests/resources/Book.xsd"))))); + + } + + @Test + public void shouldFailOnBrokenInstance() { + assertThat(new StreamSource(new File("src/tests/resources/invalidBook.xml")), + is(not(valid(new StreamSource(new File("src/tests/resources/Book.xsd")))))); + } +} Property changes on: trunk/xmlunit/src/tests/java-hamcrest/net/sf/xmlunit/matcher/ValidationMatcherTest.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |