Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java?view=diff&rev=551256&r1=551255&r2=551256 ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/services/FieldValidatorSourceImplTest.java Wed Jun 27 11:33:28 2007 @@ -63,7 +63,8 @@ replay(); - FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map); + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); try { @@ -102,7 +103,7 @@ train_getId(resources, "fred"); train_getContainerMessages(resources, containerMessages); - train_contains(containerMessages, "fred-required", false); + train_contains(containerMessages, "fred-required-message", false); train_getLocale(resources, Locale.FRENCH); @@ -117,7 +118,8 @@ replay(); - FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map); + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); FieldValidator fieldValidator = source.createValidator(field, "required", null); @@ -147,9 +149,9 @@ train_getId(resources, "fred"); train_getLocale(resources, Locale.ENGLISH); train_getContainerMessages(resources, containerMessages); - train_contains(containerMessages, "fred-required", true); + train_contains(containerMessages, "fred-required-message", true); - train_getMessageFormatter(containerMessages, "fred-required", formatter); + train_getMessageFormatter(containerMessages, "fred-required-message", formatter); train_invokeIfBlank(validator, false); train_getValueType(validator, Object.class); @@ -157,7 +159,8 @@ replay(); - FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map); + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); FieldValidator fieldValidator = source.createValidator(field, "required", null); @@ -168,6 +171,101 @@ @SuppressWarnings("unchecked") @Test + public void constraint_value_from_message_catalog() throws Exception + { + ValidationMessagesSource messagesSource = mockValidationMessagesSource(); + Validator validator = mockValidator(); + TypeCoercer coercer = mockTypeCoercer(); + FieldComponent field = newFieldComponent(); + Messages messages = mockMessages(); + MessageFormatter formatter = mockMessageFormatter(); + Object inputValue = new Object(); + ComponentResources resources = mockComponentResources(); + Messages containerMessages = mockMessages(); + + Map<String, Validator> map = singletonMap("minlength", validator); + + train_getConstraintType(validator, Integer.class); + + train_getComponentResources(field, resources); + train_getId(resources, "fred"); + + train_contains(containerMessages, "fred-minlength", true); + train_get(containerMessages, "fred-minlength", "5"); + + train_coerce(coercer, "5", Integer.class, 5); + + train_getContainerMessages(resources, containerMessages); + train_contains(containerMessages, "fred-minlength-message", false); + + train_getLocale(resources, Locale.FRENCH); + + train_getValidationMessages(messagesSource, Locale.FRENCH, messages); + + train_getMessageKey(validator, "key"); + train_getMessageFormatter(messages, "key", formatter); + + train_invokeIfBlank(validator, false); + train_getValueType(validator, Object.class); + validator.validate(field, 5, formatter, inputValue); + + replay(); + + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); + + FieldValidator fieldValidator = source.createValidators(field, "minlength"); + + fieldValidator.validate(inputValue); + + verify(); + } + + @SuppressWarnings("unchecked") + @Test + public void missing_field_validator_constraint() throws Exception + { + ValidationMessagesSource messagesSource = mockValidationMessagesSource(); + Validator validator = mockValidator(); + TypeCoercer coercer = mockTypeCoercer(); + FieldComponent field = newFieldComponent(); + ComponentResources resources = mockComponentResources(); + Messages containerMessages = mockMessages(); + + Map<String, Validator> map = singletonMap("minlength", validator); + + train_getConstraintType(validator, Integer.class); + + train_getComponentResources(field, resources); + train_getId(resources, "fred"); + train_getLocale(resources, Locale.GERMAN); + train_getContainerMessages(resources, containerMessages); + + train_contains(containerMessages, "fred-minlength", false); + + replay(); + + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); + + try + { + source.createValidators(field, "minlength"); + unreachable(); + } + catch (IllegalArgumentException ex) + { + assertEquals( + ex.getMessage(), + "Validator \'minlength\' requires a validation constraint (of type java.lang.Integer) but none was provided."); + } + + verify(); + + } + + @SuppressWarnings("unchecked") + @Test public void single_validator_via_specification() throws Exception { ValidationMessagesSource messagesSource = mockValidationMessagesSource(); @@ -187,7 +285,7 @@ train_getComponentResources(field, resources); train_getId(resources, "fred"); train_getContainerMessages(resources, containerMessages); - train_contains(containerMessages, "fred-required", false); + train_contains(containerMessages, "fred-required-message", false); train_getLocale(resources, Locale.FRENCH); @@ -202,7 +300,8 @@ replay(); - FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map); + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); FieldValidator fieldValidator = source.createValidators(field, "required"); @@ -239,7 +338,7 @@ train_getComponentResources(field, resources); train_getId(resources, "fred"); train_getContainerMessages(resources, containerMessages); - train_contains(containerMessages, "fred-required", false); + train_contains(containerMessages, "fred-required-message", false); train_getLocale(resources, Locale.FRENCH); @@ -248,7 +347,7 @@ train_getMessageKey(required, "required"); train_getMessageFormatter(messages, "required", requiredFormatter); - train_contains(containerMessages, "fred-minLength", false); + train_contains(containerMessages, "fred-minLength-message", false); train_getMessageKey(minLength, "min-length"); train_getMessageFormatter(messages, "min-length", minLengthFormatter); @@ -265,7 +364,8 @@ replay(); - FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map); + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); FieldValidator fieldValidator = source.createValidators(field, "required,minLength=15"); @@ -298,7 +398,7 @@ train_getComponentResources(field, resources); train_getId(resources, "fred"); train_getContainerMessages(resources, containerMessages); - train_contains(containerMessages, "fred-minLength", false); + train_contains(containerMessages, "fred-minLength-message", false); train_getLocale(resources, Locale.FRENCH); @@ -313,7 +413,8 @@ replay(); - FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, map); + FieldValidatorSource source = new FieldValidatorSourceImpl(messagesSource, coercer, null, + map); FieldValidator fieldValidator = source.createValidator(field, "minLength", "5");
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/validator/RegexpTest.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/validator/RegexpTest.java?view=auto&rev=551256 ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/validator/RegexpTest.java (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/validator/RegexpTest.java Wed Jun 27 11:33:28 2007 @@ -0,0 +1,73 @@ +// Copyright 2007 The Apache Software Foundation +// +// 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. + +package org.apache.tapestry.validator; + +import java.util.regex.Pattern; + +import org.apache.tapestry.Field; +import org.apache.tapestry.ValidationException; +import org.apache.tapestry.internal.test.InternalBaseTestCase; +import org.apache.tapestry.ioc.MessageFormatter; +import org.testng.annotations.Test; + +/** These are getting tedious; I'd rather do it via integration tests. */ +public class RegexpTest extends InternalBaseTestCase +{ + @Test + public void matching_pattern() throws Exception + { + Field field = mockField(); + MessageFormatter formatter = mockMessageFormatter(); + Pattern constraint = Pattern.compile("\\d{4}"); + + replay(); + + Regexp validator = new Regexp(); + + validator.validate(field, constraint, formatter, "1234"); + + verify(); + } + + @Test + public void input_mismatch() throws Exception + { + String label = "My Field"; + Field field = mockFieldWithLabel(label); + MessageFormatter formatter = mockMessageFormatter(); + String message = "{message}"; + Pattern constraint = Pattern.compile("\\d{4}"); + String value = "abc"; + + train_format(formatter, message, constraint.toString(), label); + + replay(); + + Regexp validator = new Regexp(); + + try + { + validator.validate(field, constraint, formatter, value); + unreachable(); + } + catch (ValidationException ex) + { + assertEquals(ex.getMessage(), message); + } + + verify(); + + } +} Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/BeanEditorDemo.properties URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/BeanEditorDemo.properties?view=diff&rev=551256&r1=551255&r2=551256 ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/BeanEditorDemo.properties (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/BeanEditorDemo.properties Wed Jun 27 11:33:28 2007 @@ -13,5 +13,5 @@ # limitations under the License. birthYear-label=Year of Birth -lastName-required=Everyone has to have a last name! +lastName-required-message=Everyone has to have a last name! citizen-label=U.S. Citizen Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/RegexpDemo.properties URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/RegexpDemo.properties?view=auto&rev=551256 ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/RegexpDemo.properties (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/RegexpDemo.properties Wed Jun 27 11:33:28 2007 @@ -0,0 +1,16 @@ +# Copyright 2007 The Apache Software Foundation +# +# 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. + +zipcode-regexp=\\d{5}(\-\\d{4})? +zipcode-regexp-message=A zip code consists of five or nine digits, eg: 02134 or 90125-4472. \ No newline at end of file Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/ValidForm.properties URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/ValidForm.properties?view=diff&rev=551256&r1=551255&r2=551256 ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/ValidForm.properties (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/ValidForm.properties Wed Jun 27 11:33:28 2007 @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -message-required=Please provide a detailed description of the incident. \ No newline at end of file +message-required-message=Please provide a detailed description of the incident. \ No newline at end of file
