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.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.tapestry.ioc.MappedConfiguration;
024: import org.apache.tapestry.ioc.def.ContributionDef;
025: import org.testng.annotations.Test;
026:
027: public class ValidatingMappedConfigurationWrapperTest extends
028: IOCInternalTestCase {
029: private static final String SERVICE_ID = "Baz";
030:
031: @Test
032: public void proper_key_and_value() {
033: ContributionDef def = mockContributionDef();
034: Log log = mockLog();
035: Map<Class, ContributionDef> keyToContribution = newMap();
036: MappedConfiguration<Class, Runnable> delegate = mockMappedConfiguration();
037:
038: Class key = Integer.class;
039: Runnable value = mockRunnable();
040:
041: delegate.add(key, value);
042:
043: replay();
044:
045: MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>(
046: SERVICE_ID, def, log, Class.class, Runnable.class,
047: keyToContribution, delegate);
048:
049: wrapper.add(key, value);
050:
051: verify();
052:
053: assertSame(keyToContribution.get(Integer.class), def);
054: }
055:
056: @Test
057: public void duplicate_key() {
058: ContributionDef def1 = newContributionDef("contributionPlaceholder1");
059: ContributionDef def2 = newContributionDef("contributionPlaceholder2");
060: Log log = mockLog();
061: Map<Class, ContributionDef> keyToContribution = newMap();
062:
063: keyToContribution.put(Integer.class, def1);
064:
065: MappedConfiguration<Class, Runnable> delegate = mockMappedConfiguration();
066:
067: Class key = Integer.class;
068: Runnable value = mockRunnable();
069:
070: log.warn(IOCMessages.contributionDuplicateKey(SERVICE_ID, def2,
071: def1));
072:
073: replay();
074:
075: MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>(
076: SERVICE_ID, def2, log, Class.class, Runnable.class,
077: keyToContribution, delegate);
078:
079: wrapper.add(key, value);
080:
081: verify();
082:
083: assertSame(keyToContribution.get(Integer.class), def1);
084: }
085:
086: @Test
087: public void null_key() {
088: ContributionDef def = newContributionDef("contributionPlaceholder1");
089: Log log = mockLog();
090: Map<Class, ContributionDef> keyToContribution = newMap();
091: MappedConfiguration<Class, Runnable> delegate = mockMappedConfiguration();
092: Runnable value = mockRunnable();
093:
094: log.warn(IOCMessages.contributionKeyWasNull(SERVICE_ID, def));
095:
096: replay();
097:
098: MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>(
099: SERVICE_ID, def, log, Class.class, Runnable.class,
100: keyToContribution, delegate);
101:
102: wrapper.add(null, value);
103:
104: verify();
105: }
106:
107: @SuppressWarnings("unchecked")
108: @Test
109: public void wrong_key_type() {
110: ContributionDef def = newContributionDef("contributionPlaceholder1");
111: Log log = mockLog();
112: Map<?, ContributionDef> keyToContribution = newMap();
113: MappedConfiguration delegate = mockMappedConfiguration();
114: Runnable value = mockRunnable();
115:
116: log.warn(IOCMessages.contributionWrongKeyType(SERVICE_ID, def,
117: String.class, Class.class));
118:
119: replay();
120:
121: MappedConfiguration wrapper = new ValidatingMappedConfigurationWrapper(
122: SERVICE_ID, def, log, Class.class, Runnable.class,
123: keyToContribution, delegate);
124:
125: wrapper.add("java.util.List", value);
126:
127: verify();
128: }
129:
130: @SuppressWarnings("unchecked")
131: @Test
132: public void wrong_value_type() {
133: ContributionDef def = newContributionDef("contributionPlaceholder1");
134: Log log = mockLog();
135: Map<?, ContributionDef> keyToContribution = newMap();
136: MappedConfiguration delegate = mockMappedConfiguration();
137:
138: log.warn(IOCMessages.contributionWrongValueType(SERVICE_ID,
139: def, String.class, Runnable.class));
140:
141: replay();
142:
143: MappedConfiguration wrapper = new ValidatingMappedConfigurationWrapper(
144: SERVICE_ID, def, log, Class.class, Runnable.class,
145: keyToContribution, delegate);
146:
147: wrapper.add(List.class, "do something");
148:
149: verify();
150: }
151:
152: @Test
153: public void null_value() {
154: ContributionDef def = newContributionDef("contributionPlaceholder1");
155: Log log = mockLog();
156: Map<Class, ContributionDef> keyToContribution = newMap();
157: MappedConfiguration<Class, Runnable> delegate = mockMappedConfiguration();
158:
159: log.warn(IOCMessages.contributionWasNull(SERVICE_ID, def));
160:
161: replay();
162:
163: MappedConfiguration<Class, Runnable> wrapper = new ValidatingMappedConfigurationWrapper<Class, Runnable>(
164: SERVICE_ID, def, log, Class.class, Runnable.class,
165: keyToContribution, delegate);
166:
167: wrapper.add(Integer.class, null);
168:
169: verify();
170:
171: }
172:
173: private ContributionDef newContributionDef(String methodName) {
174: return new ContributionDefImpl(SERVICE_ID,
175: findMethod(methodName), getClassFactory());
176: }
177:
178: public void contributionPlaceholder1() {
179:
180: }
181:
182: public void contributionPlaceholder2() {
183:
184: }
185:
186: }
|