@Test public void testEndpointMBeanExporterWithProperties() throws IntrospectionException, InstanceNotFoundException, MalformedObjectNameException, ReflectionException { MockEnvironment environment = new MockEnvironment(); environment.setProperty("endpoints.jmx.domain", "test-domain"); environment.setProperty("endpoints.jmx.unique_names", "true"); environment.setProperty("endpoints.jmx.static_names", "key1=value1, key2=value2"); this.context = new AnnotationConfigApplicationContext(); this.context.setEnvironment(environment); this.context.register(JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class); this.context.refresh(); this.context.getBean(EndpointMBeanExporter.class); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertThat(mbeanExporter.getServer().getMBeanInfo(ObjectNameManager.getInstance( getObjectName("test-domain", "healthEndpoint", this.context).toString() + ",key1=value1,key2=value2"))).isNotNull(); }
@Test public void testEndpointMBeanExporterWithProperties() throws IntrospectionException, InstanceNotFoundException, MalformedObjectNameException, ReflectionException { MockEnvironment environment = new MockEnvironment(); environment.setProperty("endpoints.jmx.domain", "test-domain"); environment.setProperty("endpoints.jmx.unique_names", "true"); environment.setProperty("endpoints.jmx.static_names", "key1=value1, key2=value2"); this.context = new AnnotationConfigApplicationContext(); this.context.setEnvironment(environment); this.context.register(JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class); this.context.refresh(); this.context.getBean(EndpointMBeanExporter.class); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertNotNull(mbeanExporter.getServer() .getMBeanInfo(ObjectNameManager.getInstance( getObjectName("test-domain", "healthEndpoint", this.context) .toString() + ",key1=value1,key2=value2"))); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { Class[] loggers = {SpringApplication.class, /*App.class,*/ ConfigFileApplicationListener.class, EndpointMBeanExporter.class, AutoConfigurationReportLoggingInitializer.class}; Logger log = (Logger) LoggerFactory.getLogger("ROOT"); log.setLevel(Level.ERROR); for (Class logger : loggers) { log = (Logger) LoggerFactory.getLogger(logger); log.setLevel(Level.ERROR); } }
@Bean public EndpointMBeanExporter endpointMBeanExporter(MBeanServer server) { EndpointMBeanExporter mbeanExporter = new EndpointMBeanExporter( this.objectMapper); String domain = this.properties.getDomain(); if (StringUtils.hasText(domain)) { mbeanExporter.setDomain(domain); } mbeanExporter.setServer(server); mbeanExporter.setEnsureUniqueRuntimeObjectNames(this.properties.isUniqueNames()); mbeanExporter.setObjectNameStaticProperties(this.properties.getStaticNames()); return mbeanExporter; }
@Test public void testEndpointMBeanExporterIsInstalled() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(EndpointMBeanExporter.class)).isNotNull(); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertThat(mbeanExporter.getServer() .queryNames(getObjectName("*", "*,*", this.context), null)).isNotEmpty(); }
@Test public void testEndpointMBeanExporterIsNotInstalledIfManagedResource() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, JmxAutoConfiguration.class, ManagedEndpoint.class, EndpointMBeanExportAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(EndpointMBeanExporter.class)).isNotNull(); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertThat(mbeanExporter.getServer() .queryNames(getObjectName("*", "*,*", this.context), null)).isEmpty(); }
@Test public void testEndpointMBeanExporterIsNotInstalledIfNestedInManagedResource() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, JmxAutoConfiguration.class, NestedInManagedEndpoint.class, EndpointMBeanExportAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(EndpointMBeanExporter.class)).isNotNull(); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertThat(mbeanExporter.getServer() .queryNames(getObjectName("*", "*,*", this.context), null)).isEmpty(); }
@Test(expected = NoSuchBeanDefinitionException.class) public void testEndpointMBeanExporterIsNotInstalled() { MockEnvironment environment = new MockEnvironment(); environment.setProperty("endpoints.jmx.enabled", "false"); this.context = new AnnotationConfigApplicationContext(); this.context.setEnvironment(environment); this.context.register(JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class); this.context.refresh(); this.context.getBean(EndpointMBeanExporter.class); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { Class[] loggers = { SpringApplication.class, App.class, ConfigFileApplicationListener.class, EndpointMBeanExporter.class, AutoConfigurationReportLoggingInitializer.class }; Logger log = (Logger) LoggerFactory.getLogger("ROOT"); log.setLevel(Level.ERROR); for (Class logger : loggers) { log = (Logger) LoggerFactory.getLogger(logger); log.setLevel(Level.ERROR); } }
@Test public void testEndpointMBeanExporterIsInstalled() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(EndpointMBeanExporter.class)); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertFalse(mbeanExporter.getServer() .queryNames(getObjectName("*", "*,*", this.context), null).isEmpty()); }
@Test public void testEndpointMBeanExporterIsNotInstalledIfManagedResource() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, JmxAutoConfiguration.class, ManagedEndpoint.class, EndpointMBeanExportAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(EndpointMBeanExporter.class)); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertTrue(mbeanExporter.getServer() .queryNames(getObjectName("*", "*,*", this.context), null).isEmpty()); }
@Test public void testEndpointMBeanExporterIsNotInstalledIfNestedInManagedResource() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, JmxAutoConfiguration.class, NestedInManagedEndpoint.class, EndpointMBeanExportAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(EndpointMBeanExporter.class)); MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class); assertTrue(mbeanExporter.getServer() .queryNames(getObjectName("*", "*,*", this.context), null).isEmpty()); }
@Test(expected = NoSuchBeanDefinitionException.class) public void testEndpointMBeanExporterIsNotInstalled() { MockEnvironment environment = new MockEnvironment(); environment.setProperty("endpoints.jmx.enabled", "false"); this.context = new AnnotationConfigApplicationContext(); this.context.setEnvironment(environment); this.context.register(JmxAutoConfiguration.class, EndpointAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class); this.context.refresh(); this.context.getBean(EndpointMBeanExporter.class); fail(); }