Browse Source
+ Added tests for @Aspect support in @Configuration classes + Added tests for @Inherited @Configuration behavior git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@781 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
12 changed files with 250 additions and 37 deletions
@ -0,0 +1,5 @@
@@ -0,0 +1,5 @@
|
||||
/** |
||||
* Unit tests for classes in |
||||
* {@literal org.springframework.context.annotation.support} |
||||
*/ |
||||
package org.springframework.config.java.support; |
||||
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* Copyright 2002-2009 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 test.basic; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.aop.support.AopUtils; |
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
||||
import org.springframework.config.java.Bean; |
||||
import org.springframework.config.java.Configuration; |
||||
import org.springframework.config.java.support.ConfigurationClassPostProcessor; |
||||
|
||||
|
||||
/** |
||||
* Covers the somewhat unlilely case of a {@link Configuration} class being declared |
||||
* as an abstract {@link BeanDefinition}. |
||||
* |
||||
* @author Chris Beams |
||||
* @see BeanDefinition#isAbstract() |
||||
*/ |
||||
public class AbstractBeanDefinitionConfigurationClassTests { |
||||
|
||||
@SuppressWarnings("unused") |
||||
@Test |
||||
public void abstractConfigurationClassBeanDefinitionsAreIgnored() { |
||||
@Configuration class Abstract { @Bean Object foo1() { return null; } } |
||||
@Configuration class Concrete { @Bean Object foo2() { return null; } } |
||||
|
||||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); |
||||
factory.registerBeanDefinition("abstract", |
||||
rootBeanDefinition(Abstract.class).setAbstract(true).getBeanDefinition()); |
||||
factory.registerBeanDefinition("concrete", |
||||
rootBeanDefinition(Concrete.class).setAbstract(false).getBeanDefinition()); |
||||
new ConfigurationClassPostProcessor().postProcessBeanFactory(factory); |
||||
|
||||
assertTrue("abstract configuration should be CGLIB-enhanced", |
||||
AopUtils.isCglibProxyClassName(factory.getBeanDefinition("abstract").getBeanClassName())); |
||||
assertTrue("concrete configuration should be CGLIB-enhanced", |
||||
AopUtils.isCglibProxyClassName(factory.getBeanDefinition("concrete").getBeanClassName())); |
||||
|
||||
assertFalse("abstract configuration's @Bean method should not be registered", |
||||
factory.containsBeanDefinition("foo1")); |
||||
assertTrue("concrete configuration's @Bean method should be registered", |
||||
factory.containsBeanDefinition("foo2")); |
||||
} |
||||
} |
||||
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
package test.basic; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.aspectj.lang.annotation.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.beans.factory.support.RootBeanDefinition; |
||||
import org.springframework.beans.factory.xml.XmlBeanFactory; |
||||
import org.springframework.config.java.Bean; |
||||
import org.springframework.config.java.Configuration; |
||||
import org.springframework.config.java.support.ConfigurationClassPostProcessor; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.core.io.ClassPathResource; |
||||
|
||||
import test.beans.TestBean; |
||||
|
||||
|
||||
public class AspectTests { |
||||
private void assertAdviceWasApplied(Class<?> configClass) { |
||||
GenericApplicationContext ctx = new GenericApplicationContext( |
||||
new XmlBeanFactory(new ClassPathResource("aspectj-autoproxy-config.xml", AspectTests.class))); |
||||
ctx.addBeanFactoryPostProcessor(new ConfigurationClassPostProcessor()); |
||||
ctx.registerBeanDefinition("config", new RootBeanDefinition(configClass)); |
||||
ctx.refresh(); |
||||
|
||||
TestBean testBean = ctx.getBean("testBean", TestBean.class); |
||||
assertThat(testBean.getName(), equalTo("name")); |
||||
testBean.absquatulate(); |
||||
assertThat(testBean.getName(), equalTo("advisedName")); |
||||
} |
||||
|
||||
@Test |
||||
public void aspectAnnotatedConfiguration() { |
||||
assertAdviceWasApplied(AspectConfig.class); |
||||
} |
||||
|
||||
@Test |
||||
public void configurationIncludesAspect() { |
||||
assertAdviceWasApplied(ConfigurationWithAspect.class); |
||||
} |
||||
|
||||
|
||||
@Aspect |
||||
@Configuration |
||||
static class AspectConfig { |
||||
@Bean |
||||
public TestBean testBean() { |
||||
return new TestBean("name"); |
||||
} |
||||
|
||||
@Before("execution(* test.beans.TestBean.absquatulate(..)) && target(testBean)") |
||||
public void touchBean(TestBean testBean) { |
||||
testBean.setName("advisedName"); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class ConfigurationWithAspect { |
||||
@Bean |
||||
public TestBean testBean() { |
||||
return new TestBean("name"); |
||||
} |
||||
|
||||
@Bean |
||||
public NameChangingAspect nameChangingAspect() { |
||||
return new NameChangingAspect(); |
||||
} |
||||
} |
||||
|
||||
@Aspect |
||||
static class NameChangingAspect { |
||||
@Before("execution(* test.beans.TestBean.absquatulate(..)) && target(testBean)") |
||||
public void touchBean(TestBean testBean) { |
||||
testBean.setName("advisedName"); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
/* |
||||
* Copyright 2002-2009 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 test.basic; |
||||
|
||||
import java.lang.annotation.Inherited; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
||||
import org.springframework.beans.factory.support.RootBeanDefinition; |
||||
import org.springframework.config.java.Bean; |
||||
import org.springframework.config.java.Configuration; |
||||
import org.springframework.config.java.support.ConfigurationClassPostProcessor; |
||||
|
||||
import test.beans.TestBean; |
||||
|
||||
/** |
||||
* Tests that polymorphic Configuration classes need not explicitly redeclare the |
||||
* {@link Configuration} annotation. This respects the {@link Inherited} nature |
||||
* of the Configuration annotation, even though it's being detected via ASM. |
||||
* |
||||
* @author Chris Beams |
||||
*/ |
||||
public class PolymorphicConfigurationTests { |
||||
|
||||
@Test |
||||
public void subclassNeedNotDeclareConfigurationAnnotation() { |
||||
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
||||
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(Config.class)); |
||||
new ConfigurationClassPostProcessor().postProcessBeanFactory(beanFactory); |
||||
|
||||
beanFactory.getBean("testBean", TestBean.class); |
||||
} |
||||
|
||||
@Configuration |
||||
static class SuperConfig { |
||||
@Bean |
||||
public TestBean testBean() { |
||||
return new TestBean(); |
||||
} |
||||
} |
||||
|
||||
static class Config extends SuperConfig { } |
||||
} |
||||
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:aop="http://www.springframework.org/schema/aop" |
||||
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd |
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" |
||||
xmlns:context="http://www.springframework.org/schema/context"> |
||||
|
||||
<aop:aspectj-autoproxy proxy-target-class="true"/> |
||||
</beans> |
||||
Loading…
Reference in new issue