|
|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2002-2019 the original author or authors.
|
|
|
|
|
* Copyright 2002-2022 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.
|
|
|
|
|
@@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.aop.config;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
@@ -40,7 +41,7 @@ import static org.springframework.core.testfixture.io.ResourceTestUtils.qualifie
|
|
|
|
|
* @author Juergen Hoeller
|
|
|
|
|
* @author Chris Beams
|
|
|
|
|
*/
|
|
|
|
|
public class AopNamespaceHandlerEventTests {
|
|
|
|
|
class AopNamespaceHandlerEventTests {
|
|
|
|
|
|
|
|
|
|
private static final Class<?> CLASS = AopNamespaceHandlerEventTests.class;
|
|
|
|
|
|
|
|
|
|
@@ -57,25 +58,24 @@ public class AopNamespaceHandlerEventTests {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
public void setup() {
|
|
|
|
|
void setup() {
|
|
|
|
|
this.reader = new XmlBeanDefinitionReader(this.beanFactory);
|
|
|
|
|
this.reader.setEventListener(this.eventListener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPointcutEvents() {
|
|
|
|
|
void pointcutEvents() {
|
|
|
|
|
this.reader.loadBeanDefinitions(POINTCUT_EVENTS_CONTEXT);
|
|
|
|
|
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
|
|
|
|
assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(1);
|
|
|
|
|
boolean condition = componentDefinitions[0] instanceof CompositeComponentDefinition;
|
|
|
|
|
assertThat(condition).as("No holder with nested components").isTrue();
|
|
|
|
|
assertThat(componentDefinitions).as("Incorrect number of events fired").hasSize(1);
|
|
|
|
|
assertThat(componentDefinitions[0]).as("No holder with nested components").isInstanceOf(CompositeComponentDefinition.class);
|
|
|
|
|
|
|
|
|
|
CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
|
|
|
|
|
assertThat(compositeDef.getName()).isEqualTo("aop:config");
|
|
|
|
|
|
|
|
|
|
ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
|
|
|
|
|
assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(2);
|
|
|
|
|
assertThat(nestedComponentDefs).as("Incorrect number of inner components").hasSize(2);
|
|
|
|
|
PointcutComponentDefinition pcd = null;
|
|
|
|
|
for (ComponentDefinition componentDefinition : nestedComponentDefs) {
|
|
|
|
|
if (componentDefinition instanceof PointcutComponentDefinition) {
|
|
|
|
|
@@ -84,84 +84,77 @@ public class AopNamespaceHandlerEventTests {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assertThat(pcd).as("PointcutComponentDefinition not found").isNotNull();
|
|
|
|
|
assertThat(pcd.getBeanDefinitions().length).as("Incorrect number of BeanDefinitions").isEqualTo(1);
|
|
|
|
|
assertThat(pcd.getBeanDefinitions()).as("Incorrect number of BeanDefinitions").hasSize(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testAdvisorEventsWithPointcutRef() {
|
|
|
|
|
void advisorEventsWithPointcutRef() {
|
|
|
|
|
this.reader.loadBeanDefinitions(POINTCUT_REF_CONTEXT);
|
|
|
|
|
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
|
|
|
|
assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(2);
|
|
|
|
|
assertThat(componentDefinitions).as("Incorrect number of events fired").hasSize(2);
|
|
|
|
|
|
|
|
|
|
boolean condition1 = componentDefinitions[0] instanceof CompositeComponentDefinition;
|
|
|
|
|
assertThat(condition1).as("No holder with nested components").isTrue();
|
|
|
|
|
assertThat(componentDefinitions[0]).as("No holder with nested components").isInstanceOf(CompositeComponentDefinition.class);
|
|
|
|
|
CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
|
|
|
|
|
assertThat(compositeDef.getName()).isEqualTo("aop:config");
|
|
|
|
|
|
|
|
|
|
ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
|
|
|
|
|
assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(3);
|
|
|
|
|
assertThat(nestedComponentDefs).as("Incorrect number of inner components").hasSize(3);
|
|
|
|
|
AdvisorComponentDefinition acd = null;
|
|
|
|
|
for (int i = 0; i < nestedComponentDefs.length; i++) {
|
|
|
|
|
ComponentDefinition componentDefinition = nestedComponentDefs[i];
|
|
|
|
|
for (ComponentDefinition componentDefinition : nestedComponentDefs) {
|
|
|
|
|
if (componentDefinition instanceof AdvisorComponentDefinition) {
|
|
|
|
|
acd = (AdvisorComponentDefinition) componentDefinition;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assertThat(acd).as("AdvisorComponentDefinition not found").isNotNull();
|
|
|
|
|
assertThat(acd.getBeanDefinitions().length).isEqualTo(1);
|
|
|
|
|
assertThat(acd.getBeanReferences().length).isEqualTo(2);
|
|
|
|
|
assertThat(acd.getBeanDefinitions()).hasSize(1);
|
|
|
|
|
assertThat(acd.getBeanReferences()).hasSize(2);
|
|
|
|
|
|
|
|
|
|
boolean condition = componentDefinitions[1] instanceof BeanComponentDefinition;
|
|
|
|
|
assertThat(condition).as("No advice bean found").isTrue();
|
|
|
|
|
assertThat(componentDefinitions[1]).as("No advice bean found").isInstanceOf(BeanComponentDefinition.class);
|
|
|
|
|
BeanComponentDefinition adviceDef = (BeanComponentDefinition) componentDefinitions[1];
|
|
|
|
|
assertThat(adviceDef.getBeanName()).isEqualTo("countingAdvice");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testAdvisorEventsWithDirectPointcut() {
|
|
|
|
|
void advisorEventsWithDirectPointcut() {
|
|
|
|
|
this.reader.loadBeanDefinitions(DIRECT_POINTCUT_EVENTS_CONTEXT);
|
|
|
|
|
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
|
|
|
|
assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(2);
|
|
|
|
|
assertThat(componentDefinitions).as("Incorrect number of events fired").hasSize(2);
|
|
|
|
|
|
|
|
|
|
boolean condition1 = componentDefinitions[0] instanceof CompositeComponentDefinition;
|
|
|
|
|
assertThat(condition1).as("No holder with nested components").isTrue();
|
|
|
|
|
assertThat(componentDefinitions[0]).as("No holder with nested components").isInstanceOf(CompositeComponentDefinition.class);
|
|
|
|
|
CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
|
|
|
|
|
assertThat(compositeDef.getName()).isEqualTo("aop:config");
|
|
|
|
|
|
|
|
|
|
ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
|
|
|
|
|
assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(2);
|
|
|
|
|
assertThat(nestedComponentDefs).as("Incorrect number of inner components").hasSize(2);
|
|
|
|
|
AdvisorComponentDefinition acd = null;
|
|
|
|
|
for (int i = 0; i < nestedComponentDefs.length; i++) {
|
|
|
|
|
ComponentDefinition componentDefinition = nestedComponentDefs[i];
|
|
|
|
|
for (ComponentDefinition componentDefinition : nestedComponentDefs) {
|
|
|
|
|
if (componentDefinition instanceof AdvisorComponentDefinition) {
|
|
|
|
|
acd = (AdvisorComponentDefinition) componentDefinition;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assertThat(acd).as("AdvisorComponentDefinition not found").isNotNull();
|
|
|
|
|
assertThat(acd.getBeanDefinitions().length).isEqualTo(2);
|
|
|
|
|
assertThat(acd.getBeanReferences().length).isEqualTo(1);
|
|
|
|
|
assertThat(acd.getBeanDefinitions()).hasSize(2);
|
|
|
|
|
assertThat(acd.getBeanReferences()).hasSize(1);
|
|
|
|
|
|
|
|
|
|
boolean condition = componentDefinitions[1] instanceof BeanComponentDefinition;
|
|
|
|
|
assertThat(condition).as("No advice bean found").isTrue();
|
|
|
|
|
assertThat(componentDefinitions[1]).as("No advice bean found").isInstanceOf(BeanComponentDefinition.class);
|
|
|
|
|
BeanComponentDefinition adviceDef = (BeanComponentDefinition) componentDefinitions[1];
|
|
|
|
|
assertThat(adviceDef.getBeanName()).isEqualTo("countingAdvice");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testAspectEvent() {
|
|
|
|
|
void aspectEvent() {
|
|
|
|
|
this.reader.loadBeanDefinitions(CONTEXT);
|
|
|
|
|
ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
|
|
|
|
|
assertThat(componentDefinitions.length).as("Incorrect number of events fired").isEqualTo(5);
|
|
|
|
|
assertThat(componentDefinitions).as("Incorrect number of events fired").hasSize(2);
|
|
|
|
|
|
|
|
|
|
boolean condition = componentDefinitions[0] instanceof CompositeComponentDefinition;
|
|
|
|
|
assertThat(condition).as("No holder with nested components").isTrue();
|
|
|
|
|
assertThat(componentDefinitions[0]).as("No holder with nested components").isInstanceOf(CompositeComponentDefinition.class);
|
|
|
|
|
CompositeComponentDefinition compositeDef = (CompositeComponentDefinition) componentDefinitions[0];
|
|
|
|
|
assertThat(compositeDef.getName()).isEqualTo("aop:config");
|
|
|
|
|
|
|
|
|
|
ComponentDefinition[] nestedComponentDefs = compositeDef.getNestedComponents();
|
|
|
|
|
assertThat(nestedComponentDefs.length).as("Incorrect number of inner components").isEqualTo(2);
|
|
|
|
|
assertThat(nestedComponentDefs).as("Incorrect number of inner components").hasSize(2);
|
|
|
|
|
AspectComponentDefinition acd = null;
|
|
|
|
|
for (ComponentDefinition componentDefinition : nestedComponentDefs) {
|
|
|
|
|
if (componentDefinition instanceof AspectComponentDefinition) {
|
|
|
|
|
@@ -172,9 +165,9 @@ public class AopNamespaceHandlerEventTests {
|
|
|
|
|
|
|
|
|
|
assertThat(acd).as("AspectComponentDefinition not found").isNotNull();
|
|
|
|
|
BeanDefinition[] beanDefinitions = acd.getBeanDefinitions();
|
|
|
|
|
assertThat(beanDefinitions.length).isEqualTo(5);
|
|
|
|
|
assertThat(beanDefinitions).hasSize(5);
|
|
|
|
|
BeanReference[] beanReferences = acd.getBeanReferences();
|
|
|
|
|
assertThat(beanReferences.length).isEqualTo(6);
|
|
|
|
|
assertThat(beanReferences).hasSize(6);
|
|
|
|
|
|
|
|
|
|
Set<String> expectedReferences = new HashSet<>();
|
|
|
|
|
expectedReferences.add("pc");
|
|
|
|
|
@@ -182,19 +175,16 @@ public class AopNamespaceHandlerEventTests {
|
|
|
|
|
for (BeanReference beanReference : beanReferences) {
|
|
|
|
|
expectedReferences.remove(beanReference.getBeanName());
|
|
|
|
|
}
|
|
|
|
|
assertThat(expectedReferences.size()).as("Incorrect references found").isEqualTo(0);
|
|
|
|
|
assertThat(expectedReferences).as("Incorrect references found").isEmpty();
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < componentDefinitions.length; i++) {
|
|
|
|
|
boolean condition1 = componentDefinitions[i] instanceof BeanComponentDefinition;
|
|
|
|
|
assertThat(condition1).isTrue();
|
|
|
|
|
}
|
|
|
|
|
Arrays.stream(componentDefinitions).skip(1).forEach(definition ->
|
|
|
|
|
assertThat(definition).isInstanceOf(BeanComponentDefinition.class));
|
|
|
|
|
|
|
|
|
|
ComponentDefinition[] nestedComponentDefs2 = acd.getNestedComponents();
|
|
|
|
|
assertThat(nestedComponentDefs2.length).as("Inner PointcutComponentDefinition not found").isEqualTo(1);
|
|
|
|
|
boolean condition1 = nestedComponentDefs2[0] instanceof PointcutComponentDefinition;
|
|
|
|
|
assertThat(condition1).isTrue();
|
|
|
|
|
assertThat(nestedComponentDefs2).as("Inner PointcutComponentDefinition not found").hasSize(1);
|
|
|
|
|
assertThat(nestedComponentDefs2[0]).isInstanceOf(PointcutComponentDefinition.class);
|
|
|
|
|
PointcutComponentDefinition pcd = (PointcutComponentDefinition) nestedComponentDefs2[0];
|
|
|
|
|
assertThat(pcd.getBeanDefinitions().length).as("Incorrect number of BeanDefinitions").isEqualTo(1);
|
|
|
|
|
assertThat(pcd.getBeanDefinitions()).as("Incorrect number of BeanDefinitions").hasSize(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|