@Test public void searchNarrowAddNothingWhenEmpty() throws Exception { doReturn(assignment).when(instrumentationApplier).getSensorAssignment(); doReturn(Collections.emptyList()).when(searchNarrower).narrowByClassSensorAssignment(classCache, assignment); Collection<? extends ImmutableClassType> result = instrumentation.addInstrumentationPoints(agentConfiguration, Collections.singleton(instrumentationApplier)); // assert result assertThat(result, is(empty())); // not touching the write lock verify(classCache, times(0)).executeWithWriteLock(Matchers.<Callable<?>> any()); verify(instrumentationApplier, times(1)).getSensorAssignment(); verifyNoMoreInteractions(instrumentationApplier); }
@Test public void searchNarrowAddNothingForNonInitializedType() throws Exception { when(classType.isInitialized()).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(result, is(empty())); // must be write lock verify(classCache, times(1)).executeWithWriteLock(Matchers.<Callable<?>> any()); verify(instrumentationApplier, times(1)).getSensorAssignment(); verifyNoMoreInteractions(instrumentationApplier); }
@Test public void byDirectName() { String className = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(className); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(false); ClassType classType = new ClassType(className, "hash", 0); doReturn(Collections.singleton(classType)).when(lookup).findClassTypesByPattern(eq(className), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, hasSize(1)); assertThat(result.iterator().next(), is((ImmutableClassType) classType)); verify(lookup, times(1)).findClassTypesByPattern(className, true); verifyNoMoreInteractions(lookup); }
@Test public void removedAssignmentNoChange() throws RemoteException { Collection<ClassType> types = ImmutableList.of(classTypeOne, classTypeTwo); doReturn(instrumentationApplier).when(configurationResolver).getInstrumentationApplier(sensorAssignment, environment); doReturn(types).when(classCacheSearchNarrower).narrowByClassSensorAssignment(classCache, sensorAssignment); doReturn(Collections.emptyList()).when(instrumentationService).removeInstrumentationPoints(eq(types), Matchers.<Collection<IInstrumentationApplier>> any()); doReturn(Collections.singleton(sensorAssignment)).when(event).getRemovedSensorAssignments(); job.setProfileUpdateEvent(event); job.run(); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService, times(1)).removeInstrumentationPoints(eq(types), captor.capture()); assertThat((Collection<IInstrumentationApplier>) captor.getValue(), hasSize(1)); assertThat(((Collection<IInstrumentationApplier>) captor.getValue()).iterator().next(), is(instrumentationApplier)); verifyNoMoreInteractions(instrumentationService); verifyZeroInteractions(environment, eventPublisher); } }
@Test public void removedAssignmentNoChange() { Collection<ClassType> types = Collections.singleton(classType); doReturn(instrumentationApplier).when(configurationResolver).getInstrumentationApplier(sensorAssignment, environment); doReturn(types).when(classCacheSearchNarrower).narrowByClassSensorAssignment(classCache, sensorAssignment); doReturn(Collections.emptyList()).when(instrumentationService).removeInstrumentationPoints(eq(types), Matchers.<Collection<IInstrumentationApplier>> any()); doReturn(Collections.singletonList(sensorAssignment)).when(event).getRemovedSensorAssignments(functionalAssignmentFactory); job.setEnvironmentUpdateEvent(event); job.run(); verify(configurationHolder, times(1)).update(updateEnvironment, PLATFORM_ID); ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class); verify(instrumentationService, times(1)).removeInstrumentationPoints(types, Collections.singleton(instrumentationApplier)); verifyNoMoreInteractions(instrumentationService); verifyZeroInteractions(environment, agentConfiguration, eventPublisher); } }
@Test public void searchNarrowAddNothingForNonClassTypes() throws Exception { AnnotationType annotationType = new AnnotationType(""); InterfaceType interfaceType = new InterfaceType(""); List<Type> types = new ArrayList<>(); types.add(annotationType); types.add(interfaceType); doReturn(assignment).when(instrumentationApplier).getSensorAssignment(); doReturn(types).when(searchNarrower).narrowByClassSensorAssignment(classCache, assignment); Collection<? extends ImmutableClassType> result = instrumentation.addInstrumentationPoints(agentConfiguration, Collections.singleton(instrumentationApplier)); // assert result assertThat(result, is(empty())); // must be write lock verify(classCache, times(1)).executeWithWriteLock(Matchers.<Callable<?>> any()); verify(instrumentationApplier, times(1)).getSensorAssignment(); verifyNoMoreInteractions(instrumentationApplier); } }
@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); }
@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 initializedBySuperClass() { String superClassName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(superClassName); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(true); ClassType superClassType = new ClassType(superClassName); ClassType classType = new ClassType("initialized", "hash", 0); classType.addSuperClass(superClassType); doReturn(Collections.singleton(superClassType)).when(lookup).findClassTypesByPattern(eq(superClassName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, hasSize(1)); assertThat(result.iterator().next(), is((ImmutableClassType) classType)); verify(lookup, times(1)).findClassTypesByPattern(superClassName, false); verifyNoMoreInteractions(lookup); }
@Test public void initializedByInterface() { String interfaceName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(interfaceName); when(methodSensorAssignment.isInterf()).thenReturn(true); when(methodSensorAssignment.isSuperclass()).thenReturn(false); InterfaceType interfaceType = new InterfaceType(interfaceName); ClassType classType = new ClassType("initialized", "hash", 0); classType.addInterface(interfaceType); doReturn(Collections.singleton(interfaceType)).when(lookup).findInterfaceTypesByPattern(eq(interfaceName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, hasSize(1)); assertThat(result.iterator().next(), is((ImmutableClassType) classType)); verify(lookup, times(1)).findInterfaceTypesByPattern(interfaceName, false); verifyNoMoreInteractions(lookup); }
@Test public void nonInitializedBySuperClass() { String superClassName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(superClassName); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(true); ClassType superClassType = new ClassType(superClassName); ClassType classType = new ClassType("non-initialized"); classType.addSuperClass(superClassType); doReturn(Collections.singleton(superClassType)).when(lookup).findClassTypesByPattern(eq(superClassName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, is(empty())); verify(lookup, times(1)).findClassTypesByPattern(superClassName, false); verifyNoMoreInteractions(lookup); }
@Test public void initializedByAnnotation() { String className = "*"; String annotationName = "info.novatec.MyAnnotation"; when(methodSensorAssignment.getClassName()).thenReturn(className); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(false); when(methodSensorAssignment.getAnnotation()).thenReturn(annotationName); AnnotationType annotationType = new AnnotationType(annotationName); ClassType classType = new ClassType("initialized", "hash", 0); classType.addAnnotation(annotationType); doReturn(Collections.singleton(annotationType)).when(lookup).findAnnotationTypesByPattern(eq(annotationName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, hasSize(1)); assertThat(result.iterator().next(), is((ImmutableClassType) classType)); verify(lookup, times(1)).findAnnotationTypesByPattern(annotationName, false); verifyNoMoreInteractions(lookup); }
@Test public void nonInitializedByInterface() { String interfaceName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(interfaceName); when(methodSensorAssignment.isInterf()).thenReturn(true); when(methodSensorAssignment.isSuperclass()).thenReturn(false); InterfaceType interfaceType = new InterfaceType(interfaceName); ClassType classType = new ClassType("not-initialized"); classType.addInterface(interfaceType); doReturn(Collections.singleton(interfaceType)).when(lookup).findInterfaceTypesByPattern(eq(interfaceName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, is(empty())); verify(lookup, times(1)).findInterfaceTypesByPattern(interfaceName, false); verifyNoMoreInteractions(lookup); }
@Test public void nonInitializedByAnnotation() { String className = "*"; String annotationName = "info.novatec.MyAnnotation"; when(methodSensorAssignment.getClassName()).thenReturn(className); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(false); when(methodSensorAssignment.getAnnotation()).thenReturn(annotationName); AnnotationType annotationType = new AnnotationType(annotationName); ClassType classType = new ClassType("non-initialized"); classType.addAnnotation(annotationType); doReturn(Collections.singleton(annotationType)).when(lookup).findAnnotationTypesByPattern(eq(annotationName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, is(empty())); verify(lookup, times(1)).findAnnotationTypesByPattern(annotationName, false); verifyNoMoreInteractions(lookup); }
@Test public void initializedBySuperClassIndirect() { String superClassName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(superClassName); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(true); ClassType superClassType = new ClassType(superClassName); ClassType indirectSuperClassType = new ClassType("indirectSuperClassType"); indirectSuperClassType.addSuperClass(superClassType); ClassType classType = new ClassType("initialized", "hash", 0); classType.addSuperClass(indirectSuperClassType); doReturn(Collections.singleton(superClassType)).when(lookup).findClassTypesByPattern(eq(superClassName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, hasSize(1)); assertThat(result.iterator().next(), is((ImmutableClassType) classType)); verify(lookup, times(1)).findClassTypesByPattern(superClassName, false); verifyNoMoreInteractions(lookup); }
@Test public void initializedByInterfaceIndirect() { String interfaceName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(interfaceName); when(methodSensorAssignment.isInterf()).thenReturn(true); when(methodSensorAssignment.isSuperclass()).thenReturn(false); InterfaceType interfaceType = new InterfaceType(interfaceName); InterfaceType indirectInterfaceType = new InterfaceType("indirectInterfaceType"); indirectInterfaceType.addSuperInterface(interfaceType); ClassType classType = new ClassType("initialized", "hash", 0); classType.addInterface(indirectInterfaceType); doReturn(Collections.singleton(interfaceType)).when(lookup).findInterfaceTypesByPattern(eq(interfaceName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, hasSize(1)); assertThat(result.iterator().next(), is((ImmutableClassType) classType)); verify(lookup, times(1)).findInterfaceTypesByPattern(interfaceName, false); verifyNoMoreInteractions(lookup); }
@Test public void nonInitializedByInterfaceIndirect() { String interfaceName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(interfaceName); when(methodSensorAssignment.isInterf()).thenReturn(true); when(methodSensorAssignment.isSuperclass()).thenReturn(false); InterfaceType interfaceType = new InterfaceType(interfaceName); InterfaceType indirectInterfaceType = new InterfaceType("indirectInterfaceType"); indirectInterfaceType.addSuperInterface(interfaceType); ClassType classType = new ClassType("non-initialized"); classType.addInterface(indirectInterfaceType); doReturn(Collections.singleton(interfaceType)).when(lookup).findInterfaceTypesByPattern(eq(interfaceName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, is(empty())); verify(lookup, times(1)).findInterfaceTypesByPattern(interfaceName, false); verifyNoMoreInteractions(lookup); }
@Test public void nonInitializedBySuperClassIndirect() { String superClassName = "info.novatec.MyClass"; when(methodSensorAssignment.getClassName()).thenReturn(superClassName); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(true); ClassType superClassType = new ClassType(superClassName); ClassType indirectSuperClassType = new ClassType("indirectSuperClassType"); indirectSuperClassType.addSuperClass(superClassType); ClassType classType = new ClassType("non-initialized"); classType.addSuperClass(indirectSuperClassType); doReturn(Collections.singleton(superClassType)).when(lookup).findClassTypesByPattern(eq(superClassName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, is(empty())); verify(lookup, times(1)).findClassTypesByPattern(superClassName, false); verifyNoMoreInteractions(lookup); }
@Test public void nonInitializedByMethodAnnotation() { String className = "*"; String annotationName = "info.novatec.MyAnnotation"; when(methodSensorAssignment.getClassName()).thenReturn(className); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(false); when(methodSensorAssignment.getAnnotation()).thenReturn(annotationName); AnnotationType annotationType = new AnnotationType(annotationName); MethodType methodType = new MethodType(); methodType.addAnnotation(annotationType); ClassType classType = new ClassType("non-initialized"); classType.addMethod(methodType); doReturn(Collections.singleton(annotationType)).when(lookup).findAnnotationTypesByPattern(eq(annotationName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, is(empty())); verify(lookup, times(1)).findAnnotationTypesByPattern(annotationName, false); verifyNoMoreInteractions(lookup); } }
@Test public void nonInitializedByInterfaceAnnotation() { String className = "*"; String annotationName = "info.novatec.MyAnnotation"; when(methodSensorAssignment.getClassName()).thenReturn(className); when(methodSensorAssignment.isInterf()).thenReturn(false); when(methodSensorAssignment.isSuperclass()).thenReturn(false); when(methodSensorAssignment.getAnnotation()).thenReturn(annotationName); AnnotationType annotationType = new AnnotationType(annotationName); InterfaceType interfaceType = new InterfaceType("interface"); interfaceType.addAnnotation(annotationType); ClassType classType = new ClassType("non-initialized"); classType.addInterface(interfaceType); doReturn(Collections.singleton(annotationType)).when(lookup).findAnnotationTypesByPattern(eq(annotationName), anyBoolean()); Collection<? extends ImmutableClassType> result = narrower.narrowByClassSensorAssignment(classCache, methodSensorAssignment); assertThat(result, is(empty())); verify(lookup, times(1)).findAnnotationTypesByPattern(annotationName, false); verifyNoMoreInteractions(lookup); }