/ValidateThis/tests/CF9Only/AnnotationReaderTest.cfc
ColdFusion CFScript | 263 lines | 239 code | 23 blank | 1 comment | 31 complexity | 79c95eb5bf9fdba9a92c03979adf9d00 MD5 | raw file
- <!---
- // **************************************** LICENSE INFO **************************************** \\
-
- Copyright 2010, Bob Silverberg
-
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
- compliance with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software distributed under the License is
- distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied. See the License for the specific language governing permissions and limitations under the
- License.
-
- --->
- <cfcomponent extends="validatethis.tests.BaseTestCase" output="false">
-
- <cffunction name="setUp" access="public" returntype="void">
- <cfscript>
- ValidateThisConfig = getVTConfig();
- className = "user";
- validationFactory = CreateObject("component","ValidateThis.core.ValidationFactory").init(ValidateThisConfig);
- annotationReader = validationFactory.getBean("annotationReader");
- </cfscript>
- </cffunction>
- <cffunction name="tearDown" access="public" returntype="void">
- </cffunction>
-
- <cffunction name="loadRulesFromAnnotationsReturnsEmptyStructIfNoValidAnnotationFormatFound" access="public" returntype="void">
- <cfscript>
- theObject = createObject("component","validatethis.tests.Fixture.User");
- PropertyDescs = annotationReader.loadRulesFromAnnotations(objectType="user",theObject=theObject,componentPath="").PropertyDescs;
- assertTrue(structIsEmpty(PropertyDescs));
- </cfscript>
- </cffunction>
- <cffunction name="loadRulesFromAnnotationsReturnsCorrectPropertyDescsForObjectWithJSON" access="public" returntype="void">
- <cfscript>
- theObject = createObject("component","validatethis.tests.Fixture.AnnotatedBOs.User");
- PropertyDescs = annotationReader.loadRulesFromAnnotations(objectType="user",theObject=theObject,componentPath="").PropertyDescs;
- isPropertiesStructCorrect(PropertyDescs);
- </cfscript>
- </cffunction>
- <cffunction name="loadRulesReturnsCorrectValidationsForObjectWithJSON" access="public" returntype="void">
- <cfscript>
- theObject = createObject("component","validatethis.tests.Fixture.AnnotatedBOs.User");
- Validations = annotationReader.loadRulesFromAnnotations(objectType="user",theObject=theObject,componentPath="").Validations;
- assertEquals(StructCount(Validations),1);
- assertEquals(StructCount(Validations.Contexts),3);
- Rules = Validations.Contexts.Register;
- assertEquals(ArrayLen(Rules),13);
- for (i = 1; i LTE ArrayLen(Rules); i = i + 1) {
- Validation = Rules[i];
- if (Validation.ValType eq "required" and Validation.PropertyName eq "LastName") {
- assertEquals(Validation.PropertyDesc,"Last Name");
- assertEquals(Validation.Parameters.DependentPropertyName.value,"FirstName");
- assertEquals(Validation.Parameters.DependentPropertyDesc.value,"First Name");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "UserName") {
- assertEquals(Validation.PropertyDesc,"Email Address");
- }
- if (Validation.ValType eq "email" and Validation.PropertyName eq "UserName") {
- assertEquals(Validation.PropertyDesc,"Email Address");
- assertEquals(Validation.FailureMessage,"Hey, buddy, you call that an Email Address?");
- }
- if (Validation.ValType eq "custom" and Validation.PropertyName eq "Nickname") {
- assertEquals(Validation.PropertyDesc,"Nickname");
- assertEquals(Validation.Parameters.MethodName.value,"CheckDupNickname");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "UserPass") {
- assertEquals(Validation.PropertyDesc,"Password");
- }
- if (Validation.ValType eq "rangelength" and Validation.PropertyName eq "UserPass") {
- assertEquals(Validation.PropertyDesc,"Password");
- assertEquals(Validation.Parameters.MinLength.value,5);
- assertEquals(Validation.Parameters.MaxLength.value,10);
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "VerifyPassword") {
- assertEquals(Validation.PropertyDesc,"Verify Password");
- }
- if (Validation.ValType eq "equalTo" and Validation.PropertyName eq "VerifyPassword") {
- assertEquals(Validation.PropertyDesc,"Verify Password");
- assertEquals(Validation.Parameters.ComparePropertyName.value,"UserPass");
- assertEquals(Validation.Parameters.ComparePropertyDesc.value,"Password");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "UserGroup") {
- assertEquals(Validation.PropertyDesc,"User Group");
- }
- if (Validation.ValType eq "regex" and Validation.PropertyName eq "Salutation") {
- assertEquals(Validation.PropertyDesc,"Salutation");
- assertEquals(Validation.Parameters.Regex.value,"^(Dr|Prof|Mr|Mrs|Ms|Miss)(\.)?$");
- assertEquals(Validation.FailureMessage,"Only Dr, Prof, Mr, Mrs, Ms, or Miss (with or without a period) are allowed.");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "LikeOther") {
- assertEquals(Validation.PropertyDesc,"What do you like?");
- assertEquals(Validation.FailureMessage,"If you don't like Cheese and you don't like Chocolate, you must like something!");
- assertEquals(Validation.Condition.ClientTest,"$(""[name='likecheese']"").getvalue() == 0 && $(""[name='likechocolate']"").getvalue() == 0;");
- assertEquals(Validation.Condition.ServerTest,"getLikeCheese() EQ 0 AND getLikeChocolate() EQ 0");
- }
- if (Validation.ValType eq "numeric" and Validation.PropertyName eq "HowMuch") {
- assertEquals(Validation.PropertyDesc,"How much money would you like?");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "CommunicationMethod") {
- assertEquals(Validation.PropertyDesc,"Communication Method");
- assertEquals(Validation.Parameters.DependentPropertyDesc.value,"Allow Communication");
- assertEquals(Validation.Parameters.DependentPropertyName.value,"AllowCommunication");
- assertEquals(Validation.Parameters.DependentPropertyValue.value,1);
- }
- }
- Rules = Validations.Contexts.Profile;
- assertEquals(ArrayLen(Rules),15);
- for (i = 1; i LTE ArrayLen(Rules); i = i + 1) {
- Validation = Rules[i];
- if (Validation.ValType eq "required" and Validation.PropertyName eq "Salutation") {
- assertEquals(Validation.PropertyDesc,"Salutation");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "FirstName") {
- assertEquals(Validation.PropertyDesc,"First Name");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "LastName") {
- assertEquals(Validation.PropertyDesc,"Last Name");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "UserName") {
- assertEquals(Validation.PropertyDesc,"Email Address");
- }
- if (Validation.ValType eq "email" and Validation.PropertyName eq "UserName") {
- assertEquals(Validation.PropertyDesc,"Email Address");
- assertEquals(Validation.FailureMessage,"Hey, buddy, you call that an Email Address?");
- }
- if (Validation.ValType eq "custom" and Validation.PropertyName eq "Nickname") {
- assertEquals(Validation.PropertyDesc,"Nickname");
- assertEquals(Validation.Parameters.MethodName.value,"CheckDupNickname");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "UserPass") {
- assertEquals(Validation.PropertyDesc,"Password");
- }
- if (Validation.ValType eq "rangelength" and Validation.PropertyName eq "UserPass") {
- assertEquals(Validation.PropertyDesc,"Password");
- assertEquals(Validation.Parameters.MinLength.value,5);
- assertEquals(Validation.Parameters.MaxLength.value,10);
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "VerifyPassword") {
- assertEquals(Validation.PropertyDesc,"Verify Password");
- }
- if (Validation.ValType eq "equalTo" and Validation.PropertyName eq "VerifyPassword") {
- assertEquals(Validation.PropertyDesc,"Verify Password");
- assertEquals(Validation.Parameters.ComparePropertyName.value,"UserPass");
- assertEquals(Validation.Parameters.ComparePropertyDesc.value,"Password");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "UserGroup") {
- assertEquals(Validation.PropertyDesc,"User Group");
- }
- if (Validation.ValType eq "regex" and Validation.PropertyName eq "Salutation") {
- assertEquals(Validation.PropertyDesc,"Salutation");
- assertEquals(Validation.Parameters.Regex.value,"^(Dr|Prof|Mr|Mrs|Ms|Miss)(\.)?$");
- assertEquals(Validation.FailureMessage,"Only Dr, Prof, Mr, Mrs, Ms, or Miss (with or without a period) are allowed.");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "LikeOther") {
- assertEquals(Validation.PropertyDesc,"What do you like?");
- assertEquals(Validation.FailureMessage,"If you don't like Cheese and you don't like Chocolate, you must like something!");
- assertEquals(Validation.Condition.ClientTest,"$(""[name='likecheese']"").getvalue() == 0 && $(""[name='likechocolate']"").getvalue() == 0;");
- assertEquals(Validation.Condition.ServerTest,"getLikeCheese() EQ 0 AND getLikeChocolate() EQ 0");
- }
- if (Validation.ValType eq "numeric" and Validation.PropertyName eq "HowMuch") {
- assertEquals(Validation.PropertyDesc,"How much money would you like?");
- }
- if (Validation.ValType eq "required" and Validation.PropertyName eq "CommunicationMethod") {
- assertEquals(Validation.PropertyDesc,"Communication Method");
- assertEquals(Validation.Parameters.DependentPropertyDesc.value,"Allow Communication");
- assertEquals(Validation.Parameters.DependentPropertyName.value,"AllowCommunication");
- assertEquals(Validation.Parameters.DependentPropertyValue.value,1);
- }
- }
- </cfscript>
- </cffunction>
- <cffunction name="isPropertiesStructCorrect" access="private" returntype="void">
- <cfargument type="Any" name="PropertyDescs" required="true" />
- <cfscript>
- assertEquals(arguments.PropertyDescs.AllowCommunication,"Allow Communication");
- assertEquals(arguments.PropertyDescs.CommunicationMethod,"Communication Method");
- assertEquals(arguments.PropertyDescs.FirstName,"First Name");
- assertEquals(arguments.PropertyDescs.HowMuch,"How much money would you like?");
- assertEquals(arguments.PropertyDescs.LastName,"Last Name");
- assertEquals(arguments.PropertyDescs.LikeOther,"What do you like?");
- assertEquals(arguments.PropertyDescs.UserGroup,"User Group");
- assertEquals(arguments.PropertyDescs.UserName,"Email Address");
- assertEquals(arguments.PropertyDescs.UserPass,"Password");
- assertEquals(arguments.PropertyDescs.VerifyPassword,"Verify Password");
- </cfscript>
- </cffunction>
- <!--- All of these tests are testing private methods and hence could be removed or perhaps the methods refactored into composed objects --->
- <cffunction name="getObjectMetadataReturnsAccurateMetadataForAnObject" access="public" returntype="void">
- <cfscript>
- theObject = createObject("component","validatethis.tests.Fixture.AnnotatedBOs.User");
- makePublic(annotationReader,"getObjectMetadata");
- md = annotationReader.getObjectMetadata(theObject=theObject,componentPath="");
- assertEquals(16,arrayLen(md.properties));
- </cfscript>
- </cffunction>
- <cffunction name="getObjectMetadataReturnsAccurateMetadataForAComponentPath" access="public" returntype="void">
- <cfscript>
- makePublic(annotationReader,"getObjectMetadata");
- md = annotationReader.getObjectMetadata(theObject="",componentPath="validatethis.tests.Fixture.AnnotatedBOs.User");
- assertEquals(16,arrayLen(md.properties));
- </cfscript>
- </cffunction>
-
- <cffunction name="determineAnnotationFormatReturnsJSONForJSONAnnotations" access="public" returntype="void">
- <cfscript>
- properties = getComponentMetadata("validatethis.tests.Fixture.AnnotatedBOs.User").properties;
- makePublic(annotationReader,"determineAnnotationFormat");
- format = annotationReader.determineAnnotationFormat(properties);
- assertEquals("json",format);
- </cfscript>
- </cffunction>
- <cffunction name="determineAnnotationFormatReturnsXMLForXMLAnnotations" access="public" returntype="void">
- <cfscript>
- properties = getComponentMetadata("validatethis.tests.Fixture.AnnotatedBOs.User_XML").properties;
- makePublic(annotationReader,"determineAnnotationFormat");
- format = annotationReader.determineAnnotationFormat(properties);
- assertEquals("xml",format);
- </cfscript>
- </cffunction>
- <cffunction name="builtinAnnotationTypeReadersAreLoadedAfterInit" access="public" returntype="void">
- <cfscript>
- injectMethod(annotationReader, this, "getATRs", "getATRs");
- atrs = annotationReader.getATRs();
- assertEquals(3,structCount(atrs));
- </cfscript>
- </cffunction>
- <cffunction name="getATRs" access="public" output="false" returntype="any" hint="Used to retrieve the ATRs for testing.">
- <cfparam name="variables.AnnotationTypeReaders" default="#structNew()#" />
- <cfreturn variables.AnnotationTypeReaders />
- </cffunction>
-
- <!--- 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.
- Either way, commenting them out so the test suites can pass
-
- <cffunction name="getObjectMetadataReturnsAccurateMetadataForAComponentThatExtendsAnother" access="public" returntype="void">
- <cfscript>
- fail("Place holder test");
- </cfscript>
- </cffunction>
-
- <cffunction name="getObjectMetadataReturnsAccurateMetadataForAComponentThatMappedSuperClassesAnother" access="public" returntype="void">
- <cfscript>
- fail("Place holder test");
- </cfscript>
- </cffunction>
- --->
- </cfcomponent>