use of com.newrelic.agent.instrumentation.ClassTransformerService in project newrelic-java-agent by newrelic.
the class ConfigServiceTest method fileChangeAndThenConnectDoesActuallyChangeConfig.
@Test
public void fileChangeAndThenConnectDoesActuallyChangeConfig() throws IOException {
ServiceManager mockServiceManager = mock(ServiceManager.class);
ClassTransformerService mockClassTransformerService = mock(ClassTransformerService.class);
when(mockServiceManager.getClassTransformerService()).thenReturn(mockClassTransformerService);
ServiceFactory.setServiceManager(mockServiceManager);
String appName = "Unit Test";
Map<String, Object> originalMap = ImmutableMap.<String, Object>of("app_name", appName);
File mockConfigFile = File.createTempFile("ConfigServiceTest", null);
try (OutputStream os = new FileOutputStream(mockConfigFile)) {
os.write(JSONObject.toJSONString(Collections.singletonMap("common", originalMap)).getBytes());
}
assertTrue(mockConfigFile.setLastModified(15L));
AgentConfig originalConfig = AgentConfigImpl.createAgentConfig(originalMap);
final Boolean[] circuitBreakerSetting = new Boolean[] { null };
assertTrue("Default circuitbreaker was expected to be true; it was apparently not.", originalConfig.getCircuitBreakerConfig().isEnabled());
ConfigServiceImpl target = new ConfigServiceImpl(originalConfig, mockConfigFile, originalMap, false);
target.addIAgentConfigListener(new AgentConfigListener() {
@Override
public void configChanged(String appName, AgentConfig agentConfig) {
circuitBreakerSetting[0] = agentConfig.getCircuitBreakerConfig().isEnabled();
}
});
// step 1: modify the file.
try (OutputStream os = new FileOutputStream(mockConfigFile)) {
os.write(JSONObject.toJSONString(Collections.singletonMap("common", ImmutableMap.of("app_name", appName, "circuitbreaker", Collections.singletonMap("enabled", false)))).getBytes());
}
assertTrue("unable to set the last modified time on the mock config file.", mockConfigFile.setLastModified(System.currentTimeMillis()));
target.afterHarvest(appName);
assertNotNull("circuitbreaker setting should have been set; it was not", circuitBreakerSetting[0]);
assertFalse("circuitbreaker setting has not changed from true to false; it should have!", circuitBreakerSetting[0]);
circuitBreakerSetting[0] = null;
// step 2: trigger connect.
IRPMService mockRPMService = mock(IRPMService.class);
when(mockRPMService.getApplicationName()).thenReturn(appName);
target.connected(mockRPMService, Collections.<String, Object>emptyMap());
// this should not have reverted to the original contents.
assertNotNull("circuitbreaker setting should have been set; it was not", circuitBreakerSetting[0]);
assertFalse("circuitbreaker setting has changed from false; it should not have!", circuitBreakerSetting[0]);
}
use of com.newrelic.agent.instrumentation.ClassTransformerService in project newrelic-java-agent by newrelic.
the class ExtensionServiceTest method setUpAgent.
@Before
public void setUpAgent() throws Exception {
Map<String, Object> configMap = new HashMap<>();
configMap.put(AgentConfigImpl.APP_NAME, EXTENSION_NAME);
AgentConfig config = AgentConfigImpl.createAgentConfig(configMap);
configService = ConfigServiceFactory.createConfigService(config, Collections.<String, Object>emptyMap());
serviceManager = new MockServiceManager(configService);
ServiceFactory.setServiceManager(serviceManager);
final InstrumentationProxy instrumentationProxy = Mockito.mock(InstrumentationProxy.class);
serviceManager.setCoreService(new MockCoreService() {
@Override
public InstrumentationProxy getInstrumentation() {
return instrumentationProxy;
}
});
Mockito.when(instrumentationProxy.isRetransformClassesSupported()).thenReturn(true);
Mockito.when(instrumentationProxy.getAllLoadedClasses()).thenReturn(new Class[] {});
extensionService = new ExtensionService(configService, ExtensionsLoadedListener.NOOP);
serviceManager.setExtensionService(extensionService);
serviceManager.setJmxService(Mockito.mock(JmxService.class));
ClassTransformerService classTransformerService = serviceManager.getClassTransformerService();
ClassRetransformer mockRetransformer = Mockito.mock(ClassRetransformer.class);
Mockito.when(classTransformerService.getLocalRetransformer()).thenReturn(mockRetransformer);
InstrumentationContextManager mockContextManager = Mockito.mock(InstrumentationContextManager.class);
Mockito.when(classTransformerService.getContextManager()).thenReturn(mockContextManager);
ClassWeaverService mockWeaverService = Mockito.mock(ClassWeaverService.class);
Mockito.when(mockContextManager.getClassWeaverService()).thenReturn(mockWeaverService);
Mockito.when(mockWeaverService.reloadExternalWeavePackages(Mockito.<File>anyCollection(), Mockito.<File>anyCollection())).thenReturn(new Runnable() {
@Override
public void run() {
}
});
}
use of com.newrelic.agent.instrumentation.ClassTransformerService in project newrelic-java-agent by newrelic.
the class JarExtensionTest method javaagentExtension.
@Test
public void javaagentExtension() throws IOException {
ServiceFactory.setServiceManager(Mockito.mock(ServiceManager.class));
ClassTransformerService classTransformerService = Mockito.mock(ClassTransformerService.class);
Mockito.when(ServiceFactory.getServiceManager().getClassTransformerService()).thenReturn(classTransformerService);
Instrumentation instrumentation = Mockito.mock(Instrumentation.class);
Mockito.when(classTransformerService.getExtensionInstrumentation()).thenReturn(instrumentation);
AgentBridge.agent = Mockito.mock(Agent.class);
Logger logger = Mockito.mock(Logger.class);
Mockito.when(AgentBridge.agent.getLogger()).thenReturn(logger);
JarExtension.create(Mockito.mock(IAgentLogger.class), Mockito.mock(ExtensionParsers.class), createJavaAgentExtension());
// Verify that our premain class was invoked
Mockito.verify(logger, Mockito.times(1)).log(Level.INFO, "My cool extension started! {0}", instrumentation);
}
use of com.newrelic.agent.instrumentation.ClassTransformerService in project newrelic-java-agent by newrelic.
the class SqlTraceServiceTest method createServiceManager.
private MockServiceManager createServiceManager(Map<String, Object> configMap) throws Exception {
AgentConfig config = AgentConfigImpl.createAgentConfig(configMap);
MockServiceManager serviceManager = new MockServiceManager();
ServiceFactory.setServiceManager(serviceManager);
ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap);
serviceManager.setConfigService(configService);
ThreadService threadService = new ThreadService();
serviceManager.setThreadService(threadService);
ClassTransformerService classTransformerService = Mockito.mock(ClassTransformerService.class);
serviceManager.setClassTransformerService(classTransformerService);
HarvestService harvestService = new MockHarvestService();
serviceManager.setHarvestService(harvestService);
TransactionService transactionService = new TransactionService();
serviceManager.setTransactionService(transactionService);
StatsService statsService = new StatsServiceImpl();
serviceManager.setStatsService(statsService);
DatabaseService dbService = new DatabaseService();
serviceManager.setDatabaseService(dbService);
EnvironmentService envService = new EnvironmentServiceImpl();
serviceManager.setEnvironmentService(envService);
SqlTraceService sqlTraceService = new SqlTraceServiceImpl();
serviceManager.setSqlTraceService(sqlTraceService);
MockCoreService agent = new MockCoreService();
serviceManager.setCoreService(agent);
serviceManager.setAttributesService(new AttributesService());
TransactionTraceService transactionTraceService = new TransactionTraceService();
serviceManager.setTransactionTraceService(transactionTraceService);
DistributedTraceServiceImpl distributedTraceService = new DistributedTraceServiceImpl();
serviceManager.setDistributedTraceService(distributedTraceService);
TransactionDataToDistributedTraceIntrinsics transactionDataToDistributedTraceIntrinsics = new TransactionDataToDistributedTraceIntrinsics(distributedTraceService);
serviceManager.setTransactionEventsService(new TransactionEventsService(transactionDataToDistributedTraceIntrinsics));
MockRPMServiceManager rpmServiceManager = new MockRPMServiceManager();
serviceManager.setRPMServiceManager(rpmServiceManager);
MockRPMService rpmService = new MockRPMService();
rpmService.setApplicationName(APP_NAME);
rpmService.setEverConnected(true);
rpmService.setErrorService(new ErrorServiceImpl(APP_NAME));
rpmServiceManager.setRPMService(rpmService);
configService.start();
serviceManager.start();
sqlTraceService.start();
return serviceManager;
}
Aggregations