1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // "Software"), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
4
+ using System . Collections ;
5
+ using System . Collections . Generic ;
24
6
using System . Data . SqlTypes ;
25
7
using System . IO ;
8
+ using System . Linq ;
26
9
using System . Text ;
27
10
using System . Xml ;
28
11
@@ -32,10 +15,8 @@ namespace System.Data.Tests.SqlTypes
32
15
{
33
16
public class SqlXmlTest
34
17
{
35
- // Test constructor
36
- [ Fact ] // .ctor (Stream)
37
- //[Category ("NotDotNet")] // Name cannot begin with the '.' character, hexadecimal value 0x00. Line 1, position 2
38
- public void Constructor2_Stream_Unicode ( )
18
+ [ Fact ]
19
+ public void Constructor_Stream_Unicode ( )
39
20
{
40
21
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>" ;
41
22
MemoryStream stream = new MemoryStream ( Encoding . Unicode . GetBytes ( xmlStr ) ) ;
@@ -44,8 +25,8 @@ public void Constructor2_Stream_Unicode()
44
25
Assert . Equal ( xmlStr , xmlSql . Value ) ;
45
26
}
46
27
47
- [ Fact ] // .ctor (Stream)
48
- public void Constructor2_Stream_Empty ( )
28
+ [ Fact ]
29
+ public void Constructor_Stream_Empty ( )
49
30
{
50
31
MemoryStream ms = new MemoryStream ( ) ;
51
32
SqlXml xmlSql = new SqlXml ( ms ) ;
@@ -54,16 +35,16 @@ public void Constructor2_Stream_Empty()
54
35
}
55
36
56
37
[ Fact ]
57
- public void Constructor2_Stream_Null ( )
38
+ public void Constructor_Stream_Null ( )
58
39
{
59
40
SqlXml xmlSql = new SqlXml ( ( Stream ) null ) ;
60
41
Assert . True ( xmlSql . IsNull ) ;
61
42
62
43
Assert . Throws < SqlNullValueException > ( ( ) => xmlSql . Value ) ;
63
44
}
64
45
65
- [ Fact ] // .ctor (XmlReader)
66
- public void Constructor3 ( )
46
+ [ Fact ]
47
+ public void Constructor_StringReader ( )
67
48
{
68
49
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>" ;
69
50
XmlReader xrdr = new XmlTextReader ( new StringReader ( xmlStr ) ) ;
@@ -72,8 +53,8 @@ public void Constructor3()
72
53
Assert . Equal ( xmlStr , xmlSql . Value ) ;
73
54
}
74
55
75
- [ Fact ] // .ctor (XmlReader)
76
- public void Constructor3_XmlReader_Empty ( )
56
+ [ Fact ]
57
+ public void Constructor_XmlReader_Empty ( )
77
58
{
78
59
XmlReaderSettings xs = new XmlReaderSettings ( ) ;
79
60
xs . ConformanceLevel = ConformanceLevel . Fragment ;
@@ -84,7 +65,7 @@ public void Constructor3_XmlReader_Empty()
84
65
}
85
66
86
67
[ Fact ]
87
- public void Constructor3_XmlReader_Null ( )
68
+ public void Constructor_XmlReader_Null ( )
88
69
{
89
70
SqlXml xmlSql = new SqlXml ( ( XmlReader ) null ) ;
90
71
Assert . True ( xmlSql . IsNull ) ;
@@ -93,7 +74,6 @@ public void Constructor3_XmlReader_Null()
93
74
}
94
75
95
76
[ Fact ]
96
- //[Category ("NotDotNet")] // Name cannot begin with the '.' character, hexadecimal value 0x00. Line 1, position 2
97
77
public void CreateReader_Stream_Unicode ( )
98
78
{
99
79
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>" ;
@@ -107,7 +87,7 @@ public void CreateReader_Stream_Unicode()
107
87
}
108
88
109
89
[ Fact ]
110
- public void SqlXml_fromXmlReader_CreateReaderTest ( )
90
+ public void CreateReader_XmlTextReader_CanReadContent ( )
111
91
{
112
92
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>" ;
113
93
XmlReader rdr = new XmlTextReader ( new StringReader ( xmlStr ) ) ;
@@ -119,8 +99,82 @@ public void SqlXml_fromXmlReader_CreateReaderTest()
119
99
Assert . Equal ( xmlStr , xrdr . ReadOuterXml ( ) ) ;
120
100
}
121
101
102
+ public static class CreateReader_TestFiles
103
+ {
104
+ private static TheoryData < string , string > _filesAndBaselines ;
105
+
106
+ // The test files are made available through the System.Data.Common.TestData package included in dotnet/runtime-assets
107
+ private static void EnsureFileList ( )
108
+ {
109
+ if ( _filesAndBaselines is null )
110
+ {
111
+ IEnumerable < string > text = Directory . EnumerateFiles ( Path . Combine ( "SqlXml.CreateReader" , "Baseline-Text" ) , "*.xml" ) ;
112
+ IEnumerable < string > binary = Directory . EnumerateFiles ( Path . Combine ( "SqlXml.CreateReader" , "SqlBinaryXml" ) , "*.bmx" ) ;
113
+
114
+ // Make sure that we found our test files; otherwise the theories would succeed without validating anything
115
+ Assert . NotEmpty ( text ) ;
116
+ Assert . NotEmpty ( binary ) ;
117
+
118
+ TheoryData < string , string > filesAndBaselines = new TheoryData < string , string > ( ) ;
119
+
120
+ // Use the Text XML files as their own baselines
121
+ filesAndBaselines . Append ( text . Select ( f => new string [ ] { TextXmlFileName ( f ) , TextXmlFileName ( f ) } ) . ToArray ( ) ) ;
122
+
123
+ // Use the matching Text XML files as the baselines for the SQL Binary XML files
124
+ filesAndBaselines . Append ( binary
125
+ . Select ( Path . GetFileNameWithoutExtension )
126
+ . Intersect ( text . Select ( Path . GetFileNameWithoutExtension ) )
127
+ . Select ( f => new string [ ] { SqlBinaryXmlFileName ( f ) , TextXmlFileName ( f ) } ) . ToArray ( ) ) ;
128
+
129
+ _filesAndBaselines = filesAndBaselines ;
130
+
131
+ string TextXmlFileName ( string name ) => Path . Combine ( "SqlXml.CreateReader" , "Baseline-Text" , $ "{ name } .xml") ;
132
+ string SqlBinaryXmlFileName ( string name ) => Path . Combine ( "SqlXml.CreateReader" , "SqlBinaryXml" , $ "{ name } .bmx") ;
133
+ }
134
+ }
135
+
136
+ public static TheoryData < string , string > FilesAndBaselines
137
+ {
138
+ get
139
+ {
140
+ EnsureFileList ( ) ;
141
+ return _filesAndBaselines ;
142
+ }
143
+ }
144
+
145
+ public static string ReadAllXml ( XmlReader reader )
146
+ {
147
+ using StringWriter writer = new StringWriter ( ) ;
148
+ using XmlWriter xmlWriter = new XmlTextWriter ( writer ) ;
149
+
150
+ while ( reader . Read ( ) ) xmlWriter . WriteNode ( reader , false ) ;
151
+
152
+ return writer . ToString ( ) ;
153
+ }
154
+ }
155
+
156
+ [ Theory ]
157
+ [ MemberData ( nameof ( CreateReader_TestFiles . FilesAndBaselines ) , MemberType = typeof ( CreateReader_TestFiles ) ) ]
158
+ public void CreateReader_TestAgainstBaseline ( string testFile , string baselineFile )
159
+ {
160
+ // Get our expected output by using XmlReader directly
161
+ using XmlReader baselineReader = XmlReader . Create ( baselineFile ) ;
162
+ string expected = CreateReader_TestFiles . ReadAllXml ( baselineReader ) ;
163
+
164
+ // Now produce the actual output through SqlXml.CreateReader
165
+ using FileStream xmlStream = new FileStream ( testFile , FileMode . Open ) ;
166
+ SqlXml sqlXml = new SqlXml ( xmlStream ) ;
167
+
168
+ // When the input is text, an XmlTextReader will be returned
169
+ // When the input is SQL Binary XML, an XmlSqlBinaryReader will be returned
170
+ using XmlReader actualReader = sqlXml . CreateReader ( ) ;
171
+ string actual = CreateReader_TestFiles . ReadAllXml ( actualReader ) ;
172
+
173
+ Assert . Equal ( expected , actual ) ;
174
+ }
175
+
122
176
[ Fact ]
123
- public void SqlXml_fromZeroLengthStream_CreateReaderTest ( )
177
+ public void SqlXml_FromZeroLengthStream_CreateReaderTest ( )
124
178
{
125
179
MemoryStream stream = new MemoryStream ( ) ;
126
180
SqlXml xmlSql = new SqlXml ( stream ) ;
@@ -131,7 +185,7 @@ public void SqlXml_fromZeroLengthStream_CreateReaderTest()
131
185
}
132
186
133
187
[ Fact ]
134
- public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest_withFragment ( )
188
+ public void SqlXml_FromZeroLengthXmlReader_CreateReaderTest_withFragment ( )
135
189
{
136
190
XmlReaderSettings xs = new XmlReaderSettings ( ) ;
137
191
xs . ConformanceLevel = ConformanceLevel . Fragment ;
@@ -145,7 +199,7 @@ public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest_withFragment()
145
199
}
146
200
147
201
[ Fact ]
148
- public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest ( )
202
+ public void SqlXml_FromZeroLengthXmlReader_CreateReaderTest ( )
149
203
{
150
204
XmlReader rdr = new XmlTextReader ( new StringReader ( string . Empty ) ) ;
151
205
0 commit comments