/** * {@inheritDoc} */ @Override public void afterConstructor(ICoreService coreService, long methodId, long sensorTypeId, Object object, Object[] parameters, RegisteredSensorConfig rsc) { // getting the actual object class and comparing to the registered sensor config target // class String throwableClass = object.getClass().getName(); String rscTragetClassname = rsc.getTargetClassFqn(); if (throwableClass.equals(rscTragetClassname)) { long platformId = platformManager.getPlatformId(); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); Long identityHash = Long.valueOf(System.identityHashCode(object)); // need to reset the exception handler id exceptionHandlerId.set(null); // getting the actual object with information Throwable throwable = (Throwable) object; // creating the data object ExceptionSensorData data = new ExceptionSensorData(timestamp, platformId, sensorTypeId, methodId); data.setThrowableIdentityHashCode(identityHash.longValue()); data.setExceptionEvent(ExceptionEvent.CREATED); data.setThrowableType(throwable.getClass().getName()); // set the static information of the current object setStaticInformation(data, throwable); // creating the mapping object and setting it on the thread local exceptionDataHolder.set(new IdentityHashToDataObject(identityHash, data)); // adding the data object to the core service coreService.addDefaultData(data); } }
@Test public void throwableObjectDifferentThenRSCTargetClass() throws Exception { long constructorId = 5L; long sensorTypeId = 3L; long platformId = 1L; Object[] parameters = new Object[0]; Exception exceptionObject = Exception.class.newInstance(); when(platformManager.getPlatformId()).thenReturn(platformId); when(registeredSensorConfig.getTargetClassFqn()).thenReturn(MyTestException.class.getName()); exceptionHook.afterConstructor(coreService, constructorId, sensorTypeId, exceptionObject, parameters, registeredSensorConfig); verifyNoMoreInteractions(platformManager); }
@Test public void throwableObjectWasCreated() throws Exception { long constructorId = 5L; long sensorTypeId = 3L; long platformId = 1L; Object[] parameters = new Object[0]; MyTestException exceptionObject = MyTestException.class.newInstance(); when(platformManager.getPlatformId()).thenReturn(platformId); when(registeredSensorConfig.getTargetClassFqn()).thenReturn(MyTestException.class.getName()); exceptionHook.afterConstructor(coreService, constructorId, sensorTypeId, exceptionObject, parameters, registeredSensorConfig); verify(platformManager, times(1)).getPlatformId(); verifyNoMoreInteractions(platformManager); // verify(coreService, never()); }
@Test public void differentThrowableObjectsCreatedAndThrown() throws Exception { long methodId = 5L; long constructorId = 4L; long sensorTypeId = 3L; long platformId = 1L; Object[] parameters = new Object[0]; Object object = mock(Object.class); MyTestException firstExceptionObject = MyTestException.class.newInstance(); MyTestException secondExceptionObject = MyTestException.class.newInstance(); when(platformManager.getPlatformId()).thenReturn(platformId); when(registeredSensorConfig.getTargetClassFqn()).thenReturn(MyTestException.class.getName()); exceptionHook.afterConstructor(coreService, constructorId, sensorTypeId, firstExceptionObject, parameters, registeredSensorConfig); verify(platformManager, times(1)).getPlatformId(); // verify(coreService, // times(1)).addExceptionSensorData(eq(registeredSensorTypeId), // argThat(new ExceptionSensorDataVerifier(exceptionSensorData))); exceptionHook.dispatchOnThrowInBody(coreService, methodId, sensorTypeId, object, secondExceptionObject, parameters, registeredSensorConfig); verify(platformManager, times(2)).getPlatformId(); verifyNoMoreInteractions(platformManager); }
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())); assertThat(rscCaptor.getValue().getReturnType(), is(methodInstrumentationConfig.getReturnType()));
@Test public void valueTooLong() throws Exception { long constructorId = 5L; long sensorTypeId = 3L; long platformId = 1L; Object[] parameters = new Object[0]; StringConstraint strConstraint = new StringConstraint(parameter); // the actual error message is too long, so it should be cropped later on String exceptionMessage = fillString('x', stringLength + 1); MyTestException exceptionObject = new MyTestException(exceptionMessage); ExceptionSensorData exceptionSensorData = new ExceptionSensorData(new Timestamp(System.currentTimeMillis()), platformId, sensorTypeId, constructorId); exceptionSensorData.setExceptionEvent(ExceptionEvent.CREATED); exceptionSensorData.setThrowableIdentityHashCode(System.identityHashCode(exceptionObject)); // the actual error message to be verified against exceptionSensorData.setErrorMessage(strConstraint.crop(exceptionMessage)); when(platformManager.getPlatformId()).thenReturn(platformId); when(registeredSensorConfig.getTargetClassFqn()).thenReturn(MyTestException.class.getName()); exceptionHook.afterConstructor(coreService, constructorId, sensorTypeId, exceptionObject, parameters, registeredSensorConfig); verify(platformManager, times(1)).getPlatformId(); verify(coreService, times(1)).addDefaultData(argThat(new ExceptionSensorDataVerifier(exceptionSensorData))); verifyNoMoreInteractions(platformManager); }
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())); assertThat(rscCaptor.getValue().getReturnType(), is(methodInstrumentationConfig.getReturnType()));
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())); assertThat(rscCaptor.getValue().getReturnType(), is(methodInstrumentationConfig.getReturnType()));
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())); assertThat(rscCaptor.getValue().getReturnType(), is(methodInstrumentationConfig.getReturnType()));
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())); assertThat(rscCaptor.getValue().getReturnType(), is(methodInstrumentationConfig.getReturnType()));
@Test public void throwableObjectCreatedThrownAndHandled() throws Exception { long methodId = 5L; long constructorId = 4L; long sensorTypeId = 3L; long platformId = 1L; long methodIdTwo = 20L; Object[] parameters = new Object[0]; Object object = mock(Object.class); MyTestException exceptionObject = MyTestException.class.newInstance(); ExceptionSensorData exceptionSensorData = new ExceptionSensorData(new Timestamp(System.currentTimeMillis()), platformId, sensorTypeId, methodIdTwo); exceptionSensorData.setErrorMessage(exceptionObject.getMessage()); exceptionSensorData.setThrowableIdentityHashCode(System.identityHashCode(exceptionObject)); when(platformManager.getPlatformId()).thenReturn(platformId); when(registeredSensorConfig.getTargetClassFqn()).thenReturn(MyTestException.class.getName()); exceptionSensorData.setExceptionEvent(ExceptionEvent.CREATED); exceptionHook.afterConstructor(coreService, constructorId, sensorTypeId, exceptionObject, parameters, registeredSensorConfig); verify(platformManager, times(1)).getPlatformId(); verify(coreService, times(1)).addDefaultData(argThat(new ExceptionSensorDataVerifier(exceptionSensorData))); exceptionSensorData.setExceptionEvent(ExceptionEvent.PASSED); exceptionHook.dispatchOnThrowInBody(coreService, methodId, sensorTypeId, object, exceptionObject, parameters, registeredSensorConfig); verify(platformManager, times(2)).getPlatformId(); verify(coreService, times(1)).addDefaultData(argThat(new ExceptionSensorDataVerifier(exceptionSensorData))); exceptionSensorData.setExceptionEvent(ExceptionEvent.HANDLED); exceptionHook.dispatchBeforeCatchBody(coreService, methodIdTwo, sensorTypeId, exceptionObject, registeredSensorConfig); verify(platformManager, times(3)).getPlatformId(); verify(coreService, times(1)).addDefaultData(argThat(new ExceptionSensorDataVerifier(exceptionSensorData))); verifyNoMoreInteractions(platformManager, coreService); }
when(registeredSensorConfig.getTargetClassFqn()).thenReturn(MyTestException.class.getName());