Search in sources :

Example 81 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 82 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 83 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 84 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)

Example 85 with ITestBean

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

the class AbstractAopProxyTests method testUserAttributes.

@Test
public void testUserAttributes() throws Throwable {
    class MapAwareMethodInterceptor implements MethodInterceptor {

        private final Map<String, String> expectedValues;

        private final Map<String, String> valuesToAdd;

        public MapAwareMethodInterceptor(Map<String, String> expectedValues, Map<String, String> valuesToAdd) {
            this.expectedValues = expectedValues;
            this.valuesToAdd = valuesToAdd;
        }

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            ReflectiveMethodInvocation rmi = (ReflectiveMethodInvocation) invocation;
            for (Iterator<String> it = rmi.getUserAttributes().keySet().iterator(); it.hasNext(); ) {
                Object key = it.next();
                assertEquals(expectedValues.get(key), rmi.getUserAttributes().get(key));
            }
            rmi.getUserAttributes().putAll(valuesToAdd);
            return invocation.proceed();
        }
    }
    ;
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap<>(), new HashMap<String, String>());
    Map<String, String> firstValuesToAdd = new HashMap<>();
    firstValuesToAdd.put("test", "");
    MapAwareMethodInterceptor mami2 = new MapAwareMethodInterceptor(new HashMap<>(), firstValuesToAdd);
    MapAwareMethodInterceptor mami3 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
    MapAwareMethodInterceptor mami4 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
    Map<String, String> secondValuesToAdd = new HashMap<>();
    secondValuesToAdd.put("foo", "bar");
    secondValuesToAdd.put("cat", "dog");
    MapAwareMethodInterceptor mami5 = new MapAwareMethodInterceptor(firstValuesToAdd, secondValuesToAdd);
    Map<String, String> finalExpected = new HashMap<>(firstValuesToAdd);
    finalExpected.putAll(secondValuesToAdd);
    MapAwareMethodInterceptor mami6 = new MapAwareMethodInterceptor(finalExpected, secondValuesToAdd);
    pc.addAdvice(mami1);
    pc.addAdvice(mami2);
    pc.addAdvice(mami3);
    pc.addAdvice(mami4);
    pc.addAdvice(mami5);
    pc.addAdvice(mami6);
    // We don't care about the object
    pc.setTarget(new TestBean());
    AopProxy aop = createAopProxy(pc);
    ITestBean tb = (ITestBean) aop.getProxy();
    String newName = "foo";
    tb.setName(newName);
    assertEquals(newName, tb.getName());
}
Also used : HashMap(java.util.HashMap) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) Map(java.util.Map) HashMap(java.util.HashMap) 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