From 5e4475a30c2df2b00bf60329541eaafbd2534912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Sat, 14 Oct 2017 14:04:39 +0200 Subject: [PATCH 1/4] Fix disposable pattern to allow to protect behavior when inherited (#39) --- .../ActiveDirectory/DirectoryEntryWrap.cs | 12 +++++++++++- .../ActiveDirectory/DirectorySearcherWrap.cs | 13 ++++++++++++- .../ActiveDirectory/PrincipalContextWrap.cs | 13 ++++++++++++- .../SearchResultCollectionWrap.cs | 12 +++++++++++- SystemWrapper/IO/BinaryReaderWrap.cs | 11 +++++++++++ SystemWrapper/IO/BinaryWriterWrap.cs | 10 ++++++++++ .../IO/Compression/DeflateStreamWrap.cs | 13 ++++++++++++- SystemWrapper/IO/Compression/ZipArchiveWrap.cs | 10 ++++++++++ SystemWrapper/IO/FileStreamWrap.cs | 10 ++++++++++ SystemWrapper/IO/MemoryStreamWrap.cs | 10 ++++++++++ SystemWrapper/IO/StreamReaderWrap.cs | 10 ++++++++++ SystemWrapper/IO/StreamWrap.cs | 10 ++++++++++ SystemWrapper/Microsoft.Win32/RegistryKeyWrap.cs | 11 +++++++++++ SystemWrapper/ServiceModel/ChannelWrap.cs | 3 +-- SystemWrapper/Xml/XmlWriterWrap.cs | 16 +++++++++++----- 15 files changed, 152 insertions(+), 12 deletions(-) diff --git a/SystemWrapper/ActiveDirectory/DirectoryEntryWrap.cs b/SystemWrapper/ActiveDirectory/DirectoryEntryWrap.cs index 7049255..e7e5e92 100644 --- a/SystemWrapper/ActiveDirectory/DirectoryEntryWrap.cs +++ b/SystemWrapper/ActiveDirectory/DirectoryEntryWrap.cs @@ -168,6 +168,16 @@ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { this.directoryEntry.Dispose(); } @@ -184,4 +194,4 @@ #endregion } -} \ No newline at end of file +} diff --git a/SystemWrapper/ActiveDirectory/DirectorySearcherWrap.cs b/SystemWrapper/ActiveDirectory/DirectorySearcherWrap.cs index 6d01a0d..2ded19a 100644 --- a/SystemWrapper/ActiveDirectory/DirectorySearcherWrap.cs +++ b/SystemWrapper/ActiveDirectory/DirectorySearcherWrap.cs @@ -1,5 +1,6 @@ namespace SystemWrapper.ActiveDirectory { + using System; using System.Collections.Specialized; using System.DirectoryServices; @@ -129,6 +130,16 @@ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { this.directorySearcher.Dispose(); } @@ -162,4 +173,4 @@ #endregion } -} \ No newline at end of file +} diff --git a/SystemWrapper/ActiveDirectory/PrincipalContextWrap.cs b/SystemWrapper/ActiveDirectory/PrincipalContextWrap.cs index 2fccd45..9c0efc4 100644 --- a/SystemWrapper/ActiveDirectory/PrincipalContextWrap.cs +++ b/SystemWrapper/ActiveDirectory/PrincipalContextWrap.cs @@ -1,5 +1,6 @@ namespace SystemWrapper.ActiveDirectory { + using System; using System.DirectoryServices.AccountManagement; using SystemWrapper.ActiveDirectory.Contracts; @@ -51,10 +52,20 @@ namespace SystemWrapper.ActiveDirectory /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { this.principalContext.Dispose(); } #endregion } -} \ No newline at end of file +} diff --git a/SystemWrapper/ActiveDirectory/SearchResultCollectionWrap.cs b/SystemWrapper/ActiveDirectory/SearchResultCollectionWrap.cs index 2f3d962..9e7b253 100644 --- a/SystemWrapper/ActiveDirectory/SearchResultCollectionWrap.cs +++ b/SystemWrapper/ActiveDirectory/SearchResultCollectionWrap.cs @@ -147,6 +147,16 @@ namespace SystemWrapper.ActiveDirectory /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { this.searchResultCollection.Dispose(); } @@ -191,4 +201,4 @@ namespace SystemWrapper.ActiveDirectory #endregion } -} \ No newline at end of file +} diff --git a/SystemWrapper/IO/BinaryReaderWrap.cs b/SystemWrapper/IO/BinaryReaderWrap.cs index 2221ad1..c849fef 100644 --- a/SystemWrapper/IO/BinaryReaderWrap.cs +++ b/SystemWrapper/IO/BinaryReaderWrap.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; @@ -258,6 +259,16 @@ namespace SystemWrapper.IO /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { BinaryReaderInstance.Close(); } diff --git a/SystemWrapper/IO/BinaryWriterWrap.cs b/SystemWrapper/IO/BinaryWriterWrap.cs index 24fc1a6..53220f1 100644 --- a/SystemWrapper/IO/BinaryWriterWrap.cs +++ b/SystemWrapper/IO/BinaryWriterWrap.cs @@ -247,6 +247,16 @@ namespace SystemWrapper.IO /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { BinaryWriterInstance.Close(); } diff --git a/SystemWrapper/IO/Compression/DeflateStreamWrap.cs b/SystemWrapper/IO/Compression/DeflateStreamWrap.cs index b890238..f501db2 100644 --- a/SystemWrapper/IO/Compression/DeflateStreamWrap.cs +++ b/SystemWrapper/IO/Compression/DeflateStreamWrap.cs @@ -1,4 +1,5 @@ -using System.IO.Compression; +using System; +using System.IO.Compression; using SystemInterface.IO; using SystemInterface.IO.Compression; @@ -75,6 +76,16 @@ namespace SystemWrapper.IO.Compression /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { DeflateStreamInstance.Dispose(); } diff --git a/SystemWrapper/IO/Compression/ZipArchiveWrap.cs b/SystemWrapper/IO/Compression/ZipArchiveWrap.cs index c3c8975..6f23f35 100644 --- a/SystemWrapper/IO/Compression/ZipArchiveWrap.cs +++ b/SystemWrapper/IO/Compression/ZipArchiveWrap.cs @@ -47,6 +47,16 @@ namespace SystemWrapper.IO.Compression } public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { Instance.Dispose(); } diff --git a/SystemWrapper/IO/FileStreamWrap.cs b/SystemWrapper/IO/FileStreamWrap.cs index b11c442..d8270d0 100644 --- a/SystemWrapper/IO/FileStreamWrap.cs +++ b/SystemWrapper/IO/FileStreamWrap.cs @@ -568,6 +568,16 @@ namespace SystemWrapper.IO /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { FileStreamInstance.Dispose(); } diff --git a/SystemWrapper/IO/MemoryStreamWrap.cs b/SystemWrapper/IO/MemoryStreamWrap.cs index c3284cd..460d721 100644 --- a/SystemWrapper/IO/MemoryStreamWrap.cs +++ b/SystemWrapper/IO/MemoryStreamWrap.cs @@ -406,6 +406,16 @@ namespace SystemWrapper.IO /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { MemoryStreamInstance.Dispose(); } diff --git a/SystemWrapper/IO/StreamReaderWrap.cs b/SystemWrapper/IO/StreamReaderWrap.cs index dd216b0..dbc7e9e 100644 --- a/SystemWrapper/IO/StreamReaderWrap.cs +++ b/SystemWrapper/IO/StreamReaderWrap.cs @@ -376,6 +376,16 @@ namespace SystemWrapper.IO /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { StreamReaderInstance.Dispose(); } diff --git a/SystemWrapper/IO/StreamWrap.cs b/SystemWrapper/IO/StreamWrap.cs index b463e94..4b270c5 100644 --- a/SystemWrapper/IO/StreamWrap.cs +++ b/SystemWrapper/IO/StreamWrap.cs @@ -212,6 +212,16 @@ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { this.StreamInstance.Dispose(); } diff --git a/SystemWrapper/Microsoft.Win32/RegistryKeyWrap.cs b/SystemWrapper/Microsoft.Win32/RegistryKeyWrap.cs index 2713752..1d0c6b4 100644 --- a/SystemWrapper/Microsoft.Win32/RegistryKeyWrap.cs +++ b/SystemWrapper/Microsoft.Win32/RegistryKeyWrap.cs @@ -1,3 +1,4 @@ +using System; using System.Security.AccessControl; using Microsoft.Win32; using SystemInterface.Microsoft.Win32; @@ -19,6 +20,16 @@ namespace SystemWrapper.Microsoft.Win32 /// Implementation of the Dispose. /// public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) { RegistryKeyInstance.Dispose(); } diff --git a/SystemWrapper/ServiceModel/ChannelWrap.cs b/SystemWrapper/ServiceModel/ChannelWrap.cs index b65c603..0991e05 100644 --- a/SystemWrapper/ServiceModel/ChannelWrap.cs +++ b/SystemWrapper/ServiceModel/ChannelWrap.cs @@ -2,8 +2,6 @@ { using System; using System.ServiceModel; - - using SystemInterface.Attributes; using SystemInterface.ServiceModel; /// @@ -69,6 +67,7 @@ public void Dispose() { this.Dispose(true); + GC.SuppressFinalize(true); } #endregion diff --git a/SystemWrapper/Xml/XmlWriterWrap.cs b/SystemWrapper/Xml/XmlWriterWrap.cs index e15852d..781783b 100644 --- a/SystemWrapper/Xml/XmlWriterWrap.cs +++ b/SystemWrapper/Xml/XmlWriterWrap.cs @@ -258,11 +258,17 @@ namespace SystemWrapper.Xml /// An method was called before a previous asynchronous operation finished. In this case, is thrown with the message “An asynchronous operation is already in progress.” public void Dispose() { - var disposable = this.instance as IDisposable; - if (disposable != null) - { - disposable.Dispose(); - } + Dispose(true); + GC.SuppressFinalize(true); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + /// Indicates whether or not unmanaged resources should be disposed. + protected virtual void Dispose(bool disposing) + { + this.instance.Dispose(); } /// From b744a62f1b287c6a1b6883ce43d04ca9b8a3dd65 Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Fri, 12 Jan 2018 21:17:17 +0100 Subject: [PATCH 2/4] Added link to NuGet badge (#43) --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 16faa5c..04e6471 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # SystemWrapper -![NuGet version](https://img.shields.io/nuget/v/SystemWrapper.Interfaces.svg) ![branch: master](http://img.shields.io/badge/branch-master-blue.svg?style=flat) [![Build status: master](https://ci.appveyor.com/api/projects/status/1126fol0d56a8my8/branch/master?svg=true)](https://ci.appveyor.com/project/jozefizso/systemwrapper/branch/master) ° ![branch: master](http://img.shields.io/badge/branch-develop-blue.svg?style=flat) [![Build status: develop](https://ci.appveyor.com/api/projects/status/1126fol0d56a8my8/branch/develop?svg=true)](https://ci.appveyor.com/project/jozefizso/systemwrapper/branch/develop) +[![NuGet version](https://img.shields.io/nuget/v/SystemWrapper.Interfaces.svg)](https://www.nuget.org/packages/SystemWrapper.Wrappers/) +![branch: master](http://img.shields.io/badge/branch-master-blue.svg?style=flat) [![Build status: master](https://ci.appveyor.com/api/projects/status/1126fol0d56a8my8/branch/master?svg=true)](https://ci.appveyor.com/project/jozefizso/systemwrapper/branch/master) ° ![branch: master](http://img.shields.io/badge/branch-develop-blue.svg?style=flat) [![Build status: develop](https://ci.appveyor.com/api/projects/status/1126fol0d56a8my8/branch/develop?svg=true)](https://ci.appveyor.com/project/jozefizso/systemwrapper/branch/develop) > **SystemWrapper** is .NET library for easier testing of system APIs. From abe93d53339da1a2eb83aec3a3f0161b42afdf32 Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Fri, 23 Feb 2018 17:55:29 +0100 Subject: [PATCH 3/4] Pass Encoding parameter to the ZipFile.Open method Fixes #47 --- .../IO/Compression/ZipFileWrapTests.cs | 37 +++++++++++++++--- .../SystemWrapper.Tests.csproj | 6 +++ .../TestData/Encoding_UTF8.zip | Bin 0 -> 188 bytes .../TestData/Encoding_Windows1250.zip | Bin 0 -> 134 bytes SystemWrapper/IO/Compression/ZipFileWrap.cs | 4 +- 5 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 SystemWrapper.Tests/TestData/Encoding_UTF8.zip create mode 100644 SystemWrapper.Tests/TestData/Encoding_Windows1250.zip diff --git a/SystemWrapper.Tests/IO/Compression/ZipFileWrapTests.cs b/SystemWrapper.Tests/IO/Compression/ZipFileWrapTests.cs index 3960759..ffc031f 100644 --- a/SystemWrapper.Tests/IO/Compression/ZipFileWrapTests.cs +++ b/SystemWrapper.Tests/IO/Compression/ZipFileWrapTests.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.IO; +using System.Linq; using System.Reflection; using System.Text; using System.Threading; @@ -135,14 +136,40 @@ namespace SystemWrapper.Tests.IO.Compression } [Test] - public void Open_WithEncoding_Creates_ZipArchiveWrap() + public void Open_WithUTF8Encoding_Creates_OpensZipWithCorrectEncoding() { + var assembly = Assembly.GetAssembly(typeof(ZipFileWrapTests)); + var testFilePath = assembly.CodeBase.Substring(8); //remove the "file://" from the front + testFilePath = Path.GetDirectoryName(testFilePath) + @"\TestData\Encoding_UTF8.zip"; + var instance = new ZipFileWrap(); - instance.CreateFromDirectory(ArchiveDirectory, ArchiveFileName); - using (var archive = instance.Open(ArchiveFileName, System.IO.Compression.ZipArchiveMode.Read, Encoding.UTF8)) + using (var archive = instance.Open(testFilePath, System.IO.Compression.ZipArchiveMode.Read, Encoding.UTF8)) { Assert.IsNotNull(archive); Assert.IsInstanceOf(archive); + + var entry = archive.Entries.First(); + Assert.AreEqual("text-ľščťžýáíé.txt", entry.Name); + } + } + + [Test] + public void Open_WithWindows1250Encoding_OpensZipWithCorrectEncoding() + { + var assembly = Assembly.GetAssembly(typeof(ZipFileWrapTests)); + var testFilePath = assembly.CodeBase.Substring(8); //remove the "file://" from the front + testFilePath = Path.GetDirectoryName(testFilePath) + @"\TestData\Encoding_Windows1250.zip"; + + var instance = new ZipFileWrap(); + + var encodingWindows1250 = Encoding.GetEncoding(1250); + using (var archive = instance.Open(testFilePath, System.IO.Compression.ZipArchiveMode.Read, encodingWindows1250)) + { + Assert.IsNotNull(archive); + Assert.IsInstanceOf(archive); + + var entry = archive.Entries.First(); + Assert.AreEqual("text-ľščťžýáíé.txt", entry.Name); } } @@ -158,4 +185,4 @@ namespace SystemWrapper.Tests.IO.Compression } } } -} \ No newline at end of file +} diff --git a/SystemWrapper.Tests/SystemWrapper.Tests.csproj b/SystemWrapper.Tests/SystemWrapper.Tests.csproj index 0d86362..1182f0a 100644 --- a/SystemWrapper.Tests/SystemWrapper.Tests.csproj +++ b/SystemWrapper.Tests/SystemWrapper.Tests.csproj @@ -118,6 +118,12 @@ + + PreserveNewest + + + PreserveNewest +