Search in sources :

Example 1 with MethodInvokingFactoryBean

use of cn.taketoday.beans.factory.config.MethodInvokingFactoryBean in project today-infrastructure by TAKETODAY.

the class MixinProxyTargetClassTrueConfig method testAspectsAreApplied.

@Test
public void testAspectsAreApplied() {
    ClassPathXmlApplicationContext bf = newContext("aspects.xml");
    ITestBean tb = (ITestBean) bf.getBean("adrian");
    assertThat(tb.getAge()).isEqualTo(68);
    MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean");
    assertThat(AopUtils.isAopProxy(factoryBean.getTargetObject())).isTrue();
    assertThat(((ITestBean) factoryBean.getTargetObject()).getAge()).isEqualTo(68);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInvokingFactoryBean(cn.taketoday.beans.factory.config.MethodInvokingFactoryBean) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with MethodInvokingFactoryBean

use of cn.taketoday.beans.factory.config.MethodInvokingFactoryBean in project today-infrastructure by TAKETODAY.

the class MethodInvokingFactoryBeanTests method testGetObject.

@Test
public void testGetObject() throws Exception {
    // singleton, non-static
    TestClass1 tc1 = new TestClass1();
    MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetObject(tc1);
    mcfb.setTargetMethod("method1");
    mcfb.afterPropertiesSet();
    Integer i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(1);
    i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(1);
    // non-singleton, non-static
    tc1 = new TestClass1();
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetObject(tc1);
    mcfb.setTargetMethod("method1");
    mcfb.setSingleton(false);
    mcfb.afterPropertiesSet();
    i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(1);
    i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(2);
    // singleton, static
    TestClass1._staticField1 = 0;
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("staticMethod1");
    mcfb.afterPropertiesSet();
    i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(1);
    i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(1);
    // non-singleton, static
    TestClass1._staticField1 = 0;
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setStaticMethod("cn.taketoday.beans.factory.support.MethodInvokingFactoryBeanTests$TestClass1.staticMethod1");
    mcfb.setSingleton(false);
    mcfb.afterPropertiesSet();
    i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(1);
    i = (Integer) mcfb.getObject();
    assertThat(i.intValue()).isEqualTo(2);
    // void return value
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("voidRetvalMethod");
    mcfb.afterPropertiesSet();
    assertThat(mcfb.getObject()).isNull();
    // now see if we can match methods with arguments that have supertype arguments
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("supertypes");
    mcfb.setArguments(new ArrayList<>(), new ArrayList<>(), "hello");
    // should pass
    mcfb.afterPropertiesSet();
}
Also used : MethodInvokingFactoryBean(cn.taketoday.beans.factory.config.MethodInvokingFactoryBean) Test(org.junit.jupiter.api.Test)

Example 3 with MethodInvokingFactoryBean

use of cn.taketoday.beans.factory.config.MethodInvokingFactoryBean in project today-infrastructure by TAKETODAY.

the class MethodInvokingFactoryBeanTests method testParameterValidation.

@Test
public void testParameterValidation() throws Exception {
    // assert that only static OR non static are set, but not both or none
    MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
    assertThatIllegalArgumentException().isThrownBy(mcfb::afterPropertiesSet);
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetObject(this);
    mcfb.setTargetMethod("whatever");
    assertThatExceptionOfType(NoSuchMethodException.class).isThrownBy(mcfb::afterPropertiesSet);
    // bogus static method
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("some.bogus.Method.name");
    assertThatExceptionOfType(NoSuchMethodException.class).isThrownBy(mcfb::afterPropertiesSet);
    // bogus static method
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("method1");
    assertThatIllegalArgumentException().isThrownBy(mcfb::afterPropertiesSet);
    // missing method
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetObject(this);
    assertThatIllegalArgumentException().isThrownBy(mcfb::afterPropertiesSet);
    // bogus method
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetObject(this);
    mcfb.setTargetMethod("bogus");
    assertThatExceptionOfType(NoSuchMethodException.class).isThrownBy(mcfb::afterPropertiesSet);
    // static method
    TestClass1._staticField1 = 0;
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("staticMethod1");
    mcfb.afterPropertiesSet();
    // non-static method
    TestClass1 tc1 = new TestClass1();
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetObject(tc1);
    mcfb.setTargetMethod("method1");
    mcfb.afterPropertiesSet();
}
Also used : MethodInvokingFactoryBean(cn.taketoday.beans.factory.config.MethodInvokingFactoryBean) Test(org.junit.jupiter.api.Test)

Example 4 with MethodInvokingFactoryBean

use of cn.taketoday.beans.factory.config.MethodInvokingFactoryBean in project today-framework by TAKETODAY.

the class MixinProxyTargetClassTrueConfig method testAspectsAreApplied.

@Test
public void testAspectsAreApplied() {
    ClassPathXmlApplicationContext bf = newContext("aspects.xml");
    ITestBean tb = (ITestBean) bf.getBean("adrian");
    assertThat(tb.getAge()).isEqualTo(68);
    MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean");
    assertThat(AopUtils.isAopProxy(factoryBean.getTargetObject())).isTrue();
    assertThat(((ITestBean) factoryBean.getTargetObject()).getAge()).isEqualTo(68);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInvokingFactoryBean(cn.taketoday.beans.factory.config.MethodInvokingFactoryBean) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with MethodInvokingFactoryBean

use of cn.taketoday.beans.factory.config.MethodInvokingFactoryBean in project today-framework by TAKETODAY.

the class MethodInvokingFactoryBeanTests method testArgumentConversion.

@Test
public void testArgumentConversion() throws Exception {
    MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("supertypes");
    mcfb.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", "bogus");
    assertThatExceptionOfType(NoSuchMethodException.class).as("Matched method with wrong number of args").isThrownBy(mcfb::afterPropertiesSet);
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("supertypes");
    mcfb.setArguments(1, new Object());
    assertThatExceptionOfType(NoSuchMethodException.class).as("Should have failed on getObject with mismatched argument types").isThrownBy(mcfb::afterPropertiesSet);
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("supertypes2");
    mcfb.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", "bogus");
    mcfb.afterPropertiesSet();
    assertThat(mcfb.getObject()).isEqualTo("hello");
    mcfb = new MethodInvokingFactoryBean();
    mcfb.setTargetClass(TestClass1.class);
    mcfb.setTargetMethod("supertypes2");
    mcfb.setArguments(new ArrayList<>(), new ArrayList<>(), new Object());
    assertThatExceptionOfType(NoSuchMethodException.class).as("Matched method when shouldn't have matched").isThrownBy(mcfb::afterPropertiesSet);
}
Also used : MethodInvokingFactoryBean(cn.taketoday.beans.factory.config.MethodInvokingFactoryBean) Test(org.junit.jupiter.api.Test)

Aggregations

MethodInvokingFactoryBean (cn.taketoday.beans.factory.config.MethodInvokingFactoryBean)10 Test (org.junit.jupiter.api.Test)10 StringTrimmerEditor (cn.taketoday.beans.propertyeditors.StringTrimmerEditor)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2