/** * Returns the {@link IInstrumentationApplier} for the given sensor assignment and the * {@link Environment} it's being used in. * * @param sensorAssignment * {@link ISensorAssignment} * @param environment * Environment being used. * @return {@link IInstrumentationApplier} * @throws IllegalArgumentException * If supplied assignment is not of known type. */ public IInstrumentationApplier getInstrumentationApplier(ISensorAssignment<?> sensorAssignment, Environment environment) throws IllegalArgumentException { // switch based on the class Class<?> sensorAssigmentClass = sensorAssignment.getClass(); if (TimerMethodSensorAssignment.class.isAssignableFrom(sensorAssigmentClass)) { return new TimerMethodSensorInstrumentationApplier((TimerMethodSensorAssignment) sensorAssignment, environment, registrationService); } else if (InvocationStartMethodSensorAssignment.class.isAssignableFrom(sensorAssigmentClass)) { return new InvocationStartMethodSensorInstrumentationApplier((InvocationStartMethodSensorAssignment) sensorAssignment, environment, registrationService); } else if (ExceptionSensorAssignment.class.isAssignableFrom(sensorAssigmentClass)) { return new ExceptionSensorInstrumentationApplier((ExceptionSensorAssignment) sensorAssignment, environment, registrationService); } else if (SpecialMethodSensorAssignment.class.isAssignableFrom(sensorAssigmentClass)) { return new SpecialInstrumentationApplier((SpecialMethodSensorAssignment) sensorAssignment, environment, registrationService); } else if (MethodSensorAssignment.class.isAssignableFrom(sensorAssigmentClass)) { return new MethodSensorInstrumentationApplier((MethodSensorAssignment) sensorAssignment, environment, registrationService); } throw new IllegalArgumentException("Instrumentation applier can be created. Assignment " + sensorAssignment + " is of unknow type."); }
@Test public void doesNotMatchClassFilter() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(classFilter.matches(assignment, classType, false)).thenReturn(false); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(false)); verifyZeroInteractions(methodType); }
/** * {@inheritDoc} */ @Override protected boolean matches(ClassType classType) { return getClassSensorAssignmentFilter().matches(functionalAssignment, classType, false); }
/** * {@inheritDoc} */ @Override protected void applyAssignment(AgentConfig agentConfiguration, MethodType methodType, MethodInstrumentationConfig methodInstrumentationConfig) { SpecialInstrumentationPoint specialInstrumentationPoint = getOrCreateSpecialInstrumentationPoint(agentConfiguration, methodType, methodInstrumentationConfig); MethodSensorTypeConfig methodSensorTypeConfig = getSensorTypeConfigFromConfiguration(agentConfiguration); long sensorId = methodSensorTypeConfig.getId(); specialInstrumentationPoint.setSensorId(sensorId); specialInstrumentationPoint.setSubstitutionDescriptor(functionalAssignment.getSpecialMethodSensorConfig().getSubstitutionDescriptor()); }
@Test public void remove() { when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getMethods()).thenReturn(Collections.singleton(methodType)); when(classFilter.matches(assignment, classType, false)).thenReturn(true); when(methodFilter.matches(assignment, methodType)).thenReturn(true); boolean removed = applier.removeInstrumentationPoints(classType); assertThat(removed, is(true)); verify(methodType, times(1)).setMethodInstrumentationConfig(null); }
/** * {@inheritDoc} * <p> * We only support method instrumentation with special assignments for now. No constructors of * any kind. */ @Override protected boolean matches(MethodType methodType) { return MethodType.Character.METHOD.equals(methodType.getMethodCharacter()) && getMethodSensorAssignmentFilter().matches(functionalAssignment, methodType); }
@Test public void doesNotMatchClassFilter() { when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getMethods()).thenReturn(Collections.singleton(methodType)); when(classFilter.matches(assignment, classType, false)).thenReturn(false); when(methodFilter.matches(assignment, methodType)).thenReturn(true); boolean removed = applier.removeInstrumentationPoints(classType); assertThat(removed, is(false)); verifyZeroInteractions(methodType); }
@BeforeMethod public void setup() { applier = new SpecialInstrumentationApplier(assignment, environment, registrationService); applier.assignmentFilterProvider = filterProvider; // filters to true by default when(filterProvider.getClassSensorAssignmentFilter()).thenReturn(classFilter); when(filterProvider.getMethodSensorAssignmentFilter()).thenReturn(methodFilter); when(methodFilter.matches(Matchers.<MethodSensorAssignment> any(), Matchers.<MethodType> any())).thenReturn(true); when(classFilter.matches(Matchers.<AbstractClassSensorAssignment<?>> any(), Matchers.<ClassType> any(), Matchers.eq(false))).thenReturn(true); // class to return one method when(classType.getMethods()).thenReturn(Collections.singleton(methodType)); when(methodType.getMethodCharacter()).thenReturn(Character.METHOD); }
@Test public void doesNotInstrumentConstructor() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(methodType.getMethodCharacter()).thenReturn(Character.CONSTRUCTOR); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(false)); verify(methodType).getMethodCharacter(); verifyNoMoreInteractions(methodType); }
@Test public void doesNotMatchMethodFilter() { when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getMethods()).thenReturn(Collections.singleton(methodType)); when(classFilter.matches(assignment, classType, false)).thenReturn(true); when(methodFilter.matches(assignment, methodType)).thenReturn(false); boolean removed = applier.removeInstrumentationPoints(classType); assertThat(removed, is(false)); verify(methodType).getMethodCharacter(); verifyNoMoreInteractions(methodType); }
@Test public void doesNotMatchMethodFilter() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(methodFilter.matches(assignment, methodType)).thenReturn(false); // test direct and via collection // we expect two calls to everything boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(false)); verify(methodType).getMethodCharacter(); verifyNoMoreInteractions(methodType); }
@Test public void doesNotMatchStaticConstructor() { when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getMethods()).thenReturn(Collections.singleton(methodType)); when(classFilter.matches(assignment, classType, false)).thenReturn(true); when(methodFilter.matches(assignment, methodType)).thenReturn(true); when(methodType.getMethodCharacter()).thenReturn(Character.STATIC_CONSTRUCTOR); boolean removed = applier.removeInstrumentationPoints(classType); assertThat(removed, is(false)); verify(methodType).getMethodCharacter(); verifyNoMoreInteractions(methodType); } }
@Test public void doesNotInstrumentStaticConstructor() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(methodType.getMethodCharacter()).thenReturn(Character.STATIC_CONSTRUCTOR); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(false)); verify(methodType).getMethodCharacter(); verifyNoMoreInteractions(methodType); } }
@Test public void doesNotMatchConstructor() { when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getMethods()).thenReturn(Collections.singleton(methodType)); when(classFilter.matches(assignment, classType, false)).thenReturn(true); when(methodFilter.matches(assignment, methodType)).thenReturn(true); when(methodType.getMethodCharacter()).thenReturn(Character.CONSTRUCTOR); boolean removed = applier.removeInstrumentationPoints(classType); assertThat(removed, is(false)); verify(methodType).getMethodCharacter(); verifyNoMoreInteractions(methodType); }
when(methodType.getModifiers()).thenReturn(mod); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType);