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.internal;
016:
017: import static org.easymock.EasyMock.eq;
018: import static org.easymock.EasyMock.isA;
019:
020: import java.lang.reflect.Method;
021:
022: import org.apache.tapestry.ioc.AnnotationProvider;
023: import org.apache.tapestry.ioc.Configuration;
024: import org.apache.tapestry.ioc.MappedConfiguration;
025: import org.apache.tapestry.ioc.ModuleBuilderSource;
026: import org.apache.tapestry.ioc.OrderedConfiguration;
027: import org.apache.tapestry.ioc.ObjectLocator;
028: import org.apache.tapestry.ioc.annotations.InjectService;
029: import org.apache.tapestry.ioc.def.ContributionDef;
030: import org.apache.tapestry.ioc.test.IOCTestCase;
031: import org.testng.annotations.Test;
032:
033: public class ContributionDefImplTest extends IOCTestCase implements
034: ModuleBuilderSource {
035: private Object _toContribute;
036:
037: public Object getModuleBuilder() {
038: return this ;
039: }
040:
041: @SuppressWarnings("unchecked")
042: @Test
043: public void unordered_contribution() {
044: _toContribute = new Object();
045: Configuration configuration = mockConfiguration();
046: ObjectLocator locator = mockObjectLocator();
047:
048: configuration.add(_toContribute);
049:
050: replay();
051:
052: Method m = findMethod("contributeUnordered");
053: ContributionDef def = new ContributionDefImpl("foo.Bar", m,
054: null);
055:
056: def.contribute(this , locator, configuration);
057:
058: verify();
059: }
060:
061: @SuppressWarnings("unchecked")
062: @Test
063: public void unordered_collection_with_service_lookup() {
064: Configuration configuration = mockConfiguration();
065: ObjectLocator locator = mockObjectLocator();
066: UpcaseService service = mockUpcaseService();
067:
068: train_getService(locator, "zip.Zap", UpcaseService.class,
069: service);
070:
071: configuration.add(service);
072:
073: replay();
074:
075: Method m = findMethod("contributeUnorderedParameter");
076: ContributionDef def = new ContributionDefImpl("foo.Bar", m,
077: null);
078:
079: def.contribute(this , locator, configuration);
080:
081: verify();
082: }
083:
084: @Test
085: public void unordered_collection_with_incorrect_configuration_parameter() {
086: Configuration configuration = mockConfiguration();
087: ObjectLocator locator = mockObjectLocator();
088:
089: Throwable t = new RuntimeException("Missing service.");
090:
091: expect(
092: locator.getObject(eq(MappedConfiguration.class),
093: isA(AnnotationProvider.class))).andThrow(t);
094:
095: replay();
096:
097: Method m = findMethod("contributeUnorderedWrongParameter");
098: ContributionDef def = new ContributionDefImpl("foo.Bar", m,
099: null);
100:
101: try {
102: def.contribute(this , locator, configuration);
103: unreachable();
104: } catch (RuntimeException ex) {
105: assertEquals(
106: ex.getMessage(),
107: "Error invoking service contribution method "
108: + getClass().getName()
109: + ".contributeUnorderedWrongParameter(MappedConfiguration): Missing service.");
110: }
111:
112: verify();
113: }
114:
115: // From here on in, it's an almost identical code path, so we won't be
116: // as exhaustive.
117:
118: @SuppressWarnings("unchecked")
119: @Test
120: public void ordered_collection_with_service_lookup() {
121: OrderedConfiguration configuration = mockOrderedConfiguration();
122: ObjectLocator locator = mockObjectLocator();
123: UpcaseService service = mockUpcaseService();
124:
125: train_getService(locator, "zip.Zap", UpcaseService.class,
126: service);
127:
128: configuration.add("fred", service);
129:
130: replay();
131:
132: Method m = findMethod("contributeOrderedParameter");
133: ContributionDef def = new ContributionDefImpl("foo.Bar", m,
134: null);
135:
136: def.contribute(this , locator, configuration);
137:
138: verify();
139: }
140:
141: @SuppressWarnings("unchecked")
142: @Test
143: public void mapped_collection_with_service_lookup() {
144: MappedConfiguration configuration = mockMappedConfiguration();
145: ObjectLocator locator = mockObjectLocator();
146: UpcaseService service = mockUpcaseService();
147:
148: train_getService(locator, "zip.Zap", UpcaseService.class,
149: service);
150:
151: configuration.add("upcase", service);
152:
153: replay();
154:
155: Method m = findMethod("contributeMappedParameter");
156: ContributionDef def = new ContributionDefImpl("foo.Bar", m,
157: null);
158:
159: def.contribute(this , locator, configuration);
160:
161: verify();
162: }
163:
164: private UpcaseService mockUpcaseService() {
165: return newMock(UpcaseService.class);
166: }
167:
168: @SuppressWarnings("unchecked")
169: public void contributeUnordered(Configuration configuration) {
170: configuration.add(_toContribute);
171: }
172:
173: public void contributeUnorderedParameter(
174: Configuration<UpcaseService> configuration,
175: @InjectService("zip.Zap")
176: UpcaseService service) {
177: configuration.add(service);
178: }
179:
180: public void contributeOrderedParameter(
181: OrderedConfiguration<UpcaseService> configuration,
182: @InjectService("zip.Zap")
183: UpcaseService service) {
184: configuration.add("fred", service);
185: }
186:
187: public void contributeMappedParameter(
188: MappedConfiguration<String, UpcaseService> configuration,
189: @InjectService("zip.Zap")
190: UpcaseService service) {
191: configuration.add("upcase", service);
192: }
193:
194: public void contributeUnorderedWrongParameter(
195: MappedConfiguration configuration) {
196: unreachable();
197: }
198: }
|