From: <bo...@us...> - 2013-04-27 15:09:01
|
Revision: 536 http://sourceforge.net/p/xmlunit/code/536 Author: bodewig Date: 2013-04-27 15:08:57 +0000 (Sat, 27 Apr 2013) Log Message: ----------- NUnit Constraint for XML Schema validation Modified Paths: -------------- trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs trunk/xmlunit/xmlunit.nant.build Added Paths: ----------- trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs trunk/xmlunit/src/tests/net-constraints/ValidationConstraintsTest.cs Added: trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs =================================================================== --- trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs (rev 0) +++ trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs 2013-04-27 15:08:57 UTC (rev 536) @@ -0,0 +1,83 @@ +/* + 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. +*/ + +using NUnit.Framework.Constraints; +using net.sf.xmlunit.util; +using net.sf.xmlunit.validation; + +namespace net.sf.xmlunit.constraints { + + /// <summary> + /// Constraint that validates a document against a given XML + /// schema. + /// </summary> + public class SchemaValidConstraint : Constraint { + private readonly Validator validator; + private ValidationResult result; + + /// <summary> + /// Creates the constraint validating against the given schema(s). + /// </summary> + public SchemaValidConstraint(params ISource[] schema) : base(schema) { + validator = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI); + validator.SchemaSources = schema; + } + + public override bool Matches(object o) { + this.actual = o; + if (o is ISource) { + result = validator.ValidateInstance(o as ISource); + return result.Valid; + } + return false; + } + + public override void WriteDescriptionTo(MessageWriter writer) { + writer.Write("{0} validates against {1}", GrabActual(), + GrabSystemIds()); + } + + public override void WriteActualValueTo(MessageWriter writer) { + if (actual is ISource) { + writer.Write("got validation errors: {0}", GrabProblems()); + } else { + writer.Write("{0} that is not ISource", GrabActual()); + } + } + + private string GrabSystemIds() { + return string.Join("\n", Linqy.Map<ISource, + string>(validator.SchemaSources, GrabSystemId)); + } + + private string GrabActual() { + ISource actualSource = actual as ISource; + return actualSource != null ? GrabSystemId(actualSource) + : (actual ?? "null").ToString(); + } + + private string GrabSystemId(ISource s) { + return s.SystemId; + } + + private string GrabProblems() { + return string.Join(", ", Linqy.Map<ValidationProblem, + string>(result.Problems, ProblemToString)); + } + + private string ProblemToString(ValidationProblem problem) { + return problem.ToString(); + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs =================================================================== --- trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs 2013-04-27 15:04:45 UTC (rev 535) +++ trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs 2013-04-27 15:08:57 UTC (rev 536) @@ -76,5 +76,10 @@ ex == null ? UNKNOWN : ex.LinePosition, e.Severity); } + + public override string ToString() { + return string.Format("ValidationProblem {{ line={0}, column={1}, type={2}," + + " message=\'{3}'\' }}", Line, Column, Type, Message); + } } } Added: trunk/xmlunit/src/tests/net-constraints/ValidationConstraintsTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-constraints/ValidationConstraintsTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-constraints/ValidationConstraintsTest.cs 2013-04-27 15:08:57 UTC (rev 536) @@ -0,0 +1,39 @@ +/* + 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. +*/ +using System; +using NUnit.Framework; +using net.sf.xmlunit.input; + +namespace net.sf.xmlunit.constraints { + [TestFixture] + public class ValidationConstraintTest { + [Test] + public void ShouldSuccessfullyValidateInstance() { + Assert.That(new StreamSource(TestResources.TESTS_DIR + + "BookXsdGeneratedNoSchema.xml"), + new SchemaValidConstraint( + new StreamSource(TestResources.TESTS_DIR + "Book.xsd"))); + } + + [Test] + public void ShouldFailOnBrokenSchema() { + Assert.That(new StreamSource(TestResources.TESTS_DIR + + "invalidBook.xml"), + !new SchemaValidConstraint(new StreamSource(TestResources + .TESTS_DIR + + "Book.xsd"))); + } + + } +} Property changes on: trunk/xmlunit/src/tests/net-constraints/ValidationConstraintsTest.cs ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/xmlunit/xmlunit.nant.build =================================================================== --- trunk/xmlunit/xmlunit.nant.build 2013-04-27 15:04:45 UTC (rev 535) +++ trunk/xmlunit/xmlunit.nant.build 2013-04-27 15:08:57 UTC (rev 536) @@ -120,6 +120,10 @@ <sources basedir="${gen.constraints.dir}"> <include name="**/*.cs"/> </sources> + <references basedir="${bin.dir}"> + <include name="${project::get-name()}-core.dll"/> + <include name="${nunit.v2.assembly}"/> + </references> </csc> </target> @@ -140,19 +144,21 @@ </csc> </target> - <target name="compile-constraints-tests" depends="compile-constraints"> - <!--csc target="library" + <target name="compile-constraints-tests" + depends="compile-constraints,compile-core-tests"> + <csc target="library" output="${bin.dir}/${project::get-name()}-constraints.tests.dll" debug="true" verbose="${csc.verbose}" keyfile="xmlunit.snk"> <references basedir="${bin.dir}"> <include name="${project::get-name()}-core.dll"/> <include name="${project::get-name()}-constraints.dll"/> + <include name="${project::get-name()}-core.tests.dll"/> <include name="${nunit.v2.assembly}"/> </references> <sources basedir="${constraints.tests.src.dir}"> <include name="**/*.cs"/> </sources> - </csc--> + </csc> </target> <target name="compile-tests" @@ -163,7 +169,7 @@ <formatter type="Plain"/> <formatter type="Xml" usefile="true" outputdir="${test.report.dir}"/> <test assemblyname="${bin.dir}/${project::get-name()}-core.tests.dll"/> - <!--test assemblyname="${bin.dir}/${project::get-name()}-constraints.tests.dll"/--> + <test assemblyname="${bin.dir}/${project::get-name()}-constraints.tests.dll"/> </nunit2> </target> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |