@Before public void setUp() { this.context = new AnnotationConfigApplicationContext(Config.class); this.simpleService = this.context.getBean(SimpleService.class); }
private void assertLoadsMyBean(Class<?>... annotatedClasses) { context = new AnnotationConfigApplicationContext(annotatedClasses); assertThat(context.getBean("myBean",String.class), equalTo("myBean")); } }
@Test public void bothSetOnlyResolverIsUsed() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FullCachingConfig.class); CacheInterceptor ci = context.getBean(CacheInterceptor.class); assertSame(context.getBean("cacheResolver"), ci.getCacheResolver()); assertSame(context.getBean("keyGenerator"), ci.getKeyGenerator()); context.close(); }
@Test public void fooServiceWithInterfaceCglib() { this.context = new AnnotationConfigApplicationContext(FooConfigCglib.class); FooService service = this.context.getBean(FooService.class); fooGetSimple(service); }
@Test public void bothSetOnlyResolverIsUsed() { ConfigurableApplicationContext context = new GenericXmlApplicationContext( "/org/springframework/cache/config/annotationDrivenCacheNamespace-manager-resolver.xml"); CacheInterceptor ci = context.getBean(CacheInterceptor.class); assertSame(context.getBean("cacheResolver"), ci.getCacheResolver()); context.close(); }
@Test public void emptyConfigSupport() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class); CacheInterceptor ci = context.getBean(CacheInterceptor.class); assertNotNull(ci.getCacheResolver()); assertEquals(SimpleCacheResolver.class, ci.getCacheResolver().getClass()); assertSame(context.getBean(CacheManager.class), ((SimpleCacheResolver)ci.getCacheResolver()).getCacheManager()); context.close(); }
@Test public void orderedListeners() { load(OrderedTestListener.class); OrderedTestListener listener = this.context.getBean(OrderedTestListener.class); assertTrue(listener.order.isEmpty()); this.context.publishEvent("whatever"); assertThat(listener.order, contains("first", "second", "third")); }
@Test @Ignore // SPR-15122 public void listenersReceiveEarlyEvents() { load(EventOnPostConstruct.class, OrderedTestListener.class); OrderedTestListener listener = this.context.getBean(OrderedTestListener.class); assertThat(listener.order, contains("first", "second", "third")); }
@Test public void invokedAsynchronously() { ConfigurableApplicationContext context = initContext( new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); Thread mainThread = Thread.currentThread(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertNotSame(mainThread, asyncThread); context.close(); }
@Test public void contextAwareSingletonWasCalledBack() throws Exception { ACATester aca = (ACATester) applicationContext.getBean("aca"); assertTrue("has had context set", aca.getApplicationContext() == applicationContext); Object aca2 = applicationContext.getBean("aca"); assertTrue("Same instance", aca == aca2); assertTrue("Says is singleton", applicationContext.isSingleton("aca")); }
@Test // SPR-6354 public void destroyLazyInitSchedulerWithCustomShutdownOrderDoesNotHang() { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("quartzSchedulerLifecycleTests.xml", getClass()); assertNotNull(context.getBean("lazyInitSchedulerWithCustomShutdownOrder")); StopWatch sw = new StopWatch(); sw.start("lazyScheduler"); context.close(); sw.stop(); assertTrue("Quartz Scheduler with lazy-init is hanging on destruction: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 500); }
@Test public void withAnnotationOnArgumentAndCglibProxy() { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext( ConfigWithCglibProxy.class, SampleService.class, LoggingAspect.class); SampleService sampleService = ctx.getBean(SampleService.class); sampleService.execute(new SampleDto()); sampleService.execute(new SampleInputBean()); sampleService.execute((SampleDto) null); sampleService.execute((SampleInputBean) null); }
@Test public void listenerWithResolvableTypeEvent() { load(ResolvableTypeEventListener.class); ResolvableTypeEventListener listener = this.context.getBean(ResolvableTypeEventListener.class); this.eventCollector.assertNoEventReceived(listener); GenericEventPojo<String> event = new GenericEventPojo<>("TEST"); this.context.publishEvent(event); this.eventCollector.assertEvent(listener, event); this.eventCollector.assertTotalEventsCount(1); }
@Test public void testSimpleBean() throws Exception { // start the MBeanExporter ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml"); this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher"), listener, null, null); MyNotificationPublisher publisher = (MyNotificationPublisher) ctx.getBean("publisher"); assertNotNull("NotificationPublisher should not be null", publisher.getNotificationPublisher()); publisher.sendNotification(); assertEquals("Notification not sent", 1, listener.count); }
@Test public void beanAutomaticallyHearsEvents() throws Exception { //String[] listenerNames = ((ListableBeanFactory) applicationContext).getBeanDefinitionNames(ApplicationListener.class); //assertTrue("listeners include beanThatListens", Arrays.asList(listenerNames).contains("beanThatListens")); BeanThatListens b = (BeanThatListens) applicationContext.getBean("beanThatListens"); b.zero(); assertTrue("0 events before publication", b.getEventCount() == 0); this.applicationContext.publishEvent(new MyEvent(this)); assertTrue("1 events after publication, not " + b.getEventCount(), b.getEventCount() == 1); }