/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { methodSensorMap = new HashMap<Long, IMethodSensor>(); for (IMethodSensor methodSensor : methodSensors) { methodSensorMap.put(methodSensor.getSensorTypeConfig().getId(), methodSensor); } }
/** * 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; }
if ((1 == sensors.size()) || ((2 == sensors.size()) && enhancedExceptionSensor)) { for (IMethodSensor methodSensor : sensors) { MethodSensorTypeConfig methodSensorTypeConfig = methodSensor.getSensorTypeConfig(); if (PreparedStatementParameterSensor.class.getName().equals(methodSensorTypeConfig.getClassName())) { return true;
@BeforeMethod public void setup() throws Exception { when(platformManager.getPlatformId()).thenReturn(platformId); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { ((Runnable) invocation.getArguments()[0]).run(); return null; } }).when(executorService).submit(Matchers.<Runnable> any()); doAnswer(new Answer<Future<Object>>() { @Override public Future<Object> answer(InvocationOnMock invocation) throws Throwable { final Object result = ((Callable<?>) invocation.getArguments()[0]).call(); when(future.get(anyLong(), Matchers.<TimeUnit> any())).thenReturn(result); return future; } }).when(executorService).submit(Matchers.<Callable> any()); // method sensor and config when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig); // method sensors iterator Iterator<IMethodSensor> it = Mockito.mock(Iterator.class); when(it.hasNext()).thenReturn(true, false); when(it.next()).thenReturn(methodSensor); when(methodSensors.iterator()).thenReturn(it); }
/** * 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); }
sensors.add(methodSensor); when(rsc.getMethodSensors()).thenReturn(sensors); when(exceptionSensor.getSensorTypeConfig()).thenReturn(exceptionSensorConfig); when(methodSensor.getSensorTypeConfig()).thenReturn(sensorConfig);
@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); }
MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(specialHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
@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); }
constructorHook.beforeConstructor(id, methodSensor.getSensorTypeConfig().getId(), parameters, rsc);
methodHook.beforeBody(id, methodSensor.getSensorTypeConfig().getId(), object, parameters, rsc);
MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(methodHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId);
when(methodSensorTwo.getHook()).thenReturn(constructorHookTwo); when(methodSensorThree.getHook()).thenReturn(constructorHookThree); when(methodSensorOne.getSensorTypeConfig()).thenReturn(methodSensorConfigOne); when(methodSensorTwo.getSensorTypeConfig()).thenReturn(methodSensorConfigTwo); when(methodSensorThree.getSensorTypeConfig()).thenReturn(methodSensorConfigThree);
when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
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(); } } }
when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
when(rsc.getSettings()).thenReturn(map); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);
when(timer.getCurrentTime()).thenReturn(firstTimerValue, secondTimerValue); when(rsc.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorTypeConfig);