Search in sources :

Example 36 with NopInterceptor

use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testEquals.

@Test
public void testEquals() {
    IOther a = new AllInstancesAreEqual();
    IOther b = new AllInstancesAreEqual();
    NopInterceptor i1 = new NopInterceptor();
    NopInterceptor i2 = new NopInterceptor();
    ProxyFactory pfa = new ProxyFactory(a);
    pfa.addAdvice(i1);
    ProxyFactory pfb = new ProxyFactory(b);
    pfb.addAdvice(i2);
    IOther proxyA = (IOther) createProxy(pfa);
    IOther proxyB = (IOther) createProxy(pfb);
    assertEquals(pfa.getAdvisors().length, pfb.getAdvisors().length);
    assertEquals(a, b);
    assertEquals(i1, i2);
    assertEquals(proxyA, proxyB);
    assertEquals(proxyA.hashCode(), proxyB.hashCode());
    assertFalse(proxyA.equals(a));
    // Equality checks were handled by the proxy
    assertEquals(0, i1.getCount());
    // When we invoke A, it's NopInterceptor will have count == 1
    // and won't think it's equal to B's NopInterceptor
    proxyA.absquatulate();
    assertEquals(1, i1.getCount());
    assertFalse(proxyA.equals(proxyB));
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) IOther(org.springframework.tests.sample.beans.IOther) Test(org.junit.Test)

Example 37 with NopInterceptor

use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testAddThrowsAdviceWithoutAdvisor.

@Test
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
    // Reacts to ServletException and RemoteException
    MyThrowsHandler th = new MyThrowsHandler();
    Echo target = new Echo();
    target.setA(16);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new NopInterceptor());
    pf.addAdvice(th);
    IEcho proxied = (IEcho) createProxy(pf);
    assertEquals(0, th.getCalls());
    assertEquals(target.getA(), proxied.getA());
    assertEquals(0, th.getCalls());
    Exception ex = new Exception();
    // Will be advised but doesn't match
    try {
        proxied.echoException(1, ex);
        fail();
    } catch (Exception caught) {
        assertEquals(ex, caught);
    }
    // Subclass of RemoteException
    ex = new MarshalException("");
    try {
        proxied.echoException(1, ex);
        fail();
    } catch (MarshalException caught) {
        assertEquals(ex, caught);
    }
    assertEquals(1, th.getCalls("remoteException"));
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) MarshalException(java.rmi.MarshalException) MyThrowsHandler(org.springframework.tests.aop.advice.MyThrowsHandler) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Example 38 with NopInterceptor

use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testProxyConfigString.

/**
	 * Check that the string is informative.
	 */
@Test
public void testProxyConfigString() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    MethodBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
    pc.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) createProxy(pc);
    String proxyConfigString = ((Advised) proxied).toProxyConfigString();
    assertTrue(proxyConfigString.contains(advisor.toString()));
    assertTrue(proxyConfigString.contains("1 interface"));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) Test(org.junit.Test)

Example 39 with NopInterceptor

use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testCannotRemoveAdvisorWhenFrozen.

@Test
public void testCannotRemoveAdvisorWhenFrozen() throws Throwable {
    TestBean target = new TestBean();
    target.setAge(21);
    ProxyFactory pc = new ProxyFactory(target);
    assertFalse(pc.isFrozen());
    pc.addAdvice(new NopInterceptor());
    ITestBean proxied = (ITestBean) createProxy(pc);
    pc.setFrozen(true);
    Advised advised = (Advised) proxied;
    assertTrue(pc.isFrozen());
    try {
        advised.removeAdvisor(0);
        fail("Shouldn't be able to remove Advisor when frozen");
    } catch (AopConfigException ex) {
        assertTrue(ex.getMessage().contains("frozen"));
    }
    // Didn't get removed
    assertEquals(1, advised.getAdvisors().length);
    pc.setFrozen(false);
    // Can now remove it
    advised.removeAdvisor(0);
    // Check it still works: proxy factory state shouldn't have been corrupted
    assertEquals(target.getAge(), proxied.getAge());
    assertEquals(0, advised.getAdvisors().length);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 40 with NopInterceptor

use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testUseAsHashKey.

@Test
public void testUseAsHashKey() {
    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    pf1.addAdvice(new NopInterceptor());
    ITestBean proxy1 = (ITestBean) createProxy(pf1);
    TestBean target2 = new TestBean();
    ProxyFactory pf2 = new ProxyFactory(target2);
    pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
    ITestBean proxy2 = (ITestBean) createProxy(pf2);
    HashMap<ITestBean, Object> h = new HashMap<>();
    Object value1 = "foo";
    Object value2 = "bar";
    assertNull(h.get(proxy1));
    h.put(proxy1, value1);
    h.put(proxy2, value2);
    assertEquals(h.get(proxy1), value1);
    assertEquals(h.get(proxy2), value2);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.tests.aop.interceptor.TimestampIntroductionInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) HashMap(java.util.HashMap) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Test(org.junit.Test)

Aggregations

NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)61 Test (org.junit.Test)57 ITestBean (org.springframework.tests.sample.beans.ITestBean)40 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)34 TestBean (org.springframework.tests.sample.beans.TestBean)34 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.tests.aop.advice.CountingBeforeAdvice)13 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)11 Advisor (org.springframework.aop.Advisor)10 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 Method (java.lang.reflect.Method)6 LockMixinAdvisor (test.mixin.LockMixinAdvisor)6 LockedException (test.mixin.LockedException)5 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 CountingAfterReturningAdvice (org.springframework.tests.aop.advice.CountingAfterReturningAdvice)4 FileNotFoundException (java.io.FileNotFoundException)3