alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (AspectAndAdvicePrecedenceTests.java)

This example Spring Framework source code file (AspectAndAdvicePrecedenceTests.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

abstractdependencyinjectionspringcontexttests, aspectandadviceprecedencetests, aspectandadviceprecedencetests, expected, expected, expecting, itestbean, precedencetestaspect, precedenceverifyingcollaborator, precedenceverifyingcollaborator, simplespringbeforeadvice, string, string, too

The Spring Framework AspectAndAdvicePrecedenceTests.java source code

/*
 * 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.aop.aspectj;

import org.springframework.beans.ITestBean;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

/**
 * @author Adrian Colyer
 */
public class AspectAndAdvicePrecedenceTests extends AbstractDependencyInjectionSpringContextTests {

	private PrecedenceTestAspect highPrecedenceAspect;
	private PrecedenceTestAspect lowPrecedenceAspect;
	private SimpleSpringBeforeAdvice lowPrecedenceSpringAdvice;
	private SimpleSpringBeforeAdvice highPrecedenceSpringAdvice;
	private ITestBean testBean;
	

	public AspectAndAdvicePrecedenceTests() {
		setAutowireMode(AUTOWIRE_BY_NAME);
	}

	public void setHighPrecedenceAspect(PrecedenceTestAspect highPrecedenceAspect) {
		this.highPrecedenceAspect = highPrecedenceAspect;
	}

	public void setLowPrecedenceAspect(PrecedenceTestAspect lowPrecedenceAspect) {
		this.lowPrecedenceAspect = lowPrecedenceAspect;
	}

	public void setLowPrecedenceSpringAdvice(SimpleSpringBeforeAdvice lowPrecedenceSpringAdvice) {
		this.lowPrecedenceSpringAdvice = lowPrecedenceSpringAdvice;
	}

	public void setHighPrecedenceSpringAdvice(SimpleSpringBeforeAdvice highPrecedenceSpringAdvice) {
		this.highPrecedenceSpringAdvice = highPrecedenceSpringAdvice;
	}
	
	public void setTestBean(ITestBean testBean) {
		this.testBean = testBean;
	}

	protected String getConfigPath() {
		return "advice-precedence-tests.xml";
	}


	// ========== end of test case set up, start of tests proper ===================

	public void testAdviceOrder() {
		PrecedenceTestAspect.Collaborator collaborator = new PrecedenceVerifyingCollaborator();
		this.highPrecedenceAspect.setCollaborator(collaborator);
		this.lowPrecedenceAspect.setCollaborator(collaborator);
		this.highPrecedenceSpringAdvice.setCollaborator(collaborator);
		this.lowPrecedenceSpringAdvice.setCollaborator(collaborator);
		this.testBean.getAge();
	}


	private static class PrecedenceVerifyingCollaborator implements PrecedenceTestAspect.Collaborator {

		private static final String[] EXPECTED = {
			// this order confirmed by running the same aspects (minus the Spring AOP advisors)
			// through AspectJ...
			"beforeAdviceOne(highPrecedenceAspect)",  	       // 1
			"beforeAdviceTwo(highPrecedenceAspect)",           // 2
			"aroundAdviceOne(highPrecedenceAspect)",           // 3, before proceed
			  "aroundAdviceTwo(highPrecedenceAspect)",         // 4, before proceed
			    "beforeAdviceOne(highPrecedenceSpringAdvice)", // 5
			    "beforeAdviceOne(lowPrecedenceSpringAdvice)",  // 6
			    "beforeAdviceOne(lowPrecedenceAspect)",        // 7
			    "beforeAdviceTwo(lowPrecedenceAspect)",        // 8
			    "aroundAdviceOne(lowPrecedenceAspect)",        // 9, before proceed
			      "aroundAdviceTwo(lowPrecedenceAspect)",      // 10, before proceed
			      "aroundAdviceTwo(lowPrecedenceAspect)",      // 11, after proceed
			    "aroundAdviceOne(lowPrecedenceAspect)",        // 12, after proceed
			    "afterAdviceOne(lowPrecedenceAspect)",         // 13
			    "afterAdviceTwo(lowPrecedenceAspect)",         // 14
			  "aroundAdviceTwo(highPrecedenceAspect)",         // 15, after proceed
			"aroundAdviceOne(highPrecedenceAspect)",           // 16, after proceed
			"afterAdviceOne(highPrecedenceAspect)",            // 17
			"afterAdviceTwo(highPrecedenceAspect)"             // 18
		};

		private int adviceInvocationNumber = 0;

		private void checkAdvice(String whatJustHappened) {
			//System.out.println("[" + adviceInvocationNumber + "] " + whatJustHappened + " ==> " + EXPECTED[adviceInvocationNumber]);
			if (adviceInvocationNumber > (EXPECTED.length - 1)) {
				fail("Too many advice invocations, expecting " + EXPECTED.length
						+ " but had " + adviceInvocationNumber);
			}
			String expecting = EXPECTED[adviceInvocationNumber++];
			if (!whatJustHappened.equals(expecting)) {
				fail("Expecting '" + expecting + "' on advice invocation " + adviceInvocationNumber +
						" but got '" + whatJustHappened + "'");
			}
		}

		public void beforeAdviceOne(String beanName) {
			checkAdvice("beforeAdviceOne(" + beanName + ")");
		}

		public void beforeAdviceTwo(String beanName) {
			checkAdvice("beforeAdviceTwo(" + beanName + ")");
		}

		public void aroundAdviceOne(String beanName) {
			checkAdvice("aroundAdviceOne(" + beanName + ")");
		}

		public void aroundAdviceTwo(String beanName) {
			checkAdvice("aroundAdviceTwo(" + beanName + ")");
		}

		public void afterAdviceOne(String beanName) {
			checkAdvice("afterAdviceOne(" + beanName + ")");
		}

		public void afterAdviceTwo(String beanName) {
			checkAdvice("afterAdviceTwo(" + beanName + ")");
		}
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework AspectAndAdvicePrecedenceTests.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.