diff --git a/src/SharpCompress/Compressors/LZMA/ICoder.cs b/src/SharpCompress/Compressors/LZMA/ICoder.cs index 95134979..22648533 100644 --- a/src/SharpCompress/Compressors/LZMA/ICoder.cs +++ b/src/SharpCompress/Compressors/LZMA/ICoder.cs @@ -1,12 +1,13 @@ using System; using System.IO; +using SharpCompress.Common; namespace SharpCompress.Compressors.LZMA; /// /// The exception that is thrown when an error in input stream occurs during decoding. /// -internal class DataErrorException : Exception +internal class DataErrorException : SharpCompressException { public DataErrorException() : base("Data Error") { } @@ -15,7 +16,7 @@ internal class DataErrorException : Exception /// /// The exception that is thrown when the value of an argument is outside the allowable range. /// -internal class InvalidParamException : Exception +internal class InvalidParamException : SharpCompressException { public InvalidParamException() : base("Invalid Parameter") { } diff --git a/src/SharpCompress/Compressors/Xz/XZIndexMarkerReachedException.cs b/src/SharpCompress/Compressors/Xz/XZIndexMarkerReachedException.cs index bb006a35..f7fe0428 100644 --- a/src/SharpCompress/Compressors/Xz/XZIndexMarkerReachedException.cs +++ b/src/SharpCompress/Compressors/Xz/XZIndexMarkerReachedException.cs @@ -1,5 +1,5 @@ -using System; +using SharpCompress.Common; namespace SharpCompress.Compressors.Xz; -public class XZIndexMarkerReachedException : Exception { } +public class XZIndexMarkerReachedException : SharpCompressException { } diff --git a/tests/SharpCompress.Test/ExceptionHierarchyTests.cs b/tests/SharpCompress.Test/ExceptionHierarchyTests.cs new file mode 100644 index 00000000..b543d179 --- /dev/null +++ b/tests/SharpCompress.Test/ExceptionHierarchyTests.cs @@ -0,0 +1,116 @@ +using System; +using SharpCompress.Common; +using SharpCompress.Compressors.Deflate; +using SharpCompress.Compressors.LZMA; +using SharpCompress.Compressors.Xz; +using Xunit; + +namespace SharpCompress.Test; + +public class ExceptionHierarchyTests +{ + [Fact] + public void AllSharpCompressExceptions_InheritFromSharpCompressException() + { + // Verify that ArchiveException inherits from SharpCompressException + Assert.True(typeof(SharpCompressException).IsAssignableFrom(typeof(ArchiveException))); + + // Verify that ExtractionException inherits from SharpCompressException + Assert.True(typeof(SharpCompressException).IsAssignableFrom(typeof(ExtractionException))); + + // Verify that InvalidFormatException inherits from SharpCompressException (through ExtractionException) + Assert.True( + typeof(SharpCompressException).IsAssignableFrom(typeof(InvalidFormatException)) + ); + + // Verify that CryptographicException inherits from SharpCompressException + Assert.True( + typeof(SharpCompressException).IsAssignableFrom(typeof(CryptographicException)) + ); + + // Verify that IncompleteArchiveException inherits from SharpCompressException (through ArchiveException) + Assert.True( + typeof(SharpCompressException).IsAssignableFrom(typeof(IncompleteArchiveException)) + ); + + // Verify that ReaderCancelledException inherits from SharpCompressException + Assert.True( + typeof(SharpCompressException).IsAssignableFrom(typeof(ReaderCancelledException)) + ); + + // Verify that MultipartStreamRequiredException inherits from SharpCompressException (through ExtractionException) + Assert.True( + typeof(SharpCompressException).IsAssignableFrom( + typeof(MultipartStreamRequiredException) + ) + ); + + // Verify that MultiVolumeExtractionException inherits from SharpCompressException (through ExtractionException) + Assert.True( + typeof(SharpCompressException).IsAssignableFrom(typeof(MultiVolumeExtractionException)) + ); + + // Verify that ZlibException inherits from SharpCompressException + Assert.True(typeof(SharpCompressException).IsAssignableFrom(typeof(ZlibException))); + + // Verify that XZIndexMarkerReachedException inherits from SharpCompressException + Assert.True( + typeof(SharpCompressException).IsAssignableFrom(typeof(XZIndexMarkerReachedException)) + ); + } + + [Fact] + public void SharpCompressException_CanBeCaughtByBaseType() + { + // Test that a derived exception can be caught as SharpCompressException + var exception = new InvalidFormatException("Test message"); + var caughtException = false; + + try + { + throw exception; + } + catch (SharpCompressException ex) + { + caughtException = true; + Assert.Same(exception, ex); + } + + Assert.True(caughtException, "Exception should have been caught as SharpCompressException"); + } + + [Fact] + public void InternalLzmaExceptions_InheritFromSharpCompressException() + { + // Use reflection to verify internal exception types + var dataErrorExceptionType = Type.GetType( + "SharpCompress.Compressors.LZMA.DataErrorException, SharpCompress" + ); + Assert.NotNull(dataErrorExceptionType); + Assert.True(typeof(SharpCompressException).IsAssignableFrom(dataErrorExceptionType)); + + var invalidParamExceptionType = Type.GetType( + "SharpCompress.Compressors.LZMA.InvalidParamException, SharpCompress" + ); + Assert.NotNull(invalidParamExceptionType); + Assert.True(typeof(SharpCompressException).IsAssignableFrom(invalidParamExceptionType)); + } + + [Fact] + public void ExceptionConstructors_WorkCorrectly() + { + // Test parameterless constructor + var ex1 = new SharpCompressException(); + Assert.NotNull(ex1); + + // Test message constructor + var ex2 = new SharpCompressException("Test message"); + Assert.Equal("Test message", ex2.Message); + + // Test message and inner exception constructor + var inner = new InvalidOperationException("Inner"); + var ex3 = new SharpCompressException("Test message", inner); + Assert.Equal("Test message", ex3.Message); + Assert.Same(inner, ex3.InnerException); + } +}