Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / DisplayAttributeTest.cs
1 using System;
2 using NUnit.Framework;
3 using System.Collections.Generic;
4 using System.ComponentModel.DataAnnotations;
5 using System.Globalization;
6 using System.Linq;
7 using System.Reflection;
8
9 namespace MonoTests.System.ComponentModel.DataAnnotations
10 {
11 #if NET_4_0
12         public class AttributeTargetValidation
13         {
14                 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
15                 public string WorksOnProperty { get; set; }
16
17                 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
18                 public string WorksOnMethod ()
19                 {
20                         return "";
21                 }
22
23                 [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
24                 public string worksOnField;
25
26                 public string WorksOnParameter ([Display(ResourceType = typeof(GoodResources), Name = "NameKey")] string parameter1)
27                 {
28                         return "";
29                 }
30         }
31         
32         public class GoodResources
33         {
34                 public static string Name {
35                         get { return "NameValue"; }
36                 }
37                 public static string ShortName {
38                         get { return "ShortNameValue"; }
39                 }
40                 public static string Prompt {
41                         get { return "PromptValue"; }
42                 }
43                 public static string Description {
44                         get { return "DescriptionValue"; }
45                 }
46                 public static string GroupName {
47                         get { return "GroupNameValue"; }
48                 }
49                 
50         }
51         public class BadResources
52         {
53                 private static string PrivateString {
54                         get { return "Not a public string"; }
55                 }
56                 public string InstanceString {
57                         get { return "Not a static string"; }
58                 }
59                 public string WriteOnlyString {
60                         set { }
61                 }
62         }
63         internal class InvisibleResources
64         {
65                 public static string InvisibleResource {
66                         get { return "Not a visible string "; }
67                 }
68         }
69         
70         [TestFixture]
71         public class DisplayAttributeTests
72         {
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}'.";
75         
76                 [Test]
77                 public void StringProperties_ReturnLiteralValues_Success()
78                 {
79                         var display = new DisplayAttribute()
80                         {
81                                 Name = "Name",
82                                 ShortName = "ShortName",
83                                 Prompt = "Prompt",
84                                 Description = "Description",
85                                 GroupName = "GroupName"
86                         };
87                         
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());
93                 }
94                 [Test]
95                 public void StringProperties_ReturnLocalizedValues_Success()
96                 {
97                         var display = new DisplayAttribute()
98                         {
99                                 ResourceType = typeof(GoodResources),
100                                 Name = "Name",
101                                 ShortName = "ShortName",
102                                 Prompt = "Prompt",
103                                 Description = "Description",
104                                 GroupName = "GroupName"
105                         };
106                         
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());
112                 }
113                 
114                 [Test]
115                 public void ShortName_ReturnsName_WhenNotSet()
116                 {
117                         var display = new DisplayAttribute()
118                         {
119                                 Name = "Name"
120                         };
121                         
122                         Assert.AreEqual("Name", display.GetShortName());
123                 }
124                 
125                 [Test]
126                 public void OrderAndAutoGenerateProperties_Success()
127                 {
128                         var display = new DisplayAttribute()
129                         {
130                                 Order = 1,
131                                 AutoGenerateField = true,
132                                 AutoGenerateFilter = false
133                         };
134                         
135                         Assert.AreEqual(1, display.Order);
136                         Assert.AreEqual(1, display.GetOrder());
137                         
138                         Assert.AreEqual(true, display.AutoGenerateField);
139                         Assert.AreEqual(true, display.GetAutoGenerateField());
140                         
141                         Assert.AreEqual(false, display.AutoGenerateFilter);
142                         Assert.AreEqual(false, display.GetAutoGenerateFilter());
143                 }
144                 
145                 [Test]
146                 public void StringProperties_GetUnSetProperties_ReturnsNull ()
147                 {
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);
154                         
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 ());
160                 }
161                 
162                 [Test]
163                 public void OrderAndAutoGeneratedProperties_GetUnSetProperties_ThrowsInvalidOperationException ()
164                 {
165                         var display = new DisplayAttribute();
166                         
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"));
170                 }
171                 
172                 [Test]
173                 public void AllProperties_InvisibleResource_ThrowsInvalidOperationException ()
174                 {
175                         var resourceType = typeof(InvisibleResources);
176                         var resourceKey = "InvisibleResource";
177                         var display = new DisplayAttribute()
178                         {
179                                 ResourceType = resourceType,
180                                 Name = resourceKey,
181                                 ShortName = resourceKey,
182                                 Prompt = resourceKey,
183                                 Description = resourceKey,
184                                 GroupName = resourceKey
185                         };
186                         
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));            
192                 }
193                 
194                 [Test]
195                 public void AllProperties_PrivateResource_ThrowsInvalidOperationException ()
196                 {
197                         var resourceType = typeof(BadResources);
198                         var resourceKey = "InstanceString";
199                         var display = new DisplayAttribute()
200                         {
201                                 ResourceType = resourceType,
202                                 Name = resourceKey,
203                                 ShortName = resourceKey,
204                                 Prompt = resourceKey,
205                                 Description = resourceKey,
206                                 GroupName = resourceKey
207                         };
208                         
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));            
214                 }
215                 
216                 [Test]
217                 public void AllProperties_InstanceResource_ThrowsInvalidOperationException ()
218                 {
219                         var resourceType = typeof(BadResources);
220                         var resourceKey = "InstanceString";
221                         var display = new DisplayAttribute()
222                         {
223                                 ResourceType = resourceType,
224                                 Name = resourceKey,
225                                 ShortName = resourceKey,
226                                 Prompt = resourceKey,
227                                 Description = resourceKey,
228                                 GroupName = resourceKey
229                         };
230                         
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));            
236                 }
237                 
238                 [Test]
239                 public void AllProperties_WriteOnlyResource_ThrowsInvalidOperationException ()
240                 {
241                         var resourceType = typeof(BadResources);
242                         var resourceKey = "WriteOnlyString";
243                         var display = new DisplayAttribute()
244                         {
245                                 ResourceType = resourceType,
246                                 Name = resourceKey,
247                                 ShortName = resourceKey,
248                                 Prompt = resourceKey,
249                                 Description = resourceKey,
250                                 GroupName = resourceKey
251                         };
252                         
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));            
258                 }
259         }
260         public static class ExceptionAssert
261         {
262                 public static void Throws<TException> (Action action, string expectedMessage) where TException : Exception
263                 {
264                         try
265                         {
266                                 action ();
267                         }
268                         catch (TException ex)
269                         {
270                                 Assert.AreEqual (expectedMessage, ex.Message);
271                                 return;
272                         }
273                         
274                         Assert.Fail();
275                 }
276         }
277 #endif
278 }