|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | +using Microsoft.Data.SqlClient.Server; |
| 8 | +using Microsoft.Data.SqlClient.UnitTests.UdtSerialization.SerializedTypes; |
| 9 | +using Microsoft.SqlServer.Server; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace Microsoft.Data.SqlClient.UnitTests.UdtSerialization; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Attempts to serialize types which do not meet the requirements for either user-defined or native serialization. |
| 16 | +/// </summary> |
| 17 | +public sealed class InvalidSerializationTest : IDisposable |
| 18 | +{ |
| 19 | + private readonly MemoryStream _stream; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes the MemoryStream used for all tests in this class. |
| 23 | + /// </summary> |
| 24 | + public InvalidSerializationTest() |
| 25 | + { |
| 26 | + _stream = new MemoryStream(); |
| 27 | + } |
| 28 | + |
| 29 | + void IDisposable.Dispose() |
| 30 | + { |
| 31 | + _stream.Dispose(); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Attempts to serialize a class that does not have the SqlUserDefinedType attribute. Verifies that this fails. |
| 36 | + /// </summary> |
| 37 | + [Fact] |
| 38 | + public void Serialize_MissingSqlUserDefinedTypeAttribute_Throws() |
| 39 | + { |
| 40 | + Action serialize = () => SerializationHelperSql9.Serialize(_stream, new ClassMissingSqlUserDefinedTypeAttribute()); |
| 41 | + var exception = Assert.Throws<InvalidUdtException>(serialize); |
| 42 | + |
| 43 | + Assert.Equal($"'{typeof(ClassMissingSqlUserDefinedTypeAttribute).FullName}' is an invalid user defined type, reason: no UDT attribute.", exception.Message); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Attempts to serialize a class that has a SqlUserDefinedType attribute, but specifies a Format enumeration value of |
| 48 | + /// Unknown. Verifies that this fails. |
| 49 | + /// </summary> |
| 50 | + [Fact] |
| 51 | + public void Serialize_UnknownFormattedType_Throws() |
| 52 | + { |
| 53 | + Action serialize = () => SerializationHelperSql9.Serialize(_stream, new UnknownFormattedClass()); |
| 54 | + var exception = Assert.Throws<ArgumentOutOfRangeException>("Format", serialize); |
| 55 | + |
| 56 | +#if NET |
| 57 | + Assert.Equal("The Format enumeration value, 0, is not supported by the format method. (Parameter 'Format')", exception.Message); |
| 58 | +#else |
| 59 | + Assert.Equal("The Format enumeration value, Unknown, is not supported by the format method.\r\nParameter name: Format", exception.Message); |
| 60 | +#endif |
| 61 | + } |
| 62 | +} |
0 commit comments