-
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
202 additions
and
0 deletions.
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
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,202 @@ | ||
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 SharpZipBaseException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Core")] | ||
[Category("Serialization")] | ||
public void SerializeSharpZipBaseException() | ||
{ | ||
string message = "Serialized SharpZipBaseException"; | ||
var exception = new SharpZipBaseException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as SharpZipBaseException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that InvalidNameException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Core")] | ||
[Category("Serialization")] | ||
public void SerializeInvalidNameException() | ||
{ | ||
string message = "Serialized InvalidNameException"; | ||
var exception = new InvalidNameException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as InvalidNameException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that StreamDecodingException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Core")] | ||
[Category("Serialization")] | ||
public void SerializeStreamDecodingException() | ||
{ | ||
string message = "Serialized StreamDecodingException"; | ||
var exception = new StreamDecodingException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as StreamDecodingException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that UnexpectedEndOfStreamException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Core")] | ||
[Category("Serialization")] | ||
public void SerializeUnexpectedEndOfStreamException() | ||
{ | ||
string message = "Serialized UnexpectedEndOfStreamException"; | ||
var exception = new UnexpectedEndOfStreamException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as UnexpectedEndOfStreamException; | ||
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) 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"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that StreamUnsupportedException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Core")] | ||
[Category("Serialization")] | ||
public void SerializeStreamUnsupportedException() | ||
{ | ||
string message = "Serialized StreamUnsupportedException"; | ||
var exception = new StreamUnsupportedException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as StreamUnsupportedException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that BZip2Exception can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("BZip2")] | ||
[Category("Serialization")] | ||
public void SerializeBZip2Exception() | ||
{ | ||
string message = "Serialized BZip2Exception"; | ||
var exception = new BZip2Exception(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as BZip2Exception; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that GZipException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("GZip")] | ||
[Category("Serialization")] | ||
public void SerializeGZipException() | ||
{ | ||
string message = "Serialized GZipException"; | ||
var exception = new GZipException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as GZipException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that LzwException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("LZW")] | ||
[Category("Serialization")] | ||
public void SerializeLzwException() | ||
{ | ||
string message = "Serialized LzwException"; | ||
var exception = new LzwException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as LzwException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that TarException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("LZW")] | ||
[Category("Serialization")] | ||
public void SerializeTarException() | ||
{ | ||
string message = "Serialized TarException"; | ||
var exception = new TarException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as TarException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
/// <summary> | ||
/// Test that ZipException can be serialized. | ||
/// </summary> | ||
[Test] | ||
[Category("Zip")] | ||
[Category("Serialization")] | ||
public void SerializeZipException() | ||
{ | ||
string message = "Serialized ZipException"; | ||
var exception = new ZipException(message); | ||
|
||
var deserializedException = ExceptionSerialiseHelper(exception) as ZipException; | ||
Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); | ||
} | ||
|
||
// Shared serialization helper | ||
// round trips the specified exception using DataContractSerializer | ||
private static object ExceptionSerialiseHelper<TExceptionType>(TExceptionType exception) | ||
{ | ||
DataContractSerializer ser = new DataContractSerializer(typeof(TExceptionType)); | ||
|
||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
ser.WriteObject(memoryStream, exception); | ||
|
||
memoryStream.Seek(0, loc: SeekOrigin.Begin); | ||
|
||
return ser.ReadObject(memoryStream); | ||
} | ||
} | ||
} | ||
} | ||
|