From: <bo...@us...> - 2009-06-05 15:51:47
|
Revision: 344 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=344&view=rev Author: bodewig Date: 2009-06-05 15:51:43 +0000 (Fri, 05 Jun 2009) Log Message: ----------- implement validation for .NET - DTD validation not tested, yet (and probably not complete either) Modified Paths: -------------- trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs trunk/xmlunit/src/main/net-core/validation/Validator.cs Added Paths: ----------- trunk/xmlunit/src/tests/net-core/validation/ trunk/xmlunit/src/tests/net-core/validation/ValidatorTest.cs Modified: trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs =================================================================== --- trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs 2009-06-05 10:45:00 UTC (rev 343) +++ trunk/xmlunit/src/main/net-core/validation/ValidationProblem.cs 2009-06-05 15:51:43 UTC (rev 344) @@ -68,5 +68,13 @@ return message; } } + + internal static ValidationProblem FromEvent(ValidationEventArgs e) { + XmlSchemaException ex = e.Exception; + return new ValidationProblem(e.Message, + ex == null ? UNKNOWN : ex.LineNumber, + ex == null ? UNKNOWN : ex.LinePosition, + e.Severity); + } } } Modified: trunk/xmlunit/src/main/net-core/validation/Validator.cs =================================================================== --- trunk/xmlunit/src/main/net-core/validation/Validator.cs 2009-06-05 10:45:00 UTC (rev 343) +++ trunk/xmlunit/src/main/net-core/validation/Validator.cs 2009-06-05 15:51:43 UTC (rev 344) @@ -13,7 +13,10 @@ */ using System; using System.Collections.Generic; +using System.IO; using System.Xml; +using System.Xml.Schema; +using net.sf.xmlunit.exceptions; namespace net.sf.xmlunit.validation { @@ -73,6 +76,15 @@ /// Validates a schema. /// </summary> public virtual ValidationResult ValidateSchema() { + if (language == ValidationType.Schema && sourceLocations != null) { + List<ValidationProblem> problems = + new List<ValidationProblem>(); + foreach (ISource loc in sourceLocations) { + XmlSchema s = XmlSchema.Read(loc.Reader, + CollectProblems(problems)); + } + return new ValidationResult(problems.Count == 0, problems); + } throw new NotImplementedException(); } @@ -80,10 +92,31 @@ /// Validates an instance against the schema. /// </summary> public virtual ValidationResult ValidateInstance(ISource instance) { - throw new NotImplementedException(); + XmlReaderSettings settings = new XmlReaderSettings(); + settings.ValidationType = language; + settings.ValidationFlags = + XmlSchemaValidationFlags.ProcessIdentityConstraints + | XmlSchemaValidationFlags.ReportValidationWarnings; + if (language == ValidationType.Schema && sourceLocations != null) { + foreach (ISource loc in sourceLocations) { + try { + XmlSchema s = XmlSchema.Read(loc.Reader, ThrowOnError); + settings.Schemas.Add(s); + } catch (IOException ex) { + throw new XMLUnitException("Schema is not readable", + ex); + } + } + } + List<ValidationProblem> problems = new List<ValidationProblem>(); + settings.ValidationEventHandler += CollectProblems(problems); + using (XmlReader r = XmlReader.Create(instance.Reader, + settings)) { + while (r.Read()) ; + } + return new ValidationResult(problems.Count == 0, problems); } - private static readonly IDictionary<string, ValidationType> types; static Validator() { @@ -104,5 +137,16 @@ // TODO pick a better exception type throw new NotImplementedException(); } + + private static ValidationEventHandler CollectProblems(List<ValidationProblem> problems) { + return delegate(object sender, ValidationEventArgs e) { + problems.Add(ValidationProblem.FromEvent(e)); + }; + } + + private static void ThrowOnError(object sender, ValidationEventArgs e) { + throw new XMLUnitException("Schema is invalid", e.Exception); + } + } } Added: trunk/xmlunit/src/tests/net-core/validation/ValidatorTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/validation/ValidatorTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/validation/ValidatorTest.cs 2009-06-05 15:51:43 UTC (rev 344) @@ -0,0 +1,68 @@ +/* + 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.exceptions; +using net.sf.xmlunit.input; + +namespace net.sf.xmlunit.validation { + [TestFixture] + public class ValidatorTest { + [Test] + public void ShouldSuccessfullyValidateSchema() { + Validator v = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI); + v.SchemaSource = new StreamSource("../../../src/tests/resources/Book.xsd"); + ValidationResult r = v.ValidateSchema(); + Assert.IsTrue(r.Valid); + Assert.IsFalse(r.Problems.GetEnumerator().MoveNext()); + } + + [Test] + public void ShouldSuccessfullyValidateInstance() { + Validator v = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI); + v.SchemaSource = new StreamSource("../../../src/tests/resources/Book.xsd"); + ValidationResult r = v.ValidateInstance(new StreamSource("../../../src/tests/resources/BookXsdGenerated.xml")); + Assert.IsTrue(r.Valid); + Assert.IsFalse(r.Problems.GetEnumerator().MoveNext()); + } + + [Test] + public void ShouldFailOnBrokenSchema() { + Validator v = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI); + v.SchemaSource = new StreamSource("../../../src/tests/resources/broken.xsd"); + ValidationResult r = v.ValidateSchema(); + Assert.IsFalse(r.Valid); + Assert.IsTrue(r.Problems.GetEnumerator().MoveNext()); + } + + [Test] + public void ShouldFailOnBrokenInstance() { + Validator v = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI); + v.SchemaSource = new StreamSource("../../../src/tests/resources/Book.xsd"); + ValidationResult r = v.ValidateInstance(new StreamSource("../../../src/tests/resources/invalidBook.xml")); + Assert.IsFalse(r.Valid); + Assert.IsTrue(r.Problems.GetEnumerator().MoveNext()); + } + + [Test] + public void ShouldThrowWhenValidatingInstanceAndSchemaIsInvalid() { + Validator v = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI); + v.SchemaSource = new StreamSource("../../../src/tests/resources/broken.xsd"); + Assert.Throws(typeof(XMLUnitException), delegate() { + v.ValidateInstance(new StreamSource("../../../src/tests/resources/BookXsdGenerated.xml")); + }); + } + + } +} Property changes on: trunk/xmlunit/src/tests/net-core/validation/ValidatorTest.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |