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 + "()");
}
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);
}
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;
}
Aggregations