View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2010 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.aspects;
18  
19  import javax.annotation.Resource;
20  
21  import static junit.framework.Assert.assertEquals;
22  
23  import org.aopalliance.aop.Advice;
24  import org.junit.runner.RunWith;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.aop.Advisor;
28  import org.springframework.aop.aspectj.AspectJAroundAdvice;
29  import org.springframework.aop.framework.Advised;
30  import org.springframework.test.context.ContextConfiguration;
31  
32  import ch.elca.el4j.tests.aspects.interceptor.ServiceLoggerInterceptor;
33  import ch.elca.el4j.tests.aspects.services.Service;
34  import ch.elca.el4j.tests.aspects.util.InvocationMonitor;
35  import ch.elca.el4j.tests.core.ModuleTestContextLoader;
36  import ch.elca.el4j.tests.core.context.junit4.EL4JJunit4ClassRunner;
37  
38  /**
39   * Abstract test case for aspects tests.
40   *
41   * @svnLink $Revision: 4255 $;$Date: 2010-12-21 11:51:55 +0100 (Di, 21. Dez 2010) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/tests/aspects/src/test/java/ch/elca/el4j/tests/aspects/AbstractInvokationTests.java $
42   *
43   * @author Martin Zeltner (MZE)
44   * @author Reynald Borer (RBR)
45   */
46  @RunWith(EL4JJunit4ClassRunner.class)
47  @ContextConfiguration(
48  	locations = {
49  		"classpath*:mandatory/*.xml",
50  		"classpath*:scenarios/db/raw/*.xml",
51  		"classpath*:scenarios/dataaccess/hibernate/*.xml" },
52  	loader = ModuleTestContextLoader.class)
53  public abstract class AbstractInvokationTests {
54  	/**
55  	 * Private logger.
56  	 */
57  	private static final Logger s_logger = LoggerFactory.getLogger(InvokationWithoutTrJ5ATest.class);
58  	
59  	/**
60  	 * Test service.
61  	 */
62  	@Resource
63  	protected Service myService;
64  	
65  	/**
66  	 * @param o Is the object to report aspect infos of it.
67  	 */
68  	protected void reportAspectInformation(Object o) {
69  		if (o instanceof Advised) {
70  			Advised myAdvisedService = (Advised) o;
71  			for (Advisor adv : myAdvisedService.getAdvisors()) {
72  				Advice advice = adv.getAdvice();
73  				if (advice instanceof AspectJAroundAdvice) {
74  					AspectJAroundAdvice around = (AspectJAroundAdvice) advice;
75  					s_logger.info("- around advice " + around.getAspectName() + ": " + adv.getAdvice());
76  				} else {
77  					s_logger.info("- " + adv.getAdvice());
78  				}
79  			}
80  		}
81  	}
82  	
83  	/**
84  	 * Tests the double invokation problem.
85  	 */
86  	protected void commonDoubleInvokationTest() {
87  		InvocationMonitor.initCounter(ServiceLoggerInterceptor.class);
88  		reportAspectInformation(myService);
89  		myService.oneMethod();
90  		assertEquals("Service logger interceptor called not exactly once!",
91  			1, InvocationMonitor.getCounter(ServiceLoggerInterceptor.class));
92  	}
93  }