DisplayAttributeTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. namespace MonoTests.System.ComponentModel.DataAnnotations
  9. {
  10. public class AttributeTargetValidation
  11. {
  12. [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
  13. public string WorksOnProperty { get; set; }
  14. [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
  15. public string WorksOnMethod ()
  16. {
  17. return "";
  18. }
  19. [Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
  20. public string worksOnField;
  21. public string WorksOnParameter ([Display(ResourceType = typeof(GoodResources), Name = "NameKey")] string parameter1)
  22. {
  23. return "";
  24. }
  25. }
  26. public class GoodResources
  27. {
  28. public static string Name {
  29. get { return "NameValue"; }
  30. }
  31. public static string ShortName {
  32. get { return "ShortNameValue"; }
  33. }
  34. public static string Prompt {
  35. get { return "PromptValue"; }
  36. }
  37. public static string Description {
  38. get { return "DescriptionValue"; }
  39. }
  40. public static string GroupName {
  41. get { return "GroupNameValue"; }
  42. }
  43. }
  44. public class BadResources
  45. {
  46. private static string PrivateString {
  47. get { return "Not a public string"; }
  48. }
  49. public string InstanceString {
  50. get { return "Not a static string"; }
  51. }
  52. public string WriteOnlyString {
  53. set { }
  54. }
  55. }
  56. internal class InvisibleResources
  57. {
  58. public static string InvisibleResource {
  59. get { return "Not a visible string "; }
  60. }
  61. }
  62. [TestFixture]
  63. public class DisplayAttributeTests
  64. {
  65. const string property_not_set_message = "The {0} property has not been set. Use the Get{0} method to get the value.";
  66. 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}'.";
  67. [Test]
  68. public void StringProperties_ReturnLiteralValues_Success()
  69. {
  70. var display = new DisplayAttribute()
  71. {
  72. Name = "Name",
  73. ShortName = "ShortName",
  74. Prompt = "Prompt",
  75. Description = "Description",
  76. GroupName = "GroupName"
  77. };
  78. Assert.AreEqual("Name", display.GetName());
  79. Assert.AreEqual("ShortName", display.GetShortName());
  80. Assert.AreEqual("Prompt", display.GetPrompt());
  81. Assert.AreEqual("Description", display.GetDescription());
  82. Assert.AreEqual("GroupName", display.GetGroupName());
  83. }
  84. [Test]
  85. public void StringProperties_ReturnLocalizedValues_Success()
  86. {
  87. var display = new DisplayAttribute()
  88. {
  89. ResourceType = typeof(GoodResources),
  90. Name = "Name",
  91. ShortName = "ShortName",
  92. Prompt = "Prompt",
  93. Description = "Description",
  94. GroupName = "GroupName"
  95. };
  96. Assert.AreEqual(GoodResources.Name, display.GetName());
  97. Assert.AreEqual(GoodResources.ShortName, display.GetShortName());
  98. Assert.AreEqual(GoodResources.Prompt, display.GetPrompt());
  99. Assert.AreEqual(GoodResources.Description, display.GetDescription());
  100. Assert.AreEqual(GoodResources.GroupName, display.GetGroupName());
  101. }
  102. [Test]
  103. public void ShortName_ReturnsName_WhenNotSet()
  104. {
  105. var display = new DisplayAttribute()
  106. {
  107. Name = "Name"
  108. };
  109. Assert.AreEqual("Name", display.GetShortName());
  110. }
  111. [Test]
  112. public void OrderAndAutoGenerateProperties_Success()
  113. {
  114. var display = new DisplayAttribute()
  115. {
  116. Order = 1,
  117. AutoGenerateField = true,
  118. AutoGenerateFilter = false
  119. };
  120. Assert.AreEqual(1, display.Order);
  121. Assert.AreEqual(1, display.GetOrder());
  122. Assert.AreEqual(true, display.AutoGenerateField);
  123. Assert.AreEqual(true, display.GetAutoGenerateField());
  124. Assert.AreEqual(false, display.AutoGenerateFilter);
  125. Assert.AreEqual(false, display.GetAutoGenerateFilter());
  126. }
  127. [Test]
  128. public void StringProperties_GetUnSetProperties_ReturnsNull ()
  129. {
  130. var display = new DisplayAttribute ();
  131. Assert.IsNull (display.Name);
  132. Assert.IsNull (display.ShortName);
  133. Assert.IsNull (display.Prompt);
  134. Assert.IsNull (display.Description);
  135. Assert.IsNull (display.GroupName);
  136. Assert.IsNull (display.GetName ());
  137. Assert.IsNull (display.GetShortName ());
  138. Assert.IsNull (display.GetPrompt ());
  139. Assert.IsNull (display.GetDescription ());
  140. Assert.IsNull (display.GetGroupName ());
  141. }
  142. [Test]
  143. public void OrderAndAutoGeneratedProperties_GetUnSetProperties_ThrowsInvalidOperationException ()
  144. {
  145. var display = new DisplayAttribute();
  146. ExceptionAssert.Throws<InvalidOperationException>(() => display.Order.ToString(), string.Format(property_not_set_message, "Order"));
  147. ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateField.ToString(), string.Format(property_not_set_message, "AutoGenerateField"));
  148. ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateFilter.ToString(), string.Format(property_not_set_message, "AutoGenerateFilter"));
  149. }
  150. [Test]
  151. public void AllProperties_InvisibleResource_ThrowsInvalidOperationException ()
  152. {
  153. var resourceType = typeof(InvisibleResources);
  154. var resourceKey = "InvisibleResource";
  155. var display = new DisplayAttribute()
  156. {
  157. ResourceType = resourceType,
  158. Name = resourceKey,
  159. ShortName = resourceKey,
  160. Prompt = resourceKey,
  161. Description = resourceKey,
  162. GroupName = resourceKey
  163. };
  164. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
  165. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
  166. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
  167. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
  168. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
  169. }
  170. [Test]
  171. public void AllProperties_PrivateResource_ThrowsInvalidOperationException ()
  172. {
  173. var resourceType = typeof(BadResources);
  174. var resourceKey = "InstanceString";
  175. var display = new DisplayAttribute()
  176. {
  177. ResourceType = resourceType,
  178. Name = resourceKey,
  179. ShortName = resourceKey,
  180. Prompt = resourceKey,
  181. Description = resourceKey,
  182. GroupName = resourceKey
  183. };
  184. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
  185. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
  186. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
  187. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
  188. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
  189. }
  190. [Test]
  191. public void AllProperties_InstanceResource_ThrowsInvalidOperationException ()
  192. {
  193. var resourceType = typeof(BadResources);
  194. var resourceKey = "InstanceString";
  195. var display = new DisplayAttribute()
  196. {
  197. ResourceType = resourceType,
  198. Name = resourceKey,
  199. ShortName = resourceKey,
  200. Prompt = resourceKey,
  201. Description = resourceKey,
  202. GroupName = resourceKey
  203. };
  204. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
  205. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
  206. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
  207. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
  208. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
  209. }
  210. [Test]
  211. public void AllProperties_WriteOnlyResource_ThrowsInvalidOperationException ()
  212. {
  213. var resourceType = typeof(BadResources);
  214. var resourceKey = "WriteOnlyString";
  215. var display = new DisplayAttribute()
  216. {
  217. ResourceType = resourceType,
  218. Name = resourceKey,
  219. ShortName = resourceKey,
  220. Prompt = resourceKey,
  221. Description = resourceKey,
  222. GroupName = resourceKey
  223. };
  224. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
  225. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
  226. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
  227. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
  228. ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
  229. }
  230. }
  231. public static class ExceptionAssert
  232. {
  233. public static void Throws<TException> (Action action, string expectedMessage) where TException : Exception
  234. {
  235. try
  236. {
  237. action ();
  238. }
  239. catch (TException ex)
  240. {
  241. Assert.AreEqual (expectedMessage, ex.Message);
  242. return;
  243. }
  244. Assert.Fail();
  245. }
  246. }
  247. }