PageRenderTime 7115ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ValidateThis/tests/CF9Only/AnnotationReaderTest.cfc

http://github.com/bobsilverberg/ValidateThis
ColdFusion CFScript | 263 lines | 239 code | 23 blank | 1 comment | 31 complexity | 79c95eb5bf9fdba9a92c03979adf9d00 MD5 | raw file
  1. <!---
  2. // **************************************** LICENSE INFO **************************************** \\
  3. Copyright 2010, Bob Silverberg
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
  5. compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the License is
  8. distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  9. implied. See the License for the specific language governing permissions and limitations under the
  10. License.
  11. --->
  12. <cfcomponent extends="validatethis.tests.BaseTestCase" output="false">
  13. <cffunction name="setUp" access="public" returntype="void">
  14. <cfscript>
  15. ValidateThisConfig = getVTConfig();
  16. className = "user";
  17. validationFactory = CreateObject("component","ValidateThis.core.ValidationFactory").init(ValidateThisConfig);
  18. annotationReader = validationFactory.getBean("annotationReader");
  19. </cfscript>
  20. </cffunction>
  21. <cffunction name="tearDown" access="public" returntype="void">
  22. </cffunction>
  23. <cffunction name="loadRulesFromAnnotationsReturnsEmptyStructIfNoValidAnnotationFormatFound" access="public" returntype="void">
  24. <cfscript>
  25. theObject = createObject("component","validatethis.tests.Fixture.User");
  26. PropertyDescs = annotationReader.loadRulesFromAnnotations(objectType="user",theObject=theObject,componentPath="").PropertyDescs;
  27. assertTrue(structIsEmpty(PropertyDescs));
  28. </cfscript>
  29. </cffunction>
  30. <cffunction name="loadRulesFromAnnotationsReturnsCorrectPropertyDescsForObjectWithJSON" access="public" returntype="void">
  31. <cfscript>
  32. theObject = createObject("component","validatethis.tests.Fixture.AnnotatedBOs.User");
  33. PropertyDescs = annotationReader.loadRulesFromAnnotations(objectType="user",theObject=theObject,componentPath="").PropertyDescs;
  34. isPropertiesStructCorrect(PropertyDescs);
  35. </cfscript>
  36. </cffunction>
  37. <cffunction name="loadRulesReturnsCorrectValidationsForObjectWithJSON" access="public" returntype="void">
  38. <cfscript>
  39. theObject = createObject("component","validatethis.tests.Fixture.AnnotatedBOs.User");
  40. Validations = annotationReader.loadRulesFromAnnotations(objectType="user",theObject=theObject,componentPath="").Validations;
  41. assertEquals(StructCount(Validations),1);
  42. assertEquals(StructCount(Validations.Contexts),3);
  43. Rules = Validations.Contexts.Register;
  44. assertEquals(ArrayLen(Rules),13);
  45. for (i = 1; i LTE ArrayLen(Rules); i = i + 1) {
  46. Validation = Rules[i];
  47. if (Validation.ValType eq "required" and Validation.PropertyName eq "LastName") {
  48. assertEquals(Validation.PropertyDesc,"Last Name");
  49. assertEquals(Validation.Parameters.DependentPropertyName.value,"FirstName");
  50. assertEquals(Validation.Parameters.DependentPropertyDesc.value,"First Name");
  51. }
  52. if (Validation.ValType eq "required" and Validation.PropertyName eq "UserName") {
  53. assertEquals(Validation.PropertyDesc,"Email Address");
  54. }
  55. if (Validation.ValType eq "email" and Validation.PropertyName eq "UserName") {
  56. assertEquals(Validation.PropertyDesc,"Email Address");
  57. assertEquals(Validation.FailureMessage,"Hey, buddy, you call that an Email Address?");
  58. }
  59. if (Validation.ValType eq "custom" and Validation.PropertyName eq "Nickname") {
  60. assertEquals(Validation.PropertyDesc,"Nickname");
  61. assertEquals(Validation.Parameters.MethodName.value,"CheckDupNickname");
  62. }
  63. if (Validation.ValType eq "required" and Validation.PropertyName eq "UserPass") {
  64. assertEquals(Validation.PropertyDesc,"Password");
  65. }
  66. if (Validation.ValType eq "rangelength" and Validation.PropertyName eq "UserPass") {
  67. assertEquals(Validation.PropertyDesc,"Password");
  68. assertEquals(Validation.Parameters.MinLength.value,5);
  69. assertEquals(Validation.Parameters.MaxLength.value,10);
  70. }
  71. if (Validation.ValType eq "required" and Validation.PropertyName eq "VerifyPassword") {
  72. assertEquals(Validation.PropertyDesc,"Verify Password");
  73. }
  74. if (Validation.ValType eq "equalTo" and Validation.PropertyName eq "VerifyPassword") {
  75. assertEquals(Validation.PropertyDesc,"Verify Password");
  76. assertEquals(Validation.Parameters.ComparePropertyName.value,"UserPass");
  77. assertEquals(Validation.Parameters.ComparePropertyDesc.value,"Password");
  78. }
  79. if (Validation.ValType eq "required" and Validation.PropertyName eq "UserGroup") {
  80. assertEquals(Validation.PropertyDesc,"User Group");
  81. }
  82. if (Validation.ValType eq "regex" and Validation.PropertyName eq "Salutation") {
  83. assertEquals(Validation.PropertyDesc,"Salutation");
  84. assertEquals(Validation.Parameters.Regex.value,"^(Dr|Prof|Mr|Mrs|Ms|Miss)(\.)?$");
  85. assertEquals(Validation.FailureMessage,"Only Dr, Prof, Mr, Mrs, Ms, or Miss (with or without a period) are allowed.");
  86. }
  87. if (Validation.ValType eq "required" and Validation.PropertyName eq "LikeOther") {
  88. assertEquals(Validation.PropertyDesc,"What do you like?");
  89. assertEquals(Validation.FailureMessage,"If you don't like Cheese and you don't like Chocolate, you must like something!");
  90. assertEquals(Validation.Condition.ClientTest,"$(""[name='likecheese']"").getvalue() == 0 && $(""[name='likechocolate']"").getvalue() == 0;");
  91. assertEquals(Validation.Condition.ServerTest,"getLikeCheese() EQ 0 AND getLikeChocolate() EQ 0");
  92. }
  93. if (Validation.ValType eq "numeric" and Validation.PropertyName eq "HowMuch") {
  94. assertEquals(Validation.PropertyDesc,"How much money would you like?");
  95. }
  96. if (Validation.ValType eq "required" and Validation.PropertyName eq "CommunicationMethod") {
  97. assertEquals(Validation.PropertyDesc,"Communication Method");
  98. assertEquals(Validation.Parameters.DependentPropertyDesc.value,"Allow Communication");
  99. assertEquals(Validation.Parameters.DependentPropertyName.value,"AllowCommunication");
  100. assertEquals(Validation.Parameters.DependentPropertyValue.value,1);
  101. }
  102. }
  103. Rules = Validations.Contexts.Profile;
  104. assertEquals(ArrayLen(Rules),15);
  105. for (i = 1; i LTE ArrayLen(Rules); i = i + 1) {
  106. Validation = Rules[i];
  107. if (Validation.ValType eq "required" and Validation.PropertyName eq "Salutation") {
  108. assertEquals(Validation.PropertyDesc,"Salutation");
  109. }
  110. if (Validation.ValType eq "required" and Validation.PropertyName eq "FirstName") {
  111. assertEquals(Validation.PropertyDesc,"First Name");
  112. }
  113. if (Validation.ValType eq "required" and Validation.PropertyName eq "LastName") {
  114. assertEquals(Validation.PropertyDesc,"Last Name");
  115. }
  116. if (Validation.ValType eq "required" and Validation.PropertyName eq "UserName") {
  117. assertEquals(Validation.PropertyDesc,"Email Address");
  118. }
  119. if (Validation.ValType eq "email" and Validation.PropertyName eq "UserName") {
  120. assertEquals(Validation.PropertyDesc,"Email Address");
  121. assertEquals(Validation.FailureMessage,"Hey, buddy, you call that an Email Address?");
  122. }
  123. if (Validation.ValType eq "custom" and Validation.PropertyName eq "Nickname") {
  124. assertEquals(Validation.PropertyDesc,"Nickname");
  125. assertEquals(Validation.Parameters.MethodName.value,"CheckDupNickname");
  126. }
  127. if (Validation.ValType eq "required" and Validation.PropertyName eq "UserPass") {
  128. assertEquals(Validation.PropertyDesc,"Password");
  129. }
  130. if (Validation.ValType eq "rangelength" and Validation.PropertyName eq "UserPass") {
  131. assertEquals(Validation.PropertyDesc,"Password");
  132. assertEquals(Validation.Parameters.MinLength.value,5);
  133. assertEquals(Validation.Parameters.MaxLength.value,10);
  134. }
  135. if (Validation.ValType eq "required" and Validation.PropertyName eq "VerifyPassword") {
  136. assertEquals(Validation.PropertyDesc,"Verify Password");
  137. }
  138. if (Validation.ValType eq "equalTo" and Validation.PropertyName eq "VerifyPassword") {
  139. assertEquals(Validation.PropertyDesc,"Verify Password");
  140. assertEquals(Validation.Parameters.ComparePropertyName.value,"UserPass");
  141. assertEquals(Validation.Parameters.ComparePropertyDesc.value,"Password");
  142. }
  143. if (Validation.ValType eq "required" and Validation.PropertyName eq "UserGroup") {
  144. assertEquals(Validation.PropertyDesc,"User Group");
  145. }
  146. if (Validation.ValType eq "regex" and Validation.PropertyName eq "Salutation") {
  147. assertEquals(Validation.PropertyDesc,"Salutation");
  148. assertEquals(Validation.Parameters.Regex.value,"^(Dr|Prof|Mr|Mrs|Ms|Miss)(\.)?$");
  149. assertEquals(Validation.FailureMessage,"Only Dr, Prof, Mr, Mrs, Ms, or Miss (with or without a period) are allowed.");
  150. }
  151. if (Validation.ValType eq "required" and Validation.PropertyName eq "LikeOther") {
  152. assertEquals(Validation.PropertyDesc,"What do you like?");
  153. assertEquals(Validation.FailureMessage,"If you don't like Cheese and you don't like Chocolate, you must like something!");
  154. assertEquals(Validation.Condition.ClientTest,"$(""[name='likecheese']"").getvalue() == 0 && $(""[name='likechocolate']"").getvalue() == 0;");
  155. assertEquals(Validation.Condition.ServerTest,"getLikeCheese() EQ 0 AND getLikeChocolate() EQ 0");
  156. }
  157. if (Validation.ValType eq "numeric" and Validation.PropertyName eq "HowMuch") {
  158. assertEquals(Validation.PropertyDesc,"How much money would you like?");
  159. }
  160. if (Validation.ValType eq "required" and Validation.PropertyName eq "CommunicationMethod") {
  161. assertEquals(Validation.PropertyDesc,"Communication Method");
  162. assertEquals(Validation.Parameters.DependentPropertyDesc.value,"Allow Communication");
  163. assertEquals(Validation.Parameters.DependentPropertyName.value,"AllowCommunication");
  164. assertEquals(Validation.Parameters.DependentPropertyValue.value,1);
  165. }
  166. }
  167. </cfscript>
  168. </cffunction>
  169. <cffunction name="isPropertiesStructCorrect" access="private" returntype="void">
  170. <cfargument type="Any" name="PropertyDescs" required="true" />
  171. <cfscript>
  172. assertEquals(arguments.PropertyDescs.AllowCommunication,"Allow Communication");
  173. assertEquals(arguments.PropertyDescs.CommunicationMethod,"Communication Method");
  174. assertEquals(arguments.PropertyDescs.FirstName,"First Name");
  175. assertEquals(arguments.PropertyDescs.HowMuch,"How much money would you like?");
  176. assertEquals(arguments.PropertyDescs.LastName,"Last Name");
  177. assertEquals(arguments.PropertyDescs.LikeOther,"What do you like?");
  178. assertEquals(arguments.PropertyDescs.UserGroup,"User Group");
  179. assertEquals(arguments.PropertyDescs.UserName,"Email Address");
  180. assertEquals(arguments.PropertyDescs.UserPass,"Password");
  181. assertEquals(arguments.PropertyDescs.VerifyPassword,"Verify Password");
  182. </cfscript>
  183. </cffunction>
  184. <!--- All of these tests are testing private methods and hence could be removed or perhaps the methods refactored into composed objects --->
  185. <cffunction name="getObjectMetadataReturnsAccurateMetadataForAnObject" access="public" returntype="void">
  186. <cfscript>
  187. theObject = createObject("component","validatethis.tests.Fixture.AnnotatedBOs.User");
  188. makePublic(annotationReader,"getObjectMetadata");
  189. md = annotationReader.getObjectMetadata(theObject=theObject,componentPath="");
  190. assertEquals(16,arrayLen(md.properties));
  191. </cfscript>
  192. </cffunction>
  193. <cffunction name="getObjectMetadataReturnsAccurateMetadataForAComponentPath" access="public" returntype="void">
  194. <cfscript>
  195. makePublic(annotationReader,"getObjectMetadata");
  196. md = annotationReader.getObjectMetadata(theObject="",componentPath="validatethis.tests.Fixture.AnnotatedBOs.User");
  197. assertEquals(16,arrayLen(md.properties));
  198. </cfscript>
  199. </cffunction>
  200. <cffunction name="determineAnnotationFormatReturnsJSONForJSONAnnotations" access="public" returntype="void">
  201. <cfscript>
  202. properties = getComponentMetadata("validatethis.tests.Fixture.AnnotatedBOs.User").properties;
  203. makePublic(annotationReader,"determineAnnotationFormat");
  204. format = annotationReader.determineAnnotationFormat(properties);
  205. assertEquals("json",format);
  206. </cfscript>
  207. </cffunction>
  208. <cffunction name="determineAnnotationFormatReturnsXMLForXMLAnnotations" access="public" returntype="void">
  209. <cfscript>
  210. properties = getComponentMetadata("validatethis.tests.Fixture.AnnotatedBOs.User_XML").properties;
  211. makePublic(annotationReader,"determineAnnotationFormat");
  212. format = annotationReader.determineAnnotationFormat(properties);
  213. assertEquals("xml",format);
  214. </cfscript>
  215. </cffunction>
  216. <cffunction name="builtinAnnotationTypeReadersAreLoadedAfterInit" access="public" returntype="void">
  217. <cfscript>
  218. injectMethod(annotationReader, this, "getATRs", "getATRs");
  219. atrs = annotationReader.getATRs();
  220. assertEquals(3,structCount(atrs));
  221. </cfscript>
  222. </cffunction>
  223. <cffunction name="getATRs" access="public" output="false" returntype="any" hint="Used to retrieve the ATRs for testing.">
  224. <cfparam name="variables.AnnotationTypeReaders" default="#structNew()#" />
  225. <cfreturn variables.AnnotationTypeReaders />
  226. </cffunction>
  227. <!--- TODO: I'm not sure whether these tests just need to be written, or whether the code that implements them also needs to be written.
  228. Either way, commenting them out so the test suites can pass
  229. <cffunction name="getObjectMetadataReturnsAccurateMetadataForAComponentThatExtendsAnother" access="public" returntype="void">
  230. <cfscript>
  231. fail("Place holder test");
  232. </cfscript>
  233. </cffunction>
  234. <cffunction name="getObjectMetadataReturnsAccurateMetadataForAComponentThatMappedSuperClassesAnother" access="public" returntype="void">
  235. <cfscript>
  236. fail("Place holder test");
  237. </cfscript>
  238. </cffunction>
  239. --->
  240. </cfcomponent>