Search in sources :

Example 6 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-infrastructure by TAKETODAY.

the class MethodMatchersTests method testDynamicAndStaticMethodMatcherIntersection.

@Test
public void testDynamicAndStaticMethodMatcherIntersection() throws Exception {
    MethodMatcher mm1 = MethodMatcher.TRUE;
    MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches();
    MethodMatcher intersection = MethodMatchers.intersection(mm1, mm2);
    assertThat(intersection.isRuntime()).as("Intersection is a dynamic matcher").isTrue();
    assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class)).as("2Matched setAge method").isTrue();
    DefaultMethodInvocation defaultMethodInvocation = new DefaultMethodInvocation(null, new TestBean(), ITESTBEAN_SETAGE, TestBean.class, new Object[] { 5 }, null);
    assertThat(intersection.matches(defaultMethodInvocation)).as("3Matched setAge method").isTrue();
    // Knock out dynamic part
    intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch());
    assertThat(intersection.isRuntime()).as("Intersection is a dynamic matcher").isTrue();
    assertThat(intersection.matches(ITESTBEAN_SETAGE, TestBean.class)).as("2Matched setAge method").isTrue();
    assertThat(intersection.matches(defaultMethodInvocation)).as("3 - not Matched setAge method").isFalse();
}
Also used : DefaultMethodInvocation(cn.taketoday.aop.framework.DefaultMethodInvocation) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 7 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

the class MethodMatchersTests method testSingle.

@Test
public void testSingle() throws Exception {
    MethodMatcher defaultMm = MethodMatcher.TRUE;
    assertThat(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)).isTrue();
    assertThat(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)).isTrue();
    defaultMm = MethodMatchers.intersection(defaultMm, new StartsWithMatcher("get"));
    assertThat(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)).isTrue();
    assertThat(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)).isFalse();
}
Also used : MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 8 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

the class MethodMatchersTests method testStaticMethodMatcherUnion.

@Test
public void testStaticMethodMatcherUnion() throws Exception {
    MethodMatcher getterMatcher = new StartsWithMatcher("get");
    MethodMatcher setterMatcher = new StartsWithMatcher("set");
    MethodMatcher union = MethodMatchers.union(getterMatcher, setterMatcher);
    assertThat(union.isRuntime()).as("Union is a static matcher").isFalse();
    assertThat(union.matches(ITESTBEAN_SETAGE, TestBean.class)).as("Matched setAge method").isTrue();
    assertThat(union.matches(ITESTBEAN_GETAGE, TestBean.class)).as("Matched getAge method").isTrue();
    assertThat(union.matches(IOTHER_ABSQUATULATE, TestBean.class)).as("Didn't matched absquatulate method").isFalse();
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 9 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

the class CallCountingInterceptor method testMatchExplicit.

@Test
public void testMatchExplicit() {
    String expression = "execution(int cn.taketoday.beans.testfixture.beans.TestBean.getAge())";
    Pointcut pointcut = getPointcut(expression);
    ClassFilter classFilter = pointcut.getClassFilter();
    MethodMatcher methodMatcher = pointcut.getMethodMatcher();
    assertMatchesTestBeanClass(classFilter);
    // not currently testable in a reliable fashion
    // assertDoesNotMatchStringClass(classFilter);
    assertThat(methodMatcher.isRuntime()).as("Should not be a runtime match").isFalse();
    assertMatchesGetAge(methodMatcher);
    assertThat(methodMatcher.matches(setAge, TestBean.class)).as("Expression should match setAge() method").isFalse();
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ClassFilter(cn.taketoday.aop.ClassFilter) MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Example 10 with MethodMatcher

use of cn.taketoday.aop.MethodMatcher in project today-framework by TAKETODAY.

the class CallCountingInterceptor method testMatchWithArgs.

@Test
public void testMatchWithArgs() throws Exception {
    String expression = "execution(void cn.taketoday.beans.testfixture.beans.TestBean.setSomeNumber(Number)) && args(Double)";
    Pointcut pointcut = getPointcut(expression);
    ClassFilter classFilter = pointcut.getClassFilter();
    MethodMatcher methodMatcher = pointcut.getMethodMatcher();
    assertMatchesTestBeanClass(classFilter);
    // not currently testable in a reliable fashion
    // assertDoesNotMatchStringClass(classFilter);
    DefaultMethodInvocation invocation = new DefaultMethodInvocation(null, new TestBean(), setSomeNumber, TestBean.class, new Object[] { 12D }, null);
    DefaultMethodInvocation invocation1 = new DefaultMethodInvocation(null, new TestBean(), setSomeNumber, TestBean.class, new Object[] { 11 }, null);
    assertThat(methodMatcher.matches(invocation)).as("Should match with setSomeNumber with Double input").isTrue();
    assertThat(methodMatcher.matches(invocation1)).as("Should not match setSomeNumber with Integer input").isFalse();
    assertThat(methodMatcher.matches(getAge, TestBean.class)).as("Should not match getAge").isFalse();
    assertThat(methodMatcher.isRuntime()).as("Should be a runtime match").isTrue();
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) DefaultMethodInvocation(cn.taketoday.aop.framework.DefaultMethodInvocation) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ClassFilter(cn.taketoday.aop.ClassFilter) MethodMatcher(cn.taketoday.aop.MethodMatcher) Test(org.junit.jupiter.api.Test)

Aggregations

MethodMatcher (cn.taketoday.aop.MethodMatcher)18 Test (org.junit.jupiter.api.Test)16 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)10 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)10 ClassFilter (cn.taketoday.aop.ClassFilter)6 Pointcut (cn.taketoday.aop.Pointcut)6 DefaultMethodInvocation (cn.taketoday.aop.framework.DefaultMethodInvocation)4