3 using System.Collections.Generic;
4 using System.ComponentModel.DataAnnotations;
5 using System.Globalization;
7 using System.Reflection;
9 namespace MonoTests.System.ComponentModel.DataAnnotations
11 public class AttributeTargetValidation
13 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
14 public string WorksOnProperty { get; set; }
16 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
17 public string WorksOnMethod ()
22 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
23 public string worksOnField;
25 public string WorksOnParameter ([Display(ResourceType = typeof(GoodResources), Name = "NameKey")] string parameter1)
31 public class GoodResources
33 public static string Name {
34 get { return "NameValue"; }
36 public static string ShortName {
37 get { return "ShortNameValue"; }
39 public static string Prompt {
40 get { return "PromptValue"; }
42 public static string Description {
43 get { return "DescriptionValue"; }
45 public static string GroupName {
46 get { return "GroupNameValue"; }
50 public class BadResources
52 private static string PrivateString {
53 get { return "Not a public string"; }
55 public string InstanceString {
56 get { return "Not a static string"; }
58 public string WriteOnlyString {
62 internal class InvisibleResources
64 public static string InvisibleResource {
65 get { return "Not a visible string "; }
70 public class DisplayAttributeTests
72 const string property_not_set_message = "The {0} property has not been set. Use the Get{0} method to get the value.";
73 const string localization_failed_message = "Cannot retrieve property '{0}' because localization failed. Type '{1}' is not public or does not contain a public static string property with the name '{2}'.";
76 public void StringProperties_ReturnLiteralValues_Success()
78 var display = new DisplayAttribute()
81 ShortName = "ShortName",
83 Description = "Description",
84 GroupName = "GroupName"
87 Assert.AreEqual("Name", display.GetName());
88 Assert.AreEqual("ShortName", display.GetShortName());
89 Assert.AreEqual("Prompt", display.GetPrompt());
90 Assert.AreEqual("Description", display.GetDescription());
91 Assert.AreEqual("GroupName", display.GetGroupName());
94 public void StringProperties_ReturnLocalizedValues_Success()
96 var display = new DisplayAttribute()
98 ResourceType = typeof(GoodResources),
100 ShortName = "ShortName",
102 Description = "Description",
103 GroupName = "GroupName"
106 Assert.AreEqual(GoodResources.Name, display.GetName());
107 Assert.AreEqual(GoodResources.ShortName, display.GetShortName());
108 Assert.AreEqual(GoodResources.Prompt, display.GetPrompt());
109 Assert.AreEqual(GoodResources.Description, display.GetDescription());
110 Assert.AreEqual(GoodResources.GroupName, display.GetGroupName());
114 public void ShortName_ReturnsName_WhenNotSet()
116 var display = new DisplayAttribute()
121 Assert.AreEqual("Name", display.GetShortName());
125 public void OrderAndAutoGenerateProperties_Success()
127 var display = new DisplayAttribute()
130 AutoGenerateField = true,
131 AutoGenerateFilter = false
134 Assert.AreEqual(1, display.Order);
135 Assert.AreEqual(1, display.GetOrder());
137 Assert.AreEqual(true, display.AutoGenerateField);
138 Assert.AreEqual(true, display.GetAutoGenerateField());
140 Assert.AreEqual(false, display.AutoGenerateFilter);
141 Assert.AreEqual(false, display.GetAutoGenerateFilter());
145 public void StringProperties_GetUnSetProperties_ReturnsNull ()
147 var display = new DisplayAttribute ();
148 Assert.IsNull (display.Name);
149 Assert.IsNull (display.ShortName);
150 Assert.IsNull (display.Prompt);
151 Assert.IsNull (display.Description);
152 Assert.IsNull (display.GroupName);
154 Assert.IsNull (display.GetName ());
155 Assert.IsNull (display.GetShortName ());
156 Assert.IsNull (display.GetPrompt ());
157 Assert.IsNull (display.GetDescription ());
158 Assert.IsNull (display.GetGroupName ());
162 public void OrderAndAutoGeneratedProperties_GetUnSetProperties_ThrowsInvalidOperationException ()
164 var display = new DisplayAttribute();
166 ExceptionAssert.Throws<InvalidOperationException>(() => display.Order.ToString(), string.Format(property_not_set_message, "Order"));
167 ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateField.ToString(), string.Format(property_not_set_message, "AutoGenerateField"));
168 ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateFilter.ToString(), string.Format(property_not_set_message, "AutoGenerateFilter"));
172 public void AllProperties_InvisibleResource_ThrowsInvalidOperationException ()
174 var resourceType = typeof(InvisibleResources);
175 var resourceKey = "InvisibleResource";
176 var display = new DisplayAttribute()
178 ResourceType = resourceType,
180 ShortName = resourceKey,
181 Prompt = resourceKey,
182 Description = resourceKey,
183 GroupName = resourceKey
186 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
187 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
188 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
189 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
190 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
194 public void AllProperties_PrivateResource_ThrowsInvalidOperationException ()
196 var resourceType = typeof(BadResources);
197 var resourceKey = "InstanceString";
198 var display = new DisplayAttribute()
200 ResourceType = resourceType,
202 ShortName = resourceKey,
203 Prompt = resourceKey,
204 Description = resourceKey,
205 GroupName = resourceKey
208 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
209 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
210 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
211 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
212 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
216 public void AllProperties_InstanceResource_ThrowsInvalidOperationException ()
218 var resourceType = typeof(BadResources);
219 var resourceKey = "InstanceString";
220 var display = new DisplayAttribute()
222 ResourceType = resourceType,
224 ShortName = resourceKey,
225 Prompt = resourceKey,
226 Description = resourceKey,
227 GroupName = resourceKey
230 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
231 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
232 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
233 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
234 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
238 public void AllProperties_WriteOnlyResource_ThrowsInvalidOperationException ()
240 var resourceType = typeof(BadResources);
241 var resourceKey = "WriteOnlyString";
242 var display = new DisplayAttribute()
244 ResourceType = resourceType,
246 ShortName = resourceKey,
247 Prompt = resourceKey,
248 Description = resourceKey,
249 GroupName = resourceKey
252 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
253 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
254 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
255 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
256 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
259 public static class ExceptionAssert
261 public static void Throws<TException> (Action action, string expectedMessage) where TException : Exception
267 catch (TException ex)
269 Assert.AreEqual (expectedMessage, ex.Message);