moved ApplicationContext-dependent .beans.factory.xml.* tests from .testsuite -> .context
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<!--
|
||||
Not yet in use: illustration of possible approach.
|
||||
-->
|
||||
<bean id="autoProxiedOverload" lazy-init="true"
|
||||
class="org.springframework.beans.factory.xml.OverloadLookup">
|
||||
<lookup-method name="newTestBean" bean="jenny"/>
|
||||
</bean>
|
||||
|
||||
<bean id="regularlyProxiedOverloadTarget" scope="prototype"
|
||||
class="org.springframework.beans.factory.xml.OverloadLookup">
|
||||
<lookup-method name="newTestBean" bean="jenny"/>
|
||||
</bean>
|
||||
|
||||
<bean id="regularlyProxiedOverload" lazy-init="true" class="org.springframework.aop.framework.ProxyFactoryBean">
|
||||
<property name="target"><ref local="regularlyProxiedOverloadTarget"/></property>
|
||||
<property name="proxyTargetClass"><value>true</value></property>
|
||||
<property name="interceptorNames"><value>interceptor</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="jenny" class="org.springframework.beans.TestBean" scope="prototype">
|
||||
<property name="name"><value>Jenny</value></property>
|
||||
<property name="age"><value>30</value></property>
|
||||
</bean>
|
||||
|
||||
<!-- Add autoproxy -->
|
||||
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
|
||||
<property name="beanNames"><value>autoProxiedOverload</value></property>
|
||||
<property name="proxyTargetClass"><value>true</value></property>
|
||||
<property name="interceptorNames"><value>interceptor</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="interceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.beans.factory.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.interceptor.DebugInterceptor;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Tests lookup methods wrapped by a CGLIB proxy (see SPR-391).
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class LookupMethodWrappedByCglibProxyTests {
|
||||
|
||||
private static final Class<?> CLASS = LookupMethodWrappedByCglibProxyTests.class;
|
||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||
|
||||
private static final String CONTEXT = CLASSNAME + "-context.xml";
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.applicationContext = new ClassPathXmlApplicationContext(CONTEXT, CLASS);
|
||||
resetInterceptor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoProxiedLookup() {
|
||||
OverloadLookup olup = (OverloadLookup) applicationContext.getBean("autoProxiedOverload");
|
||||
ITestBean jenny = olup.newTestBean();
|
||||
assertEquals("Jenny", jenny.getName());
|
||||
assertEquals("foo", olup.testMethod());
|
||||
assertInterceptorCount(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegularlyProxiedLookup() {
|
||||
OverloadLookup olup = (OverloadLookup) applicationContext.getBean("regularlyProxiedOverload");
|
||||
ITestBean jenny = olup.newTestBean();
|
||||
assertEquals("Jenny", jenny.getName());
|
||||
assertEquals("foo", olup.testMethod());
|
||||
assertInterceptorCount(2);
|
||||
}
|
||||
|
||||
private void assertInterceptorCount(int count) {
|
||||
DebugInterceptor interceptor = getInterceptor();
|
||||
assertEquals("Interceptor count is incorrect", count, interceptor.getCount());
|
||||
}
|
||||
|
||||
private void resetInterceptor() {
|
||||
DebugInterceptor interceptor = getInterceptor();
|
||||
interceptor.resetCount();
|
||||
}
|
||||
|
||||
private DebugInterceptor getInterceptor() {
|
||||
return (DebugInterceptor) applicationContext.getBean("interceptor");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
abstract class OverloadLookup {
|
||||
|
||||
public abstract ITestBean newTestBean();
|
||||
|
||||
public String testMethod() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
|
||||
|
||||
<context:annotation-config/>
|
||||
|
||||
<bean id="larryBean" class="org.springframework.beans.factory.xml.QualifierAnnotationTests$Person">
|
||||
<property name="name" value="LarryBean"/>
|
||||
</bean>
|
||||
|
||||
<alias name="larryBean" alias="stooge"/>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.QualifierAnnotationTests$Person">
|
||||
<property name="name" value="Larry"/>
|
||||
<qualifier value="larry"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.QualifierAnnotationTests$SpecialPerson">
|
||||
<property name="name" value="LarrySpecial"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.QualifierAnnotationTests$Person">
|
||||
<property name="name" value="Curly"/>
|
||||
<qualifier type="QualifierAnnotationTests.SimpleValueQualifier" value="curly"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.QualifierAnnotationTests$Person">
|
||||
<meta key="name" value="moe"/>
|
||||
<meta key="age" value="42"/>
|
||||
<property name="name" value="Moe Sr."/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.QualifierAnnotationTests$Person">
|
||||
<property name="name" value="Moe Jr."/>
|
||||
<qualifier type="QualifierAnnotationTests.MultipleAttributeQualifier">
|
||||
<attribute key="name" value="moe"/>
|
||||
<attribute key="age" value="15"/>
|
||||
</qualifier>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.beans.factory.xml;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.util.ClassUtils.convertClassNameToResourcePath;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class QualifierAnnotationTests {
|
||||
|
||||
private static final String CLASSNAME = QualifierAnnotationTests.class.getName();
|
||||
private static final String CONFIG_LOCATION =
|
||||
format("classpath:%s-context.xml", convertClassNameToResourcePath(CLASSNAME));
|
||||
|
||||
|
||||
@Test
|
||||
public void testNonQualifiedFieldFails() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", NonQualifiedTestBean.class);
|
||||
try {
|
||||
context.refresh();
|
||||
fail("Should have thrown a BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertTrue(e.getMessage().contains("found 6"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByValue() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", QualifiedByValueTestBean.class);
|
||||
context.refresh();
|
||||
QualifiedByValueTestBean testBean = (QualifiedByValueTestBean) context.getBean("testBean");
|
||||
Person person = testBean.getLarry();
|
||||
assertEquals("Larry", person.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByBeanName() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", QualifiedByBeanNameTestBean.class);
|
||||
context.refresh();
|
||||
QualifiedByBeanNameTestBean testBean = (QualifiedByBeanNameTestBean) context.getBean("testBean");
|
||||
Person person = testBean.getLarry();
|
||||
assertEquals("LarryBean", person.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByAlias() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", QualifiedByAliasTestBean.class);
|
||||
context.refresh();
|
||||
QualifiedByAliasTestBean testBean = (QualifiedByAliasTestBean) context.getBean("testBean");
|
||||
Person person = testBean.getStooge();
|
||||
assertEquals("LarryBean", person.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByAnnotation() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", QualifiedByAnnotationTestBean.class);
|
||||
context.refresh();
|
||||
QualifiedByAnnotationTestBean testBean = (QualifiedByAnnotationTestBean) context.getBean("testBean");
|
||||
Person person = testBean.getLarry();
|
||||
assertEquals("LarrySpecial", person.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByCustomValue() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", QualifiedByCustomValueTestBean.class);
|
||||
context.refresh();
|
||||
QualifiedByCustomValueTestBean testBean = (QualifiedByCustomValueTestBean) context.getBean("testBean");
|
||||
Person person = testBean.getCurly();
|
||||
assertEquals("Curly", person.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByAnnotationValue() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", QualifiedByAnnotationValueTestBean.class);
|
||||
context.refresh();
|
||||
QualifiedByAnnotationValueTestBean testBean = (QualifiedByAnnotationValueTestBean) context.getBean("testBean");
|
||||
Person person = testBean.getLarry();
|
||||
assertEquals("LarrySpecial", person.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByAttributesFailsWithoutCustomQualifierRegistered() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
context.registerSingleton("testBean", QualifiedByAttributesTestBean.class);
|
||||
try {
|
||||
context.refresh();
|
||||
fail("should have thrown a BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
assertTrue(e.getMessage().contains("found 6"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQualifiedByAttributesWithCustomQualifierRegistered() {
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
|
||||
reader.loadBeanDefinitions(CONFIG_LOCATION);
|
||||
QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver)
|
||||
context.getDefaultListableBeanFactory().getAutowireCandidateResolver();
|
||||
resolver.addQualifierType(MultipleAttributeQualifier.class);
|
||||
context.registerSingleton("testBean", QualifiedByAttributesTestBean.class);
|
||||
context.refresh();
|
||||
QualifiedByAttributesTestBean testBean = (QualifiedByAttributesTestBean) context.getBean("testBean");
|
||||
Person moeSenior = testBean.getMoeSenior();
|
||||
Person moeJunior = testBean.getMoeJunior();
|
||||
assertEquals("Moe Sr.", moeSenior.getName());
|
||||
assertEquals("Moe Jr.", moeJunior.getName());
|
||||
}
|
||||
|
||||
|
||||
private static class NonQualifiedTestBean {
|
||||
|
||||
@Autowired
|
||||
private Person anonymous;
|
||||
|
||||
public Person getAnonymous() {
|
||||
return anonymous;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class QualifiedByValueTestBean {
|
||||
|
||||
@Autowired @Qualifier("larry")
|
||||
private Person larry;
|
||||
|
||||
public Person getLarry() {
|
||||
return larry;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class QualifiedByBeanNameTestBean {
|
||||
|
||||
@Autowired @Qualifier("larryBean")
|
||||
private Person larry;
|
||||
|
||||
public Person getLarry() {
|
||||
return larry;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class QualifiedByAliasTestBean {
|
||||
|
||||
@Autowired @Qualifier("stooge")
|
||||
private Person stooge;
|
||||
|
||||
public Person getStooge() {
|
||||
return stooge;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class QualifiedByAnnotationTestBean {
|
||||
|
||||
@Autowired @Qualifier("special")
|
||||
private Person larry;
|
||||
|
||||
public Person getLarry() {
|
||||
return larry;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class QualifiedByCustomValueTestBean {
|
||||
|
||||
@Autowired @SimpleValueQualifier("curly")
|
||||
private Person curly;
|
||||
|
||||
public Person getCurly() {
|
||||
return curly;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class QualifiedByAnnotationValueTestBean {
|
||||
|
||||
@Autowired @SimpleValueQualifier("special")
|
||||
private Person larry;
|
||||
|
||||
public Person getLarry() {
|
||||
return larry;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class QualifiedByAttributesTestBean {
|
||||
|
||||
@Autowired @MultipleAttributeQualifier(name="moe", age=42)
|
||||
private Person moeSenior;
|
||||
|
||||
@Autowired @MultipleAttributeQualifier(name="moe", age=15)
|
||||
private Person moeJunior;
|
||||
|
||||
public Person getMoeSenior() {
|
||||
return moeSenior;
|
||||
}
|
||||
|
||||
public Person getMoeJunior() {
|
||||
return moeJunior;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class Person {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Qualifier("special")
|
||||
@SimpleValueQualifier("special")
|
||||
private static class SpecialPerson extends Person {
|
||||
}
|
||||
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Qualifier
|
||||
public @interface SimpleValueQualifier {
|
||||
|
||||
String value() default "";
|
||||
}
|
||||
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MultipleAttributeQualifier {
|
||||
|
||||
String name();
|
||||
|
||||
int age();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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:test="http://www.springframework.org/schema/beans/test"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
|
||||
http://www.springframework.org/schema/beans/test http://www.springframework.org/schema/beans/factory/xml/support/CustomNamespaceHandlerTests.xsd">
|
||||
|
||||
<test:testBean id="testBean" name="Rob Harrop" age="23"/>
|
||||
|
||||
<bean id="customisedTestBean" class="org.springframework.beans.TestBean">
|
||||
<test:set name="Rob Harrop" age="23"/>
|
||||
</bean>
|
||||
|
||||
<bean id="debuggingTestBean" class="org.springframework.beans.TestBean">
|
||||
<test:debug/>
|
||||
<property name="name" value="Rob Harrop"/>
|
||||
<property name="age" value="23"/>
|
||||
</bean>
|
||||
|
||||
<bean id="chainedTestBean" class="org.springframework.beans.TestBean">
|
||||
<test:debug/>
|
||||
<test:nop/>
|
||||
<property name="name" value="Rob Harrop"/>
|
||||
<property name="age" value="23"/>
|
||||
</bean>
|
||||
|
||||
<bean id="decorateWithAttribute" class="org.springframework.beans.TestBean" test:object-name="foo"/>
|
||||
|
||||
<util:list id="list.of.things">
|
||||
<test:person name="Fiona Apple" age="20"/>
|
||||
<test:person name="Harriet Wheeler" age="30"/>
|
||||
</util:list>
|
||||
|
||||
<util:set id="set.of.things">
|
||||
<test:person name="Fiona Apple" age="20"/>
|
||||
<test:person name="Harriet Wheeler" age="30"/>
|
||||
</util:set>
|
||||
|
||||
<util:map id="map.of.things">
|
||||
<entry key="fiona.apple">
|
||||
<test:person name="Fiona Apple" age="20"/>
|
||||
</entry>
|
||||
<entry key="harriet.wheeler">
|
||||
<test:person name="Harriet Wheeler" age="30"/>
|
||||
</entry>
|
||||
</util:map>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.beans.factory.xml.support;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.config.AbstractInterceptorDrivenBeanDefinitionDecorator;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.interceptor.DebugInterceptor;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver;
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerResolver;
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.beans.factory.xml.PluggableSchemaResolver;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import test.interceptor.NopInterceptor;
|
||||
|
||||
/**
|
||||
* Unit tests for custom XML namespace handler implementations.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class CustomNamespaceHandlerTests {
|
||||
|
||||
private static final Class<?> CLASS = CustomNamespaceHandlerTests.class;
|
||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||
private static final String FQ_PATH = "org/springframework/beans/factory/xml/support";
|
||||
|
||||
private static final String NS_PROPS = format("%s/%s.properties", FQ_PATH, CLASSNAME);
|
||||
private static final String NS_XML = format("%s/%s-context.xml", FQ_PATH, CLASSNAME);
|
||||
private static final String TEST_XSD = format("%s/%s.xsd", FQ_PATH, CLASSNAME);
|
||||
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
NamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(CLASS.getClassLoader(), NS_PROPS);
|
||||
this.beanFactory = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
|
||||
reader.setNamespaceHandlerResolver(resolver);
|
||||
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
|
||||
reader.setEntityResolver(new DummySchemaResolver());
|
||||
reader.loadBeanDefinitions(getResource());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSimpleParser() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
|
||||
assetTestBean(bean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleDecorator() throws Exception {
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean("customisedTestBean");
|
||||
assetTestBean(bean);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProxyingDecorator() throws Exception {
|
||||
ITestBean bean = (ITestBean) this.beanFactory.getBean("debuggingTestBean");
|
||||
assetTestBean(bean);
|
||||
assertTrue(AopUtils.isAopProxy(bean));
|
||||
Advisor[] advisors = ((Advised) bean).getAdvisors();
|
||||
assertEquals("Incorrect number of advisors", 1, advisors.length);
|
||||
assertEquals("Incorrect advice class.", DebugInterceptor.class, advisors[0].getAdvice().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChainedDecorators() throws Exception {
|
||||
ITestBean bean = (ITestBean) this.beanFactory.getBean("chainedTestBean");
|
||||
assetTestBean(bean);
|
||||
assertTrue(AopUtils.isAopProxy(bean));
|
||||
Advisor[] advisors = ((Advised) bean).getAdvisors();
|
||||
assertEquals("Incorrect number of advisors", 2, advisors.length);
|
||||
assertEquals("Incorrect advice class.", DebugInterceptor.class, advisors[0].getAdvice().getClass());
|
||||
assertEquals("Incorrect advice class.", NopInterceptor.class, advisors[1].getAdvice().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecorationViaAttribute() throws Exception {
|
||||
BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition("decorateWithAttribute");
|
||||
assertEquals("foo", beanDefinition.getAttribute("objectName"));
|
||||
}
|
||||
|
||||
/**
|
||||
* http://opensource.atlassian.com/projects/spring/browse/SPR-2728
|
||||
*/
|
||||
@Test
|
||||
public void testCustomElementNestedWithinUtilList() throws Exception {
|
||||
List<?> things = (List<?>) this.beanFactory.getBean("list.of.things");
|
||||
assertNotNull(things);
|
||||
assertEquals(2, things.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* http://opensource.atlassian.com/projects/spring/browse/SPR-2728
|
||||
*/
|
||||
@Test
|
||||
public void testCustomElementNestedWithinUtilSet() throws Exception {
|
||||
Set<?> things = (Set<?>) this.beanFactory.getBean("set.of.things");
|
||||
assertNotNull(things);
|
||||
assertEquals(2, things.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* http://opensource.atlassian.com/projects/spring/browse/SPR-2728
|
||||
*/
|
||||
@Test
|
||||
public void testCustomElementNestedWithinUtilMap() throws Exception {
|
||||
Map<?, ?> things = (Map<?, ?>) this.beanFactory.getBean("map.of.things");
|
||||
assertNotNull(things);
|
||||
assertEquals(2, things.size());
|
||||
}
|
||||
|
||||
|
||||
private void assetTestBean(ITestBean bean) {
|
||||
assertEquals("Invalid name", "Rob Harrop", bean.getName());
|
||||
assertEquals("Invalid age", 23, bean.getAge());
|
||||
}
|
||||
|
||||
private Resource getResource() {
|
||||
return new ClassPathResource(NS_XML);
|
||||
}
|
||||
|
||||
|
||||
private final class DummySchemaResolver extends PluggableSchemaResolver {
|
||||
|
||||
public DummySchemaResolver() {
|
||||
super(CLASS.getClassLoader());
|
||||
}
|
||||
|
||||
|
||||
public InputSource resolveEntity(String publicId, String systemId) throws IOException {
|
||||
InputSource source = super.resolveEntity(publicId, systemId);
|
||||
if (source == null) {
|
||||
Resource resource = new ClassPathResource(TEST_XSD);
|
||||
source = new InputSource(resource.getInputStream());
|
||||
source.setPublicId(publicId);
|
||||
source.setSystemId(systemId);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Custom namespace handler implementation.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
*/
|
||||
final class TestNamespaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
public void init() {
|
||||
registerBeanDefinitionParser("testBean", new TestBeanDefinitionParser());
|
||||
registerBeanDefinitionParser("person", new PersonDefinitionParser());
|
||||
|
||||
registerBeanDefinitionDecorator("set", new PropertyModifyingBeanDefinitionDecorator());
|
||||
registerBeanDefinitionDecorator("debug", new DebugBeanDefinitionDecorator());
|
||||
registerBeanDefinitionDecorator("nop", new NopInterceptorBeanDefinitionDecorator());
|
||||
registerBeanDefinitionDecoratorForAttribute("object-name", new ObjectNameBeanDefinitionDecorator());
|
||||
}
|
||||
|
||||
private static class TestBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
RootBeanDefinition definition = new RootBeanDefinition();
|
||||
definition.setBeanClass(TestBean.class);
|
||||
|
||||
MutablePropertyValues mpvs = new MutablePropertyValues();
|
||||
mpvs.addPropertyValue("name", element.getAttribute("name"));
|
||||
mpvs.addPropertyValue("age", element.getAttribute("age"));
|
||||
definition.setPropertyValues(mpvs);
|
||||
|
||||
parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id"), definition);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class PersonDefinitionParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
return TestBean.class;
|
||||
}
|
||||
|
||||
protected void doParse(Element element, BeanDefinitionBuilder builder) {
|
||||
builder.addPropertyValue("name", element.getAttribute("name"));
|
||||
builder.addPropertyValue("age", element.getAttribute("age"));
|
||||
}
|
||||
}
|
||||
|
||||
private static class PropertyModifyingBeanDefinitionDecorator implements BeanDefinitionDecorator {
|
||||
|
||||
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
|
||||
Element element = (Element) node;
|
||||
BeanDefinition def = definition.getBeanDefinition();
|
||||
|
||||
MutablePropertyValues mpvs = (def.getPropertyValues() == null) ? new MutablePropertyValues() : def.getPropertyValues();
|
||||
mpvs.addPropertyValue("name", element.getAttribute("name"));
|
||||
mpvs.addPropertyValue("age", element.getAttribute("age"));
|
||||
|
||||
((AbstractBeanDefinition) def).setPropertyValues(mpvs);
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DebugBeanDefinitionDecorator extends AbstractInterceptorDrivenBeanDefinitionDecorator {
|
||||
|
||||
protected BeanDefinition createInterceptorDefinition(Node node) {
|
||||
return new RootBeanDefinition(DebugInterceptor.class);
|
||||
}
|
||||
}
|
||||
|
||||
private static class NopInterceptorBeanDefinitionDecorator extends AbstractInterceptorDrivenBeanDefinitionDecorator {
|
||||
|
||||
protected BeanDefinition createInterceptorDefinition(Node node) {
|
||||
return new RootBeanDefinition(NopInterceptor.class);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ObjectNameBeanDefinitionDecorator implements BeanDefinitionDecorator {
|
||||
|
||||
public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
|
||||
Attr objectNameAttribute = (Attr) node;
|
||||
definition.getBeanDefinition().setAttribute("objectName", objectNameAttribute.getValue());
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
http\://www.springframework.org/schema/beans/test=org.springframework.beans.factory.xml.support.TestNamespaceHandler
|
||||
http\://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler
|
||||
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
|
||||
<xsd:schema xmlns="http://www.springframework.org/schema/beans/test"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/schema/beans/test"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<xsd:element name="person">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="id" type="xsd:string" use="optional" form="unqualified"/>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" form="unqualified"/>
|
||||
<xsd:attribute name="age" type="xsd:integer" use="required" form="unqualified"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="testBean">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="id" type="xsd:string" use="required" form="unqualified"/>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" form="unqualified"/>
|
||||
<xsd:attribute name="age" type="xsd:integer" use="required" form="unqualified"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="set">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" form="unqualified"/>
|
||||
<xsd:attribute name="age" type="xsd:integer" use="required" form="unqualified"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="debug"/>
|
||||
<xsd:element name="nop"/>
|
||||
|
||||
<xsd:attribute name="object-name" type="xsd:string"/>
|
||||
|
||||
</xsd:schema>
|
||||
Reference in New Issue
Block a user