Polishing

This commit is contained in:
Sam Brannen 2025-02-12 17:10:22 +01:00
parent de4db3812a
commit f42e886f03
2 changed files with 73 additions and 50 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@ -242,7 +242,8 @@ public class BeanMethodPolymorphismTests {
@Configuration
static class OverridingConfig extends BaseConfig {
@Bean @Lazy
@Bean
@Lazy
@Override
public BaseTestBean testBean() {
return new BaseTestBean() {
@ -258,7 +259,8 @@ public class BeanMethodPolymorphismTests {
@Configuration
static class OverridingConfigWithDifferentBeanName extends BaseConfig {
@Bean("myTestBean") @Lazy
@Bean("myTestBean")
@Lazy
@Override
public BaseTestBean testBean() {
return new BaseTestBean() {
@ -274,7 +276,8 @@ public class BeanMethodPolymorphismTests {
@Configuration
static class NarrowedOverridingConfig extends BaseConfig {
@Bean @Lazy
@Bean
@Lazy
@Override
public ExtendedTestBean testBean() {
return new ExtendedTestBean() {
@ -287,6 +290,7 @@ public class BeanMethodPolymorphismTests {
}
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false)
static class ConfigWithOverloading {
@ -302,15 +306,18 @@ public class BeanMethodPolymorphismTests {
}
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false)
static class ConfigWithOverloadingAndAdditionalMetadata {
@Bean @Lazy
@Bean
@Lazy
String aString() {
return "regular";
}
@Bean @Lazy
@Bean
@Lazy
String aString(Integer dependency) {
return "overloaded" + dependency;
}
@ -335,7 +342,8 @@ public class BeanMethodPolymorphismTests {
return 5;
}
@Bean @Lazy
@Bean
@Lazy
String aString(Integer dependency) {
return "overloaded" + dependency;
}
@ -350,7 +358,8 @@ public class BeanMethodPolymorphismTests {
return 5;
}
@Bean @Lazy
@Bean
@Lazy
String aString(List<Integer> dependency) {
return "overloaded" + dependency.get(0);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@ -83,7 +83,7 @@ class ConfigurationClassProcessingTests {
() -> ConfigWithBeanWithCustomNameConfiguredViaValueAttribute.testBean, "enigma");
}
private void customBeanNameIsRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
private static void customBeanNameIsRespected(Class<?> testClass, Supplier<TestBean> testBeanSupplier, String beanName) {
GenericApplicationContext ac = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
ac.registerBeanDefinition("config", new RootBeanDefinition(testClass));
@ -92,8 +92,8 @@ class ConfigurationClassProcessingTests {
assertThat(ac.getBean(beanName)).isSameAs(testBeanSupplier.get());
// method name should not be registered
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
ac.getBean("methodName"));
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> ac.getBean("methodName"));
}
@Test
@ -113,11 +113,12 @@ class ConfigurationClassProcessingTests {
BeanFactory factory = initBeanFactory(false, testClass);
assertThat(factory.getBean(beanName)).isSameAs(testBean);
Arrays.stream(factory.getAliases(beanName)).map(factory::getBean).forEach(alias -> assertThat(alias).isSameAs(testBean));
assertThat(factory.getAliases(beanName)).extracting(factory::getBean)
.allMatch(alias -> alias == testBean);
// method name should not be registered
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
factory.getBean("methodName"));
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> factory.getBean("methodName"));
}
@Test // SPR-11830
@ -140,8 +141,8 @@ class ConfigurationClassProcessingTests {
@Test
void finalBeanMethod() {
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() ->
initBeanFactory(false, ConfigWithFinalBean.class));
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> initBeanFactory(false, ConfigWithFinalBean.class));
}
@Test
@ -151,8 +152,8 @@ class ConfigurationClassProcessingTests {
@Test // gh-31007
void voidBeanMethod() {
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() ->
initBeanFactory(false, ConfigWithVoidBean.class));
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> initBeanFactory(false, ConfigWithVoidBean.class));
}
@Test
@ -180,23 +181,19 @@ class ConfigurationClassProcessingTests {
assertThat(factory.isTypeMatch("&factoryBean", FactoryBean.class)).isTrue();
assertThat(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class)).isFalse();
assertThat(factory.isTypeMatch("&factoryBean", ListFactoryBean.class)).isFalse();
boolean condition = factory.getBean("factoryBean") instanceof List;
assertThat(condition).isTrue();
assertThat(factory.getBean("factoryBean")).isInstanceOf(List.class);
String[] beanNames = factory.getBeanNamesForType(FactoryBean.class);
assertThat(beanNames).hasSize(1);
assertThat(beanNames[0]).isEqualTo("&factoryBean");
assertThat(beanNames).containsExactly("&factoryBean");
beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
assertThat(beanNames).hasSize(1);
assertThat(beanNames[0]).isEqualTo("&factoryBean");
assertThat(beanNames).containsExactly("&factoryBean");
beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
assertThat(beanNames).hasSize(1);
assertThat(beanNames[0]).isEqualTo("&factoryBean");
assertThat(beanNames).containsExactly("&factoryBean");
beanNames = factory.getBeanNamesForType(List.class);
assertThat(beanNames[0]).isEqualTo("factoryBean");
assertThat(beanNames).containsExactly("factoryBean");
}
@Test
@ -381,7 +378,7 @@ class ConfigurationClassProcessingTests {
static TestBean testBean = new TestBean(ConfigWithBeanWithCustomName.class.getSimpleName());
@Bean(name = "customName")
@Bean("customName")
public TestBean methodName() {
return testBean;
}
@ -405,7 +402,7 @@ class ConfigurationClassProcessingTests {
static TestBean testBean = new TestBean(ConfigWithBeanWithAliases.class.getSimpleName());
@Bean(name = {"name1", "alias1", "alias2", "alias3"})
@Bean({"name1", "alias1", "alias2", "alias3"})
public TestBean methodName() {
return testBean;
}
@ -430,7 +427,7 @@ class ConfigurationClassProcessingTests {
static TestBean testBean = new TestBean(ConfigWithBeanWithProviderImplementation.class.getSimpleName());
@Override
@Bean(name = "customName")
@Bean("customName")
public TestBean get() {
return testBean;
}
@ -443,7 +440,7 @@ class ConfigurationClassProcessingTests {
static Set<String> set = Collections.singleton("value");
@Override
@Bean(name = "customName")
@Bean("customName")
public Set<String> get() {
return set;
}
@ -453,7 +450,8 @@ class ConfigurationClassProcessingTests {
@Configuration
static class ConfigWithFinalBean {
@Bean public final TestBean testBean() {
@Bean
public final TestBean testBean() {
return new TestBean();
}
}
@ -462,7 +460,8 @@ class ConfigurationClassProcessingTests {
@Configuration(proxyBeanMethods = false)
static class ConfigWithFinalBeanWithoutProxy {
@Bean public final TestBean testBean() {
@Bean
public final TestBean testBean() {
return new TestBean();
}
}
@ -471,7 +470,8 @@ class ConfigurationClassProcessingTests {
@Configuration
static class ConfigWithVoidBean {
@Bean public void testBean() {
@Bean
public void testBean() {
}
}
@ -479,7 +479,8 @@ class ConfigurationClassProcessingTests {
@Configuration
static class SimplestPossibleConfig {
@Bean public String stringBean() {
@Bean
public String stringBean() {
return "foo";
}
}
@ -488,11 +489,13 @@ class ConfigurationClassProcessingTests {
@Configuration
static class ConfigWithNonSpecificReturnTypes {
@Bean public Object stringBean() {
@Bean
public Object stringBean() {
return "foo";
}
@Bean public FactoryBean<?> factoryBean() {
@Bean
public FactoryBean<?> factoryBean() {
ListFactoryBean fb = new ListFactoryBean();
fb.setSourceList(Arrays.asList("element1", "element2"));
return fb;
@ -503,29 +506,34 @@ class ConfigurationClassProcessingTests {
@Configuration
static class ConfigWithPrototypeBean {
@Bean public TestBean foo() {
@Bean
public TestBean foo() {
TestBean foo = new SpousyTestBean("foo");
foo.setSpouse(bar());
return foo;
}
@Bean public TestBean bar() {
@Bean
public TestBean bar() {
TestBean bar = new SpousyTestBean("bar");
bar.setSpouse(baz());
return bar;
}
@Bean @Scope("prototype")
@Bean
@Scope("prototype")
public TestBean baz() {
return new TestBean("baz");
}
@Bean @Scope("prototype")
@Bean
@Scope("prototype")
public TestBean adaptive1(InjectionPoint ip) {
return new TestBean(ip.getMember().getName());
}
@Bean @Scope("prototype")
@Bean
@Scope("prototype")
public TestBean adaptive2(DependencyDescriptor dd) {
return new TestBean(dd.getMember().getName());
}
@ -542,14 +550,17 @@ class ConfigurationClassProcessingTests {
}
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false)
static class ConfigWithMethodNameMismatch {
@Bean(name = "foo") public TestBean foo1() {
@Bean("foo")
public TestBean foo1() {
return new SpousyTestBean("foo1");
}
@Bean(name = "foo") public TestBean foo2() {
@Bean("foo")
public TestBean foo2() {
return new SpousyTestBean("foo2");
}
}
@ -558,12 +569,14 @@ class ConfigurationClassProcessingTests {
@Scope("prototype")
static class AdaptiveInjectionPoints {
@Autowired @Qualifier("adaptive1")
@Autowired
@Qualifier("adaptive1")
public TestBean adaptiveInjectionPoint1;
public TestBean adaptiveInjectionPoint2;
@Autowired @Qualifier("adaptive2")
@Autowired
@Qualifier("adaptive2")
public void setAdaptiveInjectionPoint2(TestBean adaptiveInjectionPoint2) {
this.adaptiveInjectionPoint2 = adaptiveInjectionPoint2;
}
@ -687,15 +700,16 @@ class ConfigurationClassProcessingTests {
}
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false)
public static class OverloadedBeanMismatch {
@Bean(name = "other")
@Bean("other")
public NestedTestBean foo() {
return new NestedTestBean();
}
@Bean(name = "foo")
@Bean("foo")
public TestBean foo(@Qualifier("other") NestedTestBean other) {
TestBean tb = new TestBean();
tb.setLawyer(other);
@ -728,7 +742,7 @@ class ConfigurationClassProcessingTests {
static class ConfigWithDynamicPrototype {
@Bean
@Scope(value = "prototype")
@Scope("prototype")
public PrototypeInterface getDemoBean(int i) {
return switch (i) {
case 1 -> new PrototypeOne();