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.internal.services;
016:
017: import java.util.Locale;
018:
019: import org.apache.tapestry.Field;
020: import org.apache.tapestry.FieldValidator;
021: import org.apache.tapestry.internal.test.InternalBaseTestCase;
022: import org.apache.tapestry.ioc.AnnotationProvider;
023: import org.apache.tapestry.ioc.Messages;
024: import org.apache.tapestry.services.FieldValidatorDefaultSource;
025: import org.apache.tapestry.services.FieldValidatorSource;
026: import org.apache.tapestry.services.ValidationConstraintGenerator;
027: import org.testng.annotations.Test;
028:
029: public class FieldValidatorDefaultSourceImplTest extends
030: InternalBaseTestCase {
031: @SuppressWarnings("unchecked")
032: @Test
033: public void invokes_all_constraint_generators() throws Exception {
034: getMocksControl().checkOrder(true);
035:
036: ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
037: FieldValidator fv1 = mockFieldValidator();
038: FieldValidator fv2 = mockFieldValidator();
039: FieldValidatorSource source = mockFieldValidatorSource();
040: Class propertyType = Integer.class;
041: AnnotationProvider provider = mockAnnotationProvider();
042: String overrideId = "overrideId";
043: Messages overrideMessages = mockMessages();
044: Field field = mockField();
045: Locale locale = Locale.ENGLISH;
046: String value = "*VALUE*";
047:
048: train_buildConstraints(gen, propertyType, provider, "cons1",
049: "cons2");
050:
051: train_createValidator(source, field, "cons1", null, overrideId,
052: overrideMessages, locale, fv1);
053:
054: train_createValidator(source, field, "cons2", null, overrideId,
055: overrideMessages, locale, fv2);
056:
057: fv1.validate(value);
058: fv2.validate(value);
059:
060: replay();
061:
062: FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(
063: gen, source);
064:
065: FieldValidator composite = fieldValidatorSource
066: .createDefaultValidator(field, overrideId,
067: overrideMessages, locale, propertyType,
068: provider);
069:
070: composite.validate(value);
071:
072: verify();
073: }
074:
075: @SuppressWarnings("unchecked")
076: @Test
077: public void validator_with_constraint() throws Exception {
078: ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
079: FieldValidator fv = mockFieldValidator();
080: FieldValidatorSource source = mockFieldValidatorSource();
081: Class propertyType = Integer.class;
082: AnnotationProvider provider = mockAnnotationProvider();
083: String overrideId = "overrideId";
084: Messages overrideMessages = mockMessages();
085: Field field = mockField();
086: Locale locale = Locale.ENGLISH;
087:
088: train_buildConstraints(gen, propertyType, provider, "foo=bar");
089:
090: train_createValidator(source, field, "foo", "bar", overrideId,
091: overrideMessages, locale, fv);
092:
093: replay();
094:
095: FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(
096: gen, source);
097:
098: FieldValidator composite = fieldValidatorSource
099: .createDefaultValidator(field, overrideId,
100: overrideMessages, locale, propertyType,
101: provider);
102:
103: assertSame(composite, fv);
104:
105: verify();
106: }
107:
108: @SuppressWarnings("unchecked")
109: @Test
110: public void no_validators_at_all() throws Exception {
111: ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
112: FieldValidatorSource source = mockFieldValidatorSource();
113: Class propertyType = Integer.class;
114: AnnotationProvider provider = mockAnnotationProvider();
115: String overrideId = "overrideId";
116: Messages overrideMessages = mockMessages();
117: Field field = mockField();
118: Locale locale = Locale.ENGLISH;
119: String value = "*VALUE*";
120:
121: train_buildConstraints(gen, propertyType, provider);
122:
123: replay();
124:
125: FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(
126: gen, source);
127:
128: FieldValidator composite = fieldValidatorSource
129: .createDefaultValidator(field, overrideId,
130: overrideMessages, locale, propertyType,
131: provider);
132:
133: composite.validate(value);
134:
135: verify();
136: }
137: }
|