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