@@ -16,8 +16,23 @@ namespace Microsoft.Data.SqlClient.UnitTests.UdtSerialization;
16
16
/// Tests the serialization method defined by MS-SSCLRT. Ensures that combinations of primitives and custom types round-trip.
17
17
/// </summary>
18
18
/// <seealso href="https://learn.microsoft.com/en-us/openspecs/sql_server_protocols/ms-ssclrt/77460aa9-8c2f-4449-a65e-1d649ebd77fa"/>
19
- public class NativeSerializationTest
19
+ public sealed class NativeSerializationTest : IDisposable
20
20
{
21
+ private readonly MemoryStream _stream ;
22
+
23
+ /// <summary>
24
+ /// Initializes the MemoryStream used for all tests in this class.
25
+ /// </summary>
26
+ public NativeSerializationTest ( )
27
+ {
28
+ _stream = new MemoryStream ( ) ;
29
+ }
30
+
31
+ void IDisposable . Dispose ( )
32
+ {
33
+ _stream . Dispose ( ) ;
34
+ }
35
+
21
36
/// <summary>
22
37
/// Provides a collection of test data representing non-null primitive type values and their corresponding
23
38
/// serialized byte arrays.
@@ -206,9 +221,8 @@ public void CanSerializeTopLevelClass()
206
221
Field1 = true ,
207
222
Field2 = new BoolWrapperStruct ( ) { Field1 = true }
208
223
} ;
209
- using MemoryStream stream = new ( ) ;
210
224
211
- SerializationHelperSql9 . Serialize ( stream , validWrapper ) ;
225
+ SerializationHelperSql9 . Serialize ( _stream , validWrapper ) ;
212
226
}
213
227
214
228
/// <summary>
@@ -224,9 +238,8 @@ public void CannotSerializeNestedClass()
224
238
Field1 = true ,
225
239
Field2 = new BoolWrapperClass ( ) { Field1 = true }
226
240
} ;
227
- using MemoryStream stream = new ( ) ;
228
241
229
- var ex = Assert . Throws < Exception > ( ( ) => SerializationHelperSql9 . Serialize ( stream , invalidWrapper ) ) ;
242
+ var ex = Assert . Throws < Exception > ( ( ) => SerializationHelperSql9 . Serialize ( _stream , invalidWrapper ) ) ;
230
243
string expectedException = StringsHelper . GetString ( Strings . SQL_CannotCreateNormalizer , invalidWrapper . Field2 . GetType ( ) . FullName ) ;
231
244
232
245
Assert . Equal ( expectedException , ex . Message ) ;
@@ -244,9 +257,8 @@ public void CannotSerializeNonPrimitiveType()
244
257
Field1 = 1 ,
245
258
Field2 = IntPtr . Zero
246
259
} ;
247
- using MemoryStream stream = new ( ) ;
248
260
249
- var ex = Assert . Throws < Exception > ( ( ) => SerializationHelperSql9 . Serialize ( stream , invalidWrapper ) ) ;
261
+ var ex = Assert . Throws < Exception > ( ( ) => SerializationHelperSql9 . Serialize ( _stream , invalidWrapper ) ) ;
250
262
string expectedException = StringsHelper . GetString ( Strings . SQL_CannotCreateNormalizer , invalidWrapper . Field2 . GetType ( ) . FullName ) ;
251
263
252
264
Assert . Equal ( expectedException , ex . Message ) ;
@@ -257,23 +269,22 @@ public void CannotSerializeNonPrimitiveType()
257
269
/// </summary>
258
270
/// <param name="inputValue">Object to serialize.</param>
259
271
/// <param name="expectedValue">Expected serialization output.</param>
260
- private static void RoundtripType ( object inputValue , byte [ ] expectedValue )
272
+ private void RoundtripType ( object inputValue , byte [ ] expectedValue )
261
273
{
262
- using MemoryStream stream = new ( ) ;
263
274
int typeSize = SerializationHelperSql9 . SizeInBytes ( inputValue . GetType ( ) ) ;
264
275
int objectSize = SerializationHelperSql9 . SizeInBytes ( inputValue ) ;
265
276
int maxTypeSize = SerializationHelperSql9 . GetUdtMaxLength ( inputValue . GetType ( ) ) ;
266
277
267
- SerializationHelperSql9 . Serialize ( stream , inputValue ) ;
268
- stream . Seek ( 0 , SeekOrigin . Begin ) ;
269
- object readPrimitive = SerializationHelperSql9 . Deserialize ( stream , inputValue . GetType ( ) ) ;
278
+ SerializationHelperSql9 . Serialize ( _stream , inputValue ) ;
279
+ _stream . Seek ( 0 , SeekOrigin . Begin ) ;
280
+ object readPrimitive = SerializationHelperSql9 . Deserialize ( _stream , inputValue . GetType ( ) ) ;
270
281
271
282
// For native formatting, the type size, the object size and the maximum object size will always be identical
272
283
Assert . Equal ( typeSize , objectSize ) ;
273
284
Assert . Equal ( expectedValue . Length , typeSize ) ;
274
285
Assert . Equal ( typeSize , maxTypeSize ) ;
275
286
276
- Assert . Equal ( expectedValue , stream . ToArray ( ) ) ;
287
+ Assert . Equal ( expectedValue , _stream . ToArray ( ) ) ;
277
288
Assert . Equal ( inputValue , readPrimitive ) ;
278
289
}
279
290
}
0 commit comments