001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.spring.test;
018:
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Locale;
025: import java.util.Map;
026: import java.util.Map.Entry;
027:
028: import org.springframework.beans.BeansException;
029: import org.springframework.beans.factory.BeanFactory;
030: import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
031: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
032: import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
033: import org.springframework.context.ApplicationContext;
034: import org.springframework.context.ApplicationEvent;
035: import org.springframework.context.MessageSourceResolvable;
036: import org.springframework.context.NoSuchMessageException;
037: import org.springframework.core.io.Resource;
038:
039: /**
040: * Mock application context object. This mock context allows easy creation of
041: * unit tests by allowing the user to put bean instances into the context.
042: *
043: * Only {@link #getBean(String)}, {@link #getBean(String, Class)}, and
044: * {@link #getBeansOfType(Class)} are implemented so far. Any other method
045: * throws {@link UnsupportedOperationException}.
046: *
047: * @author Igor Vaynberg (ivaynberg)
048: *
049: */
050: public class ApplicationContextMock implements ApplicationContext,
051: Serializable {
052: private Map beans = new HashMap();
053:
054: /**
055: * puts bean with the given name into the context
056: *
057: * @param name
058: * @param bean
059: */
060: public void putBean(String name, Object bean) {
061: if (beans.containsKey(name)) {
062: throw new IllegalArgumentException("a bean with name ["
063: + name + "] has alredy been added to the context");
064: }
065: beans.put(name, bean);
066: }
067:
068: /**
069: * puts bean with into the context. bean object's class name will be used as
070: * the bean name.
071: *
072: * @param bean
073: */
074: public void putBean(Object bean) {
075: putBean(bean.getClass().getName(), bean);
076: }
077:
078: /**
079: * @see org.springframework.beans.factory.BeanFactory#getBean(java.lang.String)
080: */
081: public Object getBean(String name) throws BeansException {
082: Object bean = beans.get(name);
083: if (bean == null) {
084: throw new NoSuchBeanDefinitionException(name);
085: }
086: return bean;
087: }
088:
089: /**
090: * @see org.springframework.beans.factory.BeanFactory#getBean(java.lang.String,
091: * java.lang.Class)
092: */
093: public Object getBean(String name, Class requiredType)
094: throws BeansException {
095: Object bean = getBean(name);
096: if (!(requiredType.isAssignableFrom(bean.getClass()))) {
097: throw new BeanNotOfRequiredTypeException(name,
098: requiredType, bean.getClass());
099: }
100: return bean;
101: }
102:
103: /**
104: * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(java.lang.Class)
105: */
106: public Map getBeansOfType(Class type) throws BeansException {
107: Map found = new HashMap();
108:
109: Iterator it = beans.entrySet().iterator();
110: while (it.hasNext()) {
111: final Map.Entry entry = (Entry) it.next();
112: if (type.isAssignableFrom(entry.getValue().getClass())) {
113: found.put(entry.getKey(), entry.getValue());
114: }
115: }
116:
117: return found;
118: }
119:
120: /**
121: * @see org.springframework.context.ApplicationContext#getParent()
122: */
123: public ApplicationContext getParent() {
124: throw new UnsupportedOperationException();
125: }
126:
127: /**
128: * @see org.springframework.context.ApplicationContext#getDisplayName()
129: */
130: public String getDisplayName() {
131: throw new UnsupportedOperationException();
132: }
133:
134: /**
135: * @see org.springframework.context.ApplicationContext#getStartupDate()
136: */
137: public long getStartupDate() {
138: throw new UnsupportedOperationException();
139: }
140:
141: /**
142: * @see org.springframework.context.ApplicationContext#publishEvent(org.springframework.context.ApplicationEvent)
143: */
144: public void publishEvent(ApplicationEvent event) {
145: throw new UnsupportedOperationException();
146: }
147:
148: /**
149: * @see org.springframework.beans.factory.ListableBeanFactory#containsBeanDefinition(java.lang.String)
150: */
151: public boolean containsBeanDefinition(String beanName) {
152: throw new UnsupportedOperationException();
153: }
154:
155: /**
156: * @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionCount()
157: */
158: public int getBeanDefinitionCount() {
159: throw new UnsupportedOperationException();
160: }
161:
162: /**
163: * @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionNames()
164: */
165: public String[] getBeanDefinitionNames() {
166: throw new UnsupportedOperationException();
167: }
168:
169: /**
170: * @see org.springframework.beans.factory.ListableBeanFactory#getBeanNamesForType(java.lang.Class)
171: */
172: public String[] getBeanNamesForType(Class type) {
173: ArrayList names = new ArrayList();
174: Iterator entries = beans.entrySet().iterator();
175: while (entries.hasNext()) {
176: Entry entry = (Entry) entries.next();
177: Object bean = entry.getValue();
178:
179: if (type.isAssignableFrom(bean.getClass())) {
180: String name = (String) entry.getKey();
181: names.add(name);
182: }
183: }
184: return (String[]) names.toArray(new String[names.size()]);
185: }
186:
187: /**
188: * @see org.springframework.beans.factory.ListableBeanFactory#getBeanNamesForType(java.lang.Class,
189: * boolean, boolean)
190: */
191: public String[] getBeanNamesForType(Class type,
192: boolean includePrototypes, boolean includeFactoryBeans) {
193: throw new UnsupportedOperationException();
194: }
195:
196: /**
197: * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(java.lang.Class,
198: * boolean, boolean)
199: */
200: public Map getBeansOfType(Class type, boolean includePrototypes,
201: boolean includeFactoryBeans) throws BeansException {
202: throw new UnsupportedOperationException();
203: }
204:
205: /**
206: * @see org.springframework.beans.factory.BeanFactory#containsBean(java.lang.String)
207: */
208: public boolean containsBean(String name) {
209: return beans.containsKey(name);
210: }
211:
212: /**
213: * @see org.springframework.beans.factory.BeanFactory#isSingleton(java.lang.String)
214: */
215: public boolean isSingleton(String name)
216: throws NoSuchBeanDefinitionException {
217: return true;
218: }
219:
220: /**
221: * @see org.springframework.beans.factory.BeanFactory#getType(java.lang.String)
222: */
223: public Class getType(String name)
224: throws NoSuchBeanDefinitionException {
225: throw new UnsupportedOperationException();
226: }
227:
228: /**
229: * @see org.springframework.beans.factory.BeanFactory#getAliases(java.lang.String)
230: */
231: public String[] getAliases(String name)
232: throws NoSuchBeanDefinitionException {
233: throw new UnsupportedOperationException();
234: }
235:
236: /**
237: * @see org.springframework.beans.factory.HierarchicalBeanFactory#getParentBeanFactory()
238: */
239: public BeanFactory getParentBeanFactory() {
240: return null;
241: }
242:
243: /**
244: * @see org.springframework.context.MessageSource#getMessage(java.lang.String,
245: * java.lang.Object[], java.lang.String, java.util.Locale)
246: */
247: public String getMessage(String code, Object[] args,
248: String defaultMessage, Locale locale) {
249: throw new UnsupportedOperationException();
250: }
251:
252: /**
253: * @see org.springframework.context.MessageSource#getMessage(java.lang.String,
254: * java.lang.Object[], java.util.Locale)
255: */
256: public String getMessage(String code, Object[] args, Locale locale)
257: throws NoSuchMessageException {
258: throw new UnsupportedOperationException();
259: }
260:
261: /**
262: * @see org.springframework.context.MessageSource#getMessage(org.springframework.context.MessageSourceResolvable,
263: * java.util.Locale)
264: */
265: public String getMessage(MessageSourceResolvable resolvable,
266: Locale locale) throws NoSuchMessageException {
267: throw new UnsupportedOperationException();
268: }
269:
270: /**
271: * @see org.springframework.core.io.support.ResourcePatternResolver#getResources(java.lang.String)
272: */
273: public Resource[] getResources(String locationPattern)
274: throws IOException {
275: throw new UnsupportedOperationException();
276: }
277:
278: /**
279: * @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String)
280: */
281: public Resource getResource(String location) {
282: throw new UnsupportedOperationException();
283: }
284:
285: /**
286: * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
287: */
288: public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
289: throws IllegalStateException {
290: throw new UnsupportedOperationException();
291: }
292:
293: /**
294: * @see org.springframework.beans.factory.HierarchicalBeanFactory#containsLocalBean(java.lang.String)
295: */
296: public boolean containsLocalBean(String arg0) {
297: throw new UnsupportedOperationException();
298: }
299:
300: /**
301: * @see org.springframework.core.io.ResourceLoader#getClassLoader()
302: */
303: public ClassLoader getClassLoader() {
304: throw new UnsupportedOperationException();
305: }
306:
307: }
|