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.validator;
16:
17: import java.util.regex.Pattern;
18:
19: import org.apache.tapestry.Field;
20: import org.apache.tapestry.ValidationException;
21: import org.apache.tapestry.internal.test.InternalBaseTestCase;
22: import org.apache.tapestry.ioc.MessageFormatter;
23: import org.testng.annotations.Test;
24:
25: /** These are getting tedious; I'd rather do it via integration tests. */
26: public class RegexpTest extends InternalBaseTestCase {
27: @Test
28: public void matching_pattern() throws Exception {
29: Field field = mockField();
30: MessageFormatter formatter = mockMessageFormatter();
31: Pattern constraint = Pattern.compile("\\d{4}");
32:
33: replay();
34:
35: Regexp validator = new Regexp();
36:
37: validator.validate(field, constraint, formatter, "1234");
38:
39: verify();
40: }
41:
42: @Test
43: public void input_mismatch() throws Exception {
44: String label = "My Field";
45: Field field = mockFieldWithLabel(label);
46: MessageFormatter formatter = mockMessageFormatter();
47: String message = "{message}";
48: Pattern constraint = Pattern.compile("\\d{4}");
49: String value = "abc";
50:
51: train_format(formatter, message, constraint.toString(), label);
52:
53: replay();
54:
55: Regexp validator = new Regexp();
56:
57: try {
58: validator.validate(field, constraint, formatter, value);
59: unreachable();
60: } catch (ValidationException ex) {
61: assertEquals(ex.getMessage(), message);
62: }
63:
64: verify();
65:
66: }
67: }
|