@Override public InstrumentationDefinition call() throws Exception { ClassType classType = (ClassType) type; boolean added = false; for (IInstrumentationApplier applier : appliers) { added |= applier.addInstrumentationPoints(agentConfiguration, classType); } if (added) { return createInstrumentationResult(type); } else { return null; } } });
@Override public Collection<? extends ImmutableClassType> call() throws Exception { Collection<ImmutableClassType> results = new ArrayList<>(); for (ImmutableType type : types) { // only initialized class types can have instrumentation points if (type.isClass() && type.isInitialized()) { ClassType classType = (ClassType) type.castToClass(); boolean added = false; for (IInstrumentationApplier applier : appliers) { added |= applier.addInstrumentationPoints(agentConfiguration, classType); } if (added) { results.add(type.castToClass()); } } } return results; } });
@Test public void notInstrumented() { when(classType.isInitialized()).thenReturn(true); when(instrumentationApplier.addInstrumentationPoints(agentConfiguration, classType)).thenReturn(false); InstrumentationDefinition result = instrumentation.addAndGetInstrumentationResult(classType, agentConfiguration, Collections.singleton(instrumentationApplier)); assertThat(result, is(nullValue())); }
@Test public void addNothingWhenInstrumenterDoesNotAdd() throws Exception { when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(classType.isInitialized()).thenReturn(true); when(instrumentationApplier.addInstrumentationPoints(agentConfiguration, classType)).thenReturn(false); doReturn(Collections.singleton(classType)).when(lookup).findAll(); Collection<? extends ImmutableClassType> result = instrumentation.addInstrumentationPoints(agentConfiguration, Collections.singleton(instrumentationApplier)); // assert result assertThat((Collection<ClassType>) result, is(empty())); // must be write lock verify(classCache, times(1)).executeWithWriteLock(Matchers.<Callable<?>> any()); verify(instrumentationApplier, times(1)).addInstrumentationPoints(agentConfiguration, classType); verify(instrumentationApplier, times(1)).getSensorAssignment(); verifyNoMoreInteractions(instrumentationApplier); }
@Test public void add() throws Exception { when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(classType.isInitialized()).thenReturn(true); when(instrumentationApplier.addInstrumentationPoints(agentConfiguration, classType)).thenReturn(true); doReturn(Collections.singleton(classType)).when(lookup).findAll(); Collection<? extends ImmutableClassType> result = instrumentation.addInstrumentationPoints(agentConfiguration, Collections.singleton(instrumentationApplier)); // assert result assertThat((Collection<ClassType>) result, hasItem(classType)); // must be write lock verify(classCache, times(1)).executeWithWriteLock(Matchers.<Callable<?>> any()); verify(instrumentationApplier, times(1)).addInstrumentationPoints(agentConfiguration, classType); verify(instrumentationApplier, times(1)).getSensorAssignment(); verifyNoMoreInteractions(instrumentationApplier); }
@Test public void searchNarrowAddNothingWhenInstrumenterDoesNotAdd() throws Exception { when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(classType.isInitialized()).thenReturn(true); when(instrumentationApplier.addInstrumentationPoints(agentConfiguration, classType)).thenReturn(false); doReturn(assignment).when(instrumentationApplier).getSensorAssignment(); doReturn(Collections.singleton(classType)).when(searchNarrower).narrowByClassSensorAssignment(classCache, assignment); Collection<? extends ImmutableClassType> result = instrumentation.addInstrumentationPoints(agentConfiguration, Collections.singleton(instrumentationApplier)); // assert result assertThat((Collection<ClassType>) result, is(empty())); // must be write lock verify(classCache, times(1)).executeWithWriteLock(Matchers.<Callable<?>> any()); verify(instrumentationApplier, times(1)).addInstrumentationPoints(agentConfiguration, classType); verify(instrumentationApplier, times(1)).getSensorAssignment(); verifyNoMoreInteractions(instrumentationApplier); }
@Test public void instrumented() { Collection<MethodInstrumentationConfig> configs = mock(Collection.class); when(classType.isInitialized()).thenReturn(true); when(classType.getFQN()).thenReturn(FQN); when(classType.hasInstrumentationPoints()).thenReturn(true); when(classType.getInstrumentationPoints()).thenReturn(configs); when(instrumentationApplier.addInstrumentationPoints(agentConfiguration, classType)).thenReturn(true); InstrumentationDefinition result = instrumentation.addAndGetInstrumentationResult(classType, agentConfiguration, Collections.singleton(instrumentationApplier)); assertThat(result, is(notNullValue())); assertThat(result.getClassName(), is(FQN)); assertThat(result.getMethodInstrumentationConfigs(), is(configs)); }
@Test public void searchNarrowAdd() throws Exception { when(classType.isClass()).thenReturn(true); when(classType.castToClass()).thenReturn(classType); when(classType.isInitialized()).thenReturn(true); when(instrumentationApplier.addInstrumentationPoints(agentConfiguration, classType)).thenReturn(true); doReturn(assignment).when(instrumentationApplier).getSensorAssignment(); doReturn(Collections.singleton(classType)).when(searchNarrower).narrowByClassSensorAssignment(classCache, assignment); Collection<? extends ImmutableClassType> result = instrumentation.addInstrumentationPoints(agentConfiguration, Collections.singleton(instrumentationApplier)); // assert result assertThat((Collection<ClassType>) result, hasItem(classType)); // must be write lock verify(classCache, times(1)).executeWithWriteLock(Matchers.<Callable<?>> any()); verify(instrumentationApplier, times(1)).addInstrumentationPoints(agentConfiguration, classType); verify(instrumentationApplier, times(1)).getSensorAssignment(); verifyNoMoreInteractions(instrumentationApplier); }