Skip to content

Commit

Permalink
Adding unit test to provoke exception using Sch_WhiteSpaceRestriction… (
Browse files Browse the repository at this point in the history
#35013)

* Adding unit test to provoke exception using Sch_WhiteSpaceRestriction1 as its message.
  • Loading branch information
mrj001 authored May 11, 2020
1 parent 1c10ed9 commit 23e13a3
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/libraries/System.Private.Xml/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@
<value>Derived attribute's use has to be required if base attribute's use is required.</value>
</data>
<data name="Sch_AttributeRestrictionInvalidFromWildcard" xml:space="preserve">
<value>The {base type definition} must have an {attribute wildcard} and the {target namespace} of the R's {attribute declaration} must be valid with respect to that wildcard.</value>
<value>The base type definition must have an attribute wildcard and the target namespace of the attribute declaration in the 'redefine' must be valid with respect to that wildcard.</value>
</data>
<data name="Sch_NoDerivedAttribute" xml:space="preserve">
<value>The base attribute '{0}' whose use = 'required' does not have a corresponding derived attribute while redefining attribute group '{1}'.</value>
Expand Down Expand Up @@ -1771,10 +1771,10 @@
<value>Values that are declared with fixed='true' in a base type can not be changed in a derived type.</value>
</data>
<data name="Sch_WhiteSpaceRestriction1" xml:space="preserve">
<value>It is an error if 'whiteSpace' is among the members of {facets} of {base type definition}, {value} is 'replace' or 'preserve', and the {value} of the parent 'whiteSpace' is 'collapse'.</value>
<value>It is an error if 'whiteSpace' is among the facets of the type definition, its value is 'replace' or 'preserve', and the value of the parent 'whiteSpace' is 'collapse'.</value>
</data>
<data name="Sch_WhiteSpaceRestriction2" xml:space="preserve">
<value>It is an error if 'whiteSpace' is among the members of {facets} of {base type definition}, {value} is 'preserve', and the {value} of the parent 'whiteSpace' is 'replace'.</value>
<value>It is an error if 'whiteSpace' is among the facets of the type definition, its value is 'preserve', and the value of the parent 'whiteSpace' is 'replace'.</value>
</data>
<data name="Sch_XsiNilAndFixed" xml:space="preserve">
<value>There must be no fixed value when an attribute is 'xsi:nil' and has a value of 'true'.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Text;

namespace System.Xml.Tests
{
Expand Down Expand Up @@ -1150,5 +1151,219 @@ public void TotalDigitsParseValue_Succeeds()
ss.Compile();
}
}

#region tests causing XmlSchemaException with Sch_WhiteSpaceRestriction1
public static IEnumerable<object[]> WhiteSpaceRestriction1_Throws_TestData
{
get
{
return new List<object[]>()
{
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:simpleType name='baseType'>
<xs:restriction base='xs:string'>
<xs:whiteSpace value='collapse'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name='restrictedType'>
<xs:restriction base='baseType'>
<xs:whiteSpace value='preserve'/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
"
},
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:simpleType name='baseType'>
<xs:restriction base='xs:string'>
<xs:whiteSpace value='collapse'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name='restrictedType'>
<xs:restriction base='baseType'>
<xs:whiteSpace value='replace'/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
"
}
};
}
}

[Theory]
[MemberData(nameof(WhiteSpaceRestriction1_Throws_TestData))]
public void WhiteSpaceRestriction1_Throws(string schema)
{
XmlSchemaSet ss = new XmlSchemaSet();
using (StringReader sr = new StringReader(schema))
using (XmlReader xmlrdr = XmlReader.Create(sr))
{
ss.Add(null, xmlrdr);
}

Exception ex = Assert.Throws<XmlSchemaException>(() => ss.Compile());

Assert.Contains("whiteSpace", ex.Message);
Assert.Contains("collapse", ex.Message);
Assert.Contains("preserve", ex.Message);
Assert.Contains("replace", ex.Message);
}
#endregion

#region tests causing XmlSchemaException with Sch_WhiteSpaceRestriction2
public static IEnumerable<object[]> WhiteSpaceRestriction2_Throws_TestData
{
get
{
return new List<object[]>()
{
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:simpleType name='baseType'>
<xs:restriction base='xs:string'>
<xs:whiteSpace value='replace'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name='restrictedType'>
<xs:restriction base='baseType'>
<xs:whiteSpace value='preserve'/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
"
}
};
}
}

[Theory]
[MemberData(nameof(WhiteSpaceRestriction2_Throws_TestData))]
public void WhiteSpaceRestriction2_Throws(string schema)
{
XmlSchemaSet ss = new XmlSchemaSet();
using (StringReader sr = new StringReader(schema))
using (XmlReader xmlrdr = XmlReader.Create(sr))
{
ss.Add(null, xmlrdr);
}

Exception ex = Assert.Throws<XmlSchemaException>(() => ss.Compile());

Assert.Contains("whiteSpace", ex.Message);
Assert.DoesNotContain("collapse", ex.Message);
Assert.Contains("preserve", ex.Message);
Assert.Contains("replace", ex.Message);
}
#endregion

#region Attribute Restriction Invalid From WildCard tests

public static IEnumerable<object[]> AttributeRestrictionInvalidFromWildCard_Throws_TestData
{
get
{
return new List<object[]>()
{
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:redefine schemaLocation='fake://0'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
<xs:attribute name='d' type='xs:integer'/>
</xs:attributeGroup>
</xs:redefine>
</xs:schema>
"
},
new object[]
{
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='http://www.foo.bar'>
<xs:redefine schemaLocation='fake://1'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
<xs:attribute name='d' type='xs:integer' form='qualified'/>
</xs:attributeGroup>
</xs:redefine>
</xs:schema>
"
}
};
}
}

[Theory]
[MemberData(nameof(AttributeRestrictionInvalidFromWildCard_Throws_TestData))]
public void AttributeRestrictionInvalidFromWildCard_Throws(string schema)
{
XmlSchemaSet ss = new XmlSchemaSet();
ss.XmlResolver = new FakeXmlResolverAttributeRestriction();
using (StringReader sr = new StringReader(schema))
using (XmlReader xmlrdr = XmlReader.Create(sr))
{
ss.Add(null, xmlrdr);
}

Exception ex = Assert.Throws<XmlSchemaException>(() => ss.Compile());

Assert.Contains("wildcard", ex.Message);
Assert.Contains("redefine", ex.Message);
}

private class FakeXmlResolverAttributeRestriction : XmlResolver
{
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
int uriIndex = int.Parse(absoluteUri.Host);
string[] schema = { @"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
</xs:attributeGroup>
</xs:schema>
",
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema elementFormDefault='qualified'
xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:attributeGroup name='baseGroup'>
<xs:attribute name='a' type='xs:integer'/>
<xs:attribute name='b' type='xs:integer'/>
<xs:attribute name='c' type='xs:integer'/>
<xs:anyAttribute namespace='##local'/>
</xs:attributeGroup>
</xs:schema>
"
};

return new MemoryStream(Encoding.UTF8.GetBytes(schema[uriIndex]));
}
}
#endregion
}
}

0 comments on commit 23e13a3

Please sign in to comment.