Search in sources :

Example 66 with ConfigurableApplicationContext

use of org.springframework.context.ConfigurableApplicationContext in project spring-framework by spring-projects.

the class MBeanExporterTests method testAutodetectMBeans.

@Test
public void testAutodetectMBeans() throws Exception {
    ConfigurableApplicationContext ctx = load("autodetectMBeans.xml");
    try {
        ctx.getBean("exporter");
        MBeanServer server = ctx.getBean("server", MBeanServer.class);
        ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true"));
        assertNotNull(instance);
        instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean2=true"));
        assertNotNull(instance);
        instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean3=true"));
        assertNotNull(instance);
    } finally {
        ctx.close();
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ObjectInstance(javax.management.ObjectInstance) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 67 with ConfigurableApplicationContext

use of org.springframework.context.ConfigurableApplicationContext in project spring-framework by spring-projects.

the class MBeanExporterTests method testAutodetectLazyMBeans.

@Test
public void testAutodetectLazyMBeans() throws Exception {
    ConfigurableApplicationContext ctx = load("autodetectLazyMBeans.xml");
    try {
        ctx.getBean("exporter");
        MBeanServer server = ctx.getBean("server", MBeanServer.class);
        ObjectName oname = ObjectNameManager.getInstance("spring:mbean=true");
        assertNotNull(server.getObjectInstance(oname));
        String name = (String) server.getAttribute(oname, "Name");
        assertEquals("Invalid name returned", "Rob Harrop", name);
        oname = ObjectNameManager.getInstance("spring:mbean=another");
        assertNotNull(server.getObjectInstance(oname));
        name = (String) server.getAttribute(oname, "Name");
        assertEquals("Invalid name returned", "Juergen Hoeller", name);
    } finally {
        ctx.close();
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 68 with ConfigurableApplicationContext

use of org.springframework.context.ConfigurableApplicationContext in project spring-framework by spring-projects.

the class NotificationPublisherTests method testMBean.

@Test
public void testMBean() throws Exception {
    // start the MBeanExporter
    ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml");
    this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=PublisherMBean"), listener, null, null);
    MyNotificationPublisherMBean publisher = (MyNotificationPublisherMBean) ctx.getBean("publisherMBean");
    publisher.sendNotification();
    assertEquals("Notification not sent", 1, listener.count);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Test(org.junit.Test)

Example 69 with ConfigurableApplicationContext

use of org.springframework.context.ConfigurableApplicationContext in project spring-framework by spring-projects.

the class AnnotationLazyInitMBeanTests method lazyNaming.

@Test
public void lazyNaming() throws Exception {
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/annotation/lazyNaming.xml");
    try {
        MBeanServer server = (MBeanServer) ctx.getBean("server");
        ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4");
        assertNotNull(server.getObjectInstance(oname));
        String name = (String) server.getAttribute(oname, "Name");
        assertEquals("Invalid name returned", "TEST", name);
    } finally {
        ctx.close();
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 70 with ConfigurableApplicationContext

use of org.springframework.context.ConfigurableApplicationContext in project spring-framework by spring-projects.

the class AsyncAnnotationBeanPostProcessorTests method exceptionHandlerThrowsUnexpectedException.

@Test
public void exceptionHandlerThrowsUnexpectedException() {
    Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid");
    TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(true);
    BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
    processorDefinition.getPropertyValues().add("exceptionHandler", exceptionHandler);
    processorDefinition.getPropertyValues().add("executor", new DirectExecutor());
    ConfigurableApplicationContext context = initContext(processorDefinition);
    ITestBean testBean = context.getBean("target", ITestBean.class);
    assertFalse("Handler should not have been called", exceptionHandler.isCalled());
    try {
        testBean.failWithVoid();
        exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
    } catch (Exception e) {
        fail("No unexpected exception should have been received");
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)177 Test (org.junit.Test)110 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)42 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)17 ApplicationContext (org.springframework.context.ApplicationContext)17 SpringApplication (org.springframework.boot.SpringApplication)14 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)13 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)13 DataSource (javax.sql.DataSource)8 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)7 JmsListenerContainerTestFactory (org.springframework.jms.config.JmsListenerContainerTestFactory)7 MBeanServer (javax.management.MBeanServer)6 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)6 Statement (java.sql.Statement)5 ArrayList (java.util.ArrayList)5 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)5 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 GenericXmlApplicationContext (org.springframework.context.support.GenericXmlApplicationContext)5 ObjectName (javax.management.ObjectName)4