01: // Copyright 2006, 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.bindings;
16:
17: import org.apache.tapestry.Binding;
18: import org.apache.tapestry.ComponentResources;
19: import org.apache.tapestry.Field;
20: import org.apache.tapestry.FieldValidator;
21: import org.apache.tapestry.ioc.Location;
22: import org.apache.tapestry.ioc.internal.util.TapestryException;
23: import org.apache.tapestry.runtime.Component;
24: import org.apache.tapestry.services.BindingFactory;
25: import org.apache.tapestry.services.FieldValidatorSource;
26: import org.apache.tapestry.test.TapestryTestCase;
27: import org.testng.annotations.Test;
28:
29: public class ValidateBindingFactoryTest extends TapestryTestCase {
30: private interface FieldComponent extends Field, Component {
31: };
32:
33: @Test
34: public void not_a_field() {
35: FieldValidatorSource source = mockFieldValidatorSource();
36: ComponentResources container = mockComponentResources();
37: ComponentResources component = mockComponentResources();
38: Component instance = mockComponent();
39: Location l = mockLocation();
40:
41: train_getComponent(component, instance);
42: train_getCompleteId(component, "foo.Bar:baz");
43:
44: replay();
45:
46: BindingFactory factory = new ValidateBindingFactory(source);
47:
48: try {
49: factory.newBinding("descrip", container, component,
50: "zip,zoom", l);
51: } catch (TapestryException ex) {
52: assertEquals(
53: ex.getMessage(),
54: "Component 'foo.Bar:baz' is not a field (it does not implement the Field interface) and may not be used with the validate: binding prefix.");
55: assertSame(ex.getLocation(), l);
56: }
57:
58: verify();
59: }
60:
61: @Test
62: public void success() {
63:
64: FieldValidatorSource source = mockFieldValidatorSource();
65: ComponentResources container = mockComponentResources();
66: ComponentResources component = mockComponentResources();
67: FieldComponent instance = newMock(FieldComponent.class);
68: Location l = mockLocation();
69: FieldValidator validator = mockFieldValidator();
70:
71: String expression = "required,minLength=5";
72:
73: train_getComponent(component, instance);
74:
75: expect(source.createValidators(instance, expression))
76: .andReturn(validator);
77:
78: replay();
79:
80: BindingFactory factory = new ValidateBindingFactory(source);
81:
82: Binding binding = factory.newBinding("descrip", container,
83: component, expression, l);
84:
85: assertSame(binding.get(), validator);
86:
87: verify();
88: }
89: }
|