|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.IO;
|
| 6 | +using System.Linq; |
6 | 7 | using System.Security.Cryptography;
|
7 | 8 | using System.Security.Cryptography.Xml;
|
8 | 9 | using System.Tests;
|
9 | 10 | using System.Xml;
|
10 | 11 | using Microsoft.Extensions.Configuration.Test;
|
| 12 | +using Microsoft.Extensions.FileProviders; |
11 | 13 | using Xunit;
|
12 | 14 |
|
13 | 15 | namespace Microsoft.Extensions.Configuration.Xml.Test
|
@@ -779,5 +781,64 @@ public void LoadKeyValuePairsFromValidEncryptedXml()
|
779 | 781 | Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("data.setting:inventory:connectionstring"));
|
780 | 782 | Assert.Equal("MySql", xmlConfigSrc.Get("Data.setting:Inventory:Provider"));
|
781 | 783 | }
|
| 784 | + |
| 785 | + [Theory] |
| 786 | + [InlineData(true)] |
| 787 | + [InlineData(false)] |
| 788 | + public void AddXmlFile_FileProvider_Gets_Disposed_When_It_Was_Not_Created_By_The_User(bool disposeConfigRoot) |
| 789 | + { |
| 790 | + string filePath = Path.Combine(Path.GetTempPath(), $"{nameof(AddXmlFile_FileProvider_Gets_Disposed_When_It_Was_Not_Created_By_The_User)}.xml"); |
| 791 | + File.WriteAllText(filePath, @"<settings><My><Nice>Settings</Nice></My></settings>"); |
| 792 | + |
| 793 | + IConfigurationRoot config = new ConfigurationBuilder().AddXmlFile(filePath, optional: false).Build(); |
| 794 | + XmlConfigurationProvider xmlConfigurationProvider = config.Providers.OfType<XmlConfigurationProvider>().Single(); |
| 795 | + |
| 796 | + Assert.NotNull(xmlConfigurationProvider.Source.FileProvider); |
| 797 | + PhysicalFileProvider fileProvider = (PhysicalFileProvider)xmlConfigurationProvider.Source.FileProvider; |
| 798 | + Assert.False(GetIsDisposed(fileProvider)); |
| 799 | + |
| 800 | + if (disposeConfigRoot) |
| 801 | + { |
| 802 | + (config as IDisposable).Dispose(); // disposing ConfigurationRoot |
| 803 | + } |
| 804 | + else |
| 805 | + { |
| 806 | + xmlConfigurationProvider.Dispose(); // disposing XmlConfigurationProvider |
| 807 | + } |
| 808 | + |
| 809 | + Assert.True(GetIsDisposed(fileProvider)); |
| 810 | + } |
| 811 | + |
| 812 | + [Fact] |
| 813 | + public void AddXmlFile_FileProvider_Is_Not_Disposed_When_It_Is_Owned_By_The_User() |
| 814 | + { |
| 815 | + string filePath = Path.Combine(Path.GetTempPath(), $"{nameof(AddXmlFile_FileProvider_Is_Not_Disposed_When_It_Is_Owned_By_The_User)}.xml"); |
| 816 | + File.WriteAllText(filePath, @"<settings><My><Nice>Settings</Nice></My></settings>"); |
| 817 | + |
| 818 | + PhysicalFileProvider fileProvider = new(Path.GetDirectoryName(filePath)); |
| 819 | + XmlConfigurationProvider configurationProvider = new(new XmlConfigurationSource() |
| 820 | + { |
| 821 | + Path = filePath, |
| 822 | + FileProvider = fileProvider |
| 823 | + }); |
| 824 | + IConfigurationRoot config = new ConfigurationBuilder().AddXmlFile(configurationProvider.Source.FileProvider, filePath, optional: true, reloadOnChange: false).Build(); |
| 825 | + |
| 826 | + Assert.False(GetIsDisposed(fileProvider)); |
| 827 | + |
| 828 | + (config as IDisposable).Dispose(); // disposing ConfigurationRoot that does not own the provider |
| 829 | + Assert.False(GetIsDisposed(fileProvider)); |
| 830 | + |
| 831 | + configurationProvider.Dispose(); // disposing XmlConfigurationProvider |
| 832 | + Assert.False(GetIsDisposed(fileProvider)); |
| 833 | + |
| 834 | + fileProvider.Dispose(); // disposing PhysicalFileProvider itself |
| 835 | + Assert.True(GetIsDisposed(fileProvider)); |
| 836 | + } |
| 837 | + |
| 838 | + private static bool GetIsDisposed(PhysicalFileProvider fileProvider) |
| 839 | + { |
| 840 | + System.Reflection.FieldInfo isDisposedField = typeof(PhysicalFileProvider).GetField("_disposed", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); |
| 841 | + return (bool)isDisposedField.GetValue(fileProvider); |
| 842 | + } |
782 | 843 | }
|
783 | 844 | }
|
0 commit comments