01: // Copyright 2006 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.ioc.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
18:
19: import java.util.Collections;
20: import java.util.List;
21: import java.util.Map;
22:
23: import org.apache.tapestry.ioc.internal.IOCInternalTestCase;
24: import org.apache.tapestry.ioc.services.StrategyBuilder;
25: import org.apache.tapestry.ioc.util.StrategyRegistry;
26: import org.testng.annotations.Test;
27:
28: public class StrategyBuilderImplTest extends IOCInternalTestCase {
29: private static class KindOfImpl implements KindOf {
30: private final String _value;
31:
32: public KindOfImpl(final String value) {
33: _value = value;
34: }
35:
36: public String kindOf(Object value) {
37: return _value;
38: }
39:
40: };
41:
42: @Test
43: public void standard() {
44: StrategyRegistry<KindOf> registry = buildStrategyRegistry();
45:
46: StrategyBuilder builder = getService(StrategyBuilder.class);
47:
48: KindOf service = builder.build(registry);
49:
50: assertEquals(service.kindOf(Collections.EMPTY_MAP), "MAP");
51: assertEquals(service.kindOf(Collections.EMPTY_LIST), "LIST");
52:
53: assertEquals(service.toString(),
54: "<Strategy for org.apache.tapestry.ioc.internal.services.KindOf>");
55:
56: try {
57: service.kindOf(null);
58: unreachable();
59: } catch (RuntimeException ex) {
60: assertEquals(
61: ex.getMessage(),
62: "No adapter from type void to type org.apache.tapestry.ioc.internal.services.KindOf is available (registered types are java.util.List, java.util.Map).");
63: }
64: }
65:
66: private StrategyRegistry<KindOf> buildStrategyRegistry() {
67: Map<Class, KindOf> registrations = newMap();
68:
69: registrations.put(Map.class, new KindOfImpl("MAP"));
70: registrations.put(List.class, new KindOfImpl("LIST"));
71:
72: StrategyRegistry<KindOf> registry = StrategyRegistry
73: .newInstance(KindOf.class, registrations);
74: return registry;
75: }
76: }
|