Use Gradle test fixture support for spring-beans and spring-context

See gh-23550
This commit is contained in:
Sam Brannen
2019-12-28 17:13:39 +01:00
parent 5718bf424b
commit 61d4ee594d
585 changed files with 2270 additions and 1618 deletions

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
public interface AgeHolder {
default int age() {
return getAge();
}
int getAge();
void setAge(int age);
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Stephane Nicoll
*/
@TestAnnotation
public class AnnotatedBean {
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Juergen Hoeller
* @author Chris Beams
* @since 10.06.2003
*/
public class BooleanTestBean {
private boolean bool1;
private Boolean bool2;
public boolean isBool1() {
return bool1;
}
public void setBool1(boolean bool1) {
this.bool1 = bool1;
}
public Boolean getBool2() {
return bool2;
}
public void setBool2(Boolean bool2) {
this.bool2 = bool2;
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.parsing.AliasDefinition;
import org.springframework.beans.factory.parsing.ComponentDefinition;
import org.springframework.beans.factory.parsing.DefaultsDefinition;
import org.springframework.beans.factory.parsing.ImportDefinition;
import org.springframework.beans.factory.parsing.ReaderEventListener;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class CollectingReaderEventListener implements ReaderEventListener {
private final List<DefaultsDefinition> defaults = new LinkedList<>();
private final Map<String, ComponentDefinition> componentDefinitions = new LinkedHashMap<>(8);
private final Map<String, List<AliasDefinition>> aliasMap = new LinkedHashMap<>(8);
private final List<ImportDefinition> imports = new LinkedList<>();
@Override
public void defaultsRegistered(DefaultsDefinition defaultsDefinition) {
this.defaults.add(defaultsDefinition);
}
public List<DefaultsDefinition> getDefaults() {
return Collections.unmodifiableList(this.defaults);
}
@Override
public void componentRegistered(ComponentDefinition componentDefinition) {
this.componentDefinitions.put(componentDefinition.getName(), componentDefinition);
}
public ComponentDefinition getComponentDefinition(String name) {
return this.componentDefinitions.get(name);
}
public ComponentDefinition[] getComponentDefinitions() {
Collection<ComponentDefinition> collection = this.componentDefinitions.values();
return collection.toArray(new ComponentDefinition[collection.size()]);
}
@Override
public void aliasRegistered(AliasDefinition aliasDefinition) {
List<AliasDefinition> aliases = this.aliasMap.get(aliasDefinition.getBeanName());
if (aliases == null) {
aliases = new ArrayList<>();
this.aliasMap.put(aliasDefinition.getBeanName(), aliases);
}
aliases.add(aliasDefinition);
}
public List<AliasDefinition> getAliases(String beanName) {
List<AliasDefinition> aliases = this.aliasMap.get(beanName);
return (aliases != null ? Collections.unmodifiableList(aliases) : null);
}
@Override
public void importProcessed(ImportDefinition importDefinition) {
this.imports.add(importDefinition);
}
public List<ImportDefinition> getImports() {
return Collections.unmodifiableList(this.imports);
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class Colour {
public static final Colour RED = new Colour("RED");
public static final Colour BLUE = new Colour("BLUE");
public static final Colour GREEN = new Colour("GREEN");
public static final Colour PURPLE = new Colour("PURPLE");
private final String name;
public Colour(String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Juergen Hoeller
* @since 15.03.2005
*/
public class CountingTestBean extends TestBean {
public static int count = 0;
public CountingTestBean() {
count++;
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Juergen Hoeller
*/
public enum CustomEnum {
VALUE_1, VALUE_2;
@Override
public String toString() {
return "CustomEnum: " + name();
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
/**
* Simple bean used to test dependency checking.
*
* @author Rod Johnson
* @since 04.09.2003
*/
public class DependenciesBean implements BeanFactoryAware {
private int age;
private String name;
private TestBean spouse;
private BeanFactory beanFactory;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSpouse(TestBean spouse) {
this.spouse = spouse;
}
public TestBean getSpouse() {
return spouse;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.io.Serializable;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
/**
* @author Juergen Hoeller
* @since 21.08.2003
*/
@SuppressWarnings("serial")
public class DerivedTestBean extends TestBean implements Serializable, BeanNameAware, DisposableBean {
private String beanName;
private boolean initialized;
private boolean destroyed;
public DerivedTestBean() {
}
public DerivedTestBean(String[] names) {
if (names == null || names.length < 2) {
throw new IllegalArgumentException("Invalid names array");
}
setName(names[0]);
setBeanName(names[1]);
}
public static DerivedTestBean create(String[] names) {
return new DerivedTestBean(names);
}
@Override
public void setBeanName(String beanName) {
if (this.beanName == null || beanName == null) {
this.beanName = beanName;
}
}
@Override
public String getBeanName() {
return beanName;
}
public void setActualSpouse(TestBean spouse) {
setSpouse(spouse);
}
public void setSpouseRef(String name) {
setSpouse(new TestBean(name));
}
@Override
public TestBean getSpouse() {
return (TestBean) super.getSpouse();
}
public void initialize() {
this.initialized = true;
}
public boolean wasInitialized() {
return initialized;
}
@Override
public void destroy() {
this.destroyed = true;
}
@Override
public boolean wasDestroyed() {
return destroyed;
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Costin Leau
*/
public class DummyBean {
private Object value;
private String name;
private int age;
private TestBean spouse;
public DummyBean(Object value) {
this.value = value;
}
public DummyBean(String name, int age) {
this.name = name;
this.age = age;
}
public DummyBean(int ageRef, String nameRef) {
this.name = nameRef;
this.age = ageRef;
}
public DummyBean(String name, TestBean spouse) {
this.name = name;
this.spouse = spouse;
}
public DummyBean(String name, Object value, int age) {
this.name = name;
this.value = value;
this.age = age;
}
public Object getValue() {
return value;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public TestBean getSpouse() {
return spouse;
}
}

View File

@@ -0,0 +1,185 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
/**
* Simple factory to allow testing of FactoryBean support in AbstractBeanFactory.
* Depending on whether its singleton property is set, it will return a singleton
* or a prototype instance.
*
* <p>Implements InitializingBean interface, so we can check that
* factories get this lifecycle callback if they want.
*
* @author Rod Johnson
* @author Chris Beams
* @since 10.03.2003
*/
public class DummyFactory
implements FactoryBean<Object>, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
public static final String SINGLETON_NAME = "Factory singleton";
private static boolean prototypeCreated;
/**
* Clear static state.
*/
public static void reset() {
prototypeCreated = false;
}
/**
* Default is for factories to return a singleton instance.
*/
private boolean singleton = true;
private String beanName;
private AutowireCapableBeanFactory beanFactory;
private boolean postProcessed;
private boolean initialized;
private TestBean testBean;
private TestBean otherTestBean;
public DummyFactory() {
this.testBean = new TestBean();
this.testBean.setName(SINGLETON_NAME);
this.testBean.setAge(25);
}
/**
* Return if the bean managed by this factory is a singleton.
* @see FactoryBean#isSingleton()
*/
@Override
public boolean isSingleton() {
return this.singleton;
}
/**
* Set if the bean managed by this factory is a singleton.
*/
public void setSingleton(boolean singleton) {
this.singleton = singleton;
}
@Override
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
this.beanFactory.applyBeanPostProcessorsBeforeInitialization(this.testBean, this.beanName);
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setPostProcessed(boolean postProcessed) {
this.postProcessed = postProcessed;
}
public boolean isPostProcessed() {
return postProcessed;
}
public void setOtherTestBean(TestBean otherTestBean) {
this.otherTestBean = otherTestBean;
this.testBean.setSpouse(otherTestBean);
}
public TestBean getOtherTestBean() {
return otherTestBean;
}
@Override
public void afterPropertiesSet() {
if (initialized) {
throw new RuntimeException("Cannot call afterPropertiesSet twice on the one bean");
}
this.initialized = true;
}
/**
* Was this initialized by invocation of the
* afterPropertiesSet() method from the InitializingBean interface?
*/
public boolean wasInitialized() {
return initialized;
}
public static boolean wasPrototypeCreated() {
return prototypeCreated;
}
/**
* Return the managed object, supporting both singleton
* and prototype mode.
* @see FactoryBean#getObject()
*/
@Override
public Object getObject() throws BeansException {
if (isSingleton()) {
return this.testBean;
}
else {
TestBean prototype = new TestBean("prototype created at " + System.currentTimeMillis(), 11);
if (this.beanFactory != null) {
this.beanFactory.applyBeanPostProcessorsBeforeInitialization(prototype, this.beanName);
}
prototypeCreated = true;
return prototype;
}
}
@Override
public Class<?> getObjectType() {
return TestBean.class;
}
@Override
public void destroy() {
if (this.testBean != null) {
this.testBean.setName(null);
}
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
public class Employee extends TestBean {
private String co;
public Employee() {
}
public Employee(String name) {
super(name);
}
public String getCompany() {
return co;
}
public void setCompany(String co) {
this.co = co;
}
}

View File

@@ -0,0 +1,327 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.core.io.Resource;
/**
* @author Juergen Hoeller
*/
public class GenericBean<T> {
private Set<Integer> integerSet;
private Set<? extends Number> numberSet;
private Set<ITestBean> testBeanSet;
private List<Resource> resourceList;
private List<TestBean> testBeanList;
private List<List<Integer>> listOfLists;
private ArrayList<String[]> listOfArrays;
private List<Map<Integer, Long>> listOfMaps;
private Map<?, ?> plainMap;
private Map<Short, Integer> shortMap;
private HashMap<Long, ?> longMap;
private Map<Number, Collection<? extends Object>> collectionMap;
private Map<String, Map<Integer, Long>> mapOfMaps;
private Map<Integer, List<Integer>> mapOfLists;
private CustomEnum customEnum;
private CustomEnum[] customEnumArray;
private Set<CustomEnum> customEnumSet;
private EnumSet<CustomEnum> standardEnumSet;
private EnumMap<CustomEnum, Integer> standardEnumMap;
private T genericProperty;
private List<T> genericListProperty;
public GenericBean() {
}
public GenericBean(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
public GenericBean(Set<Integer> integerSet, List<Resource> resourceList) {
this.integerSet = integerSet;
this.resourceList = resourceList;
}
public GenericBean(HashSet<Integer> integerSet, Map<Short, Integer> shortMap) {
this.integerSet = integerSet;
this.shortMap = shortMap;
}
public GenericBean(Map<Short, Integer> shortMap, Resource resource) {
this.shortMap = shortMap;
this.resourceList = Collections.singletonList(resource);
}
public GenericBean(Map<?, ?> plainMap, Map<Short, Integer> shortMap) {
this.plainMap = plainMap;
this.shortMap = shortMap;
}
public GenericBean(HashMap<Long, ?> longMap) {
this.longMap = longMap;
}
public GenericBean(boolean someFlag, Map<Number, Collection<? extends Object>> collectionMap) {
this.collectionMap = collectionMap;
}
public Set<Integer> getIntegerSet() {
return integerSet;
}
public void setIntegerSet(Set<Integer> integerSet) {
this.integerSet = integerSet;
}
public Set<? extends Number> getNumberSet() {
return numberSet;
}
public void setNumberSet(Set<? extends Number> numberSet) {
this.numberSet = numberSet;
}
public Set<ITestBean> getTestBeanSet() {
return testBeanSet;
}
public void setTestBeanSet(Set<ITestBean> testBeanSet) {
this.testBeanSet = testBeanSet;
}
public List<Resource> getResourceList() {
return resourceList;
}
public void setResourceList(List<Resource> resourceList) {
this.resourceList = resourceList;
}
public List<TestBean> getTestBeanList() {
return testBeanList;
}
public void setTestBeanList(List<TestBean> testBeanList) {
this.testBeanList = testBeanList;
}
public List<List<Integer>> getListOfLists() {
return listOfLists;
}
public ArrayList<String[]> getListOfArrays() {
return listOfArrays;
}
public void setListOfArrays(ArrayList<String[]> listOfArrays) {
this.listOfArrays = listOfArrays;
}
public void setListOfLists(List<List<Integer>> listOfLists) {
this.listOfLists = listOfLists;
}
public List<Map<Integer, Long>> getListOfMaps() {
return listOfMaps;
}
public void setListOfMaps(List<Map<Integer, Long>> listOfMaps) {
this.listOfMaps = listOfMaps;
}
public Map<?, ?> getPlainMap() {
return plainMap;
}
public Map<Short, Integer> getShortMap() {
return shortMap;
}
public void setShortMap(Map<Short, Integer> shortMap) {
this.shortMap = shortMap;
}
public HashMap<Long, ?> getLongMap() {
return longMap;
}
public void setLongMap(HashMap<Long, ?> longMap) {
this.longMap = longMap;
}
public Map<Number, Collection<? extends Object>> getCollectionMap() {
return collectionMap;
}
public void setCollectionMap(Map<Number, Collection<? extends Object>> collectionMap) {
this.collectionMap = collectionMap;
}
public Map<String, Map<Integer, Long>> getMapOfMaps() {
return mapOfMaps;
}
public void setMapOfMaps(Map<String, Map<Integer, Long>> mapOfMaps) {
this.mapOfMaps = mapOfMaps;
}
public Map<Integer, List<Integer>> getMapOfLists() {
return mapOfLists;
}
public void setMapOfLists(Map<Integer, List<Integer>> mapOfLists) {
this.mapOfLists = mapOfLists;
}
public T getGenericProperty() {
return genericProperty;
}
public void setGenericProperty(T genericProperty) {
this.genericProperty = genericProperty;
}
public List<T> getGenericListProperty() {
return genericListProperty;
}
public void setGenericListProperty(List<T> genericListProperty) {
this.genericListProperty = genericListProperty;
}
public CustomEnum getCustomEnum() {
return customEnum;
}
public void setCustomEnum(CustomEnum customEnum) {
this.customEnum = customEnum;
}
public CustomEnum[] getCustomEnumArray() {
return customEnumArray;
}
public void setCustomEnumArray(CustomEnum[] customEnum) {
this.customEnumArray = customEnum;
}
public Set<CustomEnum> getCustomEnumSet() {
return customEnumSet;
}
public void setCustomEnumSet(Set<CustomEnum> customEnumSet) {
this.customEnumSet = customEnumSet;
}
public Set<CustomEnum> getCustomEnumSetMismatch() {
return customEnumSet;
}
public void setCustomEnumSetMismatch(Set<String> customEnumSet) {
this.customEnumSet = new HashSet<>(customEnumSet.size());
for (Iterator<String> iterator = customEnumSet.iterator(); iterator.hasNext(); ) {
this.customEnumSet.add(CustomEnum.valueOf(iterator.next()));
}
}
public EnumSet<CustomEnum> getStandardEnumSet() {
return standardEnumSet;
}
public void setStandardEnumSet(EnumSet<CustomEnum> standardEnumSet) {
this.standardEnumSet = standardEnumSet;
}
public EnumMap<CustomEnum, Integer> getStandardEnumMap() {
return standardEnumMap;
}
public void setStandardEnumMap(EnumMap<CustomEnum, Integer> standardEnumMap) {
this.standardEnumMap = standardEnumMap;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static GenericBean createInstance(Set<Integer> integerSet) {
return new GenericBean(integerSet);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static GenericBean createInstance(Set<Integer> integerSet, List<Resource> resourceList) {
return new GenericBean(integerSet, resourceList);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static GenericBean createInstance(HashSet<Integer> integerSet, Map<Short, Integer> shortMap) {
return new GenericBean(integerSet, shortMap);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static GenericBean createInstance(Map<Short, Integer> shortMap, Resource resource) {
return new GenericBean(shortMap, resource);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static GenericBean createInstance(Map map, Map<Short, Integer> shortMap) {
return new GenericBean(map, shortMap);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static GenericBean createInstance(HashMap<Long, ?> longMap) {
return new GenericBean(longMap);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static GenericBean createInstance(boolean someFlag, Map<Number, Collection<? extends Object>> collectionMap) {
return new GenericBean(someFlag, collectionMap);
}
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Juergen Hoeller
*/
public class GenericIntegerBean extends GenericBean<Integer> {
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.util.Set;
/**
* @author Juergen Hoeller
*/
public class GenericSetOfIntegerBean extends GenericBean<Set<Integer>> {
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* Bean exposing a map. Used for bean factory tests.
*
* @author Rod Johnson
* @since 05.06.2003
*/
public class HasMap {
private Map<?, ?> map;
private Set<?> set;
private Properties props;
private Object[] objectArray;
private Integer[] intArray;
private Class<?>[] classArray;
private List<Class<?>> classList;
private IdentityHashMap<?, ?> identityMap;
private CopyOnWriteArraySet<?> concurrentSet;
private HasMap() {
}
public Map<?, ?> getMap() {
return map;
}
public void setMap(Map<?, ?> map) {
this.map = map;
}
public Set<?> getSet() {
return set;
}
public void setSet(Set<?> set) {
this.set = set;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public Object[] getObjectArray() {
return objectArray;
}
public void setObjectArray(Object[] objectArray) {
this.objectArray = objectArray;
}
public Integer[] getIntegerArray() {
return intArray;
}
public void setIntegerArray(Integer[] is) {
intArray = is;
}
public Class<?>[] getClassArray() {
return classArray;
}
public void setClassArray(Class<?>[] classArray) {
this.classArray = classArray;
}
public List<Class<?>> getClassList() {
return classList;
}
public void setClassList(List<Class<?>> classList) {
this.classList = classList;
}
public IdentityHashMap<?, ?> getIdentityMap() {
return identityMap;
}
public void setIdentityMap(IdentityHashMap<?, ?> identityMap) {
this.identityMap = identityMap;
}
public CopyOnWriteArraySet<?> getConcurrentSet() {
return concurrentSet;
}
public void setConcurrentSet(CopyOnWriteArraySet<?> concurrentSet) {
this.concurrentSet = concurrentSet;
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
public interface INestedTestBean {
public String getCompany();
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
public interface IOther {
void absquatulate();
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.io.IOException;
/**
* Interface used for {@link org.springframework.beans.test.fixtures.beans.TestBean}.
*
* <p>Two methods are the same as on Person, but if this
* extends person it breaks quite a few tests..
*
* @author Rod Johnson
* @author Juergen Hoeller
*/
public interface ITestBean extends AgeHolder {
String getName();
void setName(String name);
ITestBean getSpouse();
void setSpouse(ITestBean spouse);
ITestBean[] getSpouses();
String[] getStringArray();
void setStringArray(String[] stringArray);
Integer[][] getNestedIntegerArray();
Integer[] getSomeIntegerArray();
void setSomeIntegerArray(Integer[] someIntegerArray);
void setNestedIntegerArray(Integer[][] nestedIntegerArray);
int[] getSomeIntArray();
void setSomeIntArray(int[] someIntArray);
int[][] getNestedIntArray();
void setNestedIntArray(int[][] someNestedArray);
/**
* Throws a given (non-null) exception.
*/
void exceptional(Throwable t) throws Throwable;
Object returnsThis();
INestedTestBean getDoctor();
INestedTestBean getLawyer();
IndexedTestBean getNestedIndexedBean();
/**
* Increment the age by one.
* @return the previous age
*/
int haveBirthday();
void unreliableFileOperation() throws IOException;
}

View File

@@ -0,0 +1,149 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* @author Juergen Hoeller
* @since 11.11.2003
*/
@SuppressWarnings("rawtypes")
public class IndexedTestBean {
private TestBean[] array;
private Collection<?> collection;
private List list;
private Set<? super Object> set;
private SortedSet<? super Object> sortedSet;
private Map map;
private SortedMap sortedMap;
public IndexedTestBean() {
this(true);
}
public IndexedTestBean(boolean populate) {
if (populate) {
populate();
}
}
@SuppressWarnings("unchecked")
public void populate() {
TestBean tb0 = new TestBean("name0", 0);
TestBean tb1 = new TestBean("name1", 0);
TestBean tb2 = new TestBean("name2", 0);
TestBean tb3 = new TestBean("name3", 0);
TestBean tb4 = new TestBean("name4", 0);
TestBean tb5 = new TestBean("name5", 0);
TestBean tb6 = new TestBean("name6", 0);
TestBean tb7 = new TestBean("name7", 0);
TestBean tb8 = new TestBean("name8", 0);
TestBean tbX = new TestBean("nameX", 0);
TestBean tbY = new TestBean("nameY", 0);
this.array = new TestBean[] {tb0, tb1};
this.list = new ArrayList<>();
this.list.add(tb2);
this.list.add(tb3);
this.set = new TreeSet<>();
this.set.add(tb6);
this.set.add(tb7);
this.map = new HashMap<>();
this.map.put("key1", tb4);
this.map.put("key2", tb5);
this.map.put("key.3", tb5);
List list = new ArrayList();
list.add(tbX);
list.add(tbY);
this.map.put("key4", list);
this.map.put("key5[foo]", tb8);
}
public TestBean[] getArray() {
return array;
}
public void setArray(TestBean[] array) {
this.array = array;
}
public Collection<?> getCollection() {
return collection;
}
public void setCollection(Collection<?> collection) {
this.collection = collection;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set<?> getSet() {
return set;
}
public void setSet(Set<? super Object> set) {
this.set = set;
}
public SortedSet<? super Object> getSortedSet() {
return sortedSet;
}
public void setSortedSet(SortedSet<? super Object> sortedSet) {
this.sortedSet = sortedSet;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public SortedMap getSortedMap() {
return sortedMap;
}
public void setSortedMap(SortedMap sortedMap) {
this.sortedMap = sortedMap;
}
}

View File

@@ -0,0 +1,170 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* Simple test of BeanFactory initialization and lifecycle callbacks.
*
* @author Rod Johnson
* @author Colin Sampaleanu
* @author Chris Beams
* @since 12.03.2003
*/
public class LifecycleBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
protected boolean initMethodDeclared = false;
protected String beanName;
protected BeanFactory owningFactory;
protected boolean postProcessedBeforeInit;
protected boolean inited;
protected boolean initedViaDeclaredInitMethod;
protected boolean postProcessedAfterInit;
protected boolean destroyed;
public void setInitMethodDeclared(boolean initMethodDeclared) {
this.initMethodDeclared = initMethodDeclared;
}
public boolean isInitMethodDeclared() {
return initMethodDeclared;
}
@Override
public void setBeanName(String name) {
this.beanName = name;
}
public String getBeanName() {
return beanName;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.owningFactory = beanFactory;
}
public void postProcessBeforeInit() {
if (this.inited || this.initedViaDeclaredInitMethod) {
throw new RuntimeException("Factory called postProcessBeforeInit after afterPropertiesSet");
}
if (this.postProcessedBeforeInit) {
throw new RuntimeException("Factory called postProcessBeforeInit twice");
}
this.postProcessedBeforeInit = true;
}
@Override
public void afterPropertiesSet() {
if (this.owningFactory == null) {
throw new RuntimeException("Factory didn't call setBeanFactory before afterPropertiesSet on lifecycle bean");
}
if (!this.postProcessedBeforeInit) {
throw new RuntimeException("Factory didn't call postProcessBeforeInit before afterPropertiesSet on lifecycle bean");
}
if (this.initedViaDeclaredInitMethod) {
throw new RuntimeException("Factory initialized via declared init method before initializing via afterPropertiesSet");
}
if (this.inited) {
throw new RuntimeException("Factory called afterPropertiesSet twice");
}
this.inited = true;
}
public void declaredInitMethod() {
if (!this.inited) {
throw new RuntimeException("Factory didn't call afterPropertiesSet before declared init method");
}
if (this.initedViaDeclaredInitMethod) {
throw new RuntimeException("Factory called declared init method twice");
}
this.initedViaDeclaredInitMethod = true;
}
public void postProcessAfterInit() {
if (!this.inited) {
throw new RuntimeException("Factory called postProcessAfterInit before afterPropertiesSet");
}
if (this.initMethodDeclared && !this.initedViaDeclaredInitMethod) {
throw new RuntimeException("Factory called postProcessAfterInit before calling declared init method");
}
if (this.postProcessedAfterInit) {
throw new RuntimeException("Factory called postProcessAfterInit twice");
}
this.postProcessedAfterInit = true;
}
/**
* Dummy business method that will fail unless the factory
* managed the bean's lifecycle correctly
*/
public void businessMethod() {
if (!this.inited || (this.initMethodDeclared && !this.initedViaDeclaredInitMethod) ||
!this.postProcessedAfterInit) {
throw new RuntimeException("Factory didn't initialize lifecycle object correctly");
}
}
@Override
public void destroy() {
if (this.destroyed) {
throw new IllegalStateException("Already destroyed");
}
this.destroyed = true;
}
public boolean isDestroyed() {
return destroyed;
}
public static class PostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
if (bean instanceof LifecycleBean) {
((LifecycleBean) bean).postProcessBeforeInit();
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
if (bean instanceof LifecycleBean) {
((LifecycleBean) bean).postProcessAfterInit();
}
return bean;
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import org.springframework.beans.factory.InitializingBean;
/**
* Simple test of BeanFactory initialization
* @author Rod Johnson
* @since 12.03.2003
*/
public class MustBeInitialized implements InitializingBean {
private boolean inited;
/**
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
this.inited = true;
}
/**
* Dummy business method that will fail unless the factory
* managed the bean's lifecycle correctly
*/
public void businessMethod() {
if (!this.inited)
throw new RuntimeException("Factory didn't call afterPropertiesSet() on MustBeInitialized object");
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* Simple nested test bean used for testing bean factories, AOP framework etc.
*
* @author Trevor D. Cook
* @since 30.09.2003
*/
public class NestedTestBean implements INestedTestBean {
private String company = "";
public NestedTestBean() {
}
public NestedTestBean(String company) {
setCompany(company);
}
public void setCompany(String company) {
this.company = (company != null ? company : "");
}
@Override
public String getCompany() {
return company;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof NestedTestBean)) {
return false;
}
NestedTestBean ntb = (NestedTestBean) obj;
return this.company.equals(ntb.company);
}
@Override
public int hashCode() {
return this.company.hashCode();
}
@Override
public String toString() {
return "NestedTestBean: " + this.company;
}
}

View File

@@ -0,0 +1,143 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* @author Juergen Hoeller
* @since 10.06.2003
*/
public class NumberTestBean {
private short short1;
private Short short2;
private int int1;
private Integer int2;
private long long1;
private Long long2;
private BigInteger bigInteger;
private float float1;
private Float float2;
private double double1;
private Double double2;
private BigDecimal bigDecimal;
public short getShort1() {
return short1;
}
public void setShort1(short short1) {
this.short1 = short1;
}
public Short getShort2() {
return short2;
}
public void setShort2(Short short2) {
this.short2 = short2;
}
public int getInt1() {
return int1;
}
public void setInt1(int int1) {
this.int1 = int1;
}
public Integer getInt2() {
return int2;
}
public void setInt2(Integer int2) {
this.int2 = int2;
}
public long getLong1() {
return long1;
}
public void setLong1(long long1) {
this.long1 = long1;
}
public Long getLong2() {
return long2;
}
public void setLong2(Long long2) {
this.long2 = long2;
}
public BigInteger getBigInteger() {
return bigInteger;
}
public void setBigInteger(BigInteger bigInteger) {
this.bigInteger = bigInteger;
}
public float getFloat1() {
return float1;
}
public void setFloat1(float float1) {
this.float1 = float1;
}
public Float getFloat2() {
return float2;
}
public void setFloat2(Float float2) {
this.float2 = float2;
}
public double getDouble1() {
return double1;
}
public void setDouble1(double double1) {
this.double1 = double1;
}
public Double getDouble2() {
return double2;
}
public void setDouble2(Double double2) {
this.double2 = double2;
}
public BigDecimal getBigDecimal() {
return bigDecimal;
}
public void setBigDecimal(BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @see org.springframework.beans.factory.config.FieldRetrievingFactoryBeanTests
*
* @author Rick Evans
* @author Chris Beams
*/
class PackageLevelVisibleBean {
public static final String CONSTANT = "Wuby";
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
*
* @author Rod Johnson
*/
public interface Person {
String getName();
void setName(String name);
int getAge();
void setAge(int i);
/**
* Test for non-property method matching. If the parameter is a Throwable, it will be
* thrown rather than returned.
*/
Object echo(Object o) throws Throwable;
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* @author Rob Harrop
* @since 2.0
*/
public class Pet {
private String name;
public Pet(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Pet pet = (Pet) o;
if (name != null ? !name.equals(pet.name) : pet.name != null) return false;
return true;
}
@Override
public int hashCode() {
return (name != null ? name.hashCode() : 0);
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.io.Serializable;
import org.springframework.util.ObjectUtils;
/**
* Serializable implementation of the Person interface.
*
* @author Rod Johnson
*/
@SuppressWarnings("serial")
public class SerializablePerson implements Person, Serializable {
private String name;
private int age;
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public Object echo(Object o) throws Throwable {
if (o instanceof Throwable) {
throw (Throwable) o;
}
return o;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof SerializablePerson)) {
return false;
}
SerializablePerson p = (SerializablePerson) other;
return p.age == age && ObjectUtils.nullSafeEquals(name, p.name);
}
@Override
public int hashCode() {
return SerializablePerson.class.hashCode();
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
/**
* Bean that changes state on a business invocation, so that
* we can check whether it's been invoked
* @author Rod Johnson
*/
public class SideEffectBean {
private int count;
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return this.count;
}
public void doWork() {
++count;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author Stephane Nicoll
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
}

View File

@@ -0,0 +1,499 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.util.ObjectUtils;
/**
* Simple test bean used for testing bean factories, the AOP framework etc.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 15 April 2001
*/
public class TestBean implements BeanNameAware, BeanFactoryAware, ITestBean, IOther, Comparable<Object> {
private String beanName;
private String country;
private BeanFactory beanFactory;
private boolean postProcessed;
private String name;
private String sex;
private int age;
private boolean jedi;
private ITestBean spouse;
private String touchy;
private String[] stringArray;
private Integer[] someIntegerArray;
private Integer[][] nestedIntegerArray;
private int[] someIntArray;
private int[][] nestedIntArray;
private Date date = new Date();
private Float myFloat = new Float(0.0);
private Collection<? super Object> friends = new LinkedList<>();
private Set<?> someSet = new HashSet<>();
private Map<?, ?> someMap = new HashMap<>();
private List<?> someList = new ArrayList<>();
private Properties someProperties = new Properties();
private INestedTestBean doctor = new NestedTestBean();
private INestedTestBean lawyer = new NestedTestBean();
private IndexedTestBean nestedIndexedBean;
private boolean destroyed;
private Number someNumber;
private Colour favouriteColour;
private Boolean someBoolean;
private List<?> otherColours;
private List<?> pets;
public TestBean() {
}
public TestBean(String name) {
this.name = name;
}
public TestBean(ITestBean spouse) {
this.spouse = spouse;
}
public TestBean(String name, int age) {
this.name = name;
this.age = age;
}
public TestBean(ITestBean spouse, Properties someProperties) {
this.spouse = spouse;
this.someProperties = someProperties;
}
public TestBean(List<?> someList) {
this.someList = someList;
}
public TestBean(Set<?> someSet) {
this.someSet = someSet;
}
public TestBean(Map<?, ?> someMap) {
this.someMap = someMap;
}
public TestBean(Properties someProperties) {
this.someProperties = someProperties;
}
@Override
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setPostProcessed(boolean postProcessed) {
this.postProcessed = postProcessed;
}
public boolean isPostProcessed() {
return postProcessed;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
if (this.name == null) {
this.name = sex;
}
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age = age;
}
public boolean isJedi() {
return jedi;
}
public void setJedi(boolean jedi) {
this.jedi = jedi;
}
@Override
public ITestBean getSpouse() {
return this.spouse;
}
@Override
public void setSpouse(ITestBean spouse) {
this.spouse = spouse;
}
@Override
public ITestBean[] getSpouses() {
return (spouse != null ? new ITestBean[] {spouse} : null);
}
public String getTouchy() {
return touchy;
}
public void setTouchy(String touchy) throws Exception {
if (touchy.indexOf('.') != -1) {
throw new Exception("Can't contain a .");
}
if (touchy.indexOf(',') != -1) {
throw new NumberFormatException("Number format exception: contains a ,");
}
this.touchy = touchy;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String[] getStringArray() {
return stringArray;
}
@Override
public void setStringArray(String[] stringArray) {
this.stringArray = stringArray;
}
@Override
public Integer[] getSomeIntegerArray() {
return someIntegerArray;
}
@Override
public void setSomeIntegerArray(Integer[] someIntegerArray) {
this.someIntegerArray = someIntegerArray;
}
@Override
public Integer[][] getNestedIntegerArray() {
return nestedIntegerArray;
}
@Override
public void setNestedIntegerArray(Integer[][] nestedIntegerArray) {
this.nestedIntegerArray = nestedIntegerArray;
}
@Override
public int[] getSomeIntArray() {
return someIntArray;
}
@Override
public void setSomeIntArray(int[] someIntArray) {
this.someIntArray = someIntArray;
}
@Override
public int[][] getNestedIntArray() {
return nestedIntArray;
}
@Override
public void setNestedIntArray(int[][] nestedIntArray) {
this.nestedIntArray = nestedIntArray;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Float getMyFloat() {
return myFloat;
}
public void setMyFloat(Float myFloat) {
this.myFloat = myFloat;
}
public Collection<? super Object> getFriends() {
return friends;
}
public void setFriends(Collection<? super Object> friends) {
this.friends = friends;
}
public Set<?> getSomeSet() {
return someSet;
}
public void setSomeSet(Set<?> someSet) {
this.someSet = someSet;
}
public Map<?, ?> getSomeMap() {
return someMap;
}
public void setSomeMap(Map<?, ?> someMap) {
this.someMap = someMap;
}
public List<?> getSomeList() {
return someList;
}
public void setSomeList(List<?> someList) {
this.someList = someList;
}
public Properties getSomeProperties() {
return someProperties;
}
public void setSomeProperties(Properties someProperties) {
this.someProperties = someProperties;
}
@Override
public INestedTestBean getDoctor() {
return doctor;
}
public void setDoctor(INestedTestBean doctor) {
this.doctor = doctor;
}
@Override
public INestedTestBean getLawyer() {
return lawyer;
}
public void setLawyer(INestedTestBean lawyer) {
this.lawyer = lawyer;
}
public Number getSomeNumber() {
return someNumber;
}
public void setSomeNumber(Number someNumber) {
this.someNumber = someNumber;
}
public Colour getFavouriteColour() {
return favouriteColour;
}
public void setFavouriteColour(Colour favouriteColour) {
this.favouriteColour = favouriteColour;
}
public Boolean getSomeBoolean() {
return someBoolean;
}
public void setSomeBoolean(Boolean someBoolean) {
this.someBoolean = someBoolean;
}
@Override
public IndexedTestBean getNestedIndexedBean() {
return nestedIndexedBean;
}
public void setNestedIndexedBean(IndexedTestBean nestedIndexedBean) {
this.nestedIndexedBean = nestedIndexedBean;
}
public List<?> getOtherColours() {
return otherColours;
}
public void setOtherColours(List<?> otherColours) {
this.otherColours = otherColours;
}
public List<?> getPets() {
return pets;
}
public void setPets(List<?> pets) {
this.pets = pets;
}
/**
* @see org.springframework.beans.test.fixtures.beans.ITestBean#exceptional(Throwable)
*/
@Override
public void exceptional(Throwable t) throws Throwable {
if (t != null) {
throw t;
}
}
@Override
public void unreliableFileOperation() throws IOException {
throw new IOException();
}
/**
* @see org.springframework.beans.test.fixtures.beans.ITestBean#returnsThis()
*/
@Override
public Object returnsThis() {
return this;
}
/**
* @see org.springframework.beans.test.fixtures.beans.IOther#absquatulate()
*/
@Override
public void absquatulate() {
}
@Override
public int haveBirthday() {
return age++;
}
public void destroy() {
this.destroyed = true;
}
public boolean wasDestroyed() {
return destroyed;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof TestBean)) {
return false;
}
TestBean tb2 = (TestBean) other;
return (ObjectUtils.nullSafeEquals(this.name, tb2.name) && this.age == tb2.age);
}
@Override
public int hashCode() {
return this.age;
}
@Override
public int compareTo(Object other) {
if (this.name != null && other instanceof TestBean) {
return this.name.compareTo(((TestBean) other).getName());
}
else {
return 1;
}
}
@Override
public String toString() {
return this.name;
}
}

View File

@@ -0,0 +1,186 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.test.fixtures.beans.TestBean;
/**
* Simple factory to allow testing of FactoryBean support in AbstractBeanFactory.
* Depending on whether its singleton property is set, it will return a singleton
* or a prototype instance.
*
* <p>Implements InitializingBean interface, so we can check that
* factories get this lifecycle callback if they want.
*
* @author Rod Johnson
* @author Chris Beams
* @since 10.03.2003
*/
public class DummyFactory
implements FactoryBean<Object>, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
public static final String SINGLETON_NAME = "Factory singleton";
private static boolean prototypeCreated;
/**
* Clear static state.
*/
public static void reset() {
prototypeCreated = false;
}
/**
* Default is for factories to return a singleton instance.
*/
private boolean singleton = true;
private String beanName;
private AutowireCapableBeanFactory beanFactory;
private boolean postProcessed;
private boolean initialized;
private TestBean testBean;
private TestBean otherTestBean;
public DummyFactory() {
this.testBean = new TestBean();
this.testBean.setName(SINGLETON_NAME);
this.testBean.setAge(25);
}
/**
* Return if the bean managed by this factory is a singleton.
* @see FactoryBean#isSingleton()
*/
@Override
public boolean isSingleton() {
return this.singleton;
}
/**
* Set if the bean managed by this factory is a singleton.
*/
public void setSingleton(boolean singleton) {
this.singleton = singleton;
}
@Override
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public String getBeanName() {
return beanName;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
this.beanFactory.applyBeanPostProcessorsBeforeInitialization(this.testBean, this.beanName);
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public void setPostProcessed(boolean postProcessed) {
this.postProcessed = postProcessed;
}
public boolean isPostProcessed() {
return postProcessed;
}
public void setOtherTestBean(TestBean otherTestBean) {
this.otherTestBean = otherTestBean;
this.testBean.setSpouse(otherTestBean);
}
public TestBean getOtherTestBean() {
return otherTestBean;
}
@Override
public void afterPropertiesSet() {
if (initialized) {
throw new RuntimeException("Cannot call afterPropertiesSet twice on the one bean");
}
this.initialized = true;
}
/**
* Was this initialized by invocation of the
* afterPropertiesSet() method from the InitializingBean interface?
*/
public boolean wasInitialized() {
return initialized;
}
public static boolean wasPrototypeCreated() {
return prototypeCreated;
}
/**
* Return the managed object, supporting both singleton
* and prototype mode.
* @see FactoryBean#getObject()
*/
@Override
public Object getObject() throws BeansException {
if (isSingleton()) {
return this.testBean;
}
else {
TestBean prototype = new TestBean("prototype created at " + System.currentTimeMillis(), 11);
if (this.beanFactory != null) {
this.beanFactory.applyBeanPostProcessorsBeforeInitialization(prototype, this.beanName);
}
prototypeCreated = true;
return prototype;
}
}
@Override
public Class<?> getObjectType() {
return TestBean.class;
}
@Override
public void destroy() {
if (this.testBean != null) {
this.testBean.setName(null);
}
}
}

View File

@@ -0,0 +1,4 @@
/**
* General purpose sample beans that can be used with tests.
*/
package org.springframework.beans.test.fixtures.beans;

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.beans.subpkg;
/**
* Used for testing pointcut matching.
*
* @see org.springframework.aop.aspectj.AspectJExpressionPointcutTests#testWithinRootAndSubpackages()
*
* @author Chris Beams
*/
public class DeepBean {
public void aMethod(String foo) {
// no-op
}
}

View File

@@ -0,0 +1,295 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.factory.xml;
import java.beans.PropertyEditorSupport;
import java.util.StringTokenizer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanIsNotAFactoryException;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.test.fixtures.beans.LifecycleBean;
import org.springframework.beans.test.fixtures.beans.MustBeInitialized;
import org.springframework.beans.test.fixtures.beans.TestBean;
import org.springframework.beans.test.fixtures.beans.factory.DummyFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Subclasses must initialize the bean factory and any other variables they need.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
*/
public abstract class AbstractBeanFactoryTests {
protected abstract BeanFactory getBeanFactory();
/**
* Roderick bean inherits from rod, overriding name only.
*/
@Test
public void inheritance() {
assertThat(getBeanFactory().containsBean("rod")).isTrue();
assertThat(getBeanFactory().containsBean("roderick")).isTrue();
TestBean rod = (TestBean) getBeanFactory().getBean("rod");
TestBean roderick = (TestBean) getBeanFactory().getBean("roderick");
assertThat(rod != roderick).as("not == ").isTrue();
assertThat(rod.getName().equals("Rod")).as("rod.name is Rod").isTrue();
assertThat(rod.getAge() == 31).as("rod.age is 31").isTrue();
assertThat(roderick.getName().equals("Roderick")).as("roderick.name is Roderick").isTrue();
assertThat(roderick.getAge() == rod.getAge()).as("roderick.age was inherited").isTrue();
}
@Test
public void getBeanWithNullArg() {
assertThatIllegalArgumentException().isThrownBy(() ->
getBeanFactory().getBean((String) null));
}
/**
* Test that InitializingBean objects receive the afterPropertiesSet() callback
*/
@Test
public void initializingBeanCallback() {
MustBeInitialized mbi = (MustBeInitialized) getBeanFactory().getBean("mustBeInitialized");
// The dummy business method will throw an exception if the
// afterPropertiesSet() callback wasn't invoked
mbi.businessMethod();
}
/**
* Test that InitializingBean/BeanFactoryAware/DisposableBean objects receive the
* afterPropertiesSet() callback before BeanFactoryAware callbacks
*/
@Test
public void lifecycleCallbacks() {
LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle");
assertThat(lb.getBeanName()).isEqualTo("lifecycle");
// The dummy business method will throw an exception if the
// necessary callbacks weren't invoked in the right order.
lb.businessMethod();
boolean condition = !lb.isDestroyed();
assertThat(condition).as("Not destroyed").isTrue();
}
@Test
public void findsValidInstance() {
Object o = getBeanFactory().getBean("rod");
boolean condition = o instanceof TestBean;
assertThat(condition).as("Rod bean is a TestBean").isTrue();
TestBean rod = (TestBean) o;
assertThat(rod.getName().equals("Rod")).as("rod.name is Rod").isTrue();
assertThat(rod.getAge() == 31).as("rod.age is 31").isTrue();
}
@Test
public void getInstanceByMatchingClass() {
Object o = getBeanFactory().getBean("rod", TestBean.class);
boolean condition = o instanceof TestBean;
assertThat(condition).as("Rod bean is a TestBean").isTrue();
}
@Test
public void getInstanceByNonmatchingClass() {
assertThatExceptionOfType(BeanNotOfRequiredTypeException.class).isThrownBy(() ->
getBeanFactory().getBean("rod", BeanFactory.class))
.satisfies(ex -> {
assertThat(ex.getBeanName()).isEqualTo("rod");
assertThat(ex.getRequiredType()).isEqualTo(BeanFactory.class);
assertThat(ex.getActualType()).isEqualTo(TestBean.class).isEqualTo(getBeanFactory().getBean("rod").getClass());
});
}
@Test
public void getSharedInstanceByMatchingClass() {
Object o = getBeanFactory().getBean("rod", TestBean.class);
boolean condition = o instanceof TestBean;
assertThat(condition).as("Rod bean is a TestBean").isTrue();
}
@Test
public void getSharedInstanceByMatchingClassNoCatch() {
Object o = getBeanFactory().getBean("rod", TestBean.class);
boolean condition = o instanceof TestBean;
assertThat(condition).as("Rod bean is a TestBean").isTrue();
}
@Test
public void getSharedInstanceByNonmatchingClass() {
assertThatExceptionOfType(BeanNotOfRequiredTypeException.class).isThrownBy(() ->
getBeanFactory().getBean("rod", BeanFactory.class))
.satisfies(ex -> {
assertThat(ex.getBeanName()).isEqualTo("rod");
assertThat(ex.getRequiredType()).isEqualTo(BeanFactory.class);
assertThat(ex.getActualType()).isEqualTo(TestBean.class);
});
}
@Test
public void sharedInstancesAreEqual() {
Object o = getBeanFactory().getBean("rod");
boolean condition1 = o instanceof TestBean;
assertThat(condition1).as("Rod bean1 is a TestBean").isTrue();
Object o1 = getBeanFactory().getBean("rod");
boolean condition = o1 instanceof TestBean;
assertThat(condition).as("Rod bean2 is a TestBean").isTrue();
assertThat(o == o1).as("Object equals applies").isTrue();
}
@Test
public void prototypeInstancesAreIndependent() {
TestBean tb1 = (TestBean) getBeanFactory().getBean("kathy");
TestBean tb2 = (TestBean) getBeanFactory().getBean("kathy");
assertThat(tb1 != tb2).as("ref equal DOES NOT apply").isTrue();
assertThat(tb1.equals(tb2)).as("object equal true").isTrue();
tb1.setAge(1);
tb2.setAge(2);
assertThat(tb1.getAge() == 1).as("1 age independent = 1").isTrue();
assertThat(tb2.getAge() == 2).as("2 age independent = 2").isTrue();
boolean condition = !tb1.equals(tb2);
assertThat(condition).as("object equal now false").isTrue();
}
@Test
public void notThere() {
assertThat(getBeanFactory().containsBean("Mr Squiggle")).isFalse();
assertThatExceptionOfType(BeansException.class).isThrownBy(() ->
getBeanFactory().getBean("Mr Squiggle"));
}
@Test
public void validEmpty() {
Object o = getBeanFactory().getBean("validEmpty");
boolean condition = o instanceof TestBean;
assertThat(condition).as("validEmpty bean is a TestBean").isTrue();
TestBean ve = (TestBean) o;
assertThat(ve.getName() == null && ve.getAge() == 0 && ve.getSpouse() == null).as("Valid empty has defaults").isTrue();
}
@Test
public void typeMismatch() {
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> getBeanFactory().getBean("typeMismatch"))
.withCauseInstanceOf(TypeMismatchException.class);
}
@Test
public void grandparentDefinitionFoundInBeanFactory() throws Exception {
TestBean dad = (TestBean) getBeanFactory().getBean("father");
assertThat(dad.getName().equals("Albert")).as("Dad has correct name").isTrue();
}
@Test
public void factorySingleton() throws Exception {
assertThat(getBeanFactory().isSingleton("&singletonFactory")).isTrue();
assertThat(getBeanFactory().isSingleton("singletonFactory")).isTrue();
TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
assertThat(tb.getName().equals(DummyFactory.SINGLETON_NAME)).as("Singleton from factory has correct name, not " + tb.getName()).isTrue();
DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
TestBean tb2 = (TestBean) getBeanFactory().getBean("singletonFactory");
assertThat(tb == tb2).as("Singleton references ==").isTrue();
assertThat(factory.getBeanFactory() != null).as("FactoryBean is BeanFactoryAware").isTrue();
}
@Test
public void factoryPrototype() throws Exception {
assertThat(getBeanFactory().isSingleton("&prototypeFactory")).isTrue();
assertThat(getBeanFactory().isSingleton("prototypeFactory")).isFalse();
TestBean tb = (TestBean) getBeanFactory().getBean("prototypeFactory");
boolean condition = !tb.getName().equals(DummyFactory.SINGLETON_NAME);
assertThat(condition).isTrue();
TestBean tb2 = (TestBean) getBeanFactory().getBean("prototypeFactory");
assertThat(tb != tb2).as("Prototype references !=").isTrue();
}
/**
* Check that we can get the factory bean itself.
* This is only possible if we're dealing with a factory
*/
@Test
public void getFactoryItself() throws Exception {
assertThat(getBeanFactory().getBean("&singletonFactory")).isNotNull();
}
/**
* Check that afterPropertiesSet gets called on factory
*/
@Test
public void factoryIsInitialized() throws Exception {
TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
assertThat(tb).isNotNull();
DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
assertThat(factory.wasInitialized()).as("Factory was initialized because it implemented InitializingBean").isTrue();
}
/**
* It should be illegal to dereference a normal bean as a factory.
*/
@Test
public void rejectsFactoryGetOnNormalBean() {
assertThatExceptionOfType(BeanIsNotAFactoryException.class).isThrownBy(() ->
getBeanFactory().getBean("&rod"));
}
// TODO: refactor in AbstractBeanFactory (tests for AbstractBeanFactory)
// and rename this class
@Test
public void aliasing() {
BeanFactory bf = getBeanFactory();
if (!(bf instanceof ConfigurableBeanFactory)) {
return;
}
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) bf;
String alias = "rods alias";
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
cbf.getBean(alias))
.satisfies(ex -> assertThat(ex.getBeanName()).isEqualTo(alias));
// Create alias
cbf.registerAlias("rod", alias);
Object rod = getBeanFactory().getBean("rod");
Object aliasRod = getBeanFactory().getBean(alias);
assertThat(rod == aliasRod).isTrue();
}
public static class TestBeanEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) {
TestBean tb = new TestBean();
StringTokenizer st = new StringTokenizer(text, "_");
tb.setName(st.nextToken());
tb.setAge(Integer.parseInt(st.nextToken()));
setValue(tb);
}
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.test.fixtures.factory.xml;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.test.fixtures.beans.TestBean;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFactoryTests {
/** Subclasses must initialize this */
protected ListableBeanFactory getListableBeanFactory() {
BeanFactory bf = getBeanFactory();
if (!(bf instanceof ListableBeanFactory)) {
throw new IllegalStateException("ListableBeanFactory required");
}
return (ListableBeanFactory) bf;
}
/**
* Subclasses can override this.
*/
@Test
public void count() {
assertCount(13);
}
protected final void assertCount(int count) {
String[] defnames = getListableBeanFactory().getBeanDefinitionNames();
assertThat(defnames.length == count).as("We should have " + count + " beans, not " + defnames.length).isTrue();
}
protected void assertTestBeanCount(int count) {
String[] defNames = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, false);
assertThat(defNames.length == count).as("We should have " + count + " beans for class org.springframework.beans.test.fixtures.beans.TestBean, not " +
defNames.length).isTrue();
int countIncludingFactoryBeans = count + 2;
String[] names = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, true);
assertThat(names.length == countIncludingFactoryBeans).as("We should have " + countIncludingFactoryBeans +
" beans for class org.springframework.beans.test.fixtures.beans.TestBean, not " + names.length).isTrue();
}
@Test
public void getDefinitionsForNoSuchClass() {
String[] defnames = getListableBeanFactory().getBeanNamesForType(String.class);
assertThat(defnames.length == 0).as("No string definitions").isTrue();
}
/**
* Check that count refers to factory class, not bean class. (We don't know
* what type factories may return, and it may even change over time.)
*/
@Test
public void getCountForFactoryClass() {
assertThat(getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2).as("Should have 2 factories, not " +
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length).isTrue();
assertThat(getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2).as("Should have 2 factories, not " +
getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length).isTrue();
}
@Test
public void containsBeanDefinition() {
assertThat(getListableBeanFactory().containsBeanDefinition("rod")).isTrue();
assertThat(getListableBeanFactory().containsBeanDefinition("roderick")).isTrue();
}
}