Search in sources :

Example 6 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)

Example 7 with ITestBean

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

the class PerThisAspect method testNamedPointcutFromAspectLibraryWithBinding.

@Test
public void testNamedPointcutFromAspectLibraryWithBinding() {
    TestBean target = new TestBean();
    ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new NamedPointcutAspectFromLibraryWithBinding(), "someBean")), ITestBean.class);
    itb.setAge(10);
    assertEquals("Around advice must apply", 20, itb.getAge());
    assertEquals(20, target.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 8 with ITestBean

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

the class NamedPointcutWithArgs method testBindingInPointcutUsedByAdvice.

@Test(expected = IllegalArgumentException.class)
public void testBindingInPointcutUsedByAdvice() {
    TestBean tb = new TestBean();
    AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb);
    proxyFactory.addAspect(NamedPointcutWithArgs.class);
    ITestBean proxiedTestBean = proxyFactory.getProxy();
    proxiedTestBean.setName("Supercalifragalisticexpialidocious");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 9 with ITestBean

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

the class DefaultListableBeanFactoryTests method testSimpleReference.

@Test
public void testSimpleReference() {
    String PREFIX = "beans.";
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
    p.setProperty(PREFIX + "rod.name", "Rod");
    p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
    p.setProperty(PREFIX + "kerry.name", "Kerry");
    p.setProperty(PREFIX + "kerry.age", "35");
    p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");
    int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
    assertTrue("2 beans registered, not " + count, count == 2);
    TestBean kerry = lbf.getBean("kerry", TestBean.class);
    assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
    ITestBean spouse = kerry.getSpouse();
    assertTrue("Kerry spouse is non null", spouse != null);
    assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 10 with ITestBean

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

the class AbstractAopProxyTests method testTestBeanIntroduction.

private void testTestBeanIntroduction(ProxyFactory pc) {
    int newAge = 65;
    ITestBean itb = (ITestBean) createProxy(pc);
    itb.setAge(newAge);
    assertEquals(newAge, itb.getAge());
    Lockable lockable = (Lockable) itb;
    assertFalse(lockable.locked());
    lockable.lock();
    assertEquals(newAge, itb.getAge());
    try {
        itb.setAge(1);
        fail("Setters should fail when locked");
    } catch (LockedException ex) {
    // ok
    }
    assertEquals(newAge, itb.getAge());
    // Unlock
    assertTrue(lockable.locked());
    lockable.unlock();
    itb.setAge(1);
    assertEquals(1, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) LockedException(test.mixin.LockedException) Lockable(test.mixin.Lockable)

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