@Test public void notSent() throws Exception { helper.afterPropertiesSet(); String fqn = "fqn"; String hash = "hash"; boolean sent = helper.isSent(fqn, hash); assertThat(sent, is(false)); }
@Test public void load() throws Exception { String fqn = "fqn"; String hash = "hash"; when(configurationStorage.isClassCacheExistsOnCmr()).thenReturn(true); new File(TEST_CACHE_FILE).createNewFile(); Object javaRuntimeVersion = UnderlyingSystemInfo.JAVA_RUNTIME_VERSION; Object hashes = Collections.singletonMap(fqn, Collections.singleton(hash)); when(serializationManager.deserialize(Matchers.<Input> any())).thenReturn(javaRuntimeVersion).thenReturn(hashes); helper.afterPropertiesSet(); boolean sent = helper.isSent(fqn, hash); assertThat(sent, is(true)); } }
@Test public void register() throws Exception { helper.afterPropertiesSet(); String fqn = "fqn"; String hash = "hash"; helper.registerSent(fqn, hash); boolean sent = helper.isSent(fqn, hash); assertThat(sent, is(true)); }
@Test public void initialInstrumentationPoints() throws Exception { String fqn = "fqn"; String hash = "hash"; InstrumentationDefinition instrumentationResult = mock(InstrumentationDefinition.class); Map<Collection<String>, InstrumentationDefinition> initInstrumentations = Collections.<Collection<String>, InstrumentationDefinition> singletonMap(Collections.singleton(hash), instrumentationResult); when(instrumentationResult.getClassName()).thenReturn(fqn); when(configurationStorage.getInitialInstrumentationResults()).thenReturn(initInstrumentations); helper.afterPropertiesSet(); assertThat(helper.isEmpty(), is(false)); assertThat(helper.isSent(fqn, hash), is(true)); assertThat(helper.getInstrumentationDefinition(fqn), is(instrumentationResult)); }
@Test public void noInstrumentationConnectionOffline() throws Exception { String className = TestClass.class.getName(); ClassLoader classLoader = TestClass.class.getClassLoader(); byte[] byteCode = getByteCode(className); ArgumentCaptor<String> fqnCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(connection.isConnected()).thenReturn(false); byte[] instrumentedByteCode = byteCodeAnalyzer.analyzeAndInstrument(byteCode, className, classLoader); // as no instrumentation happened, we get a null object assertThat(instrumentedByteCode, is(nullValue())); verify(connection, times(1)).isConnected(); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verifyZeroInteractions(hookDispatcherMapper); verifyNoMoreInteractions(connection, classHashHelper); }
@Test public void notToBeSentNoInstrumentation() throws IOException { String className = TestClass.class.getName(); ClassLoader classLoader = TestClass.class.getClassLoader(); byte[] byteCode = getByteCode(className); ArgumentCaptor<String> fqnCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(true); when(classHashHelper.getInstrumentationDefinition(fqnCaptor.capture())).thenReturn(null); byte[] instrumentedByteCode = byteCodeAnalyzer.analyzeAndInstrument(byteCode, className, classLoader); // we did not send the class type assertThat(instrumentedByteCode, is(nullValue())); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); // but we asked for the instrumentation result verify(classHashHelper, times(1)).getInstrumentationDefinition(fqnCaptor.getValue()); verifyNoMoreInteractions(classHashHelper); verifyZeroInteractions(platformManager, connection, hookDispatcherMapper); }
@Test public void analyzeInterruptedException() throws Exception { String className = TestClass.class.getName(); ClassLoader classLoader = TestClass.class.getClassLoader(); byte[] byteCode = getByteCode(className); ArgumentCaptor<String> fqnCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); doAnswer(new Answer<Future<Object>>() { @Override public Future<Object> answer(InvocationOnMock invocation) throws Throwable { when(future.get(anyLong(), Matchers.<TimeUnit> any())).thenThrow(InterruptedException.class); return future; } }).when(executorService).submit(Matchers.<Callable> any()); byte[] instrumentedByteCode = byteCodeAnalyzer.analyzeAndInstrument(byteCode, className, classLoader); // as no instrumentation happened, we get a null object assertThat(instrumentedByteCode, is(nullValue())); // assert we kept the interrupted exception assertThat(Thread.interrupted(), is(true)); verify(connection, times(1)).isConnected(); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verifyZeroInteractions(hookDispatcherMapper); verifyNoMoreInteractions(connection, classHashHelper); }
@Test public void analyzeTimeout() throws Exception { String className = TestClass.class.getName(); ClassLoader classLoader = TestClass.class.getClassLoader(); byte[] byteCode = getByteCode(className); ArgumentCaptor<String> fqnCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); doAnswer(new Answer<Future<Object>>() { @Override public Future<Object> answer(InvocationOnMock invocation) throws Throwable { when(future.get(anyLong(), Matchers.<TimeUnit> any())).thenThrow(TimeoutException.class); return future; } }).when(executorService).submit(Matchers.<Callable> any()); byte[] instrumentedByteCode = byteCodeAnalyzer.analyzeAndInstrument(byteCode, className, classLoader); // as no instrumentation happened, we get a null object assertThat(instrumentedByteCode, is(nullValue())); verify(connection, times(1)).isConnected(); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verifyZeroInteractions(hookDispatcherMapper); verifyNoMoreInteractions(connection, classHashHelper); }
@Test public void noInstrumentationResult() throws Exception { String className = TestClass.class.getName(); ClassLoader classLoader = TestClass.class.getClassLoader(); byte[] byteCode = getByteCode(className); ArgumentCaptor<String> fqnCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<ClassType> classCaptor = ArgumentCaptor.forClass(ClassType.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); when(connection.analyze(eq(platformId.longValue()), anyString(), classCaptor.capture())).thenReturn(null); byte[] instrumentedByteCode = byteCodeAnalyzer.analyzeAndInstrument(byteCode, className, classLoader); // as no instrumentation happened, we get a null object assertThat(instrumentedByteCode, is(nullValue())); verify(connection, times(2)).isConnected(); verify(connection, times(1)).analyze(platformId.longValue(), hashCaptor.getValue(), classCaptor.getValue()); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).registerSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerInstrumentationDefinition(fqnCaptor.getValue(), null); verifyZeroInteractions(hookDispatcherMapper); verifyNoMoreInteractions(connection, classHashHelper); }
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<ClassType> classCaptor = ArgumentCaptor.forClass(ClassType.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); verify(connection, times(1)).analyze(platformId.longValue(), hashCaptor.getValue(), classCaptor.getValue()); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).registerSent(fqnCaptor.getValue(), hashCaptor.getValue());
if (classHashHelper.isSent(className, hash)) {
when(classHashHelper.isSent(anyString(), anyString())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(classHashHelper.isAnalyzed(AbstractSubTest.class.getName())).thenReturn(true);
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<ClassType> classCaptor = ArgumentCaptor.forClass(ClassType.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); assertThat((Map<Long, long[]>) captor.getValue(), hasEntry(sscId, new long[] { sensorId })); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).registerSent(fqnCaptor.getValue(), hashCaptor.getValue());
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<ClassType> classCaptor = ArgumentCaptor.forClass(ClassType.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); assertThat((Map<Long, long[]>) captor.getValue(), hasEntry(rscId, sensorIds)); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).registerSent(fqnCaptor.getValue(), hashCaptor.getValue());
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<ClassType> classCaptor = ArgumentCaptor.forClass(ClassType.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); assertThat((Map<Long, long[]>) captor.getValue(), hasEntry(rscId, sensorIds)); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).registerSent(fqnCaptor.getValue(), hashCaptor.getValue());
when(classHashHelper.isSent(anyString(), anyString())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(classHashHelper.isAnalyzed(AbstractSubTest.class.getName())).thenReturn(false); verify(classHashHelper, times(1)).isSent(eq(TestClass.class.getName()), anyString()); verify(classHashHelper, times(1)).isSent(eq(AbstractSubTest.class.getName()), anyString()); verify(classHashHelper, times(1)).registerAnalyzed(TestClass.class.getName()); verify(classHashHelper, times(1)).registerAnalyzed(AbstractSubTest.class.getName());
when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(true); when(classHashHelper.getInstrumentationDefinition(fqnCaptor.capture())).thenReturn(instrumentationResult); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue());
ArgumentCaptor<String> hashCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<ClassType> classCaptor = ArgumentCaptor.forClass(ClassType.class); when(classHashHelper.isSent(fqnCaptor.capture(), hashCaptor.capture())).thenReturn(false); when(classHashHelper.isAnalyzed(anyString())).thenReturn(true); when(connection.isConnected()).thenReturn(true); assertThat((Map<Long, long[]>) captor.getValue(), hasEntry(rscId, sensorIds)); verify(classHashHelper, atLeastOnce()).isAnalyzed(anyString()); verify(classHashHelper, times(1)).isSent(fqnCaptor.getValue(), hashCaptor.getValue()); verify(classHashHelper, times(1)).registerAnalyzed(fqnCaptor.getValue()); verify(classHashHelper, times(1)).registerSent(fqnCaptor.getValue(), hashCaptor.getValue());