1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.ComponentModel;
6 using System.Configuration;
7 using Xunit;
8 
9 namespace System.ConfigurationTests
10 {
11     public class ConfigurationPropertyTests
12     {
13         [Fact]
ConfigurationSectionThrows()14         public void ConfigurationSectionThrows()
15         {
16             Assert.Throws<ConfigurationErrorsException>(() => new ConfigurationProperty("foo", typeof(ConfigurationSection)));
17         }
18 
19         [Fact]
AppSettingsSectionThrows()20         public void AppSettingsSectionThrows()
21         {
22             Assert.Throws<ConfigurationErrorsException>(() => new ConfigurationProperty("foo", typeof(AppSettingsSection)));
23         }
24 
25         [Fact]
NullNameThrows()26         public void NullNameThrows()
27         {
28             AssertExtensions.Throws<ArgumentException>("name", () => new ConfigurationProperty(null, typeof(string)));
29         }
30 
31         [Fact]
EmptyNameThrows()32         public void EmptyNameThrows()
33         {
34             AssertExtensions.Throws<ArgumentException>("name", () => new ConfigurationProperty("", typeof(string)));
35         }
36 
37         [Theory,
38             InlineData("lock"),
39             InlineData("locks"),
40             InlineData("config"),
41             InlineData("configuration")
42             ]
ReservedNameThrows(string name)43         public void ReservedNameThrows(string name)
44         {
45             AssertExtensions.Throws<ArgumentException>(null, () => new ConfigurationProperty(name, typeof(string)));
46         }
47 
48         [Theory,
49             InlineData("Lock"),
50             InlineData("ilock"),
51             InlineData("LOCKS"),
52             InlineData("CoNfig"),
53             InlineData("conFIGuration")
54             ]
ReservedNameOrdinallyCompared(string name)55         public void ReservedNameOrdinallyCompared(string name)
56         {
57             // Want to make sure the comparison is ordinal and starts with if people have depended on this
58             new ConfigurationProperty(name, typeof(string));
59         }
60 
61         // Base class returns false for CanValidate by default
62         public class CantValidateValidator : ConfigurationValidatorBase
63         {
Validate(object value)64             public override void Validate(object value)
65             {
66                 throw new NotImplementedException();
67             }
68         }
69 
70         [Fact]
NonMatchingValidatorThrows()71         public void NonMatchingValidatorThrows()
72         {
73             CantValidateValidator validator = new CantValidateValidator();
74             Assert.Throws<ConfigurationErrorsException>(() => new ConfigurationProperty("foo", typeof(string), null, null, validator, ConfigurationPropertyOptions.None));
75         }
76 
77         public class FooFailsValidator : ConfigurationValidatorBase
78         {
CanValidate(Type type)79             public override bool CanValidate(Type type)
80             {
81                 return true;
82             }
83 
Validate(object value)84             public override void Validate(object value)
85             {
86                 if (string.Equals(value, "foo"))
87                     throw new InvalidOperationException();
88             }
89         }
90 
91         [Fact]
BadDefaultValueThrows()92         public void BadDefaultValueThrows()
93         {
94             FooFailsValidator validator = new FooFailsValidator();
95             Action action = () => new ConfigurationProperty("bar", typeof(string), "foo", null, validator, ConfigurationPropertyOptions.None);
96             Assert.IsType<InvalidOperationException>(Assert.Throws<ConfigurationErrorsException>(action).InnerException);
97         }
98 
99         [TypeConverter(typeof(DummyCantConverter))]
100         public class SimpleConfigurationElement : ConfigurationElement
101         {
102         }
103 
104         // By default can't convert from or to
105         public class DummyCantConverter : TypeConverter
106         {
107         }
108 
109         public class DummyCanConverter : TypeConverter
110         {
CanConvertFrom(ITypeDescriptorContext context, Type sourceType)111             public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
112             {
113                 return sourceType == typeof(string);
114             }
115 
CanConvertTo(ITypeDescriptorContext context, Type destinationType)116             public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
117             {
118                 return destinationType == typeof(string);
119             }
120         }
121 
122         [Fact]
ConfigurationElementConverterIgnored()123         public void ConfigurationElementConverterIgnored()
124         {
125             ConfigurationProperty property = new ConfigurationProperty("foo", typeof(SimpleConfigurationElement));
126             Assert.Null(property.Converter);
127         }
128 
129         [TypeConverter(typeof(DummyCanConverter))]
130         public class MyConvertableClass
131         {
132         }
133 
134         [Fact]
TypeConverterRecognized()135         public void TypeConverterRecognized()
136         {
137             ConfigurationProperty property = new ConfigurationProperty("foo", typeof(MyConvertableClass));
138             Assert.IsType<DummyCanConverter>(property.Converter);
139         }
140 
141         [TypeConverter(typeof(DummyCantConverter))]
142         public class MyUnconvertableClass
143         {
144         }
145 
146         [Fact]
UnconvertableFailsOnConverterAccess()147         public void UnconvertableFailsOnConverterAccess()
148         {
149             ConfigurationProperty property = new ConfigurationProperty("foo", typeof(MyUnconvertableClass));
150             Assert.Throws<ConfigurationErrorsException>(() => property.Converter);
151         }
152 
153         [TypeConverter(typeof(DummyCantConverter))]
154         public enum AllSay
155         {
156             Yea,
157             Nay
158         }
159 
160         [Fact]
EnumsGetGenericEnumConverter()161         public void EnumsGetGenericEnumConverter()
162         {
163             ConfigurationProperty property = new ConfigurationProperty("foo", typeof(AllSay));
164             Assert.IsType<GenericEnumConverter>(property.Converter);
165         }
166 
167         [Theory,
168             InlineData(typeof(string), typeof(StringConverter)),
169             InlineData(typeof(int), typeof(Int32Converter))
170             ]
FindConverterForBuiltInTypes(Type type, Type converterType)171         public void FindConverterForBuiltInTypes(Type type, Type converterType)
172         {
173             ConfigurationProperty property = new ConfigurationProperty("foo", type);
174             Assert.IsType(converterType, property.Converter);
175         }
176 
177         [Fact]
IsRequiredExposed()178         public void IsRequiredExposed()
179         {
180             Assert.False(new ConfigurationProperty("foo", typeof(string)).IsRequired);
181             Assert.True(new ConfigurationProperty("foo", typeof(string), null, ConfigurationPropertyOptions.IsRequired).IsRequired);
182         }
183 
184         [Fact]
IsKeyExposed()185         public void IsKeyExposed()
186         {
187             Assert.False(new ConfigurationProperty("foo", typeof(string)).IsRequired);
188             Assert.True(new ConfigurationProperty("foo", typeof(string), null, ConfigurationPropertyOptions.IsKey).IsKey);
189         }
190 
191         [Fact]
IsDefaultCollectionExposed()192         public void IsDefaultCollectionExposed()
193         {
194             Assert.False(new ConfigurationProperty("foo", typeof(string)).IsDefaultCollection);
195             Assert.True(new ConfigurationProperty("foo", typeof(string), null, ConfigurationPropertyOptions.IsDefaultCollection).IsDefaultCollection);
196         }
197 
198         [Fact]
IsTypeStringTransformationRequiredExposed()199         public void IsTypeStringTransformationRequiredExposed()
200         {
201             Assert.False(new ConfigurationProperty("foo", typeof(string)).IsTypeStringTransformationRequired);
202             Assert.True(new ConfigurationProperty("foo", typeof(string), null, ConfigurationPropertyOptions.IsTypeStringTransformationRequired).IsTypeStringTransformationRequired);
203         }
204 
205         [Fact]
IsAssemblyStringTransformationRequiredExposed()206         public void IsAssemblyStringTransformationRequiredExposed()
207         {
208             Assert.False(new ConfigurationProperty("foo", typeof(string)).IsAssemblyStringTransformationRequired);
209             Assert.True(new ConfigurationProperty("foo", typeof(string), null, ConfigurationPropertyOptions.IsAssemblyStringTransformationRequired).IsAssemblyStringTransformationRequired);
210         }
211 
212         [Fact]
IsVersionCheckRequiredExposed()213         public void IsVersionCheckRequiredExposed()
214         {
215             Assert.False(new ConfigurationProperty("foo", typeof(string)).IsVersionCheckRequired);
216             Assert.True(new ConfigurationProperty("foo", typeof(string), null, ConfigurationPropertyOptions.IsVersionCheckRequired).IsVersionCheckRequired);
217         }
218 
219         [Fact]
TypeIsExposed()220         public void TypeIsExposed()
221         {
222             Assert.Equal(typeof(string), new ConfigurationProperty("foo", typeof(string)).Type);
223         }
224 
225         [Fact]
DefaultValueIsExposed()226         public void DefaultValueIsExposed()
227         {
228             Assert.Equal("bar", new ConfigurationProperty("foo", typeof(string), "bar").DefaultValue);
229         }
230     }
231 }
232