/** * {@inheritDoc} */ @Override public Object dispatchSpecialMethodAfterBody(long id, Object object, Object[] parameters, Object returnValue) { try { SpecialSensorConfig ssc = specialMappings.get(id); IMethodSensor methodSensor = ssc.getSensor(); ISpecialHook specialHook = (ISpecialHook) methodSensor.getHook(); Object result = specialHook.afterBody(id, object, parameters, returnValue, ssc); if (null != result) { return result; } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (after special method)", throwable); } return null; }
/** * {@inheritDoc} */ @Override public Object dispatchSpecialMethodBeforeBody(long id, Object object, Object[] parameters) { try { SpecialSensorConfig ssc = specialMappings.get(id); IMethodSensor methodSensor = ssc.getSensor(); ISpecialHook specialHook = (ISpecialHook) methodSensor.getHook(); Object result = specialHook.beforeBody(id, object, parameters, ssc); if (null != result) { return result; } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (before special method)", throwable); } return null; }
when(specialHook.beforeBody(methodId, object, parameters, specialSensorConfig)).thenReturn(resultBefore); when(specialHook.afterBody(methodId, object, parameters, returnValue, specialSensorConfig)).thenReturn(resultAfter); verify(specialHook, times(1)).beforeBody(methodId, object, parameters, specialSensorConfig); assertThat(result, is(resultBefore)); verify(specialHook, times(1)).afterBody(methodId, object, parameters, returnValue, specialSensorConfig); assertThat(result, is(resultAfter));
@Test public void dispatchOneSpecialHook() { long sensorTypeId = 7L; IMethodSensor methodSensor = mock(IMethodSensor.class); ISpecialHook specialHook = mock(ISpecialHook.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(specialHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); SpecialSensorConfig specialSensorConfig = mock(SpecialSensorConfig.class); when(specialSensorConfig.getSensor()).thenReturn(methodSensor); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; Object returnValue = mock(Object.class); hookDispatcher.addMapping(methodId, specialSensorConfig); Object result = hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, object, parameters); verify(specialSensorConfig, times(1)).getSensor(); verify(specialHook, times(1)).beforeBody(methodId, object, parameters, specialSensorConfig); assertThat(result, is(nullValue())); result = hookDispatcher.dispatchSpecialMethodAfterBody(methodId, object, parameters, returnValue); verify(specialSensorConfig, times(2)).getSensor(); verify(specialHook, times(1)).afterBody(methodId, object, parameters, returnValue, specialSensorConfig); assertThat(result, is(nullValue())); verifyZeroInteractions(object, coreService, returnValue); verifyNoMoreInteractions(specialSensorConfig, specialHook); }