/** * {@inheritDoc} */ @Override public void secondAfterBody(ICoreService coreService, long methodId, long sensorTypeId, Object object, Object[] parameters, Object result, boolean exception, RegisteredSensorConfig rsc) { // NOCHK:8-params if (threadLast.get().booleanValue()) { threadLast.set(Boolean.FALSE); List<String> parameterTypes = rsc.getParameterTypes(); if (METHOD_VALUE_MAP.containsKey(rsc.getTargetMethodName()) && !parameterTypes.isEmpty() && "int".equals(parameterTypes.get(0))) { // subtract one as the index starts at 1, and not at 0 int index = ((Integer) parameters[0]).intValue() - 1; Object value = METHOD_VALUE_MAP.get(rsc.getTargetMethodName()); statementStorage.addParameter(object, index, value); } else if ((parameterTypes.size() >= 2) && "int".equals(parameterTypes.get(0))) { // subtract one as the index starts at 1, and not at 0 int index = ((Integer) parameters[0]).intValue() - 1; Object value = parameters[1]; statementStorage.addParameter(object, index, value); } else if ("clearParameters".equals(rsc.getTargetMethodName())) { statementStorage.clearParameters(object); } } }
/** * {@inheritDoc} */ @Override public ResponseAdapter getClientResponseAdapter(Object object, Object[] parameters, Object result, boolean exception, RegisteredSensorConfig rsc) { Object urlConnection = object; if ((urlConnection instanceof HttpURLConnection) && GET_INPUT_STREAM_METHOD.equals(rsc.getTargetMethodName())) { HttpResponse response = new UrlConnectionHttpClientRequestResponse((HttpURLConnection) urlConnection); HttpResponseAdapter adapter = new HttpResponseAdapter(response); if (exception) { // we can delegate as we don't depend on result return new ThrowableAwareResponseAdapter(adapter, result.getClass().getSimpleName()); } else { return adapter; } } else { return null; } }
@Test public void notInputStreamMethod() throws IOException { when(rsc.getTargetMethodName()).thenReturn("connect"); ResponseAdapter adapter = sensor.getClientResponseAdapter(urlConnection, null, result, false, rsc); assertThat(adapter, is(nullValue())); verify(rsc).getTargetMethodName(); verifyNoMoreInteractions(rsc); verifyZeroInteractions(result); }
@Test public void statusIOException() throws IOException { when(urlConnection.getResponseCode()).thenThrow(new IOException()); when(rsc.getTargetMethodName()).thenReturn("getInputStream"); ResponseAdapter adapter = sensor.getClientResponseAdapter(urlConnection, null, result, false, rsc); Map<String, String> tags = adapter.getTags(); assertThat(tags.size(), is(0)); verify(rsc).getTargetMethodName(); verifyNoMoreInteractions(rsc); verifyZeroInteractions(result); }
@Test public void notUrlConnection() throws IOException { when(rsc.getTargetMethodName()).thenReturn("getInputStream"); ResponseAdapter adapter = sensor.getClientResponseAdapter(new Object(), null, result, false, rsc); assertThat(adapter, is(nullValue())); verifyZeroInteractions(result, rsc); }
@Test public void status() throws IOException { int status = 200; when(urlConnection.getResponseCode()).thenReturn(status); when(rsc.getTargetMethodName()).thenReturn("getInputStream"); ResponseAdapter adapter = sensor.getClientResponseAdapter(urlConnection, null, result, false, rsc); Map<String, String> tags = adapter.getTags(); assertThat(tags.size(), is(1)); assertThat(tags, hasEntry(Tags.HTTP_STATUS.getKey(), String.valueOf(status))); verify(rsc).getTargetMethodName(); verifyNoMoreInteractions(rsc); verifyZeroInteractions(result); }
@Test public void exception() throws IOException { int status = 200; when(urlConnection.getResponseCode()).thenReturn(status); when(rsc.getTargetMethodName()).thenReturn("getInputStream"); ResponseAdapter adapter = sensor.getClientResponseAdapter(urlConnection, null, new NullPointerException(), true, rsc); Map<String, String> tags = adapter.getTags(); assertThat(tags.size(), is(3)); assertThat(tags, hasEntry(Tags.HTTP_STATUS.getKey(), String.valueOf(status))); assertThat(tags, hasEntry(Tags.ERROR.getKey(), String.valueOf(true))); assertThat(tags, hasEntry(ExtraTags.THROWABLE_TYPE, NullPointerException.class.getSimpleName())); verify(rsc).getTargetMethodName(); verifyNoMoreInteractions(rsc); }
/** * Tests arbitrary method with wrong parameter count. */ @SuppressWarnings("unchecked") @Test public void wrongMethodInstrumentation() { List<String> parameterTypes = Mockito.mock(List.class); Mockito.when(parameterTypes.size()).thenReturn(1); Mockito.when(parameterTypes.get(0)).thenReturn("int"); Mockito.when(rsc.getParameterTypes()).thenReturn(parameterTypes); Mockito.when(rsc.getTargetMethodName()).thenReturn("myMethod"); Object[] parameters = new Object[] { 1 }; Object object = new Object(); preparedStatementParameterHook.beforeBody(0, 0, object, parameters, rsc); preparedStatementParameterHook.firstAfterBody(0, 0, object, parameters, null, false, rsc); preparedStatementParameterHook.secondAfterBody(coreService, 0, 0, object, parameters, null, false, rsc); Mockito.verifyZeroInteractions(statementStorage); Mockito.verifyZeroInteractions(coreService); }
/** * Tests clear parameters method being invoked. */ @SuppressWarnings("unchecked") @Test public void clearParameters() { List<String> parameterTypes = Mockito.mock(List.class); Mockito.when(parameterTypes.size()).thenReturn(0); Mockito.when(rsc.getParameterTypes()).thenReturn(parameterTypes); Mockito.when(rsc.getTargetMethodName()).thenReturn("clearParameters"); Object[] parameters = new Object[0]; Object object = new Object(); preparedStatementParameterHook.beforeBody(0, 0, object, parameters, rsc); preparedStatementParameterHook.firstAfterBody(0, 0, object, parameters, null, false, rsc); preparedStatementParameterHook.secondAfterBody(coreService, 0, 0, object, parameters, null, false, rsc); Mockito.verify(statementStorage, Mockito.times(1)).clearParameters(object); Mockito.verifyNoMoreInteractions(statementStorage); Mockito.verifyZeroInteractions(coreService); }
/** * Test that setNull() methods of the prepare statement will set the <code>null</code> as the * parameter value. */ @SuppressWarnings("unchecked") @Test public void setNull() { List<String> parameterTypes = Mockito.mock(List.class); Mockito.when(parameterTypes.size()).thenReturn(2); Mockito.when(parameterTypes.get(0)).thenReturn("int"); Mockito.when(rsc.getParameterTypes()).thenReturn(parameterTypes); Mockito.when(rsc.getTargetMethodName()).thenReturn("setNull"); Object[] parameters = new Object[] { 1, 2 }; Object object = new Object(); preparedStatementParameterHook.beforeBody(0, 0, object, parameters, rsc); preparedStatementParameterHook.firstAfterBody(0, 0, object, parameters, null, false, rsc); preparedStatementParameterHook.secondAfterBody(coreService, 0, 0, object, parameters, null, false, rsc); Mockito.verify(statementStorage, Mockito.times(1)).addParameter(object, 0, null); Mockito.verifyNoMoreInteractions(statementStorage); Mockito.verifyZeroInteractions(coreService); }
/** * Tests that the methods with big data structures will not set the complete structure, but a * simple marker instead. * * @param methodName * Method name that holds big structure. */ @SuppressWarnings("unchecked") @Test(dataProvider = "methodsWithBigDataStructures") public void methodWithBigData(String methodName) { List<String> parameterTypes = Mockito.mock(List.class); Mockito.when(parameterTypes.size()).thenReturn(2); Mockito.when(parameterTypes.get(0)).thenReturn("int"); Mockito.when(rsc.getParameterTypes()).thenReturn(parameterTypes); Mockito.when(rsc.getTargetMethodName()).thenReturn(methodName); Object[] parameters = new Object[] { 1, "Value" }; Object object = new Object(); preparedStatementParameterHook.beforeBody(0, 0, object, parameters, rsc); preparedStatementParameterHook.firstAfterBody(0, 0, object, parameters, null, false, rsc); preparedStatementParameterHook.secondAfterBody(coreService, 0, 0, object, parameters, null, false, rsc); String expected = "[" + methodName.substring("set".length()) + "]"; Mockito.verify(statementStorage, Mockito.times(1)).addParameter(object, 0, expected); Mockito.verifyNoMoreInteractions(statementStorage); Mockito.verifyZeroInteractions(coreService); }
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().isStartsInvocation(), is(sensorInstrumentationPoint.isStartsInvocation()));
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().isStartsInvocation(), is(sensorInstrumentationPoint.isStartsInvocation()));
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().isStartsInvocation(), is(sensorInstrumentationPoint.isStartsInvocation()));
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().isStartsInvocation(), is(sensorInstrumentationPoint.isStartsInvocation()));
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().isStartsInvocation(), is(sensorInstrumentationPoint.isStartsInvocation()));