Search in sources :

Example 6 with Signature

use of org.aspectj.lang.Signature in project sakuli by ConSol.

the class RhinoAspectTest method testAddActionLog.

@Test(dataProvider = "logLevel")
public void testAddActionLog(LogLevel logLevel) throws Exception {
    initMocks();
    Report sahiReport = BeanLoader.loadBaseActionLoader().getSahiReport();
    final int lisSize = sahiReport.getListResult().size();
    final String classContent = "Test-Action-Content";
    final String className = TestCaseAction.class.getSimpleName();
    final String methodName = "actionMethod";
    final String sampleMessage = "sample-message-for-log";
    final String arg1 = "ARG1";
    final String arg2 = "NULL";
    TestCaseAction testAction = mock(TestCaseAction.class);
    when(testAction.toString()).thenReturn(classContent);
    JoinPoint jp = mock(JoinPoint.class);
    when(jp.getTarget()).thenReturn(testAction);
    Signature signature = mock(Signature.class);
    when(jp.getSignature()).thenReturn(signature);
    when(signature.getDeclaringType()).thenReturn(TestCaseAction.class);
    when(signature.getName()).thenReturn(methodName);
    when(signature.getDeclaringTypeName()).thenReturn(TestCaseAction.class.getName());
    when(jp.getArgs()).thenReturn(new Object[] { arg1, null });
    LogToResult logToResult = mock(LogToResult.class);
    when(logToResult.logClassInstance()).thenReturn(true);
    when(logToResult.message()).thenReturn(sampleMessage);
    when(logToResult.logArgs()).thenReturn(true);
    when(logToResult.level()).thenReturn(logLevel);
    RhinoAspect testling = BeanLoader.loadBean(RhinoAspect.class);
    testling.addActionLog(jp, logToResult);
    assertLastLine(logFile, className, logLevel, "\"" + classContent + "\" " + className + "." + methodName + "() - " + sampleMessage + " with arg(s) [" + arg1 + ", " + arg2 + "]");
    verifySahiReport(logLevel.getResultType(), lisSize);
    //hide args
    when(logToResult.logArgs()).thenReturn(false);
    testling.addActionLog(jp, logToResult);
    assertLastLine(logFile, className, logLevel, "\"" + classContent + "\" " + className + "." + methodName + "() - " + sampleMessage + " with arg(s) [****, ****]");
    //without class values
    when(logToResult.logClassInstance()).thenReturn(false);
    testling.addActionLog(jp, logToResult);
    assertLastLine(logFile, className, logLevel, className + "." + methodName + "() - " + sampleMessage + " with arg(s) [****, ****]");
    //without args
    when(jp.getArgs()).thenReturn(null);
    testling.addActionLog(jp, logToResult);
    assertLastLine(logFile, className, logLevel, className + "." + methodName + "() - " + sampleMessage);
    //without message
    when(logToResult.message()).thenReturn(null);
    testling.addActionLog(jp, logToResult);
    assertLastLine(logFile, className, logLevel, className + "." + methodName + "()");
}
Also used : LogToResult(org.sakuli.actions.logging.LogToResult) Report(net.sf.sahi.report.Report) TestCaseAction(org.sakuli.actions.TestCaseAction) Signature(org.aspectj.lang.Signature) JoinPoint(org.aspectj.lang.JoinPoint) JoinPoint(org.aspectj.lang.JoinPoint) Test(org.testng.annotations.Test)

Example 7 with Signature

use of org.aspectj.lang.Signature in project uPortal by Jasig.

the class RequestCacheAspect method createCacheKey.

protected CacheKey createCacheKey(ProceedingJoinPoint pjp, RequestCache requestCache) {
    final Signature signature = pjp.getSignature();
    final Class<?> declaringType = signature.getDeclaringType();
    final String signatureLongString = signature.toLongString();
    final boolean[] keyMask = requestCache.keyMask();
    final Object[] args = pjp.getArgs();
    final Object[] keyArgs;
    if (keyMask.length == 0) {
        keyArgs = args;
    } else if (keyMask.length != args.length) {
        throw new AnnotationFormatError("RequestCache.keyMask has an invalid length on: " + signature.toLongString());
    } else {
        keyArgs = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
            if (keyMask[i]) {
                keyArgs[i] = args[i];
            }
        }
    }
    return CacheKey.build(signatureLongString, declaringType, keyArgs);
}
Also used : Signature(org.aspectj.lang.Signature) AnnotationFormatError(java.lang.annotation.AnnotationFormatError)

Example 8 with Signature

use of org.aspectj.lang.Signature in project myjdbc by beijing-penguin.

the class TransactionManager method doAround.

// 用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型
public Object doAround(ProceedingJoinPoint call) throws Throwable {
    Signature sig = call.getSignature();
    MethodSignature ms = (MethodSignature) sig;
    Method method = call.getTarget().getClass().getDeclaredMethod(ms.getName(), ms.getParameterTypes());
    Transactional transactional = method.getAnnotation(Transactional.class);
    if (transactional == null) {
        // 方法无注解,查找类上注解,并判断当前调用方法是否为当前类定义的(防止父类方法触发事务边界)
        transactional = method.getDeclaringClass().getAnnotation(Transactional.class);
    }
    if (transactional != null) {
        // 如果不为空,则开启事务
        if (transactional.readOnly() == false) {
            ConnectionManager.setTransaction(true);
        } else {
            ConnectionManager.setReadOnly(true);
        }
    } else {
        ConnectionManager.setTransaction(false);
        ConnectionManager.setReadOnly(false);
    }
    Object invokeObj = null;
    try {
        // 执行目标方法
        invokeObj = call.proceed();
        // invokeObj = method.invoke(call.getTarget(), call.getArgs());
        ConnectionManager.commitAll();
    } catch (Throwable e) {
        ConnectionManager.rollbackAll();
        throw e;
    } finally {
        ConnectionManager.closeConnectionAll();
    }
    return invokeObj;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Signature (org.aspectj.lang.Signature)8 JoinPoint (org.aspectj.lang.JoinPoint)3 MethodSignature (org.aspectj.lang.reflect.MethodSignature)3 Method (java.lang.reflect.Method)2 CodeSignature (org.aspectj.lang.reflect.CodeSignature)2 Test (org.testng.annotations.Test)2 AnnotationFormatError (java.lang.annotation.AnnotationFormatError)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 ObjectName (javax.management.ObjectName)1 Report (net.sf.sahi.report.Report)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1 Before (org.junit.Before)1 ConfigurationServiceFacade (org.mifos.config.servicefacade.ConfigurationServiceFacade)1 ApprovalMethod (org.mifos.rest.approval.domain.ApprovalMethod)1 MethodArgHolder (org.mifos.rest.approval.domain.MethodArgHolder)1 ApprovalService (org.mifos.rest.approval.service.ApprovalService)1