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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -242,7 +242,8 @@ public class BeanMethodPolymorphismTests {
@Configuration @Configuration
static class OverridingConfig extends BaseConfig { static class OverridingConfig extends BaseConfig {
@Bean @Lazy @Bean
@Lazy
@Override @Override
public BaseTestBean testBean() { public BaseTestBean testBean() {
return new BaseTestBean() { return new BaseTestBean() {
@ -258,7 +259,8 @@ public class BeanMethodPolymorphismTests {
@Configuration @Configuration
static class OverridingConfigWithDifferentBeanName extends BaseConfig { static class OverridingConfigWithDifferentBeanName extends BaseConfig {
@Bean("myTestBean") @Lazy @Bean("myTestBean")
@Lazy
@Override @Override
public BaseTestBean testBean() { public BaseTestBean testBean() {
return new BaseTestBean() { return new BaseTestBean() {
@ -274,7 +276,8 @@ public class BeanMethodPolymorphismTests {
@Configuration @Configuration
static class NarrowedOverridingConfig extends BaseConfig { static class NarrowedOverridingConfig extends BaseConfig {
@Bean @Lazy @Bean
@Lazy
@Override @Override
public ExtendedTestBean testBean() { public ExtendedTestBean testBean() {
return new ExtendedTestBean() { return new ExtendedTestBean() {
@ -287,6 +290,7 @@ public class BeanMethodPolymorphismTests {
} }
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false) @Configuration(enforceUniqueMethods = false)
static class ConfigWithOverloading { static class ConfigWithOverloading {
@ -302,15 +306,18 @@ public class BeanMethodPolymorphismTests {
} }
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false) @Configuration(enforceUniqueMethods = false)
static class ConfigWithOverloadingAndAdditionalMetadata { static class ConfigWithOverloadingAndAdditionalMetadata {
@Bean @Lazy @Bean
@Lazy
String aString() { String aString() {
return "regular"; return "regular";
} }
@Bean @Lazy @Bean
@Lazy
String aString(Integer dependency) { String aString(Integer dependency) {
return "overloaded" + dependency; return "overloaded" + dependency;
} }
@ -335,7 +342,8 @@ public class BeanMethodPolymorphismTests {
return 5; return 5;
} }
@Bean @Lazy @Bean
@Lazy
String aString(Integer dependency) { String aString(Integer dependency) {
return "overloaded" + dependency; return "overloaded" + dependency;
} }
@ -350,7 +358,8 @@ public class BeanMethodPolymorphismTests {
return 5; return 5;
} }
@Bean @Lazy @Bean
@Lazy
String aString(List<Integer> dependency) { String aString(List<Integer> dependency) {
return "overloaded" + dependency.get(0); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -83,7 +83,7 @@ class ConfigurationClassProcessingTests {
() -> ConfigWithBeanWithCustomNameConfiguredViaValueAttribute.testBean, "enigma"); () -> 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(); GenericApplicationContext ac = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
ac.registerBeanDefinition("config", new RootBeanDefinition(testClass)); ac.registerBeanDefinition("config", new RootBeanDefinition(testClass));
@ -92,8 +92,8 @@ class ConfigurationClassProcessingTests {
assertThat(ac.getBean(beanName)).isSameAs(testBeanSupplier.get()); assertThat(ac.getBean(beanName)).isSameAs(testBeanSupplier.get());
// method name should not be registered // method name should not be registered
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
ac.getBean("methodName")); .isThrownBy(() -> ac.getBean("methodName"));
} }
@Test @Test
@ -113,11 +113,12 @@ class ConfigurationClassProcessingTests {
BeanFactory factory = initBeanFactory(false, testClass); BeanFactory factory = initBeanFactory(false, testClass);
assertThat(factory.getBean(beanName)).isSameAs(testBean); 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 // method name should not be registered
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
factory.getBean("methodName")); .isThrownBy(() -> factory.getBean("methodName"));
} }
@Test // SPR-11830 @Test // SPR-11830
@ -140,8 +141,8 @@ class ConfigurationClassProcessingTests {
@Test @Test
void finalBeanMethod() { void finalBeanMethod() {
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() -> assertThatExceptionOfType(BeanDefinitionParsingException.class)
initBeanFactory(false, ConfigWithFinalBean.class)); .isThrownBy(() -> initBeanFactory(false, ConfigWithFinalBean.class));
} }
@Test @Test
@ -151,8 +152,8 @@ class ConfigurationClassProcessingTests {
@Test // gh-31007 @Test // gh-31007
void voidBeanMethod() { void voidBeanMethod() {
assertThatExceptionOfType(BeanDefinitionParsingException.class).isThrownBy(() -> assertThatExceptionOfType(BeanDefinitionParsingException.class)
initBeanFactory(false, ConfigWithVoidBean.class)); .isThrownBy(() -> initBeanFactory(false, ConfigWithVoidBean.class));
} }
@Test @Test
@ -180,23 +181,19 @@ class ConfigurationClassProcessingTests {
assertThat(factory.isTypeMatch("&factoryBean", FactoryBean.class)).isTrue(); assertThat(factory.isTypeMatch("&factoryBean", FactoryBean.class)).isTrue();
assertThat(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class)).isFalse(); assertThat(factory.isTypeMatch("&factoryBean", BeanClassLoaderAware.class)).isFalse();
assertThat(factory.isTypeMatch("&factoryBean", ListFactoryBean.class)).isFalse(); assertThat(factory.isTypeMatch("&factoryBean", ListFactoryBean.class)).isFalse();
boolean condition = factory.getBean("factoryBean") instanceof List; assertThat(factory.getBean("factoryBean")).isInstanceOf(List.class);
assertThat(condition).isTrue();
String[] beanNames = factory.getBeanNamesForType(FactoryBean.class); String[] beanNames = factory.getBeanNamesForType(FactoryBean.class);
assertThat(beanNames).hasSize(1); assertThat(beanNames).containsExactly("&factoryBean");
assertThat(beanNames[0]).isEqualTo("&factoryBean");
beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class); beanNames = factory.getBeanNamesForType(BeanClassLoaderAware.class);
assertThat(beanNames).hasSize(1); assertThat(beanNames).containsExactly("&factoryBean");
assertThat(beanNames[0]).isEqualTo("&factoryBean");
beanNames = factory.getBeanNamesForType(ListFactoryBean.class); beanNames = factory.getBeanNamesForType(ListFactoryBean.class);
assertThat(beanNames).hasSize(1); assertThat(beanNames).containsExactly("&factoryBean");
assertThat(beanNames[0]).isEqualTo("&factoryBean");
beanNames = factory.getBeanNamesForType(List.class); beanNames = factory.getBeanNamesForType(List.class);
assertThat(beanNames[0]).isEqualTo("factoryBean"); assertThat(beanNames).containsExactly("factoryBean");
} }
@Test @Test
@ -381,7 +378,7 @@ class ConfigurationClassProcessingTests {
static TestBean testBean = new TestBean(ConfigWithBeanWithCustomName.class.getSimpleName()); static TestBean testBean = new TestBean(ConfigWithBeanWithCustomName.class.getSimpleName());
@Bean(name = "customName") @Bean("customName")
public TestBean methodName() { public TestBean methodName() {
return testBean; return testBean;
} }
@ -405,7 +402,7 @@ class ConfigurationClassProcessingTests {
static TestBean testBean = new TestBean(ConfigWithBeanWithAliases.class.getSimpleName()); static TestBean testBean = new TestBean(ConfigWithBeanWithAliases.class.getSimpleName());
@Bean(name = {"name1", "alias1", "alias2", "alias3"}) @Bean({"name1", "alias1", "alias2", "alias3"})
public TestBean methodName() { public TestBean methodName() {
return testBean; return testBean;
} }
@ -430,7 +427,7 @@ class ConfigurationClassProcessingTests {
static TestBean testBean = new TestBean(ConfigWithBeanWithProviderImplementation.class.getSimpleName()); static TestBean testBean = new TestBean(ConfigWithBeanWithProviderImplementation.class.getSimpleName());
@Override @Override
@Bean(name = "customName") @Bean("customName")
public TestBean get() { public TestBean get() {
return testBean; return testBean;
} }
@ -443,7 +440,7 @@ class ConfigurationClassProcessingTests {
static Set<String> set = Collections.singleton("value"); static Set<String> set = Collections.singleton("value");
@Override @Override
@Bean(name = "customName") @Bean("customName")
public Set<String> get() { public Set<String> get() {
return set; return set;
} }
@ -453,7 +450,8 @@ class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class ConfigWithFinalBean { static class ConfigWithFinalBean {
@Bean public final TestBean testBean() { @Bean
public final TestBean testBean() {
return new TestBean(); return new TestBean();
} }
} }
@ -462,7 +460,8 @@ class ConfigurationClassProcessingTests {
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class ConfigWithFinalBeanWithoutProxy { static class ConfigWithFinalBeanWithoutProxy {
@Bean public final TestBean testBean() { @Bean
public final TestBean testBean() {
return new TestBean(); return new TestBean();
} }
} }
@ -471,7 +470,8 @@ class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class ConfigWithVoidBean { static class ConfigWithVoidBean {
@Bean public void testBean() { @Bean
public void testBean() {
} }
} }
@ -479,7 +479,8 @@ class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class SimplestPossibleConfig { static class SimplestPossibleConfig {
@Bean public String stringBean() { @Bean
public String stringBean() {
return "foo"; return "foo";
} }
} }
@ -488,11 +489,13 @@ class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class ConfigWithNonSpecificReturnTypes { static class ConfigWithNonSpecificReturnTypes {
@Bean public Object stringBean() { @Bean
public Object stringBean() {
return "foo"; return "foo";
} }
@Bean public FactoryBean<?> factoryBean() { @Bean
public FactoryBean<?> factoryBean() {
ListFactoryBean fb = new ListFactoryBean(); ListFactoryBean fb = new ListFactoryBean();
fb.setSourceList(Arrays.asList("element1", "element2")); fb.setSourceList(Arrays.asList("element1", "element2"));
return fb; return fb;
@ -503,29 +506,34 @@ class ConfigurationClassProcessingTests {
@Configuration @Configuration
static class ConfigWithPrototypeBean { static class ConfigWithPrototypeBean {
@Bean public TestBean foo() { @Bean
public TestBean foo() {
TestBean foo = new SpousyTestBean("foo"); TestBean foo = new SpousyTestBean("foo");
foo.setSpouse(bar()); foo.setSpouse(bar());
return foo; return foo;
} }
@Bean public TestBean bar() { @Bean
public TestBean bar() {
TestBean bar = new SpousyTestBean("bar"); TestBean bar = new SpousyTestBean("bar");
bar.setSpouse(baz()); bar.setSpouse(baz());
return bar; return bar;
} }
@Bean @Scope("prototype") @Bean
@Scope("prototype")
public TestBean baz() { public TestBean baz() {
return new TestBean("baz"); return new TestBean("baz");
} }
@Bean @Scope("prototype") @Bean
@Scope("prototype")
public TestBean adaptive1(InjectionPoint ip) { public TestBean adaptive1(InjectionPoint ip) {
return new TestBean(ip.getMember().getName()); return new TestBean(ip.getMember().getName());
} }
@Bean @Scope("prototype") @Bean
@Scope("prototype")
public TestBean adaptive2(DependencyDescriptor dd) { public TestBean adaptive2(DependencyDescriptor dd) {
return new TestBean(dd.getMember().getName()); return new TestBean(dd.getMember().getName());
} }
@ -542,14 +550,17 @@ class ConfigurationClassProcessingTests {
} }
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false) @Configuration(enforceUniqueMethods = false)
static class ConfigWithMethodNameMismatch { static class ConfigWithMethodNameMismatch {
@Bean(name = "foo") public TestBean foo1() { @Bean("foo")
public TestBean foo1() {
return new SpousyTestBean("foo1"); return new SpousyTestBean("foo1");
} }
@Bean(name = "foo") public TestBean foo2() { @Bean("foo")
public TestBean foo2() {
return new SpousyTestBean("foo2"); return new SpousyTestBean("foo2");
} }
} }
@ -558,12 +569,14 @@ class ConfigurationClassProcessingTests {
@Scope("prototype") @Scope("prototype")
static class AdaptiveInjectionPoints { static class AdaptiveInjectionPoints {
@Autowired @Qualifier("adaptive1") @Autowired
@Qualifier("adaptive1")
public TestBean adaptiveInjectionPoint1; public TestBean adaptiveInjectionPoint1;
public TestBean adaptiveInjectionPoint2; public TestBean adaptiveInjectionPoint2;
@Autowired @Qualifier("adaptive2") @Autowired
@Qualifier("adaptive2")
public void setAdaptiveInjectionPoint2(TestBean adaptiveInjectionPoint2) { public void setAdaptiveInjectionPoint2(TestBean adaptiveInjectionPoint2) {
this.adaptiveInjectionPoint2 = adaptiveInjectionPoint2; this.adaptiveInjectionPoint2 = adaptiveInjectionPoint2;
} }
@ -687,15 +700,16 @@ class ConfigurationClassProcessingTests {
} }
@SuppressWarnings("deprecation")
@Configuration(enforceUniqueMethods = false) @Configuration(enforceUniqueMethods = false)
public static class OverloadedBeanMismatch { public static class OverloadedBeanMismatch {
@Bean(name = "other") @Bean("other")
public NestedTestBean foo() { public NestedTestBean foo() {
return new NestedTestBean(); return new NestedTestBean();
} }
@Bean(name = "foo") @Bean("foo")
public TestBean foo(@Qualifier("other") NestedTestBean other) { public TestBean foo(@Qualifier("other") NestedTestBean other) {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setLawyer(other); tb.setLawyer(other);
@ -728,7 +742,7 @@ class ConfigurationClassProcessingTests {
static class ConfigWithDynamicPrototype { static class ConfigWithDynamicPrototype {
@Bean @Bean
@Scope(value = "prototype") @Scope("prototype")
public PrototypeInterface getDemoBean(int i) { public PrototypeInterface getDemoBean(int i) {
return switch (i) { return switch (i) {
case 1 -> new PrototypeOne(); case 1 -> new PrototypeOne();