From 276054218235f922df11732997394747e6c47073 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 11 Oct 2011 04:02:03 +0000 Subject: [PATCH] Add repro test case for package-private @Bean issue Reproduces the issue described in SPR-8756 in addition to demonstrating the suggested workaround. Issue: SPR-8756, SPR-8725 --- ...kagePrivateBeanMethodInheritanceTests.java | 94 +++++++++++++++++++ .../configuration/a/BaseConfig.java | 43 +++++++++ 2 files changed, 137 insertions(+) create mode 100644 org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/PackagePrivateBeanMethodInheritanceTests.java create mode 100644 org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/a/BaseConfig.java diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/PackagePrivateBeanMethodInheritanceTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/PackagePrivateBeanMethodInheritanceTests.java new file mode 100644 index 0000000000..3e5e7ed77e --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/PackagePrivateBeanMethodInheritanceTests.java @@ -0,0 +1,94 @@ +/* + * Copyright 2002-2011 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.context.annotation.configuration; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Reproduces SPR-8756, which has been marked as "won't fix" for reasons + * described in the JIRA issue. Also demonstrates the suggested workaround. + * + * @author Chris Beams + */ +public class PackagePrivateBeanMethodInheritanceTests { + + @Test + public void repro() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(ReproConfig.class); + ctx.refresh(); + Foo foo1 = ctx.getBean("foo1", Foo.class); + Foo foo2 = ctx.getBean("foo2", Foo.class); + ctx.getBean("packagePrivateBar", Bar.class); // <-- i.e. @Bean was registered + assertThat(foo1.bar, not(is(foo2.bar))); // <-- i.e. @Bean *not* enhanced + } + + @Test + public void workaround() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(WorkaroundConfig.class); + ctx.refresh(); + Foo foo1 = ctx.getBean("foo1", Foo.class); + Foo foo2 = ctx.getBean("foo2", Foo.class); + ctx.getBean("protectedBar", Bar.class); // <-- i.e. @Bean was registered + assertThat(foo1.bar, is(foo2.bar)); // <-- i.e. @Bean *was* enhanced + } + + public static class Foo { + final Bar bar; + public Foo(Bar bar) { + this.bar = bar; + } + } + + public static class Bar { + } + + @Configuration + public static class ReproConfig extends org.springframework.context.annotation.configuration.a.BaseConfig { + @Bean + public Foo foo1() { + return new Foo(reproBar()); + } + + @Bean + public Foo foo2() { + return new Foo(reproBar()); + } + } + + @Configuration + public static class WorkaroundConfig extends org.springframework.context.annotation.configuration.a.BaseConfig { + @Bean + public Foo foo1() { + return new Foo(workaroundBar()); + } + + @Bean + public Foo foo2() { + return new Foo(workaroundBar()); + } + } +} + diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/a/BaseConfig.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/a/BaseConfig.java new file mode 100644 index 0000000000..e83c165715 --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/a/BaseConfig.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2011 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.context.annotation.configuration.a; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.configuration.PackagePrivateBeanMethodInheritanceTests.Bar; + +public abstract class BaseConfig { + + // ---- reproduce ---- + @Bean + Bar packagePrivateBar() { + return new Bar(); + } + + public Bar reproBar() { + return packagePrivateBar(); + } + + // ---- workaround ---- + @Bean + protected Bar protectedBar() { + return new Bar(); + } + + public Bar workaroundBar() { + return protectedBar(); + } +} \ No newline at end of file