Search in sources :

Example 76 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class PerThisAspect method testIntroductionWithArgumentBinding.

/* prereq AspectJ 1.6.7
	@Test
	public void testIntroductionBasedOnAnnotationMatch_Spr5307() {
		AnnotatedTarget target = new AnnotatedTargetImpl();

		List<Advisor> advisors = getFixture().getAdvisors(
				new SingletonMetadataAwareAspectInstanceFactory(new MakeAnnotatedTypeModifiable(),"someBean"));
		Object proxy = createProxy(target,
				advisors,
				AnnotatedTarget.class);
		System.out.println(advisors.get(1));
		assertTrue(proxy instanceof Lockable);
		Lockable lockable = (Lockable)proxy;
		lockable.locked();
	}
	*/
// TODO: Why does this test fail? It hasn't been run before, so it maybe never actually passed...
@Test
@Ignore
public void testIntroductionWithArgumentBinding() {
    TestBean target = new TestBean();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeITestBeanModifiable(), "someBean"));
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")));
    Modifiable modifiable = (Modifiable) createProxy(target, advisors, ITestBean.class);
    assertThat(modifiable, instanceOf(Modifiable.class));
    Lockable lockable = (Lockable) modifiable;
    assertFalse(lockable.locked());
    ITestBean itb = (ITestBean) modifiable;
    assertFalse(modifiable.isModified());
    int oldAge = itb.getAge();
    itb.setAge(oldAge + 1);
    assertTrue(modifiable.isModified());
    modifiable.acceptChanges();
    assertFalse(modifiable.isModified());
    itb.setAge(itb.getAge());
    assertFalse("Setting same value does not modify", modifiable.isModified());
    itb.setName("And now for something completely different");
    assertTrue(modifiable.isModified());
    lockable.lock();
    assertTrue(lockable.locked());
    try {
        itb.setName("Else");
        fail("Should be locked");
    } catch (IllegalStateException ex) {
    // Ok
    }
    lockable.unlock();
    itb.setName("Tony");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) SyntheticInstantiationAdvisor(org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) Advisor(org.springframework.aop.Advisor) Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 77 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetMethodSignatureFromJoinPoint.

@Test
public void testCanGetMethodSignatureFromJoinPoint() {
    final Object raw = new TestBean();
    // Will be set by advice during a method call
    final int newAge = 23;
    ProxyFactory pf = new ProxyFactory(raw);
    pf.setExposeProxy(true);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice(new MethodBeforeAdvice() {

        private int depth;

        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable {
            JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
            assertTrue("Method named in toString", jp.toString().contains(method.getName()));
            // Ensure that these don't cause problems
            jp.toShortString();
            jp.toLongString();
            assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
            assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));
            ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
            assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));
            assertNotSame(target, thisProxy);
            // Check getting again doesn't cause a problem
            assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());
            // Be sure to increment depth to avoid infinite recursion
            if (depth++ == 0) {
                // Check that toString doesn't cause a problem
                thisProxy.toString();
                // Change age, so this will be returned by invocation
                thisProxy.setAge(newAge);
                assertEquals(newAge, thisProxy.getAge());
            }
            assertSame(AopContext.currentProxy(), thisProxy);
            assertSame(target, raw);
            assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
            assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());
            MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
            assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature());
            assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint());
            assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
            assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
            assertEquals(method.getReturnType(), msig.getReturnType());
            assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
            msig.toLongString();
            msig.toShortString();
        }
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    assertEquals("Advice reentrantly set age", newAge, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) Method(java.lang.reflect.Method) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.Test)

Example 78 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetStaticPartFromJoinPoint.

@Test
public void testCanGetStaticPartFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice(new MethodBeforeAdvice() {

        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable {
            StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
            assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart());
            assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind());
            assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature());
            assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation());
        }
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) Method(java.lang.reflect.Method) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) Test(org.junit.Test)

Example 79 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetSourceLocationFromJoinPoint.

@Test
public void testCanGetSourceLocationFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice(new MethodBeforeAdvice() {

        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable {
            SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
            assertEquals("Same source location must be returned on subsequent requests", sloc, AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
            assertEquals(TestBean.class, sloc.getWithinType());
            try {
                sloc.getLine();
                fail("Can't get line number");
            } catch (UnsupportedOperationException ex) {
            // Expected
            }
            try {
                sloc.getFileName();
                fail("Can't get file name");
            } catch (UnsupportedOperationException ex) {
            // Expected
            }
        }
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : SourceLocation(org.aspectj.lang.reflect.SourceLocation) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 80 with ITestBean

use of org.springframework.tests.sample.beans.ITestBean in project spring-framework by spring-projects.

the class IntroductionBenchmarkTests method timeManyInvocations.

@Test
public void timeManyInvocations() {
    StopWatch sw = new StopWatch();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.setProxyTargetClass(false);
    pf.addAdvice(new SimpleCounterIntroduction());
    ITestBean proxy = (ITestBean) pf.getProxy();
    Counter counter = (Counter) proxy;
    sw.start(INVOCATIONS + " invocations on proxy, not hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        proxy.getAge();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on proxy, hitting introduction");
    for (int i = 0; i < INVOCATIONS; i++) {
        counter.getCount();
    }
    sw.stop();
    sw.start(INVOCATIONS + " invocations on target");
    for (int i = 0; i < INVOCATIONS; i++) {
        target.getAge();
    }
    sw.stop();
    System.out.println(sw.prettyPrint());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Aggregations

ITestBean (org.springframework.tests.sample.beans.ITestBean)221 Test (org.junit.Test)205 TestBean (org.springframework.tests.sample.beans.TestBean)127 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)37 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)24 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)21 IOException (java.io.IOException)15 Advisor (org.springframework.aop.Advisor)15 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)15 Method (java.lang.reflect.Method)14 ProxyFactory (org.springframework.aop.framework.ProxyFactory)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)14 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)13 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)13 MethodInvocation (org.aopalliance.intercept.MethodInvocation)12 Advised (org.springframework.aop.framework.Advised)12 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)11 LockedException (test.mixin.LockedException)11 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)10