📄 applicationcontextmock.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.spring.test;import java.io.IOException;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.Map.Entry;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanNotOfRequiredTypeException;import org.springframework.beans.factory.NoSuchBeanDefinitionException;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationEvent;import org.springframework.context.MessageSourceResolvable;import org.springframework.context.NoSuchMessageException;import org.springframework.core.io.Resource;/** * Mock application context object. This mock context allows easy creation of unit tests by allowing * the user to put bean instances into the context. * * Only {@link #getBean(String)}, {@link #getBean(String, Class)}, and * {@link #getBeansOfType(Class)} are implemented so far. Any other method throws * {@link UnsupportedOperationException}. * * @author Igor Vaynberg (ivaynberg) * */public class ApplicationContextMock implements ApplicationContext, Serializable{ private Map beans = new HashMap(); /** * puts bean with the given name into the context * * @param name * @param bean */ public void putBean(String name, Object bean) { if (beans.containsKey(name)) { throw new IllegalArgumentException("a bean with name [" + name + "] has alredy been added to the context"); } beans.put(name, bean); } /** * puts bean with into the context. bean object's class name will be used as the bean name. * * @param bean */ public void putBean(Object bean) { putBean(bean.getClass().getName(), bean); } /** * @see org.springframework.beans.factory.BeanFactory#getBean(java.lang.String) */ public Object getBean(String name) throws BeansException { Object bean = beans.get(name); if (bean == null) { throw new NoSuchBeanDefinitionException(name); } return bean; } /** * @see org.springframework.beans.factory.BeanFactory#getBean(java.lang.String, java.lang.Class) */ public Object getBean(String name, Class requiredType) throws BeansException { Object bean = getBean(name); if (!(requiredType.isAssignableFrom(bean.getClass()))) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return bean; } /** * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(java.lang.Class) */ public Map getBeansOfType(Class type) throws BeansException { Map found = new HashMap(); Iterator it = beans.entrySet().iterator(); while (it.hasNext()) { final Map.Entry entry = (Entry)it.next(); if (type.isAssignableFrom(entry.getValue().getClass())) { found.put(entry.getKey(), entry.getValue()); } } return found; } /** * @see org.springframework.context.ApplicationContext#getParent() */ public ApplicationContext getParent() { throw new UnsupportedOperationException(); } /** * @see org.springframework.context.ApplicationContext#getDisplayName() */ public String getDisplayName() { throw new UnsupportedOperationException(); } /** * @see org.springframework.context.ApplicationContext#getStartupDate() */ public long getStartupDate() { throw new UnsupportedOperationException(); } /** * @see org.springframework.context.ApplicationContext#publishEvent(org.springframework.context.ApplicationEvent) */ public void publishEvent(ApplicationEvent event) { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.ListableBeanFactory#containsBeanDefinition(java.lang.String) */ public boolean containsBeanDefinition(String beanName) { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionCount() */ public int getBeanDefinitionCount() { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionNames() */ public String[] getBeanDefinitionNames() { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.ListableBeanFactory#getBeanNamesForType(java.lang.Class) */ public String[] getBeanNamesForType(Class type) { ArrayList names = new ArrayList(); Iterator entries = beans.entrySet().iterator(); while (entries.hasNext()) { Entry entry = (Entry)entries.next(); Object bean = entry.getValue(); if (type.isAssignableFrom(bean.getClass())) { String name = (String)entry.getKey(); names.add(name); } } return (String[])names.toArray(new String[names.size()]); } /** * @see org.springframework.beans.factory.ListableBeanFactory#getBeanNamesForType(java.lang.Class, * boolean, boolean) */ public String[] getBeanNamesForType(Class type, boolean includePrototypes, boolean includeFactoryBeans) { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(java.lang.Class, * boolean, boolean) */ public Map getBeansOfType(Class type, boolean includePrototypes, boolean includeFactoryBeans) throws BeansException { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.BeanFactory#containsBean(java.lang.String) */ public boolean containsBean(String name) { return beans.containsKey(name); } /** * @see org.springframework.beans.factory.BeanFactory#isSingleton(java.lang.String) */ public boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return true; } /** * @see org.springframework.beans.factory.BeanFactory#getType(java.lang.String) */ public Class getType(String name) throws NoSuchBeanDefinitionException { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.BeanFactory#getAliases(java.lang.String) */ public String[] getAliases(String name) throws NoSuchBeanDefinitionException { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.HierarchicalBeanFactory#getParentBeanFactory() */ public BeanFactory getParentBeanFactory() { return null; } /** * @see org.springframework.context.MessageSource#getMessage(java.lang.String, * java.lang.Object[], java.lang.String, java.util.Locale) */ public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) { throw new UnsupportedOperationException(); } /** * @see org.springframework.context.MessageSource#getMessage(java.lang.String, * java.lang.Object[], java.util.Locale) */ public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException { throw new UnsupportedOperationException(); } /** * @see org.springframework.context.MessageSource#getMessage(org.springframework.context.MessageSourceResolvable, * java.util.Locale) */ public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { throw new UnsupportedOperationException(); } /** * @see org.springframework.core.io.support.ResourcePatternResolver#getResources(java.lang.String) */ public Resource[] getResources(String locationPattern) throws IOException { throw new UnsupportedOperationException(); } /** * @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String) */ public Resource getResource(String location) { throw new UnsupportedOperationException(); } /** * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory() */ public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException { throw new UnsupportedOperationException(); } /** * @see org.springframework.beans.factory.HierarchicalBeanFactory#containsLocalBean(java.lang.String) */ public boolean containsLocalBean(String arg0) { throw new UnsupportedOperationException(); } /** * @see org.springframework.core.io.ResourceLoader#getClassLoader() */ public ClassLoader getClassLoader() { throw new UnsupportedOperationException(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -