Search in sources :

Example 11 with TestBean

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

the class MethodInvocationTests method testToStringDoesntHitTarget.

/**
	 * toString on target can cause failure.
	 */
@Test
public void testToStringDoesntHitTarget() throws Throwable {
    Object target = new TestBean() {

        @Override
        public String toString() {
            throw new UnsupportedOperationException("toString");
        }
    };
    List<Object> is = new LinkedList<>();
    Method m = Object.class.getMethod("hashCode");
    Object proxy = new Object();
    ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, m, null, null, is);
    // If it hits target, the test will fail with the UnsupportedOpException
    // in the inner class above.
    invocation.toString();
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 12 with TestBean

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

the class PerThisAspect method testMultiplePerTargetAspectsWithOrderAnnotation.

@Test
public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException {
    TestBean target = new TestBean();
    int realAge = 65;
    target.setAge(realAge);
    List<Advisor> advisors = new LinkedList<>();
    PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10();
    aspect1.count = 100;
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
    PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
    Collections.sort(advisors, new OrderComparator());
    TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
    assertEquals("Around advice must NOT apply", realAge, itb.getAge());
    // Hit the method in the per clause to instantiate the aspect
    itb.getSpouse();
    assertEquals("Around advice must apply", 0, itb.getAge());
    assertEquals("Around advice must apply", 1, itb.getAge());
}
Also used : 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) OrderComparator(org.springframework.core.OrderComparator) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 13 with TestBean

use of org.springframework.tests.sample.beans.TestBean 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 14 with TestBean

use of org.springframework.tests.sample.beans.TestBean 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 15 with TestBean

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

the class DefaultListableBeanFactoryTests method testBeanPostProcessorWithWrappedObjectAndDisposableBean.

@Test
public void testBeanPostProcessorWithWrappedObjectAndDisposableBean() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(BeanWithDisposableBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return new TestBean();
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            return bean;
        }
    });
    BeanWithDisposableBean.closed = false;
    lbf.preInstantiateSingletons();
    lbf.destroySingletons();
    assertTrue("Destroy method invoked", BeanWithDisposableBean.closed);
}
Also used : 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) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)788 Test (org.junit.Test)740 ITestBean (org.springframework.tests.sample.beans.ITestBean)456 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)248 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)225 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)164 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)160 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)144 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)86 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)44 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)40 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)40 Properties (java.util.Properties)35 Errors (org.springframework.validation.Errors)34 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)33 PropertyEditorSupport (java.beans.PropertyEditorSupport)31 HashMap (java.util.HashMap)30 List (java.util.List)27 Map (java.util.Map)27 PageContext (javax.servlet.jsp.PageContext)27