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 org.apache.tapestry.Binding;
018: import org.apache.tapestry.ComponentResources;
019: import org.apache.tapestry.TapestryConstants;
020: import org.apache.tapestry.internal.test.InternalBaseTestCase;
021: import org.apache.tapestry.ioc.Messages;
022: import org.apache.tapestry.ioc.services.ClassPropertyAdapter;
023: import org.apache.tapestry.ioc.services.PropertyAccess;
024: import org.apache.tapestry.ioc.services.PropertyAdapter;
025: import org.apache.tapestry.runtime.Component;
026: import org.apache.tapestry.services.BindingSource;
027: import org.apache.tapestry.services.ComponentDefaultProvider;
028: import org.testng.annotations.Test;
029:
030: public class ComponentDefaultProviderImplTest extends
031: InternalBaseTestCase {
032: @Test
033: public void default_label_key_exists() {
034: ComponentResources resources = mockComponentResources();
035: ComponentResources container = mockComponentResources();
036: Messages messages = mockMessages();
037:
038: String componentId = "myfield";
039: String key = componentId + "-label";
040: String message = "My Lovely Field";
041:
042: train_getId(resources, componentId);
043: train_getContainerResources(resources, container);
044: train_getMessages(container, messages);
045: train_contains(messages, key, true);
046: train_get(messages, key, message);
047:
048: replay();
049:
050: ComponentDefaultProvider provider = new ComponentDefaultProviderImpl(
051: null, null);
052:
053: assertSame(provider.defaultLabel(resources), message);
054:
055: verify();
056: }
057:
058: @Test
059: public void default_label_key_missing() {
060: ComponentResources resources = mockComponentResources();
061: ComponentResources container = mockComponentResources();
062: Messages messages = mockMessages();
063:
064: String componentId = "myField";
065: String key = componentId + "-label";
066:
067: train_getId(resources, componentId);
068: train_getContainerResources(resources, container);
069: train_getMessages(container, messages);
070: train_contains(messages, key, false);
071:
072: replay();
073:
074: ComponentDefaultProvider provider = new ComponentDefaultProviderImpl(
075: null, null);
076:
077: assertEquals(provider.defaultLabel(resources), "My Field");
078:
079: verify();
080: }
081:
082: @Test
083: public void no_matching_property_for_default() {
084: String parameterName = "myparam";
085:
086: String id = "mycomponentid";
087:
088: ComponentResources resources = mockComponentResources();
089: Component container = mockComponent();
090: PropertyAccess access = newPropertyAccess();
091: ClassPropertyAdapter classPropertyAdapter = newClassPropertyAdapter();
092: BindingSource bindingSource = mockBindingSource();
093:
094: train_getId(resources, id);
095: train_getContainer(resources, container);
096:
097: train_getAdapter(access, container, classPropertyAdapter);
098: train_getPropertyAdapter(classPropertyAdapter, id, null);
099:
100: replay();
101:
102: ComponentDefaultProvider source = new ComponentDefaultProviderImpl(
103: access, bindingSource);
104:
105: assertNull(source.defaultBinding(parameterName, resources));
106:
107: verify();
108: }
109:
110: @Test
111: public void default_property_exists() {
112: String parameterName = "myparam";
113:
114: String id = "mycomponentid";
115:
116: ComponentResources resources = mockComponentResources();
117: Component container = mockComponent();
118: PropertyAccess access = newPropertyAccess();
119: ClassPropertyAdapter classPropertyAdapter = newClassPropertyAdapter();
120: PropertyAdapter propertyAdapter = newPropertyAdapter();
121: BindingSource bindingSource = mockBindingSource();
122: Binding binding = mockBinding();
123: ComponentResources containerResources = mockComponentResources();
124:
125: train_getId(resources, id);
126: train_getContainer(resources, container);
127:
128: train_getAdapter(access, container, classPropertyAdapter);
129: train_getPropertyAdapter(classPropertyAdapter, id,
130: propertyAdapter);
131:
132: train_getContainerResources(resources, containerResources);
133:
134: train_newBinding(bindingSource, "default myparam",
135: containerResources,
136: TapestryConstants.PROP_BINDING_PREFIX, id, binding);
137:
138: replay();
139:
140: ComponentDefaultProvider source = new ComponentDefaultProviderImpl(
141: access, bindingSource);
142:
143: assertSame(source.defaultBinding(parameterName, resources),
144: binding);
145:
146: verify();
147: }
148:
149: protected final PropertyAdapter newPropertyAdapter() {
150: return newMock(PropertyAdapter.class);
151: }
152:
153: protected final ClassPropertyAdapter newClassPropertyAdapter() {
154: return newMock(ClassPropertyAdapter.class);
155: }
156:
157: protected final PropertyAccess newPropertyAccess() {
158: return newMock(PropertyAccess.class);
159: }
160:
161: protected final void train_getPropertyAdapter(
162: ClassPropertyAdapter classPropertyAdapter,
163: String propertyName, PropertyAdapter propertyAdapter) {
164: expect(classPropertyAdapter.getPropertyAdapter(propertyName))
165: .andReturn(propertyAdapter).atLeastOnce();
166: }
167:
168: protected final void train_getAdapter(PropertyAccess access,
169: Object object, ClassPropertyAdapter classPropertyAdapter) {
170: expect(access.getAdapter(object)).andReturn(
171: classPropertyAdapter);
172: }
173: }
|