01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.beaneditor;
16:
17: import java.util.Arrays;
18:
19: import org.apache.tapestry.PropertyConduit;
20: import org.apache.tapestry.beaneditor.Validate;
21: import org.apache.tapestry.internal.test.InternalBaseTestCase;
22: import org.apache.tapestry.services.ValidationConstraintGenerator;
23: import org.testng.annotations.Test;
24:
25: public class ValidateAnnotationConstraintGeneratorTest extends
26: InternalBaseTestCase {
27: @Test
28: public void no_annotation() {
29: PropertyConduit conduit = mockPropertyConduit();
30:
31: train_getAnnotation(conduit, Validate.class, null);
32:
33: replay();
34:
35: ValidationConstraintGenerator gen = new ValidateAnnotationConstraintGenerator();
36:
37: assertNull(gen.buildConstraints(Object.class, conduit));
38:
39: verify();
40: }
41:
42: @Test
43: public void single_constraint() {
44: PropertyConduit conduit = mockPropertyConduit();
45: Validate validate = newValidate("required");
46:
47: train_getAnnotation(conduit, Validate.class, validate);
48:
49: replay();
50:
51: ValidationConstraintGenerator gen = new ValidateAnnotationConstraintGenerator();
52:
53: assertEquals(gen.buildConstraints(Object.class, conduit),
54: Arrays.asList("required"));
55:
56: verify();
57: }
58:
59: @Test
60: public void multiple_constraints() {
61: PropertyConduit conduit = mockPropertyConduit();
62: Validate validate = newValidate("required,minlength=3");
63:
64: train_getAnnotation(conduit, Validate.class, validate);
65:
66: replay();
67:
68: ValidationConstraintGenerator gen = new ValidateAnnotationConstraintGenerator();
69:
70: assertEquals(gen.buildConstraints(null, conduit), Arrays
71: .asList("required", "minlength=3"));
72:
73: verify();
74: }
75:
76: private Validate newValidate(String value) {
77: Validate annotation = newMock(Validate.class);
78:
79: expect(annotation.value()).andReturn(value).atLeastOnce();
80:
81: return annotation;
82: }
83: }
|