polishing .aop tests
This commit is contained in:
parent
56e868b2d7
commit
beea69d83b
|
@ -23,7 +23,6 @@ import org.junit.Test;
|
|||
import org.springframework.aop.config.AopConfigUtils;
|
||||
import org.springframework.aop.config.AopNamespaceUtils;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.parsing.CollectingReaderEventListener;
|
||||
import org.springframework.beans.factory.parsing.PassThroughSourceExtractor;
|
||||
import org.springframework.beans.factory.parsing.SourceExtractor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
|
@ -32,6 +31,8 @@ import org.springframework.beans.factory.xml.ParserContext;
|
|||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.beans.factory.xml.XmlReaderContext;
|
||||
|
||||
import test.parsing.CollectingReaderEventListener;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Chris Beams
|
||||
|
|
|
@ -26,13 +26,14 @@ import org.junit.Test;
|
|||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanReference;
|
||||
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
|
||||
import org.springframework.beans.factory.parsing.CollectingReaderEventListener;
|
||||
import org.springframework.beans.factory.parsing.ComponentDefinition;
|
||||
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import test.parsing.CollectingReaderEventListener;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
|
@ -40,11 +41,20 @@ import org.springframework.core.io.ClassPathResource;
|
|||
*/
|
||||
public final class AopNamespaceHandlerEventTests {
|
||||
|
||||
private static final Class<?> CLASS = AopNamespaceHandlerEventTests.class;
|
||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||
|
||||
private static final String CONTEXT = CLASSNAME + "-context.xml";
|
||||
private static final String POINTCUT_EVENTS_CONTEXT = CLASSNAME + "-pointcutEvents.xml";
|
||||
private static final String POINTCUT_REF_CONTEXT = CLASSNAME + "-pointcutRefEvents.xml";
|
||||
private static final String DIRECT_POINTCUT_EVENTS_CONTEXT = CLASSNAME + "-directPointcutEvents.xml";
|
||||
|
||||
private CollectingReaderEventListener eventListener = new CollectingReaderEventListener();
|
||||
|
||||
private XmlBeanDefinitionReader reader;
|
||||
|
||||
private DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
||||
|
||||
|
||||
|
||||
@Before
|
||||
|
@ -55,7 +65,7 @@ public final class AopNamespaceHandlerEventTests {
|
|||
|
||||
@Test
|
||||
public void testPointcutEvents() throws Exception {
|
||||
loadBeansFrom("aopNamespaceHandlerPointcutEventTests.xml");
|
||||
loadBeansFrom(POINTCUT_EVENTS_CONTEXT);
|
||||
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
||||
assertEquals("Incorrect number of events fired", 1, componentDefinitions.length);
|
||||
assertTrue("No holder with nested components", componentDefinitions[0] instanceof CompositeComponentDefinition);
|
||||
|
@ -79,7 +89,7 @@ public final class AopNamespaceHandlerEventTests {
|
|||
|
||||
@Test
|
||||
public void testAdvisorEventsWithPointcutRef() throws Exception {
|
||||
loadBeansFrom("aopNamespaceHandlerAdvisorWithPointcutRefEventTests.xml");
|
||||
loadBeansFrom(POINTCUT_REF_CONTEXT);
|
||||
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
||||
assertEquals("Incorrect number of events fired", 2, componentDefinitions.length);
|
||||
|
||||
|
@ -108,7 +118,7 @@ public final class AopNamespaceHandlerEventTests {
|
|||
|
||||
@Test
|
||||
public void testAdvisorEventsWithDirectPointcut() throws Exception {
|
||||
loadBeansFrom("aopNamespaceHandlerAdvisorWithDirectPointcutEventTests.xml");
|
||||
loadBeansFrom(DIRECT_POINTCUT_EVENTS_CONTEXT);
|
||||
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
||||
assertEquals("Incorrect number of events fired", 2, componentDefinitions.length);
|
||||
|
||||
|
@ -137,7 +147,7 @@ public final class AopNamespaceHandlerEventTests {
|
|||
|
||||
@Test
|
||||
public void testAspectEvent() throws Exception {
|
||||
loadBeansFrom("aopNamespaceHandlerAspectEventTests.xml");
|
||||
loadBeansFrom(CONTEXT);
|
||||
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
||||
assertEquals("Incorrect number of events fired", 5, componentDefinitions.length);
|
||||
|
||||
|
|
|
@ -29,12 +29,20 @@ import org.springframework.core.io.ClassPathResource;
|
|||
* @author Chris Beams
|
||||
*/
|
||||
public final class AopNamespaceHandlerPointcutErrorTests {
|
||||
|
||||
private static final Class<?> CLASS = AopNamespaceHandlerPointcutErrorTests.class;
|
||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||
|
||||
private static final ClassPathResource DUPLICATION_CONTEXT =
|
||||
new ClassPathResource(CLASSNAME + "-pointcutDuplication.xml", CLASS);
|
||||
|
||||
private static final ClassPathResource MISSING_CONTEXT =
|
||||
new ClassPathResource(CLASSNAME + "-pointcutMissing.xml", CLASS);
|
||||
|
||||
@Test
|
||||
public void testDuplicatePointcutConfig() {
|
||||
try {
|
||||
new XmlBeanFactory(new ClassPathResource(
|
||||
"org/springframework/aop/config/aopNamespaceHandlerPointcutDuplicationTests.xml"));
|
||||
new XmlBeanFactory(DUPLICATION_CONTEXT);
|
||||
fail("parsing should have caused a BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
|
@ -45,8 +53,7 @@ public final class AopNamespaceHandlerPointcutErrorTests {
|
|||
@Test
|
||||
public void testMissingPointcutConfig() {
|
||||
try {
|
||||
new XmlBeanFactory(new ClassPathResource(
|
||||
"org/springframework/aop/config/aopNamespaceHandlerPointcutMissingTests.xml"));
|
||||
new XmlBeanFactory(MISSING_CONTEXT);
|
||||
fail("parsing should have caused a BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
|
|
|
@ -27,14 +27,19 @@ import org.springframework.core.io.ClassPathResource;
|
|||
* Tests that the <aop:config/> element can be used as a top level element.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class TopLevelAopTagTests {
|
||||
|
||||
private static final Class<?> CLASS = TopLevelAopTagTests.class;
|
||||
private static final ClassPathResource CONTEXT =
|
||||
new ClassPathResource(CLASS.getSimpleName() + "-context.xml", CLASS);
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("topLevelAop.xml", getClass()));
|
||||
reader.loadBeanDefinitions(CONTEXT);
|
||||
|
||||
assertTrue(beanFactory.containsBeanDefinition("testPointcut"));
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* 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.
|
||||
|
@ -14,37 +14,43 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.parsing;
|
||||
package test.parsing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.beans.factory.parsing.AliasDefinition;
|
||||
import org.springframework.beans.factory.parsing.ComponentDefinition;
|
||||
import org.springframework.beans.factory.parsing.DefaultsDefinition;
|
||||
import org.springframework.beans.factory.parsing.ImportDefinition;
|
||||
import org.springframework.beans.factory.parsing.ReaderEventListener;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class CollectingReaderEventListener implements ReaderEventListener {
|
||||
|
||||
private final List defaults = new LinkedList();
|
||||
private final List<DefaultsDefinition> defaults = new LinkedList<DefaultsDefinition>();
|
||||
|
||||
private final Map componentDefinitions = CollectionFactory.createLinkedMapIfPossible(8);
|
||||
private final Map<String, Object> componentDefinitions = new LinkedHashMap<String, Object>(8);
|
||||
|
||||
private final Map aliasMap = CollectionFactory.createLinkedMapIfPossible(8);
|
||||
private final Map<String, Object> aliasMap = new LinkedHashMap<String, Object>(8);
|
||||
|
||||
private final List imports = new LinkedList();
|
||||
private final List<ImportDefinition> imports = new LinkedList<ImportDefinition>();
|
||||
|
||||
|
||||
public void defaultsRegistered(DefaultsDefinition defaultsDefinition) {
|
||||
this.defaults.add(defaultsDefinition);
|
||||
}
|
||||
|
||||
public List getDefaults() {
|
||||
public List<DefaultsDefinition> getDefaults() {
|
||||
return Collections.unmodifiableList(this.defaults);
|
||||
}
|
||||
|
||||
|
@ -57,10 +63,11 @@ public class CollectingReaderEventListener implements ReaderEventListener {
|
|||
}
|
||||
|
||||
public ComponentDefinition[] getComponentDefinitions() {
|
||||
Collection collection = this.componentDefinitions.values();
|
||||
return (ComponentDefinition[]) collection.toArray(new ComponentDefinition[collection.size()]);
|
||||
Collection<Object> collection = this.componentDefinitions.values();
|
||||
return collection.toArray(new ComponentDefinition[collection.size()]);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void aliasRegistered(AliasDefinition aliasDefinition) {
|
||||
List aliases = (List) this.aliasMap.get(aliasDefinition.getBeanName());
|
||||
if(aliases == null) {
|
||||
|
@ -70,8 +77,8 @@ public class CollectingReaderEventListener implements ReaderEventListener {
|
|||
aliases.add(aliasDefinition);
|
||||
}
|
||||
|
||||
public List getAliases(String beanName) {
|
||||
List aliases = (List) this.aliasMap.get(beanName);
|
||||
public List<?> getAliases(String beanName) {
|
||||
List<?> aliases = (List<?>) this.aliasMap.get(beanName);
|
||||
return aliases == null ? null : Collections.unmodifiableList(aliases);
|
||||
}
|
||||
|
||||
|
@ -79,7 +86,7 @@ public class CollectingReaderEventListener implements ReaderEventListener {
|
|||
this.imports.add(importDefinition);
|
||||
}
|
||||
|
||||
public List getImports() {
|
||||
public List<ImportDefinition> getImports() {
|
||||
return Collections.unmodifiableList(this.imports);
|
||||
}
|
||||
|
Loading…
Reference in New Issue