StrutsInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { if (DISABLED || !STRUTS_COUNTER.isDisplayed()) { return invocation.invoke(); } boolean systemError = false; try { final String actionName = getRequestName(invocation); STRUTS_COUNTER.bindContextIncludingCpu(actionName); return invocation.invoke(); } catch (final Error e) { systemError = true; throw e; } finally { STRUTS_COUNTER.addRequestForCurrentContext(systemError); } } StrutsInterceptor(); @Override String intercept(ActionInvocation invocation);  }
@Test public void testStruts() throws Exception { final Counter strutsCounter = MonitoringProxy.getStrutsCounter(); strutsCounter.clear(); final StrutsInterceptor strutsInterceptor = new StrutsInterceptor(); final ActionInvocation invocation = createNiceMock(ActionInvocation.class); final MockActionProxy proxy = new MockActionProxy(); proxy.setInvocation(invocation); proxy.setActionName("test.action"); proxy.setMethod(null); proxy.setNamespace("testnamespace"); expect(invocation.getProxy()).andReturn(proxy).anyTimes(); replay(invocation); strutsCounter.setDisplayed(false); strutsInterceptor.intercept(invocation); final String requestsCount = "requestsCount"; assertSame(requestsCount, 0, strutsCounter.getRequestsCount()); strutsCounter.setDisplayed(true); strutsInterceptor.intercept(invocation); assertSame(requestsCount, 1, strutsCounter.getRequestsCount()); verify(invocation); final ActionInvocation invocation2 = createNiceMock(ActionInvocation.class); final MockActionProxy proxy2 = new MockActionProxy(); proxy2.setInvocation(invocation2); proxy2.setActionName("test2.action"); proxy2.setMethod("execute"); proxy2.setNamespace("testnamespace"); expect(invocation2.getProxy()).andReturn(proxy2).anyTimes(); replay(invocation2); strutsInterceptor.intercept(invocation2); assertSame(requestsCount, 2, strutsCounter.getRequestsCount()); verify(invocation2); final ActionInvocation invocation3 = createNiceMock(ActionInvocation.class); final MockActionProxy proxy3 = new MockActionProxy(); proxy3.setInvocation(invocation3); proxy3.setActionName("test3.action"); proxy3.setMethod("testmethod"); proxy3.setNamespace("testnamespace"); expect(invocation3.getProxy()).andReturn(proxy3).anyTimes(); expect(invocation3.invoke()).andThrow(new UnknownError("test d'erreur")).anyTimes(); replay(invocation3); try { strutsInterceptor.intercept(invocation3); } catch (final UnknownError e) { assertNotNull("ok", e); } assertSame(requestsCount, 3, strutsCounter.getRequestsCount()); verify(invocation3); }