-
Notifications
You must be signed in to change notification settings - Fork 977
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit tests for exception serialization.
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
test/ICSharpCode.SharpZipLib.Tests/Serialization/SerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.Serialization; | ||
|
||
using NUnit.Framework; | ||
using ICSharpCode.SharpZipLib.BZip2; | ||
using ICSharpCode.SharpZipLib.Core; | ||
using ICSharpCode.SharpZipLib.GZip; | ||
using ICSharpCode.SharpZipLib.Lzw; | ||
using ICSharpCode.SharpZipLib.Tar; | ||
using ICSharpCode.SharpZipLib.Zip; | ||
|
||
namespace ICSharpCode.SharpZipLib.Tests.Serialization | ||
{ | ||
[TestFixture] | ||
public class SerializationTests | ||
{ | ||
/// <summary> | ||
/// Test that SharpZipLib Custom Exceptions can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Core")] | ||
[Category("Serialization")] | ||
[TestCase(typeof(BZip2Exception))] | ||
[TestCase(typeof(GZipException))] | ||
[TestCase(typeof(InvalidNameException))] | ||
[TestCase(typeof(LzwException))] | ||
[TestCase(typeof(SharpZipBaseException))] | ||
[TestCase(typeof(StreamDecodingException))] | ||
[TestCase(typeof(StreamUnsupportedException))] | ||
[TestCase(typeof(TarException))] | ||
[TestCase(typeof(UnexpectedEndOfStreamException))] | ||
[TestCase(typeof(ZipException))] | ||
public void SerializeException(Type exceptionType) | ||
{ | ||
string message = $"Serialized {exceptionType.Name}"; | ||
var exception = Activator.CreateInstance(exceptionType, message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception, exceptionType) as Exception; | ||
Assert.That(deserializedException, Is.InstanceOf(exceptionType), "deserialized object should have the correct type"); | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that ValueOutOfRangeException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Core")] | ||
[Category("Serialization")] | ||
public void SerializeValueOutOfRangeException() | ||
{ | ||
string message = "Serialized ValueOutOfRangeException"; | ||
var exception = new ValueOutOfRangeException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception, typeof(ValueOutOfRangeException)) as ValueOutOfRangeException; | ||
|
||
// ValueOutOfRangeException appends 'out of range' to the end of the message | ||
Assert.That(deserializedException.Message, Is.EqualTo($"{message} out of range"), "should have expected message"); | ||
} | ||
|
||
// Shared serialization helper | ||
// round trips the specified exception using DataContractSerializer | ||
private static object ExceptionSerialiseHelper(object exception, Type exceptionType) | ||
{ | ||
DataContractSerializer ser = new DataContractSerializer(exceptionType); | ||
|
||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
ser.WriteObject(memoryStream, exception); | ||
|
||
memoryStream.Seek(0, loc: SeekOrigin.Begin); | ||
|
||
return ser.ReadObject(memoryStream); | ||
} | ||
} | ||
} | ||
} | ||
|