/** * Returns if the given {@link InvocationSequenceData} should be removed due to the exception * constructor delegation. * * @param rsc * {@link RegisteredSensorConfig} * @param invocationSequenceData * {@link InvocationSequenceData} to check. * @return True if the invocation should be removed. */ private boolean removeDueToExceptionDelegation(RegisteredSensorConfig rsc, InvocationSequenceData invocationSequenceData) { List<IMethodSensor> sensors = rsc.getMethodSensors(); if (1 == sensors.size()) { MethodSensorTypeConfig methodSensorTypeConfig = sensors.get(0).getSensorTypeConfig(); if (ExceptionSensor.class.getName().equals(methodSensorTypeConfig.getClassName())) { return CollectionUtils.isEmpty(invocationSequenceData.getExceptionSensorDataObjects()); } } return false; }
@Test public void dispatchNoConstructorHooks() { int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; RegisteredSensorConfig registeredSensorConfig = mock(RegisteredSensorConfig.class); when(registeredSensorConfig.getMethodSensors()).thenReturn(Collections.<IMethodSensor> emptyList()); when(registeredSensorConfig.getMethodSensorsReverse()).thenReturn(Collections.<IMethodSensor> emptyList()); hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchConstructorBeforeBody(methodId, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); hookDispatcher.dispatchConstructorAfterBody(methodId, object, parameters); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensors(); verifyZeroInteractions(object, coreService); verifyNoMoreInteractions(registeredSensorConfig); }
@Test public void dispatchNoMethodHooks() { int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; Object returnValue = mock(Object.class); RegisteredSensorConfig registeredSensorConfig = mock(RegisteredSensorConfig.class); when(registeredSensorConfig.getMethodSensors()).thenReturn(Collections.<IMethodSensor> emptyList()); when(registeredSensorConfig.getMethodSensorsReverse()).thenReturn(Collections.<IMethodSensor> emptyList()); hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchMethodBeforeBody(methodId, object, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); hookDispatcher.dispatchFirstMethodAfterBody(methodId, object, parameters, returnValue, false); verify(registeredSensorConfig, times(1)).getMethodSensors(); hookDispatcher.dispatchSecondMethodAfterBody(methodId, object, parameters, returnValue, false); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(2)).getMethodSensors(); verifyZeroInteractions(object, coreService, returnValue); verifyNoMoreInteractions(registeredSensorConfig); }
List<IMethodSensor> sensors = rsc.getMethodSensors(); if ((1 == sensors.size()) || ((2 == sensors.size()) && enhancedExceptionSensor)) { for (IMethodSensor methodSensor : sensors) {
@Test public void dispatchOneConstructorHookWithoutInvocationTrace() { long sensorTypeId = 7L; IConstructorHook constructorHook = mock(IConstructorHook.class); IMethodSensor methodSensor = mock(IMethodSensor.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(constructorHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); RegisteredSensorConfig registeredSensorConfig = mock(RegisteredSensorConfig.class); when(registeredSensorConfig.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(registeredSensorConfig.getMethodSensorsReverse()).thenReturn(Collections.singletonList(methodSensor)); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchConstructorBeforeBody(methodId, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); verify(constructorHook, times(1)).beforeConstructor(methodId, sensorTypeId, parameters, registeredSensorConfig); hookDispatcher.dispatchConstructorAfterBody(methodId, object, parameters); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensors(); verify(constructorHook, times(1)).afterConstructor(coreService, methodId, sensorTypeId, object, parameters, registeredSensorConfig); verifyZeroInteractions(object, coreService); verifyNoMoreInteractions(registeredSensorConfig, constructorHook); }
/** * Tests that skip is activated when certain sensor is only defined in the * {@link RegisteredSensorConfig}. * * @see #skippingSensors() */ @Test(dataProvider = "skippingSensors") public void skipSingleSensor(Class<? extends ISensor> sensorClass) { long methodId = 3L; long sensorTypeId = 11L; Object object = mock(Object.class); Object[] parameters = new Object[0]; Object result = mock(Object.class); MethodSensorTypeConfig sensorConfig = mock(MethodSensorTypeConfig.class); when(sensorConfig.getClassName()).thenReturn(sensorClass.getName()); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(sensorConfig); invocationSequenceHook.beforeBody(methodId, sensorTypeId, object, parameters, rsc); invocationSequenceHook.firstAfterBody(methodId, sensorTypeId, object, parameters, result, false, rsc); invocationSequenceHook.secondAfterBody(coreService, methodId, sensorTypeId, object, parameters, result, false, rsc); verifyZeroInteractions(timer, coreService, realCoreService); }
when(registeredSensorConfig.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(registeredSensorConfig.getMethodSensorsReverse()).thenReturn(Collections.singletonList(methodSensor)); verify(registeredSensorConfig, times(1)).getMethodSensors(); verify(methodHook, times(1)).firstAfterBody(methodId, sensorTypeId, object, parameters, returnValue, exception, registeredSensorConfig); verify(registeredSensorConfig, times(2)).getMethodSensors(); verify(methodHook, times(1)).secondAfterBody(coreService, methodId, sensorTypeId, object, parameters, returnValue, exception, registeredSensorConfig);
sensors.add(exceptionSensor); sensors.add(methodSensor); when(rsc.getMethodSensors()).thenReturn(sensors); when(exceptionSensor.getSensorTypeConfig()).thenReturn(exceptionSensorConfig); when(methodSensor.getSensorTypeConfig()).thenReturn(sensorConfig);
for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); methodHook.secondAfterBody(coreService, id, methodSensor.getSensorTypeConfig().getId(), object, parameters, returnValue, exception, rsc);
when(exceptionSensorConfig.getClassName()).thenReturn(ExceptionSensor.class.getName()); when(rsc.getMethodSensors()).thenReturn(Collections.<IMethodSensor> emptyList()); when(removingRsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(exceptionSensorConfig);
/** * {@inheritDoc} */ @Override public void dispatchFirstMethodAfterBody(long id, Object object, Object[] parameters, Object returnValue, boolean exception) { if (!executionMarker.isActive()) { try { executionMarker.active(); try { RegisteredSensorConfig rsc = mappings.get(id); // Now iterate over all registered sensor types and execute them // normal execution (sensor with highest priority first) for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); methodHook.firstAfterBody(id, methodSensor.getSensorTypeConfig().getId(), object, parameters, returnValue, exception, rsc); } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (after body)", throwable); } } finally { executionMarker.deactive(); } } }
verify(hookDispatcherMapper, times(1)).addMapping(eq(rscId), rscCaptor.capture()); assertThat(rscCaptor.getValue().getId(), is(rscId)); assertThat(rscCaptor.getValue().getMethodSensors(), hasSize(1)); assertThat(rscCaptor.getValue().getMethodSensors(), hasItem(methodSensor)); assertThat(rscCaptor.getValue().getTargetClassFqn(), is(methodInstrumentationConfig.getTargetClassFqn())); assertThat(rscCaptor.getValue().getTargetMethodName(), is(methodInstrumentationConfig.getTargetMethodName()));
double secondTimerValue = 1323.0d; when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
double secondTimerValue = 1323.0d; when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
double thirdTimerValue = 1881.0d; when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue, thirdTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.<IMethodSensor> emptyList());
double secondTimerValue = 1323.0d; when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
map.put("minduration", minDuration); when(rsc.getSettings()).thenReturn(map); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
double secondTimerValue = 1323.0d; when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
double fourthTimerValue = 2562.0d; when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue, thirdTimerValue, fourthTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig); TimerData timerData = new TimerData();
double fourthTimerValue = 2562.0d; when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue, thirdTimerValue, fourthTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.<IMethodSensor> emptyList());