/** * {@inheritDoc} */ @Override protected boolean matches(MethodType methodType) { return getMethodSensorAssignmentFilter().matches(methodSensorAssignment, methodType); }
@BeforeMethod public void init() { filter = new MethodSensorAssignmentFilter(); }
/** * Checks if the {@link MethodType} matches the the {@link MethodSensorAssignment}. * * @param methodSensorAssignment * {@link MethodSensorAssignment}. * @param methodType * method type to check * @return <code>true</code> if the method matches the {@link MethodSensorAssignment} */ public boolean matches(MethodSensorAssignment methodSensorAssignment, ImmutableMethodType methodType) { if (!matchesMethodName(methodSensorAssignment, methodType)) { return false; } if (!matchesParameters(methodSensorAssignment, methodType)) { return false; } if (!matchesModifiers(methodSensorAssignment, methodType)) { return false; } if (!matchesAnnotation(methodSensorAssignment, methodType)) { return false; } return true; }
/** * Checks if the annotation matches the {@link MethodSensorAssignment}. * * @param methodSensorAssignment * {@link MethodSensorAssignment}. * @param methodType * method type to check * @return <code>true</code> if the method parameters matches the {@link MethodSensorAssignment} */ private boolean matchesAnnotation(MethodSensorAssignment methodSensorAssignment, ImmutableMethodType methodType) { // first check if annotation is defined at all if (StringUtils.isEmpty(methodSensorAssignment.getAnnotation())) { return true; } ImmutableTypeWithMethods type = methodType.getImmutableClassOrInterfaceType(); // Safety check for class type // it has to be class type if (!type.isClass()) { return false; } // check if class matches, if yes, then all methods match as well boolean classMatches = super.matchesAnnotation(methodSensorAssignment, type.castToClass()); if (classMatches) { return true; } IMatchPattern pattern = PatternFactory.getPattern(methodSensorAssignment.getAnnotation()); // if not check if the method has annotation return checkAnnotations(methodType.getImmutableAnnotations(), pattern); }
/** * {@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 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)); verifyZeroInteractions(registrationService, methodType); }
@Test public void constructorNotMatchingAssignment() { when(assignment.isConstructor()).thenReturn(false); when(methodType.getMethodCharacter()).thenReturn(Character.CONSTRUCTOR); boolean matches = filter.matches(assignment, methodType); assertThat(matches, is(false)); }
@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 constructorNotMatchingAssignmentWildcard() { when(assignment.getMethodName()).thenReturn("*"); when(assignment.isConstructor()).thenReturn(false); when(methodType.getMethodCharacter()).thenReturn(Character.CONSTRUCTOR); boolean matches = filter.matches(assignment, methodType); assertThat(matches, is(false)); }
@BeforeMethod public void setup() { applier = new MethodSensorInstrumentationApplier(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)); }
@BeforeMethod public void setup() { applier = new InvocationStartMethodSensorInstrumentationApplier(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)); }
@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); }
@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); }
@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)); verifyZeroInteractions(methodType); } }
@BeforeMethod public void setup() { applier = new TimerMethodSensorInstrumentationApplier(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)); }
@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); }
@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); }
@Test public void constructorNotMatchingMethodType() { // default when(assignment.isPublicModifier()).thenReturn(true); when(methodType.getModifiers()).thenReturn(Modifiers.getModifiers(Modifier.PUBLIC)); // specific when(assignment.isConstructor()).thenReturn(true); when(methodType.getMethodCharacter()).thenReturn(Character.METHOD); boolean matches = filter.matches(assignment, methodType); assertThat(matches, is(false)); }
@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 base() { // default when(assignment.isPublicModifier()).thenReturn(true); when(methodType.getModifiers()).thenReturn(Modifiers.getModifiers(Modifier.PUBLIC)); when(methodType.getMethodCharacter()).thenReturn(Character.METHOD); boolean result = filter.matches(assignment, methodType); assertThat(result, is(true)); }