001: // Copyright 2006, 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 static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.Collections;
020: import java.util.Map;
021:
022: import org.apache.tapestry.internal.test.InternalBaseTestCase;
023: import org.apache.tapestry.ioc.AnnotationProvider;
024: import org.apache.tapestry.ioc.ObjectProvider;
025: import org.apache.tapestry.ioc.ObjectLocator;
026: import org.apache.tapestry.services.Alias;
027: import org.apache.tapestry.services.AliasManager;
028: import org.testng.annotations.Test;
029:
030: public class AliasImplTest extends InternalBaseTestCase {
031: private Map<Class, Object> _emptyMap = Collections.emptyMap();
032:
033: protected final AliasManager newAliasManager() {
034: return newMock(AliasManager.class);
035: }
036:
037: @Test
038: public void resolve_object_within_mode() {
039: String mode = "papyrus";
040:
041: AliasManager manager = newAliasManager();
042: AliasManager overridesManager = newAliasManager();
043: AnnotationProvider annotationProvider = mockAnnotationProvider();
044:
045: ObjectLocator locator = mockObjectLocator();
046: Runnable r = mockRunnable();
047:
048: Map<Class, Object> configuration = newMap();
049: configuration.put(Runnable.class, r);
050:
051: train_getAliasesForMode(manager, mode, configuration);
052: train_getAliasesForMode(overridesManager, mode, _emptyMap);
053:
054: replay();
055:
056: Alias alias = new AliasImpl(manager, mode, overridesManager);
057:
058: // Do not assume that infra and provider are the same;
059: // that's an implementation choice.
060:
061: ObjectProvider provider = alias.getObjectProvider();
062:
063: // Run through a couple of times to ensure that values are cached.
064: for (int i = 0; i < 2; i++) {
065: Runnable actual = provider.provide(Runnable.class,
066: annotationProvider, locator);
067:
068: assertSame(actual, r);
069: }
070:
071: verify();
072: }
073:
074: @Test
075: public void overrides_manager_has_precendence() {
076: String mode = "papyrus";
077:
078: AliasManager manager = newAliasManager();
079: AliasManager overridesManager = newAliasManager();
080: AnnotationProvider annotationProvider = mockAnnotationProvider();
081:
082: ObjectLocator locator = mockObjectLocator();
083: Runnable r = mockRunnable();
084: Runnable override = mockRunnable();
085:
086: Map<Class, Object> configuration = newMap();
087: configuration.put(Runnable.class, r);
088:
089: train_getAliasesForMode(manager, mode, configuration);
090:
091: Map<Class, Object> overrideConfiguration = newMap();
092: configuration.put(Runnable.class, override);
093:
094: train_getAliasesForMode(overridesManager, mode,
095: overrideConfiguration);
096:
097: replay();
098:
099: Alias alias = new AliasImpl(manager, mode, overridesManager);
100:
101: ObjectProvider provider = alias.getObjectProvider();
102:
103: Runnable actual = provider.provide(Runnable.class,
104: annotationProvider, locator);
105:
106: assertSame(actual, override);
107:
108: verify();
109: }
110:
111: @Test
112: public void type_not_found_in_configuration() {
113: String mode = "papyrus";
114:
115: AliasManager manager = newAliasManager();
116: AliasManager overridesManager = newAliasManager();
117: AnnotationProvider annotationProvider = mockAnnotationProvider();
118:
119: ObjectLocator locator = mockObjectLocator();
120:
121: train_getAliasesForMode(manager, mode, _emptyMap);
122: train_getAliasesForMode(overridesManager, mode, _emptyMap);
123:
124: replay();
125:
126: Alias alias = new AliasImpl(manager, mode, overridesManager);
127:
128: // Do not assume that infra and provider are the same;
129: // that's an implementation choice.
130:
131: ObjectProvider provider = alias.getObjectProvider();
132:
133: assertNull(provider.provide(Runnable.class, annotationProvider,
134: locator));
135:
136: verify();
137:
138: }
139: }
|