Search in sources :

Example 61 with MethodInvocation

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

the class ThrowsAdviceInterceptorTests method testCorrectHandlerUsedForSubclass.

@Test
public void testCorrectHandlerUsedForSubclass() throws Throwable {
    MyThrowsHandler th = new MyThrowsHandler();
    ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
    // Extends RemoteException
    ConnectException ex = new ConnectException("");
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.proceed()).willThrow(ex);
    try {
        ti.invoke(mi);
        fail();
    } catch (Exception caught) {
        assertEquals(ex, caught);
    }
    assertEquals(1, th.getCalls());
    assertEquals(1, th.getCalls("remoteException"));
}
Also used : MethodInvocation(org.aopalliance.intercept.MethodInvocation) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ConnectException(java.rmi.ConnectException) RemoteException(java.rmi.RemoteException) ConnectException(java.rmi.ConnectException) Test(org.junit.Test)

Example 62 with MethodInvocation

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

the class CustomizableTraceInterceptorTests method testSunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching.

@Test
public void testSunnyDayPathLogsCorrectlyWithPrettyMuchAllPlaceholdersMatching() throws Throwable {
    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString", new Class[0]));
    given(methodInvocation.getThis()).willReturn(this);
    given(methodInvocation.getArguments()).willReturn(new Object[] { "$ One \\$", new Long(2) });
    given(methodInvocation.proceed()).willReturn("Hello!");
    Log log = mock(Log.class);
    given(log.isTraceEnabled()).willReturn(true);
    CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log);
    interceptor.setEnterMessage(new StringBuffer().append("Entering the '").append(CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME).append("' method of the [").append(CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_NAME).append("] class with the following args (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS).append(") and arg types (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES).append(").").toString());
    interceptor.setExitMessage(new StringBuffer().append("Exiting the '").append(CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME).append("' method of the [").append(CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_SHORT_NAME).append("] class with the following args (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENTS).append(") and arg types (").append(CustomizableTraceInterceptor.PLACEHOLDER_ARGUMENT_TYPES).append("), returning '").append(CustomizableTraceInterceptor.PLACEHOLDER_RETURN_VALUE).append("' and taking '").append(CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME).append("' this long.").toString());
    interceptor.invoke(methodInvocation);
    verify(log, times(2)).trace(anyString());
}
Also used : Log(org.apache.commons.logging.Log) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 63 with MethodInvocation

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

the class DebugInterceptorTests method testSunnyDayPathLogsCorrectly.

@Test
public void testSunnyDayPathLogsCorrectly() throws Throwable {
    MethodInvocation methodInvocation = mock(MethodInvocation.class);
    Log log = mock(Log.class);
    given(log.isTraceEnabled()).willReturn(true);
    DebugInterceptor interceptor = new StubDebugInterceptor(log);
    interceptor.invoke(methodInvocation);
    checkCallCountTotal(interceptor);
    verify(log, times(2)).trace(anyString());
}
Also used : Log(org.apache.commons.logging.Log) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 64 with MethodInvocation

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

the class SimpleTraceInterceptorTests method testSunnyDayPathLogsCorrectly.

@Test
public void testSunnyDayPathLogsCorrectly() throws Throwable {
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.getMethod()).willReturn(String.class.getMethod("toString", new Class[] {}));
    given(mi.getThis()).willReturn(this);
    Log log = mock(Log.class);
    SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true);
    interceptor.invokeUnderTrace(mi, log);
    verify(log, times(2)).trace(anyString());
}
Also used : Log(org.apache.commons.logging.Log) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 65 with MethodInvocation

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

the class SimpleTraceInterceptorTests method testExceptionPathStillLogsCorrectly.

@Test
public void testExceptionPathStillLogsCorrectly() throws Throwable {
    MethodInvocation mi = mock(MethodInvocation.class);
    given(mi.getMethod()).willReturn(String.class.getMethod("toString", new Class[] {}));
    given(mi.getThis()).willReturn(this);
    IllegalArgumentException exception = new IllegalArgumentException();
    given(mi.proceed()).willThrow(exception);
    Log log = mock(Log.class);
    final SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true);
    try {
        interceptor.invokeUnderTrace(mi, log);
        fail("Must have propagated the IllegalArgumentException.");
    } catch (IllegalArgumentException expected) {
    }
    verify(log).trace(anyString());
    verify(log).trace(anyString(), eq(exception));
}
Also used : Log(org.apache.commons.logging.Log) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Aggregations

MethodInvocation (org.aopalliance.intercept.MethodInvocation)84 Test (org.junit.Test)59 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)22 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)15 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 Method (java.lang.reflect.Method)11 Log (org.apache.commons.logging.Log)9 EvaluationContext (org.springframework.expression.EvaluationContext)9 Expression (org.springframework.expression.Expression)9 PreInvocationExpressionAttribute (org.springframework.security.access.expression.method.PreInvocationExpressionAttribute)9 Authentication (org.springframework.security.core.Authentication)9 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 TestBean (org.springframework.tests.sample.beans.TestBean)9 IOException (java.io.IOException)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)7 FileNotFoundException (java.io.FileNotFoundException)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)5 AccessibleObject (java.lang.reflect.AccessibleObject)4 ConnectException (java.rmi.ConnectException)4 RemoteException (java.rmi.RemoteException)4