⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aopnamespacehandlereventtests.java

📁 struts+spring 源码 希望能给大家带来帮助
💻 JAVA
字号:
/*
 * Copyright 2002-2006 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.aop.config;

import java.util.HashSet;
import java.util.Set;

import junit.framework.TestCase;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanReference;
import org.springframework.beans.factory.parsing.CollectingReaderEventListener;
import org.springframework.beans.factory.parsing.ComponentDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;

/**
 * @author Rob Harrop
 */
public class AopNamespaceHandlerEventTests extends TestCase {

	private CollectingReaderEventListener eventListener = new CollectingReaderEventListener();

	private XmlBeanDefinitionReader reader;

	private DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();


	protected void setUp() throws Exception {
		this.reader = new XmlBeanDefinitionReader(this.beanFactory);
		this.reader.setEventListener(this.eventListener);
	}

	public void testPointcutEvents() throws Exception {
		loadBeansFrom("aopNamespaceHandlerPointcutEventTests.xml");
		ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
		assertEquals("Incorrect number of events fired", 2, componentDefinitions.length);
		ComponentDefinition pcDefinition = this.eventListener.getComponentDefinition("myPointcut");
		assertTrue(pcDefinition instanceof PointcutComponentDefinition);

		PointcutComponentDefinition pointcutComponentDefinition = (PointcutComponentDefinition) pcDefinition;
		assertEquals("Incorrect number of BeanDefintions", 1, pointcutComponentDefinition.getBeanDefinitions().length);
	}

	public void testAdvisorEventsWithPointcutRef() throws Exception {
		loadBeansFrom("aopNamespaceHandlerAdvisorWithPointcutRefEventTests.xml");
		ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
		assertEquals("Incorrect number of events fired", 4, componentDefinitions.length);
		AdvisorComponentDefinition acd = null;
		for (int i = 0; i < componentDefinitions.length; i++) {
			ComponentDefinition componentDefinition = componentDefinitions[i];
			if (componentDefinition instanceof AdvisorComponentDefinition) {
				acd = (AdvisorComponentDefinition) componentDefinition;
				break;
			}
		}

		assertNotNull("AdvisorComponentDefinition not found", acd);
		assertEquals(1, acd.getBeanDefinitions().length);
		assertEquals(2, acd.getBeanReferences().length);
	}

	public void testAdvisorEventsWithDirectPointcut() throws Exception {
		loadBeansFrom("aopNamespaceHandlerAdvisorWithDirectPointcutEventTests.xml");
		ComponentDefinition[] componentDefinitions = this.eventListener.getComponentDefinitions();
		assertEquals("Incorrect number of events fired", 3, componentDefinitions.length);
		AdvisorComponentDefinition acd = null;
		for (int i = 0; i < componentDefinitions.length; i++) {
			ComponentDefinition componentDefinition = componentDefinitions[i];
			if (componentDefinition instanceof AdvisorComponentDefinition) {
				acd = (AdvisorComponentDefinition) componentDefinition;
				break;
			}
		}

		assertNotNull("AdvisorComponentDefinition not found", acd);
		assertEquals(2, acd.getBeanDefinitions().length);
		assertEquals(1, acd.getBeanReferences().length);
	}

	public void testAspectEvent() throws Exception {
		loadBeansFrom("aopNamespaceHandlerAspectEventTests.xml");
		ComponentDefinition componentDefinition = this.eventListener.getComponentDefinition("countAgeCalls");
		assertNotNull(componentDefinition);
		BeanDefinition[] beanDefinitions = componentDefinition.getBeanDefinitions();
		assertEquals(6, beanDefinitions.length);
		BeanReference[] beanReferences = componentDefinition.getBeanReferences();
		assertEquals(6, beanReferences.length);
		Set expectedReferences = new HashSet();
		expectedReferences.add("pc");
		expectedReferences.add("countingAdvice");
		for (int i = 0; i < beanReferences.length; i++) {
			BeanReference beanReference = beanReferences[i];
			expectedReferences.remove(beanReference.getBeanName());
		}
		assertEquals("Incorrect references found", 0, expectedReferences.size());
	}

	private void loadBeansFrom(String path) {
		this.reader.loadBeanDefinitions(new ClassPathResource(path, getClass()));
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -