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
+ using System . Collections ;
5
+ using System . Collections . Generic ;
4
6
using System . Data . SqlTypes ;
5
7
using System . IO ;
8
+ using System . Linq ;
6
9
using System . Text ;
7
10
using System . Xml ;
8
11
@@ -13,7 +16,7 @@ namespace System.Data.Tests.SqlTypes
13
16
public class SqlXmlTest
14
17
{
15
18
[ Fact ]
16
- public void Constructor2_Stream_Unicode ( )
19
+ public void Constructor_Stream_Unicode ( )
17
20
{
18
21
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>" ;
19
22
MemoryStream stream = new MemoryStream ( Encoding . Unicode . GetBytes ( xmlStr ) ) ;
@@ -23,7 +26,7 @@ public void Constructor2_Stream_Unicode()
23
26
}
24
27
25
28
[ Fact ]
26
- public void Constructor2_Stream_Empty ( )
29
+ public void Constructor_Stream_Empty ( )
27
30
{
28
31
MemoryStream ms = new MemoryStream ( ) ;
29
32
SqlXml xmlSql = new SqlXml ( ms ) ;
@@ -32,7 +35,7 @@ public void Constructor2_Stream_Empty()
32
35
}
33
36
34
37
[ Fact ]
35
- public void Constructor2_Stream_Null ( )
38
+ public void Constructor_Stream_Null ( )
36
39
{
37
40
SqlXml xmlSql = new SqlXml ( ( Stream ) null ) ;
38
41
Assert . True ( xmlSql . IsNull ) ;
@@ -41,7 +44,7 @@ public void Constructor2_Stream_Null()
41
44
}
42
45
43
46
[ Fact ]
44
- public void Constructor3 ( )
47
+ public void Constructor_StringReader ( )
45
48
{
46
49
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>" ;
47
50
XmlReader xrdr = new XmlTextReader ( new StringReader ( xmlStr ) ) ;
@@ -51,7 +54,7 @@ public void Constructor3()
51
54
}
52
55
53
56
[ Fact ]
54
- public void Constructor3_XmlReader_Empty ( )
57
+ public void Constructor_XmlReader_Empty ( )
55
58
{
56
59
XmlReaderSettings xs = new XmlReaderSettings ( ) ;
57
60
xs . ConformanceLevel = ConformanceLevel . Fragment ;
@@ -62,7 +65,7 @@ public void Constructor3_XmlReader_Empty()
62
65
}
63
66
64
67
[ Fact ]
65
- public void Constructor3_XmlReader_Null ( )
68
+ public void Constructor_XmlReader_Null ( )
66
69
{
67
70
SqlXml xmlSql = new SqlXml ( ( XmlReader ) null ) ;
68
71
Assert . True ( xmlSql . IsNull ) ;
@@ -84,7 +87,7 @@ public void CreateReader_Stream_Unicode()
84
87
}
85
88
86
89
[ Fact ]
87
- public void SqlXml_fromXmlReader_CreateReaderTest ( )
90
+ public void CreateReader_XmlTextReader_CanReadContent ( )
88
91
{
89
92
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>" ;
90
93
XmlReader rdr = new XmlTextReader ( new StringReader ( xmlStr ) ) ;
@@ -96,84 +99,82 @@ public void SqlXml_fromXmlReader_CreateReaderTest()
96
99
Assert . Equal ( xmlStr , xrdr . ReadOuterXml ( ) ) ;
97
100
}
98
101
99
- [ Theory ]
100
- [ InlineData ( "element_whitespace-text.xml" ) ]
101
- [ InlineData ( "root_qname.xml" ) ]
102
- [ InlineData ( "sample_ecommerce.xml" ) ]
103
- [ InlineData ( "sql_batch_request.xml" ) ]
104
- [ InlineData ( "sql_batch_response.xml" ) ]
105
- [ InlineData ( "sql_datatypes-1.xml" ) ]
106
- [ InlineData ( "sql_datatypes-2.xml" ) ]
107
- [ InlineData ( "sql_datatypes-3.xml" ) ]
108
- [ InlineData ( "xmlns-1.xml" ) ]
109
- [ InlineData ( "xmlns-2.xml" ) ]
110
- [ InlineData ( "xmlns-3.xml" ) ]
111
- [ InlineData ( "xmlns-4.xml" ) ]
112
- [ InlineData ( "comments_pis.xml" ) ]
113
- [ InlineData ( "element_content_growth.xml" ) ]
114
- [ InlineData ( "element_nested-1.xml" ) ]
115
- [ InlineData ( "element_nested-2.xml" ) ]
116
- [ InlineData ( "element_nested-3.xml" ) ]
117
- [ InlineData ( "element_single.xml" ) ]
118
- [ InlineData ( "element_stack_growth.xml" ) ]
119
- [ InlineData ( "element_tagname_growth.xml" ) ]
120
- [ InlineData ( "element_types.xml" ) ]
121
- [ InlineData ( "element_whitespace-modes.xml" ) ]
122
- public void SqlXml_fromXmlReader_TextXml ( string filename )
102
+ public static class CreateReader_TestFiles
123
103
{
124
- string filepath = Path . Combine ( "TestFiles/SqlXml/TextXml" , filename ) ;
125
-
126
- using FileStream xmlStream = new FileStream ( filepath , FileMode . Open ) ;
127
- SqlXml sqlXml = new SqlXml ( xmlStream ) ;
128
-
129
- // Reading XML stored as SQL Binary XML will result in using
130
- // the XmlTextReader implementation
131
- using XmlReader sqlXmlReader = sqlXml . CreateReader ( ) ;
132
-
133
- // Read to the end to verify no exceptions are thrown
134
- while ( sqlXmlReader . Read ( ) ) ;
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
+ }
135
154
}
136
155
137
156
[ Theory ]
138
- [ InlineData ( "element_whitespace-text.bmx" ) ]
139
- [ InlineData ( "root_qname.bmx" ) ]
140
- [ InlineData ( "sample_ecommerce.bmx" ) ]
141
- [ InlineData ( "sql_batch_request.bmx" ) ]
142
- [ InlineData ( "sql_batch_response.bmx" ) ]
143
- [ InlineData ( "sql_datatypes-1.bmx" ) ]
144
- [ InlineData ( "sql_datatypes-2.bmx" ) ]
145
- [ InlineData ( "sql_datatypes-3.bmx" ) ]
146
- [ InlineData ( "xmlns-1.bmx" ) ]
147
- [ InlineData ( "xmlns-2.bmx" ) ]
148
- [ InlineData ( "xmlns-3.bmx" ) ]
149
- [ InlineData ( "xmlns-4.bmx" ) ]
150
- [ InlineData ( "comments_pis.bmx" ) ]
151
- [ InlineData ( "element_content_growth.bmx" ) ]
152
- [ InlineData ( "element_nested-1.bmx" ) ]
153
- [ InlineData ( "element_nested-2.bmx" ) ]
154
- [ InlineData ( "element_nested-3.bmx" ) ]
155
- [ InlineData ( "element_single.bmx" ) ]
156
- [ InlineData ( "element_stack_growth.bmx" ) ]
157
- [ InlineData ( "element_tagname_growth.bmx" ) ]
158
- [ InlineData ( "element_types.bmx" ) ]
159
- [ InlineData ( "element_whitespace-modes.bmx" ) ]
160
- public void SqlXml_fromXmlReader_SqlBinaryXml ( string filename )
157
+ [ MemberData ( nameof ( CreateReader_TestFiles . FilesAndBaselines ) , MemberType = typeof ( CreateReader_TestFiles ) ) ]
158
+ public void CreateReader_TestAgainstBaseline ( string testFile , string baselineFile )
161
159
{
162
- string filepath = Path . Combine ( "TestFiles/SqlXml/SqlBinaryXml" , filename ) ;
160
+ // Get our expected output by using XmlReader directly
161
+ using XmlReader baselineReader = XmlReader . Create ( baselineFile ) ;
162
+ string expected = CreateReader_TestFiles . ReadAllXml ( baselineReader ) ;
163
163
164
- using FileStream xmlStream = new FileStream ( filepath , FileMode . Open ) ;
164
+ // Now produce the actual output through SqlXml.CreateReader
165
+ using FileStream xmlStream = new FileStream ( testFile , FileMode . Open ) ;
165
166
SqlXml sqlXml = new SqlXml ( xmlStream ) ;
166
167
167
- // Reading XML stored as SQL Binary XML will result in using
168
- // the XmlSqlBinaryReader implementation
169
- using XmlReader sqlXmlReader = sqlXml . CreateReader ( ) ;
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 ) ;
170
172
171
- // Read to the end to verify no exceptions are thrown
172
- while ( sqlXmlReader . Read ( ) ) ;
173
+ Assert . Equal ( expected , actual ) ;
173
174
}
174
175
175
176
[ Fact ]
176
- public void SqlXml_fromZeroLengthStream_CreateReaderTest ( )
177
+ public void SqlXml_FromZeroLengthStream_CreateReaderTest ( )
177
178
{
178
179
MemoryStream stream = new MemoryStream ( ) ;
179
180
SqlXml xmlSql = new SqlXml ( stream ) ;
@@ -184,7 +185,7 @@ public void SqlXml_fromZeroLengthStream_CreateReaderTest()
184
185
}
185
186
186
187
[ Fact ]
187
- public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest_withFragment ( )
188
+ public void SqlXml_FromZeroLengthXmlReader_CreateReaderTest_withFragment ( )
188
189
{
189
190
XmlReaderSettings xs = new XmlReaderSettings ( ) ;
190
191
xs . ConformanceLevel = ConformanceLevel . Fragment ;
@@ -198,7 +199,7 @@ public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest_withFragment()
198
199
}
199
200
200
201
[ Fact ]
201
- public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest ( )
202
+ public void SqlXml_FromZeroLengthXmlReader_CreateReaderTest ( )
202
203
{
203
204
XmlReader rdr = new XmlTextReader ( new StringReader ( string . Empty ) ) ;
204
205
0 commit comments