001 // Copyright 2007 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.form.validator; 016 017 import java.util.Locale; 018 import org.apache.tapestry.IBinding; 019 import org.apache.tapestry.IComponent; 020 import org.apache.tapestry.form.ValidationMessagesImpl; 021 import org.easymock.EasyMock; 022 import static org.easymock.EasyMock.expect; 023 import static org.easymock.EasyMock.aryEq; 024 import static org.easymock.EasyMock.eq; 025 import static org.easymock.EasyMock.isNull; 026 import static org.easymock.EasyMock.checkOrder; 027 028 import org.apache.tapestry.IMarkupWriter; 029 import org.apache.tapestry.IRequestCycle; 030 import org.apache.tapestry.form.FormComponentContributorContext; 031 import org.apache.tapestry.form.IFormComponent; 032 import org.apache.tapestry.form.ValidationMessages; 033 import org.apache.tapestry.json.JSONObject; 034 import org.apache.tapestry.valid.ValidationConstraint; 035 import org.apache.tapestry.valid.ValidationStrings; 036 import org.apache.tapestry.valid.ValidatorException; 037 import org.testng.annotations.Test; 038 039 /** 040 * Tests for {@link org.apache.tapestry.form.validator.Identity}. 041 * 042 * @since 4.1.2 043 */ 044 @Test 045 public class TestIdentity extends BaseValidatorTestCase 046 { 047 public void testOK() throws Exception 048 { 049 IFormComponent field = newField(); 050 IFormComponent otherField = newField(); 051 052 trainGetContainerAndComponent(field, "newPass", otherField); 053 trainGetValueBinding(otherField, "pass"); 054 055 ValidationMessages messages = newMessages(); 056 057 replay(); 058 059 new Identity("match=newPass").validate(field, messages, "pass"); 060 061 verify(); 062 } 063 064 public void testFail() 065 { 066 IFormComponent field = newField(); 067 IFormComponent otherField = newField(); 068 069 trainGetContainerAndComponent(field, "newPass", otherField); 070 trainGetValueBinding(otherField, "pass"); 071 expect(field.getDisplayName()).andReturn("Password-1"); 072 expect(otherField.getDisplayName()).andReturn("Password-2"); 073 074 ValidationMessages messages = newMessages(); 075 trainIdentityMessages(messages, null, "Password-1", "Password-2", 1, "err1"); 076 077 replay(); 078 079 try 080 { 081 new Identity("match=newPass").validate(field, messages, "passOTHER"); 082 } 083 catch (ValidatorException ex) 084 { 085 assertEquals(ex.getMessage(), "err1"); 086 assertEquals(ex.getConstraint(), ValidationConstraint.CONSISTENCY); 087 } 088 } 089 090 public void testFailCustomMessage() 091 { 092 IFormComponent field = newField(); 093 IFormComponent otherField = newField(); 094 095 trainGetContainerAndComponent(field, "newPass", otherField); 096 trainGetValueBinding(otherField, "pass"); 097 expect(field.getDisplayName()).andReturn("Password-1"); 098 expect(otherField.getDisplayName()).andReturn("Password-2"); 099 100 ValidationMessages messages = newMessages(); 101 String msgOverride = "Should differ!"; 102 trainIdentityMessages(messages, msgOverride, "Password-1", "Password-2", 0, msgOverride); 103 104 replay(); 105 106 try 107 { 108 new Identity("differ=newPass,message=Should differ!").validate(field, messages, "pass"); 109 } 110 catch (ValidatorException ex) 111 { 112 assertEquals(ex.getMessage(), msgOverride); 113 assertEquals(ex.getConstraint(), ValidationConstraint.CONSISTENCY); 114 } 115 } 116 117 public void test_Render_Contribution() 118 { 119 JSONObject json = new JSONObject(); 120 121 IFormComponent field = newField("Password", "pass1"); 122 expect(field.isDisabled()).andReturn(false); 123 124 IFormComponent otherField = newField("Verify Password", "pass2"); 125 trainGetContainerAndComponent(field, "other", otherField); 126 127 FormComponentContributorContext context = newMock(FormComponentContributorContext.class); 128 expect(context.getProfile()).andReturn(json); 129 130 trainIdentityMessages(context, null, "Password", "Verify Password", 1, "Fields must match"); 131 132 IMarkupWriter writer = newWriter(); 133 IRequestCycle cycle = newCycle(); 134 135 replay(); 136 137 new Identity("match=other").renderContribution(writer, cycle, context, field); 138 139 verify(); 140 141 assertEquals("{\"constraints\":{\"pass1\":[[tapestry.form.validation.isEqual,\"pass2\"]]}," 142 + "\"pass1\":{\"constraints\":[\"Fields must match\"]}}", 143 json.toString()); 144 } 145 146 public void testNotRequired() 147 { 148 assertEquals(false, new Identity().isRequired()); 149 } 150 151 private void trainGetContainerAndComponent(IFormComponent field, String name, IFormComponent other) { 152 IComponent container = newComponent(); 153 expect(field.getContainer()).andReturn(container); 154 expect(container.getComponent(name)).andReturn(other); 155 } 156 157 private void trainGetValueBinding(IFormComponent field, String value) { 158 IBinding ret = newBinding(value); 159 expect(field.getBinding("value")).andReturn(ret); 160 } 161 162 private void trainIdentityMessages(ValidationMessages messages, String msgOverride, 163 String name1, String name2, int match, String result) 164 { 165 trainFormatMessage(messages, msgOverride, "invalid-field-equality", 166 new Object[]{ name1, new Integer(match), name2 }, 167 result); 168 } 169 }