Search in sources :

Example 91 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project jetcache by alibaba.

the class JetCacheInterceptorTest method test1.

@Test
public void test1() throws Throwable {
    final Method m = I1.class.getMethod("foo");
    final C1 c = new C1();
    pc.matches(m, C1.class);
    final MethodInvocation mi = mock(MethodInvocation.class);
    when(mi.getMethod()).thenReturn(m);
    when(mi.getThis()).thenReturn(c);
    when(mi.getArguments()).thenReturn(null);
    when(mi.proceed()).thenReturn(100);
    interceptor.invoke(mi);
    interceptor.invoke(mi);
    verify(mi, times(1)).proceed();
    globalCacheConfig.setEnableMethodCache(false);
    interceptor.invoke(mi);
    verify(mi, times(2)).proceed();
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 92 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project jetcache by alibaba.

the class JetCacheInterceptorTest method test2.

@Test
public void test2() throws Throwable {
    final Method m = I2.class.getMethod("foo");
    final C2 c = new C2();
    pc.matches(m, C2.class);
    final MethodInvocation mi = mock(MethodInvocation.class);
    when(mi.getMethod()).thenReturn(m);
    when(mi.getThis()).thenReturn(c);
    when(mi.getArguments()).thenReturn(null);
    when(mi.proceed()).thenReturn(100);
    interceptor.invoke(mi);
    interceptor.invoke(mi);
    CacheContext.enableCache(() -> {
        try {
            interceptor.invoke(mi);
            interceptor.invoke(mi);
            return null;
        } catch (Throwable e) {
            fail(e);
            return null;
        }
    });
    verify(mi, times(3)).proceed();
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 93 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class MethodExpressionVoterTests method matchingArgAgainstAuthenticationNameIsSuccessful.

@Test
public void matchingArgAgainstAuthenticationNameIsSuccessful() throws Exception {
    MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAString(), "joe");
    assertThat(this.am.vote(this.joe, mi, createAttributes(new PreInvocationExpressionAttribute(null, null, "(#argument == principal) and (principal == 'joe')")))).isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 94 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class MethodExpressionVoterTests method accessIsGrantedIfNoPreAuthorizeAttributeIsUsed.

@Test
public void accessIsGrantedIfNoPreAuthorizeAttributeIsUsed() throws Exception {
    Collection arg = createCollectionArg("joe", "bob", "sam");
    MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingACollection(), arg);
    assertThat(this.am.vote(this.joe, mi, createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'jim')", "collection", null)))).isEqualTo(AccessDecisionVoter.ACCESS_GRANTED);
    // All objects should have been removed, because the expression is always false
    assertThat(arg).isEmpty();
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) Collection(java.util.Collection) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Example 95 with MethodInvocation

use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.

the class MethodExpressionVoterTests method nullNamedFilterTargetIsRejected.

@Test
public void nullNamedFilterTargetIsRejected() throws Exception {
    MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingACollection(), new Object[] { null });
    assertThatIllegalArgumentException().isThrownBy(() -> this.am.vote(this.joe, mi, createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'joe')", "collection", null))));
}
Also used : SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) SimpleMethodInvocation(org.springframework.security.util.SimpleMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.jupiter.api.Test)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)124 Test (org.junit.jupiter.api.Test)50 Test (org.junit.Test)36 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)27 Method (java.lang.reflect.Method)24 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 ArrayList (java.util.ArrayList)11 Log (org.apache.commons.logging.Log)11 Authentication (org.springframework.security.core.Authentication)10 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 List (java.util.List)7 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 MyThrowsHandler (org.springframework.aop.testfixture.advice.MyThrowsHandler)7 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5