PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.ComponentModel.DataAnnotations/Test/System.ComponentModel.DataAnnotations/ValidationAttributeTest.cs

https://bitbucket.org/danipen/mono
C# | 709 lines | 541 code | 89 blank | 79 comment | 5 complexity | 7bdf545482a1a1984fc7b54816ce6c2b MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // ValidationAttributeTest.cs
  3. //
  4. // Authors:
  5. // Marek Habersack <mhabersack@novell.com>
  6. //
  7. // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.ComponentModel.DataAnnotations;
  31. using System.Text;
  32. using NUnit.Framework;
  33. using MonoTests.Common;
  34. namespace MonoTests.System.ComponentModel.DataAnnotations
  35. {
  36. [TestFixture]
  37. public class ValidationAttributeTest
  38. {
  39. const string TEST_ERROR_MESSAGE = "Test Error Message";
  40. string ErrorMessageAccessor ()
  41. {
  42. return TEST_ERROR_MESSAGE;
  43. }
  44. [Test]
  45. public void Constructor ()
  46. {
  47. var attr = new ValidateFooAttribute ();
  48. Assert.IsNull (attr.ErrorMessage, "#A1");
  49. Assert.IsNull (attr.ErrorMessageResourceName, "#A2");
  50. Assert.IsNull (attr.ErrorMessageResourceType, "#A3");
  51. Assert.IsNotNull (attr.GetErrorMessageString (), "#A4");
  52. }
  53. [Test]
  54. public void Constructor_Func ()
  55. {
  56. var attr = new ValidateFooAttribute (ErrorMessageAccessor);
  57. Assert.IsNull (attr.ErrorMessage, "#A1");
  58. Assert.IsNull (attr.ErrorMessageResourceName, "#A2");
  59. Assert.IsNull (attr.ErrorMessageResourceType, "#A3");
  60. Assert.IsNotNull (attr.GetErrorMessageString (), "#A4");
  61. Assert.AreEqual (TEST_ERROR_MESSAGE, attr.GetErrorMessageString (), "#A4");
  62. }
  63. [Test]
  64. public void Constructor_String ()
  65. {
  66. var attr = new ValidateFooAttribute ("Another Test Error Message");
  67. Assert.IsNull (attr.ErrorMessage, "#A1");
  68. Assert.IsNull (attr.ErrorMessageResourceName, "#A2");
  69. Assert.IsNull (attr.ErrorMessageResourceType, "#A3");
  70. Assert.IsNotNull (attr.GetErrorMessageString (), "#A4");
  71. Assert.IsNotNull (attr.GetErrorMessageString (), "#A4-1");
  72. Assert.AreEqual ("Another Test Error Message", attr.GetErrorMessageString (), "#A4-2");
  73. }
  74. [Test]
  75. public void ErrorMessage ()
  76. {
  77. var attr = new ValidateFooAttribute ();
  78. Assert.IsNull (attr.ErrorMessage, "#A1");
  79. attr.ErrorMessage = "Test";
  80. Assert.AreEqual ("Test", attr.ErrorMessage, "#A2");
  81. #if NET_4_0
  82. attr.ErrorMessage = String.Empty;
  83. Assert.AreEqual (String.Empty, attr.ErrorMessage, "#A3");
  84. attr.ErrorMessage = null;
  85. Assert.IsNull (attr.ErrorMessage, "#A4");
  86. #else
  87. try {
  88. attr.ErrorMessage = String.Empty;
  89. Assert.Fail ("#A3");
  90. } catch (InvalidOperationException) {
  91. // success
  92. }
  93. attr = new ValidateFooAttribute ("Test");
  94. try {
  95. attr.ErrorMessage = null;
  96. Assert.Fail ("#A4");
  97. } catch (ArgumentException) {
  98. // success
  99. }
  100. attr = new ValidateFooAttribute ("Test");
  101. try {
  102. attr.ErrorMessage = String.Empty;
  103. Assert.Fail ("#A4");
  104. } catch (ArgumentException) {
  105. // success
  106. }
  107. attr = new ValidateFooAttribute ();
  108. attr.ErrorMessageResourceName = "ErrorProperty1";
  109. try {
  110. attr.ErrorMessage = "Test Message";
  111. Assert.Fail ("#E1");
  112. } catch (InvalidOperationException) {
  113. // success
  114. }
  115. #endif
  116. }
  117. [Test]
  118. public void ErrorMessageResourceName ()
  119. {
  120. var attr = new ValidateFooAttribute ();
  121. Assert.IsNull (attr.ErrorMessageResourceName, "#A1");
  122. attr.ErrorMessageResourceName = "Test";
  123. Assert.IsNotNull (attr.ErrorMessageResourceName, "#A2-1");
  124. Assert.AreEqual ("Test", attr.ErrorMessageResourceName, "#A2-2");
  125. #if NET_4_0
  126. attr.ErrorMessageResourceName = String.Empty;
  127. Assert.IsNotNull (attr.ErrorMessageResourceName, "#A3-1");
  128. Assert.AreEqual (String.Empty, attr.ErrorMessageResourceName, "#A3-2");
  129. attr.ErrorMessageResourceName = null;
  130. Assert.IsNull (attr.ErrorMessageResourceName, "#A3-1");
  131. #else
  132. try {
  133. attr.ErrorMessageResourceName = String.Empty;
  134. Assert.Fail ("#A3-1");
  135. } catch (InvalidOperationException) {
  136. // success
  137. }
  138. attr = new ValidateFooAttribute ("Test");
  139. try {
  140. attr.ErrorMessageResourceName = String.Empty;
  141. Assert.Fail ("#A3-2");
  142. } catch (ArgumentException) {
  143. // success
  144. }
  145. attr = new ValidateFooAttribute ("Test");
  146. try {
  147. attr.ErrorMessageResourceName = null;
  148. Assert.Fail ("#A3-3");
  149. } catch (ArgumentException) {
  150. // success
  151. }
  152. attr = new ValidateFooAttribute ();
  153. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  154. try {
  155. attr.ErrorMessageResourceName = "NoSuchProperty";
  156. Assert.Fail ("#A3-4");
  157. } catch (InvalidOperationException) {
  158. // success
  159. }
  160. #endif
  161. }
  162. [Test]
  163. public void ErrorMessageResourceType ()
  164. {
  165. var attr = new ValidateFooAttribute ();
  166. Assert.IsNull (attr.ErrorMessageResourceType, "#A1");
  167. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  168. Assert.IsNotNull (attr.ErrorMessageResourceType, "#A2-1");
  169. Assert.AreEqual (typeof (FooErrorMessageProvider), attr.ErrorMessageResourceType, "#A2-2");
  170. #if !NET_4_0
  171. attr = new ValidateFooAttribute ();
  172. attr.ErrorMessageResourceName = "NoSuchProperty";
  173. try {
  174. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  175. Assert.Fail ("#A3");
  176. } catch (InvalidOperationException) {
  177. // success
  178. }
  179. #endif
  180. }
  181. [Test]
  182. public void ErrorMessageString ()
  183. {
  184. var attr = new ValidateFooAttribute ();
  185. Assert.IsNotNull (attr.GetErrorMessageString (), "#A1-1");
  186. Assert.IsTrue (attr.GetErrorMessageString ().Length > 0, "#A1-2");
  187. attr = new ValidateFooAttribute ();
  188. attr.ErrorMessageResourceName = "TestResource";
  189. try {
  190. attr.GetErrorMessageString ();
  191. Assert.Fail ("#A2-1");
  192. } catch (InvalidOperationException) {
  193. // success
  194. }
  195. #if NET_4_0
  196. attr = new ValidateFooAttribute ();
  197. attr.ErrorMessageResourceName = String.Empty;
  198. try {
  199. attr.GetErrorMessageString ();
  200. Assert.Fail ("#A2-1");
  201. } catch (InvalidOperationException) {
  202. // success
  203. }
  204. attr = new ValidateFooAttribute ();
  205. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  206. attr.ErrorMessageResourceName = null;
  207. try {
  208. attr.GetErrorMessageString ();
  209. Assert.Fail ("#A3-1");
  210. } catch (InvalidOperationException) {
  211. // success
  212. }
  213. attr = new ValidateFooAttribute ();
  214. attr.ErrorMessageResourceName = String.Empty;
  215. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  216. try {
  217. string s = attr.GetErrorMessageString ();
  218. Assert.Fail ("#A3-2");
  219. } catch (InvalidOperationException) {
  220. // success
  221. }
  222. attr = new ValidateFooAttribute ();
  223. attr.ErrorMessageResourceName = "NoSuchProperty";
  224. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  225. try {
  226. attr.GetErrorMessageString ();
  227. Assert.Fail ("#A4");
  228. } catch (InvalidOperationException) {
  229. // success
  230. }
  231. attr = new ValidateFooAttribute ();
  232. attr.ErrorMessageResourceName = "ErrorProperty2";
  233. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  234. try {
  235. attr.GetErrorMessageString ();
  236. Assert.Fail ("#A5");
  237. } catch (InvalidOperationException) {
  238. // success
  239. }
  240. attr = new ValidateFooAttribute ();
  241. attr.ErrorMessageResourceName = "ErrorProperty3";
  242. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  243. try {
  244. attr.GetErrorMessageString ();
  245. Assert.Fail ("#A5");
  246. } catch (InvalidOperationException) {
  247. // success
  248. }
  249. attr = new ValidateFooAttribute ();
  250. attr.ErrorMessageResourceName = "ErrorProperty4";
  251. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  252. try {
  253. attr.GetErrorMessageString ();
  254. Assert.Fail ("#A6");
  255. } catch (InvalidOperationException) {
  256. // success
  257. }
  258. attr = new ValidateFooAttribute ();
  259. attr.ErrorMessageResourceName = "ErrorProperty5";
  260. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  261. try {
  262. attr.GetErrorMessageString ();
  263. Assert.Fail ("#A7");
  264. } catch (InvalidOperationException) {
  265. // success
  266. }
  267. attr = new ValidateFooAttribute ();
  268. attr.ErrorMessageResourceName = "ErrorField1";
  269. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  270. try {
  271. attr.GetErrorMessageString ();
  272. Assert.Fail ("#B1");
  273. } catch (InvalidOperationException) {
  274. // success
  275. }
  276. attr = new ValidateFooAttribute ();
  277. attr.ErrorMessageResourceName = "ErrorField2";
  278. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  279. try {
  280. attr.GetErrorMessageString ();
  281. Assert.Fail ("#B2");
  282. } catch (InvalidOperationException) {
  283. // success
  284. }
  285. #endif
  286. attr = new ValidateFooAttribute ();
  287. attr.ErrorMessageResourceName = "ErrorProperty1";
  288. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  289. Assert.IsNotNull (attr.GetErrorMessageString (), "#C1-1");
  290. Assert.AreEqual ("Error Message 1", attr.GetErrorMessageString (), "#C1-2");
  291. attr = new ValidateFooAttribute (ErrorMessageAccessor);
  292. Assert.IsNotNull (attr.GetErrorMessageString (), "#D1-1");
  293. Assert.AreEqual (TEST_ERROR_MESSAGE, attr.GetErrorMessageString (), "#D1-2");
  294. attr = new ValidateFooAttribute ();
  295. attr.ErrorMessageResourceName = "ErrorProperty1";
  296. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  297. Assert.IsNotNull (attr.GetErrorMessageString (), "#D1-3");
  298. Assert.AreEqual ("Error Message 1", attr.GetErrorMessageString (), "#D1-4");
  299. #if NET_4_0
  300. attr.ErrorMessage = "Test Message";
  301. try {
  302. attr.GetErrorMessageString ();
  303. Assert.Fail ("#E1");
  304. } catch (InvalidOperationException) {
  305. // success
  306. }
  307. #endif
  308. }
  309. [Test]
  310. public void FormatErrorMessage ()
  311. {
  312. var attr = new ValidateFooAttribute ();
  313. Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#A1-1");
  314. Assert.AreEqual ("The field SomeField is invalid.", attr.FormatErrorMessage ("SomeField"), "#A1-2");
  315. attr.ErrorMessage = "Test: {0}";
  316. Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#A2-1");
  317. Assert.AreEqual ("Test: SomeField", attr.FormatErrorMessage ("SomeField"), "#A2-2");
  318. #if !NET_4_0
  319. attr = new ValidateFooAttribute ();
  320. #else
  321. attr.ErrorMessage = null;
  322. #endif
  323. attr.ErrorMessageResourceName = "ErrorProperty1";
  324. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  325. Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B1-1");
  326. Assert.AreEqual ("Error Message 1", attr.FormatErrorMessage ("SomeField"), "#B1-2");
  327. #if !NET_4_0
  328. attr = new ValidateFooAttribute ();
  329. #endif
  330. attr.ErrorMessageResourceName = "ErrorProperty6";
  331. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  332. Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B2-1");
  333. Assert.AreEqual ("Error Message 6: SomeField", attr.FormatErrorMessage ("SomeField"), "#B2-2");
  334. #if !NET_4_0
  335. attr = new ValidateFooAttribute ();
  336. #endif
  337. attr.ErrorMessageResourceName = "ErrorProperty6";
  338. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  339. Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B3-1");
  340. Assert.AreEqual ("Error Message 6: ", attr.FormatErrorMessage (null), "#B3-2");
  341. }
  342. #if NET_4_0
  343. [Test]
  344. public void GetValidationResult ()
  345. {
  346. var attr = new ValidateBarAttribute ();
  347. try {
  348. attr.GetValidationResult ("stuff", null);
  349. Assert.Fail ("#A1");
  350. } catch (ArgumentNullException) {
  351. // success
  352. }
  353. var vc = new ValidationContext ("stuff", null, null);
  354. vc.DisplayName = "MyStuff";
  355. var vr = attr.GetValidationResult ("stuff", vc);
  356. Assert.IsNull (vr, "#A2");
  357. vr = attr.GetValidationResult (null, vc);
  358. Assert.IsNotNull(vr, "#A3-1");
  359. Assert.IsNotNull (vr.ErrorMessage, "#A3-2");
  360. Assert.AreEqual ("The field MyStuff is invalid.", vr.ErrorMessage, "#A3-3");
  361. attr.ErrorMessage = "My Error Message: {0}";
  362. vr = attr.GetValidationResult (null, vc);
  363. Assert.IsNotNull (vr, "#A4-1");
  364. Assert.IsNotNull (vr.ErrorMessage, "#A4-2");
  365. Assert.AreEqual ("My Error Message: MyStuff", vr.ErrorMessage, "#A4-3");
  366. attr.ErrorMessage = null;
  367. attr.ErrorMessageResourceName = "ErrorProperty1";
  368. attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
  369. vr = attr.GetValidationResult (null, vc);
  370. Assert.IsNotNull (vr, "#A5-1");
  371. Assert.IsNotNull (vr.ErrorMessage, "#A5-2");
  372. Assert.AreEqual ("Error Message 1", vr.ErrorMessage, "#A5-3");
  373. attr.ErrorMessage = "My Error Message: {0}";
  374. attr.ErrorMessageResourceName = null;
  375. attr.ErrorMessageResourceType = null;
  376. vr = attr.GetValidationResult (null, vc);
  377. Assert.IsNotNull (vr, "#A6-1");
  378. Assert.IsNotNull (vr.MemberNames, "#A6-2");
  379. int count = 0;
  380. foreach (string s in vr.MemberNames)
  381. count++;
  382. Assert.AreEqual (0, count, "#A6-3");
  383. Assert.AreEqual ("My Error Message: MyStuff", vr.ErrorMessage, "#A6-4");
  384. attr.ValidationResultErrorMessage = "My VR message";
  385. vr = attr.GetValidationResult (null, vc);
  386. Assert.IsNotNull (vr, "#A7-1");
  387. Assert.AreEqual ("My VR message", vr.ErrorMessage, "#A7-2");
  388. }
  389. [Test]
  390. public void IsValid_Object ()
  391. {
  392. var attr = new ValidateFooAttribute ();
  393. AssertExtensions.Throws <NotImplementedException> (() => {
  394. // It calls IsValid (object, validationContext) which throws the NIEX, but when that overload is called directly, there's
  395. // no exception.
  396. //
  397. // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
  398. // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
  399. // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 450
  400. attr.IsValid (null);
  401. }, "#A1-1");
  402. AssertExtensions.Throws <NotImplementedException> (() => {
  403. attr.IsValid ("stuff");
  404. }, "#A1-2");
  405. }
  406. [Test]
  407. public void IsValid_Object_ValidationContext ()
  408. {
  409. var attr = new ValidateBarAttribute ();
  410. AssertExtensions.Throws <NullReferenceException> (() => {
  411. attr.CallIsValid (null, null);
  412. }, "#A1");
  413. var vc = new ValidationContext ("stuff", null, null);
  414. var vr = attr.CallIsValid (null, vc);
  415. Assert.IsNotNull (vr, "#A2-1");
  416. Assert.IsNotNull (vr.ErrorMessage, "#A2-2");
  417. Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A2-3");
  418. Assert.IsNotNull (vr.MemberNames, "#A2-4");
  419. int count = 0;
  420. foreach (string s in vr.MemberNames)
  421. count++;
  422. Assert.AreEqual (0, count, "#A2-5");
  423. vc.MemberName = "SomeMember";
  424. vr = attr.CallIsValid (null, vc);
  425. Assert.IsNotNull (vr, "#A3-1");
  426. Assert.IsNotNull (vr.ErrorMessage, "#A3-2");
  427. Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A3-3");
  428. Assert.IsNotNull (vr.MemberNames, "#A3-4");
  429. var list = new List <string> ();
  430. foreach (string s in vr.MemberNames)
  431. list.Add (s);
  432. Assert.AreEqual (1, list.Count, "#A3-5");
  433. Assert.AreEqual ("SomeMember", list [0], "#A3-6");
  434. }
  435. [Test]
  436. public void IsValid_Object_ValidationContext_CrossCallsWithNIEX ()
  437. {
  438. var attr = new ValidateSomethingAttribute ();
  439. AssertExtensions.Throws<NotImplementedException> (() => {
  440. // Thrown from the IsValid (object, ValidationContext) overload!
  441. //
  442. // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_02:
  443. // System.NotImplementedException : IsValid(object value) has not been implemented by this class. The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).
  444. //
  445. // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
  446. // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 639
  447. // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
  448. // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 639
  449. // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_02() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 514
  450. attr.IsValid ("stuff");
  451. }, "#A1");
  452. AssertExtensions.Throws<NotImplementedException> (() => {
  453. // And this one is thrown from the IsValid (object) overload!
  454. //
  455. // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_CrossCallsWithNIEX:
  456. // System.NotImplementedException : IsValid(object value) has not been implemented by this class. The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).
  457. //
  458. // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
  459. // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 660
  460. // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
  461. // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 660
  462. // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.CallIsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 667
  463. // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_CrossCallsWithNIEX() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 530
  464. attr.CallIsValid ("stuff", null);
  465. }, "#A2");
  466. }
  467. [Test]
  468. public void Validate_Object_ValidationContext ()
  469. {
  470. var attr = new ValidateBazAttribute ();
  471. try {
  472. attr.Validate ("stuff", (ValidationContext) null);
  473. Assert.Fail ("#A1");
  474. } catch (ArgumentNullException) {
  475. // success
  476. }
  477. var vc = new ValidationContext ("stuff", null, null);
  478. try {
  479. attr.Validate (null, vc);
  480. Assert.Fail ("#A2-1");
  481. } catch (ValidationException) {
  482. // success
  483. }
  484. Assert.AreEqual (3, attr.Calls.Count, "#A2-1");
  485. Assert.AreEqual ("ValidationResult IsValid (object value, ValidationContext validationContext)", attr.Calls [0], "#A2-2");
  486. Assert.AreEqual ("bool IsValid (object value)", attr.Calls [1], "#A2-3");
  487. Assert.AreEqual ("string FormatErrorMessage (string name)", attr.Calls [2], "#A2-4");
  488. }
  489. #endif
  490. [Test]
  491. public void Validate_Object_String ()
  492. {
  493. var attr = new ValidateBazAttribute ();
  494. try {
  495. attr.Validate (null, (string) null);
  496. Assert.Fail ("#A2");
  497. } catch (ValidationException) {
  498. // success
  499. }
  500. Assert.AreEqual (2, attr.Calls.Count, "#A2-1");
  501. Assert.AreEqual ("bool IsValid (object value)", attr.Calls [0], "#A2-2");
  502. Assert.AreEqual ("string FormatErrorMessage (string name)", attr.Calls [1], "#A2-3");
  503. }
  504. }
  505. class ValidateFooAttribute : ValidationAttribute
  506. {
  507. public ValidateFooAttribute ()
  508. : base ()
  509. { }
  510. public ValidateFooAttribute (Func<string> errorMessageAccessor)
  511. : base (errorMessageAccessor)
  512. { }
  513. public ValidateFooAttribute (string errorMessage)
  514. : base (errorMessage)
  515. { }
  516. public string GetErrorMessageString ()
  517. {
  518. return ErrorMessageString;
  519. }
  520. #if !NET_4_0
  521. public override bool IsValid (object value)
  522. {
  523. return value != null;
  524. }
  525. #endif
  526. }
  527. class ValidateBarAttribute : ValidateFooAttribute
  528. {
  529. public string ValidationResultErrorMessage
  530. {
  531. get;
  532. set;
  533. }
  534. public override bool IsValid (object value)
  535. {
  536. return value != null;
  537. }
  538. #if NET_4_0
  539. protected override ValidationResult IsValid (object value, ValidationContext validationContext)
  540. {
  541. if (!IsValid (value))
  542. return new ValidationResult (ValidationResultErrorMessage);
  543. return null;
  544. }
  545. public ValidationResult CallIsValid (object value, ValidationContext validationContext)
  546. {
  547. return base.IsValid (value, validationContext);
  548. }
  549. #endif
  550. }
  551. class ValidateBazAttribute : ValidateBarAttribute
  552. {
  553. public readonly List<string> Calls = new List<string> ();
  554. public override bool IsValid (object value)
  555. {
  556. Calls.Add ("bool IsValid (object value)");
  557. return base.IsValid (value);
  558. }
  559. #if NET_4_0
  560. protected override ValidationResult IsValid (object value, ValidationContext validationContext)
  561. {
  562. Calls.Add ("ValidationResult IsValid (object value, ValidationContext validationContext)");
  563. return base.IsValid (value, validationContext);
  564. }
  565. #endif
  566. public override string FormatErrorMessage (string name)
  567. {
  568. Calls.Add ("string FormatErrorMessage (string name)");
  569. return base.FormatErrorMessage (name);
  570. }
  571. }
  572. #if NET_4_0
  573. class ValidateSomethingAttribute : ValidationAttribute
  574. {
  575. public override bool IsValid (object value)
  576. {
  577. return base.IsValid (value, null) == ValidationResult.Success;
  578. }
  579. protected override ValidationResult IsValid (object value, ValidationContext validationContext)
  580. {
  581. if (base.IsValid (value))
  582. return ValidationResult.Success;
  583. return new ValidationResult ("failed to validate in base class");
  584. }
  585. public ValidationResult CallIsValid (object value, ValidationContext validationContext)
  586. {
  587. return IsValid (value, validationContext);
  588. }
  589. }
  590. #endif
  591. class FooErrorMessageProvider
  592. {
  593. public static string ErrorProperty1
  594. {
  595. get { return "Error Message 1"; }
  596. }
  597. public static int ErrorProperty2
  598. {
  599. get { return 1; }
  600. }
  601. public string ErrorProperty3
  602. {
  603. get { return "Error Message 2"; }
  604. }
  605. protected static string ErrorProperty4
  606. {
  607. get { return "Error Message 3"; }
  608. }
  609. public static string ErrorProperty5
  610. {
  611. set { }
  612. }
  613. public static string ErrorProperty6
  614. {
  615. get { return "Error Message 6: {0}"; }
  616. }
  617. public string ErrorField1 = "Error Message 4";
  618. public static string ErrorField2 = "Error Message 5";
  619. }
  620. }