|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.Serialization; |
| 4 | + |
| 5 | +using NUnit.Framework; |
| 6 | +using ICSharpCode.SharpZipLib.BZip2; |
| 7 | +using ICSharpCode.SharpZipLib.Core; |
| 8 | +using ICSharpCode.SharpZipLib.GZip; |
| 9 | +using ICSharpCode.SharpZipLib.Lzw; |
| 10 | +using ICSharpCode.SharpZipLib.Tar; |
| 11 | +using ICSharpCode.SharpZipLib.Zip; |
| 12 | + |
| 13 | +namespace ICSharpCode.SharpZipLib.Tests.Serialization |
| 14 | +{ |
| 15 | + [TestFixture] |
| 16 | + public class SerializationTests |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Test that SharpZipBaseException can be serialized. |
| 20 | + /// </summary> |
| 21 | + [Test] |
| 22 | + [Category("Core")] |
| 23 | + [Category("Serialization")] |
| 24 | + public void SerializeSharpZipBaseException() |
| 25 | + { |
| 26 | + string message = "Serialized SharpZipBaseException"; |
| 27 | + var exception = new SharpZipBaseException(message); |
| 28 | + |
| 29 | + var deserializedException = ExceptionSerialiseHelper(exception) as SharpZipBaseException; |
| 30 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Test that InvalidNameException can be serialized. |
| 35 | + /// </summary> |
| 36 | + [Test] |
| 37 | + [Category("Core")] |
| 38 | + [Category("Serialization")] |
| 39 | + public void SerializeInvalidNameException() |
| 40 | + { |
| 41 | + string message = "Serialized InvalidNameException"; |
| 42 | + var exception = new InvalidNameException(message); |
| 43 | + |
| 44 | + var deserializedException = ExceptionSerialiseHelper(exception) as InvalidNameException; |
| 45 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Test that StreamDecodingException can be serialized. |
| 50 | + /// </summary> |
| 51 | + [Test] |
| 52 | + [Category("Core")] |
| 53 | + [Category("Serialization")] |
| 54 | + public void SerializeStreamDecodingException() |
| 55 | + { |
| 56 | + string message = "Serialized StreamDecodingException"; |
| 57 | + var exception = new StreamDecodingException(message); |
| 58 | + |
| 59 | + var deserializedException = ExceptionSerialiseHelper(exception) as StreamDecodingException; |
| 60 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Test that UnexpectedEndOfStreamException can be serialized. |
| 65 | + /// </summary> |
| 66 | + [Test] |
| 67 | + [Category("Core")] |
| 68 | + [Category("Serialization")] |
| 69 | + public void SerializeUnexpectedEndOfStreamException() |
| 70 | + { |
| 71 | + string message = "Serialized UnexpectedEndOfStreamException"; |
| 72 | + var exception = new UnexpectedEndOfStreamException(message); |
| 73 | + |
| 74 | + var deserializedException = ExceptionSerialiseHelper(exception) as UnexpectedEndOfStreamException; |
| 75 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Test that ValueOutOfRangeException can be serialized. |
| 80 | + /// </summary> |
| 81 | + [Test] |
| 82 | + [Category("Core")] |
| 83 | + [Category("Serialization")] |
| 84 | + public void SerializeValueOutOfRangeException() |
| 85 | + { |
| 86 | + string message = "Serialized ValueOutOfRangeException"; |
| 87 | + var exception = new ValueOutOfRangeException(message); |
| 88 | + |
| 89 | + var deserializedException = ExceptionSerialiseHelper(exception) as ValueOutOfRangeException; |
| 90 | + |
| 91 | + // ValueOutOfRangeException appends 'out of range' to the end of the message |
| 92 | + Assert.That(deserializedException.Message, Is.EqualTo($"{message} out of range"), "should have expected message"); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Test that StreamUnsupportedException can be serialized. |
| 97 | + /// </summary> |
| 98 | + [Test] |
| 99 | + [Category("Core")] |
| 100 | + [Category("Serialization")] |
| 101 | + public void SerializeStreamUnsupportedException() |
| 102 | + { |
| 103 | + string message = "Serialized StreamUnsupportedException"; |
| 104 | + var exception = new StreamUnsupportedException(message); |
| 105 | + |
| 106 | + var deserializedException = ExceptionSerialiseHelper(exception) as StreamUnsupportedException; |
| 107 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Test that BZip2Exception can be serialized. |
| 112 | + /// </summary> |
| 113 | + [Test] |
| 114 | + [Category("BZip2")] |
| 115 | + [Category("Serialization")] |
| 116 | + public void SerializeBZip2Exception() |
| 117 | + { |
| 118 | + string message = "Serialized BZip2Exception"; |
| 119 | + var exception = new BZip2Exception(message); |
| 120 | + |
| 121 | + var deserializedException = ExceptionSerialiseHelper(exception) as BZip2Exception; |
| 122 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Test that GZipException can be serialized. |
| 127 | + /// </summary> |
| 128 | + [Test] |
| 129 | + [Category("GZip")] |
| 130 | + [Category("Serialization")] |
| 131 | + public void SerializeGZipException() |
| 132 | + { |
| 133 | + string message = "Serialized GZipException"; |
| 134 | + var exception = new GZipException(message); |
| 135 | + |
| 136 | + var deserializedException = ExceptionSerialiseHelper(exception) as GZipException; |
| 137 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Test that LzwException can be serialized. |
| 142 | + /// </summary> |
| 143 | + [Test] |
| 144 | + [Category("LZW")] |
| 145 | + [Category("Serialization")] |
| 146 | + public void SerializeLzwException() |
| 147 | + { |
| 148 | + string message = "Serialized LzwException"; |
| 149 | + var exception = new LzwException(message); |
| 150 | + |
| 151 | + var deserializedException = ExceptionSerialiseHelper(exception) as LzwException; |
| 152 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Test that TarException can be serialized. |
| 157 | + /// </summary> |
| 158 | + [Test] |
| 159 | + [Category("LZW")] |
| 160 | + [Category("Serialization")] |
| 161 | + public void SerializeTarException() |
| 162 | + { |
| 163 | + string message = "Serialized TarException"; |
| 164 | + var exception = new TarException(message); |
| 165 | + |
| 166 | + var deserializedException = ExceptionSerialiseHelper(exception) as TarException; |
| 167 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 168 | + } |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// Test that ZipException can be serialized. |
| 172 | + /// </summary> |
| 173 | + [Test] |
| 174 | + [Category("Zip")] |
| 175 | + [Category("Serialization")] |
| 176 | + public void SerializeZipException() |
| 177 | + { |
| 178 | + string message = "Serialized ZipException"; |
| 179 | + var exception = new ZipException(message); |
| 180 | + |
| 181 | + var deserializedException = ExceptionSerialiseHelper(exception) as ZipException; |
| 182 | + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); |
| 183 | + } |
| 184 | + |
| 185 | + // Shared serialization helper |
| 186 | + // round trips the specified exception using DataContractSerializer |
| 187 | + private static object ExceptionSerialiseHelper<TExceptionType>(TExceptionType exception) |
| 188 | + { |
| 189 | + DataContractSerializer ser = new DataContractSerializer(typeof(TExceptionType)); |
| 190 | + |
| 191 | + using (var memoryStream = new MemoryStream()) |
| 192 | + { |
| 193 | + ser.WriteObject(memoryStream, exception); |
| 194 | + |
| 195 | + memoryStream.Seek(0, loc: SeekOrigin.Begin); |
| 196 | + |
| 197 | + return ser.ReadObject(memoryStream); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
0 commit comments