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.ioc.util;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.apache.tapestry.ioc.test.IOCTestCase;
025: import org.testng.annotations.Test;
026:
027: public class StrategyRegistryTest extends IOCTestCase {
028: @Test
029: public void adapter_not_found() {
030: Runnable r1 = mockRunnable();
031: Runnable r2 = mockRunnable();
032:
033: replay();
034:
035: Map<Class, Runnable> registrations = newMap();
036:
037: registrations.put(List.class, r1);
038: registrations.put(Map.class, r2);
039:
040: StrategyRegistry<Runnable> r = StrategyRegistry.newInstance(
041: Runnable.class, registrations);
042:
043: try {
044: r.get(Set.class);
045: unreachable();
046: } catch (IllegalArgumentException ex) {
047: assertEquals(
048: ex.getMessage(),
049: "No adapter from type java.util.Set to type java.lang.Runnable is available (registered types are java.util.List, java.util.Map).");
050: }
051:
052: verify();
053: }
054:
055: @Test
056: public void adapter_found() {
057: Runnable r1 = mockRunnable();
058: Runnable r2 = mockRunnable();
059:
060: replay();
061:
062: Map<Class, Runnable> registrations = newMap();
063:
064: registrations.put(List.class, r1);
065: registrations.put(Map.class, r2);
066:
067: StrategyRegistry<Runnable> r = StrategyRegistry.newInstance(
068: Runnable.class, registrations);
069:
070: Runnable actual = r.get(ArrayList.class);
071:
072: assertSame(actual, r1);
073:
074: // The cache is almost impossible to "test", but we can at least collect some
075: // code coverage over those lines.
076:
077: Runnable actual2 = r.get(ArrayList.class);
078: assertSame(actual2, r1);
079:
080: r.clearCache();
081:
082: Runnable actual3 = r.get(ArrayList.class);
083: assertSame(actual3, r1);
084:
085: verify();
086: }
087:
088: @Test
089: public void registration_map_is_copied_by_constructor() {
090: Runnable r1 = mockRunnable();
091: Runnable r2 = mockRunnable();
092:
093: replay();
094:
095: Map<Class, Runnable> registrations = newMap();
096:
097: registrations.put(List.class, r1);
098: registrations.put(Map.class, r2);
099:
100: StrategyRegistry<Runnable> r = StrategyRegistry.newInstance(
101: Runnable.class, registrations);
102:
103: registrations.clear();
104:
105: Runnable actual = r.get(ArrayList.class);
106:
107: assertSame(actual, r1);
108: }
109:
110: @Test
111: public void adapter_found_by_instance() {
112: Runnable r1 = mockRunnable();
113: Runnable r2 = mockRunnable();
114:
115: replay();
116:
117: Map<Class, Runnable> registrations = newMap();
118:
119: registrations.put(List.class, r1);
120: registrations.put(Map.class, r2);
121:
122: StrategyRegistry<Runnable> r = StrategyRegistry.newInstance(
123: Runnable.class, registrations);
124:
125: assertSame(r.getByInstance(registrations), r2);
126:
127: verify();
128: }
129:
130: @Test
131: public void null_instance_matches_class_void() {
132: Runnable r1 = mockRunnable();
133: Runnable r2 = mockRunnable();
134: Runnable r3 = mockRunnable();
135:
136: replay();
137:
138: Map<Class, Runnable> registrations = newMap();
139:
140: registrations.put(List.class, r1);
141: registrations.put(Map.class, r2);
142: registrations.put(void.class, r3);
143:
144: StrategyRegistry<Runnable> r = StrategyRegistry.newInstance(
145: Runnable.class, registrations);
146:
147: assertSame(r.getByInstance(null), r3);
148:
149: verify();
150: }
151: }
|