diff --git a/ClosedXML_Tests/TestHelper.cs b/ClosedXML_Tests/TestHelper.cs index 0655c5a..2e5feb6 100644 --- a/ClosedXML_Tests/TestHelper.cs +++ b/ClosedXML_Tests/TestHelper.cs @@ -31,7 +31,7 @@ private const bool CompareWithResources = true; - private static readonly ResourceFileExtractor _extractor = new ResourceFileExtractor(null, ".Resource."); + private static readonly ResourceFileExtractor _extractor = new ResourceFileExtractor(".Resource."); public static void SaveWorkbook(XLWorkbook workbook, params string[] fileNameParts) { @@ -80,7 +80,7 @@ if (CompareWithResources) { string resourcePath = "Examples." + filePartName.Replace('\\', '.').TrimStart('.'); - using (var streamExpected = _extractor.ReadFileFromResToStream(resourcePath)) + using (var streamExpected = _extractor.ReadFileFromResourceToStream(resourcePath)) using (var streamActual = File.OpenRead(filePath2)) { var success = ExcelDocsComparer.Compare(streamActual, streamExpected, out string message); @@ -116,7 +116,7 @@ if (CompareWithResources) { string resourcePath = referenceResource.Replace('\\', '.').TrimStart('.'); - using (var streamExpected = _extractor.ReadFileFromResToStream(resourcePath)) + using (var streamExpected = _extractor.ReadFileFromResourceToStream(resourcePath)) using (var streamActual = File.OpenRead(filePath2)) { var success = ExcelDocsComparer.Compare(streamActual, streamExpected, out string message); @@ -137,17 +137,21 @@ public static Stream GetStreamFromResource(string resourcePath) { - var extractor = new ResourceFileExtractor(null, ".Resource."); - return extractor.ReadFileFromResToStream(resourcePath); + return _extractor.ReadFileFromResourceToStream(resourcePath); } public static void LoadFile(string filePartName) { + IXLWorkbook wb; using (var stream = GetStreamFromResource(GetResourcePath(filePartName))) { - var wb = new XLWorkbook(stream); - wb.Dispose(); + Assert.DoesNotThrow(() => wb = new XLWorkbook(stream), "Unable to load resource {0}", filePartName); } } + + public static IEnumerable ListResourceFiles(Func predicate = null) + { + return _extractor.GetFileNames(predicate); + } } } diff --git a/ClosedXML_Tests/Utils/ResourceFileExtractor.cs b/ClosedXML_Tests/Utils/ResourceFileExtractor.cs index 3c9159e..ede7623 100644 --- a/ClosedXML_Tests/Utils/ResourceFileExtractor.cs +++ b/ClosedXML_Tests/Utils/ResourceFileExtractor.cs @@ -1,6 +1,6 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Reflection; @@ -15,8 +15,7 @@ #region Private fields - private static readonly Dictionary ms_defaultExtractors = - new Dictionary(); + private static readonly IDictionary extractors = new ConcurrentDictionary(); #endregion Private fields @@ -29,18 +28,14 @@ { Assembly _assembly = Assembly.GetCallingAssembly(); string _key = _assembly.GetName().FullName; - if (!ms_defaultExtractors.TryGetValue(_key, out ResourceFileExtractor _return)) + if (!extractors.TryGetValue(_key, out ResourceFileExtractor extractor) + && !extractors.TryGetValue(_key, out extractor)) { - lock (ms_defaultExtractors) - { - if (!ms_defaultExtractors.TryGetValue(_key, out _return)) - { - _return = new ResourceFileExtractor(_assembly, true, null); - ms_defaultExtractors.Add(_key, _return); - } - } + extractor = new ResourceFileExtractor(_assembly, true, null); + extractors.Add(_key, extractor); } - return _return; + + return extractor; } } @@ -52,10 +47,9 @@ private readonly Assembly m_assembly; private readonly ResourceFileExtractor m_baseExtractor; - private readonly string m_assemblyName; private bool m_isStatic; - private string m_resourceFilePath; + //private string ResourceFilePath { get; } #endregion Private fields @@ -69,7 +63,7 @@ public ResourceFileExtractor(string resourceFilePath, ResourceFileExtractor baseExtractor) : this(Assembly.GetCallingAssembly(), baseExtractor) { - m_resourceFilePath = resourceFilePath; + ResourceFilePath = resourceFilePath; } /// @@ -98,7 +92,7 @@ public ResourceFileExtractor(Assembly assembly, string resourcePath) : this(assembly ?? Assembly.GetCallingAssembly()) { - m_resourceFilePath = resourcePath; + ResourceFilePath = resourcePath; } /// @@ -139,18 +133,18 @@ { #region Check - if (ReferenceEquals(assembly, null)) + if (assembly is null) { throw new ArgumentNullException("assembly"); } #endregion Check - m_assembly = assembly; + Assembly = assembly; m_baseExtractor = baseExtractor; - m_assemblyName = Assembly.GetName().Name; + AssemblyName = Assembly.GetName().Name; IsStatic = isStatic; - m_resourceFilePath = ".Resources."; + ResourceFilePath = ".Resources."; } #endregion Constructors @@ -158,44 +152,26 @@ #region Public properties /// Work assembly - public Assembly Assembly - { - [DebuggerStepThrough] - get { return m_assembly; } - } + public Assembly Assembly { get; } /// Work assembly name - public string AssemblyName - { - [DebuggerStepThrough] - get { return m_assemblyName; } - } + public string AssemblyName { get; } /// /// Path to read resource files. Example: .Resources.Upgrades. /// - public string ResourceFilePath - { - [DebuggerStepThrough] - get { return m_resourceFilePath; } - [DebuggerStepThrough] - set { m_resourceFilePath = value; } - } + public string ResourceFilePath { get; } - public bool IsStatic - { - [DebuggerStepThrough] - get { return m_isStatic; } - [DebuggerStepThrough] - set { m_isStatic = value; } - } + public bool IsStatic { get; set; } - public IEnumerable GetFileNames() + public IEnumerable GetFileNames(Func predicate = null) { - string _path = AssemblyName + m_resourceFilePath; + predicate = predicate ?? (s => true); + + string _path = AssemblyName + ResourceFilePath; foreach (string _resourceName in Assembly.GetManifestResourceNames()) { - if (_resourceName.StartsWith(_path)) + if (_resourceName.StartsWith(_path) && predicate(_resourceName)) { yield return _resourceName.Replace(_path, string.Empty); } @@ -206,9 +182,9 @@ #region Public methods - public string ReadFileFromRes(string fileName) + public string ReadFileFromResource(string fileName) { - Stream _stream = ReadFileFromResToStream(fileName); + Stream _stream = ReadFileFromResourceToStream(fileName); string _result; StreamReader sr = new StreamReader(_stream); try @@ -222,9 +198,9 @@ return _result; } - public string ReadFileFromResFormat(string fileName, params object[] formatArgs) + public string ReadFileFromResourceFormat(string fileName, params object[] formatArgs) { - return string.Format(ReadFileFromRes(fileName), formatArgs); + return string.Format(ReadFileFromResource(fileName), formatArgs); } /// @@ -233,10 +209,10 @@ /// Specific path /// Read file name /// - public string ReadSpecificFileFromRes(string specificPath, string fileName) + public string ReadSpecificFileFromResource(string specificPath, string fileName) { ResourceFileExtractor _ext = new ResourceFileExtractor(Assembly, specificPath); - return _ext.ReadFileFromRes(fileName); + return _ext.ReadFileFromResource(fileName); } /// @@ -245,25 +221,25 @@ /// /// /// ApplicationException. - public Stream ReadFileFromResToStream(string fileName) + public Stream ReadFileFromResourceToStream(string fileName) { - string _nameResFile = AssemblyName + m_resourceFilePath + fileName; + string _nameResFile = AssemblyName + ResourceFilePath + fileName; Stream _stream = Assembly.GetManifestResourceStream(_nameResFile); #region Not found - if (ReferenceEquals(_stream, null)) + if (_stream is null) { #region Get from base extractor - if (!ReferenceEquals(m_baseExtractor, null)) + if (!(m_baseExtractor is null)) { - return m_baseExtractor.ReadFileFromResToStream(fileName); + return m_baseExtractor.ReadFileFromResourceToStream(fileName); } #endregion Get from base extractor - throw new ApplicationException("Can't find resource file " + _nameResFile); + throw new ArgumentException("Can't find resource file " + _nameResFile, nameof(fileName)); } #endregion Not found