PerformanceMonitorAspect { @Around("@annotation(performanceMonitorAnnotation)") public Object monitor(final ProceedingJoinPoint pjp, PerformanceMonitor performanceMonitorAnnotation) throws Throwable { String monitorName = performanceMonitorAnnotation.value(); if (logger.isDebugEnabled()) { logger.debug("Have @PerformanceMonitor method with monitor name {}, " + "wrapping call on method {} of target object {}", new Object[]{ monitorName, pjp.getSignature().getName(), pjp.getTarget()}); } org.fishwife.jrugged.PerformanceMonitor performanceMonitor = performanceMonitorFactory.findPerformanceMonitor( monitorName); if (performanceMonitor == null) { performanceMonitor = performanceMonitorFactory.createPerformanceMonitor( monitorName); } return performanceMonitor.invoke( new Callable<Object>() { public Object call() throws Exception { Object retval; try { retval = pjp.proceed(); } catch (Throwable e) { if (e instanceof Exception) { throw (Exception) e; } else { throw (Error) e; } } return retval; } } ); } PerformanceMonitorAspect(); void setPerformanceMonitorFactory(
            PerformanceMonitorFactory performanceMonitorFactory); PerformanceMonitorFactory getPerformanceMonitorFactory(); @Around("@annotation(performanceMonitorAnnotation)") Object monitor(final ProceedingJoinPoint pjp,
            PerformanceMonitor performanceMonitorAnnotation);  }
@Test public void testMonitor() throws Throwable { ProceedingJoinPoint mockPjp = createPjpMock(mockSignature, 2); expect(mockPjp.proceed()).andReturn(null).times(2); replay(mockPjp); aspect.monitor(mockPjp, mockAnnotation); aspect.monitor(mockPjp, mockAnnotation); String otherMonitor = "OtherMonitor"; ProceedingJoinPoint otherMockPjp = createPjpMock(mockSignature, 1); expect(otherMockPjp.proceed()).andReturn(null).times(1); replay(otherMockPjp); PerformanceMonitor otherMockAnnotation = createMock(PerformanceMonitor.class); expect(otherMockAnnotation.value()).andReturn(otherMonitor); replay(otherMockAnnotation); aspect.monitor(otherMockPjp, otherMockAnnotation); verifyMonitor(TEST_MONITOR, 2, 0); verifyMonitor(otherMonitor, 1, 0); verify(mockPjp); verify(mockAnnotation); verify(mockSignature); verify(otherMockPjp); verify(otherMockAnnotation); }
@Test(expected = Throwable.class) public void testMonitorWithThrowable() throws Throwable { ProceedingJoinPoint mockPjp = createPjpMock(mockSignature, 1); expect(mockPjp.proceed()).andThrow(new Throwable()); replay(mockPjp); aspect.monitor(mockPjp, mockAnnotation); verifyMonitor(TEST_MONITOR, 0, 1); verify(mockPjp); verify(mockAnnotation); verify(mockSignature); }
@Test(expected = Exception.class) public void testMonitorWithException() throws Throwable { ProceedingJoinPoint mockPjp = createPjpMock(mockSignature, 1); expect(mockPjp.proceed()).andThrow(new Exception()); replay(mockPjp); aspect.monitor(mockPjp, mockAnnotation); verifyMonitor(TEST_MONITOR, 0, 1); verify(mockPjp); verify(mockAnnotation); verify(mockSignature); }