3 using System.Collections.Generic;
4 using System.ComponentModel.DataAnnotations;
5 using System.Globalization;
7 using System.Reflection;
9 namespace MonoTests.System.ComponentModel.DataAnnotations
12 public class AttributeTargetValidation
14 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
15 public string WorksOnProperty { get; set; }
17 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
18 public string WorksOnMethod ()
23 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
24 public string worksOnField;
26 public string WorksOnParameter ([Display(ResourceType = typeof(GoodResources), Name = "NameKey")] string parameter1)
32 public class GoodResources
34 public static string Name {
35 get { return "NameValue"; }
37 public static string ShortName {
38 get { return "ShortNameValue"; }
40 public static string Prompt {
41 get { return "PromptValue"; }
43 public static string Description {
44 get { return "DescriptionValue"; }
46 public static string GroupName {
47 get { return "GroupNameValue"; }
51 public class BadResources
53 private static string PrivateString {
54 get { return "Not a public string"; }
56 public string InstanceString {
57 get { return "Not a static string"; }
59 public string WriteOnlyString {
63 internal class InvisibleResources
65 public static string InvisibleResource {
66 get { return "Not a visible string "; }
71 public class DisplayAttributeTests
73 const string property_not_set_message = "The {0} property has not been set. Use the Get{0} method to get the value.";
74 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}'.";
77 public void StringProperties_ReturnLiteralValues_Success()
79 var display = new DisplayAttribute()
82 ShortName = "ShortName",
84 Description = "Description",
85 GroupName = "GroupName"
88 Assert.AreEqual("Name", display.GetName());
89 Assert.AreEqual("ShortName", display.GetShortName());
90 Assert.AreEqual("Prompt", display.GetPrompt());
91 Assert.AreEqual("Description", display.GetDescription());
92 Assert.AreEqual("GroupName", display.GetGroupName());
95 public void StringProperties_ReturnLocalizedValues_Success()
97 var display = new DisplayAttribute()
99 ResourceType = typeof(GoodResources),
101 ShortName = "ShortName",
103 Description = "Description",
104 GroupName = "GroupName"
107 Assert.AreEqual(GoodResources.Name, display.GetName());
108 Assert.AreEqual(GoodResources.ShortName, display.GetShortName());
109 Assert.AreEqual(GoodResources.Prompt, display.GetPrompt());
110 Assert.AreEqual(GoodResources.Description, display.GetDescription());
111 Assert.AreEqual(GoodResources.GroupName, display.GetGroupName());
115 public void ShortName_ReturnsName_WhenNotSet()
117 var display = new DisplayAttribute()
122 Assert.AreEqual("Name", display.GetShortName());
126 public void OrderAndAutoGenerateProperties_Success()
128 var display = new DisplayAttribute()
131 AutoGenerateField = true,
132 AutoGenerateFilter = false
135 Assert.AreEqual(1, display.Order);
136 Assert.AreEqual(1, display.GetOrder());
138 Assert.AreEqual(true, display.AutoGenerateField);
139 Assert.AreEqual(true, display.GetAutoGenerateField());
141 Assert.AreEqual(false, display.AutoGenerateFilter);
142 Assert.AreEqual(false, display.GetAutoGenerateFilter());
146 public void StringProperties_GetUnSetProperties_ReturnsNull ()
148 var display = new DisplayAttribute ();
149 Assert.IsNull (display.Name);
150 Assert.IsNull (display.ShortName);
151 Assert.IsNull (display.Prompt);
152 Assert.IsNull (display.Description);
153 Assert.IsNull (display.GroupName);
155 Assert.IsNull (display.GetName ());
156 Assert.IsNull (display.GetShortName ());
157 Assert.IsNull (display.GetPrompt ());
158 Assert.IsNull (display.GetDescription ());
159 Assert.IsNull (display.GetGroupName ());
163 public void OrderAndAutoGeneratedProperties_GetUnSetProperties_ThrowsInvalidOperationException ()
165 var display = new DisplayAttribute();
167 ExceptionAssert.Throws<InvalidOperationException>(() => display.Order.ToString(), string.Format(property_not_set_message, "Order"));
168 ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateField.ToString(), string.Format(property_not_set_message, "AutoGenerateField"));
169 ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateFilter.ToString(), string.Format(property_not_set_message, "AutoGenerateFilter"));
173 public void AllProperties_InvisibleResource_ThrowsInvalidOperationException ()
175 var resourceType = typeof(InvisibleResources);
176 var resourceKey = "InvisibleResource";
177 var display = new DisplayAttribute()
179 ResourceType = resourceType,
181 ShortName = resourceKey,
182 Prompt = resourceKey,
183 Description = resourceKey,
184 GroupName = resourceKey
187 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
188 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
189 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
190 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
191 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
195 public void AllProperties_PrivateResource_ThrowsInvalidOperationException ()
197 var resourceType = typeof(BadResources);
198 var resourceKey = "InstanceString";
199 var display = new DisplayAttribute()
201 ResourceType = resourceType,
203 ShortName = resourceKey,
204 Prompt = resourceKey,
205 Description = resourceKey,
206 GroupName = resourceKey
209 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
210 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
211 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
212 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
213 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
217 public void AllProperties_InstanceResource_ThrowsInvalidOperationException ()
219 var resourceType = typeof(BadResources);
220 var resourceKey = "InstanceString";
221 var display = new DisplayAttribute()
223 ResourceType = resourceType,
225 ShortName = resourceKey,
226 Prompt = resourceKey,
227 Description = resourceKey,
228 GroupName = resourceKey
231 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
232 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
233 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
234 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
235 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
239 public void AllProperties_WriteOnlyResource_ThrowsInvalidOperationException ()
241 var resourceType = typeof(BadResources);
242 var resourceKey = "WriteOnlyString";
243 var display = new DisplayAttribute()
245 ResourceType = resourceType,
247 ShortName = resourceKey,
248 Prompt = resourceKey,
249 Description = resourceKey,
250 GroupName = resourceKey
253 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
254 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
255 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
256 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
257 ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
260 public static class ExceptionAssert
262 public static void Throws<TException> (Action action, string expectedMessage) where TException : Exception
268 catch (TException ex)
270 Assert.AreEqual (expectedMessage, ex.Message);