-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endrer navn på ValidationRunner til XmlValidationRunner
- Loading branch information
Aleksander Aas Sjåfjell
committed
Jun 13, 2016
1 parent
8045c69
commit 95f4d77
Showing
6 changed files
with
111 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 43 additions & 38 deletions
81
...ester/Validation/ValidationRunnerTests.cs → ...er/Validation/XmlValidationRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,44 @@ | ||
using Difi.Felles.Utility.Validation; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Difi.Felles.Utility.Tester.Validation | ||
{ | ||
[TestClass] | ||
public class ValidationRunnerTests | ||
{ | ||
[TestClass] | ||
public class ConstructorMethod : ValidationRunnerTests | ||
{ | ||
[TestMethod] | ||
public void SimpleInitialization() | ||
{ | ||
//Arrange | ||
var validationRunner = new ValidationRunner(TestGenerator.XmlSchemaSet()); | ||
|
||
} | ||
} | ||
|
||
[TestClass] | ||
public class ValidateMethod : ValidationMessagesTests | ||
{ | ||
[TestMethod] | ||
public void AddsValidationMessage() | ||
{ | ||
//Arrange | ||
var validationRunner = new ValidationRunner(TestGenerator.XmlSchemaSet()); | ||
var invalidTestCouple = new TestGenerator.InvalidContentTestCouple(); | ||
|
||
//Act | ||
validationRunner.Validate(invalidTestCouple.Input()); | ||
|
||
//Assert | ||
Assert.AreEqual(1, validationRunner.ValidationMessages.Count); | ||
} | ||
} | ||
} | ||
using Difi.Felles.Utility.Validation; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Difi.Felles.Utility.Tester.Validation | ||
{ | ||
[TestClass] | ||
public class XmlValidationRunnerTests | ||
{ | ||
[TestClass] | ||
public class ConstructorMethod : XmlValidationRunnerTests | ||
{ | ||
[TestMethod] | ||
public void SimpleInitialization() | ||
{ | ||
//Arrange | ||
var xmlSchemaSet = TestGenerator.XmlSchemaSet(); | ||
|
||
//Act | ||
var validationRunner = new XmlValidationRunner(xmlSchemaSet); | ||
|
||
//Assert | ||
Assert.AreEqual(xmlSchemaSet, validationRunner.XmlSchemaSet); | ||
} | ||
} | ||
|
||
[TestClass] | ||
public class ValidateMethod : ValidationMessagesTests | ||
{ | ||
[TestMethod] | ||
public void AddsValidationMessage() | ||
{ | ||
//Arrange | ||
var validationRunner = new XmlValidationRunner(TestGenerator.XmlSchemaSet()); | ||
var invalidTestCouple = new TestGenerator.InvalidContentTestCouple(); | ||
|
||
//Act | ||
validationRunner.Validate(invalidTestCouple.Input()); | ||
|
||
//Assert | ||
Assert.AreEqual(1, validationRunner.ValidationMessages.Count); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 59 additions & 59 deletions
118
...es.Utility/Validation/ValidationRunner.cs → ...Utility/Validation/XmlValidationRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
|
||
namespace Difi.Felles.Utility.Validation | ||
{ | ||
internal class ValidationRunner | ||
{ | ||
private const string ToleratedXsdIdErrorEnUs = "It is an error if there is a member of the attribute uses of a type definition with type xs:ID or derived from xs:ID and another attribute with type xs:ID matches an attribute wildcard."; | ||
private const string ToleratedXsdIdErrorNbNo = "Det er en feil hvis det finnes et medlem av attributtet som bruker en typedefinisjon med typen xs:ID eller avledet fra xs:ID og et annet attributt med typen xs:ID tilsvarer et attributtjokertegn."; | ||
private const string ToleratedPrefixListErrorEnUs = "The 'PrefixList' attribute is invalid - The value '' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:NMTOKENS' - The attribute value cannot be empty."; | ||
private const string ToleratedPrefixListErrorNbNo = "Attributtet PrefixList er ugyldig - Verdien er ugyldig i henhold til datatypen http://www.w3.org/2001/XMLSchema:NMTOKENS - Attributtverdien kan ikke være tom."; | ||
|
||
internal static readonly List<string> ToleratedErrors = new List<string> {ToleratedXsdIdErrorEnUs, ToleratedXsdIdErrorNbNo, ToleratedPrefixListErrorEnUs, ToleratedPrefixListErrorNbNo}; | ||
|
||
private readonly XmlSchemaSet _schemaSet; | ||
|
||
internal ValidationRunner(XmlSchemaSet schemaSet) | ||
{ | ||
_schemaSet = schemaSet; | ||
} | ||
|
||
internal ValidationMessages ValidationMessages { get; } = new ValidationMessages(); | ||
|
||
internal bool Validate(string document) | ||
{ | ||
var settings = new XmlReaderSettings(); | ||
settings.Schemas.Add(_schemaSet); | ||
settings.ValidationType = ValidationType.Schema; | ||
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; | ||
settings.ValidationEventHandler += ValidationEventHandler; | ||
|
||
var xmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(document)), settings); | ||
|
||
while (xmlReader.Read()) | ||
{ | ||
} | ||
|
||
return !ValidationMessages.HasErrors && !ValidationMessages.HasWarnings; | ||
} | ||
|
||
private void ValidationEventHandler(object sender, ValidationEventArgs e) | ||
{ | ||
if (IsToleratedError(e)) | ||
{ | ||
} | ||
else | ||
{ | ||
ValidationMessages.Add(e.Severity, e.Message); | ||
} | ||
} | ||
|
||
private static bool IsToleratedError(ValidationEventArgs e) | ||
{ | ||
return ToleratedErrors.Contains(e.Message); | ||
} | ||
} | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Xml; | ||
using System.Xml.Schema; | ||
|
||
namespace Difi.Felles.Utility.Validation | ||
{ | ||
internal class XmlValidationRunner | ||
{ | ||
private const string ToleratedXsdIdErrorEnUs = "It is an error if there is a member of the attribute uses of a type definition with type xs:ID or derived from xs:ID and another attribute with type xs:ID matches an attribute wildcard."; | ||
private const string ToleratedXsdIdErrorNbNo = "Det er en feil hvis det finnes et medlem av attributtet som bruker en typedefinisjon med typen xs:ID eller avledet fra xs:ID og et annet attributt med typen xs:ID tilsvarer et attributtjokertegn."; | ||
private const string ToleratedPrefixListErrorEnUs = "The 'PrefixList' attribute is invalid - The value '' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:NMTOKENS' - The attribute value cannot be empty."; | ||
private const string ToleratedPrefixListErrorNbNo = "Attributtet PrefixList er ugyldig - Verdien er ugyldig i henhold til datatypen http://www.w3.org/2001/XMLSchema:NMTOKENS - Attributtverdien kan ikke være tom."; | ||
|
||
internal static readonly List<string> ToleratedErrors = new List<string> {ToleratedXsdIdErrorEnUs, ToleratedXsdIdErrorNbNo, ToleratedPrefixListErrorEnUs, ToleratedPrefixListErrorNbNo}; | ||
|
||
internal XmlSchemaSet XmlSchemaSet { get; } | ||
|
||
internal XmlValidationRunner(XmlSchemaSet xmlSchemaSet) | ||
{ | ||
XmlSchemaSet = xmlSchemaSet; | ||
} | ||
|
||
internal ValidationMessages ValidationMessages { get; } = new ValidationMessages(); | ||
|
||
internal bool Validate(string document) | ||
{ | ||
var settings = new XmlReaderSettings(); | ||
settings.Schemas.Add(XmlSchemaSet); | ||
settings.ValidationType = ValidationType.Schema; | ||
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; | ||
settings.ValidationEventHandler += ValidationEventHandler; | ||
|
||
var xmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(document)), settings); | ||
|
||
while (xmlReader.Read()) | ||
{ | ||
} | ||
|
||
return !ValidationMessages.HasErrors && !ValidationMessages.HasWarnings; | ||
} | ||
|
||
private void ValidationEventHandler(object sender, ValidationEventArgs e) | ||
{ | ||
if (IsToleratedError(e)) | ||
{ | ||
} | ||
else | ||
{ | ||
ValidationMessages.Add(e.Severity, e.Message); | ||
} | ||
} | ||
|
||
private static bool IsToleratedError(ValidationEventArgs e) | ||
{ | ||
return ToleratedErrors.Contains(e.Message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters