@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholders() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); context.getBeanFactory().initializeBean(formattingService, "formattingService"); formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
public class WebContextTestExecutionListener extends AbstractTestExecutionListener { @Override public void prepareTestInstance(TestContext testContext) { if (testContext.getApplicationContext() instanceof GenericApplicationContext) { GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new SimpleThreadScope()); beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SimpleThreadScope()); } } }
@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholdersAndFactoryBean() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.registerBeanDefinition("formattingService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class)); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); formattingService = context.getBean("formattingService", FormattingConversionService.class); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
@Before public void setUp() throws Exception { GenericApplicationContext ctx = new GenericApplicationContext(); new XmlBeanDefinitionReader(ctx).loadBeanDefinitions( new ClassPathResource("jeeNamespaceHandlerTests.xml", getClass())); ctx.refresh(); this.beanFactory = ctx.getBeanFactory(); this.beanFactory.getBeanNamesForType(ITestBean.class); }
GenericConversionService cs = new GenericConversionService(); cs.addConverter(String.class, String.class, String::trim); ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs); RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
@Test public void resolveOptionalHeaderAsEmpty() throws Exception { GenericApplicationContext cxt = new GenericApplicationContext(); cxt.refresh(); HeaderMethodArgumentResolver resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory()); Message<String> message = MessageBuilder.withPayload("foo").build(); Object result = resolver.resolveArgument(paramOptional, message); assertEquals(Optional.empty(), result); }
@Test public void testScopedOverride() throws Exception { GenericApplicationContext ctx = new GenericApplicationContext(); new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT); SimpleMapScope scope = new SimpleMapScope(); ctx.getBeanFactory().registerScope("request", scope); ctx.refresh(); ITestBean bean = (ITestBean) ctx.getBean("testBean"); assertEquals("male", bean.getName()); assertEquals(99, bean.getAge()); assertTrue(scope.getMap().containsKey("scopedTarget.testBean")); assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass()); }
@Test public void resolveOptionalHeaderWithValue() throws Exception { GenericApplicationContext cxt = new GenericApplicationContext(); cxt.refresh(); HeaderMethodArgumentResolver resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory()); Message<String> message = MessageBuilder.withPayload("foo").setHeader("foo", "bar").build(); Object result = resolver.resolveArgument(paramOptional, message); assertEquals(Optional.of("bar"), result); }
@Test public void individualBeanWithSupplier() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class))); context.registerBean(BeanB.class, BeanB::new); context.registerBean(BeanC.class, BeanC::new); context.refresh(); assertTrue(context.getBeanFactory().containsSingleton(BeanA.class.getName())); assertSame(context.getBean(BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean(BeanC.class), context.getBean(BeanA.class).c); assertSame(context, context.getBean(BeanB.class).applicationContext); assertArrayEquals(new String[] {BeanA.class.getName()}, context.getDefaultListableBeanFactory().getDependentBeans(BeanB.class.getName())); assertArrayEquals(new String[] {BeanA.class.getName()}, context.getDefaultListableBeanFactory().getDependentBeans(BeanC.class.getName())); }
@Test public void testContextRefreshedEventStartsTheConnectionByDefault() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); // and the connection is start()ed after the listener is registered... ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.afterPropertiesSet(); GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerSingleton("messageListenerContainer", this.container); context.refresh(); verify(connection).setExceptionListener(this.container); verify(connection).start(); }
@Before public void setup() { @SuppressWarnings("resource") GenericApplicationContext cxt = new GenericApplicationContext(); cxt.refresh(); this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory()); Method method = ReflectionUtils.findMethod(getClass(), "handleMessage", (Class<?>[]) null); this.paramRequired = new SynthesizingMethodParameter(method, 0); this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1); this.paramSystemPropertyDefaultValue = new SynthesizingMethodParameter(method, 2); this.paramSystemPropertyName = new SynthesizingMethodParameter(method, 3); this.paramNotAnnotated = new SynthesizingMethodParameter(method, 4); this.paramOptional = new SynthesizingMethodParameter(method, 5); this.paramNativeHeader = new SynthesizingMethodParameter(method, 6); this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer()); GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class); }
@Test public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.setAutoStartup(false); this.container.afterPropertiesSet(); GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerSingleton("messageListenerContainer", this.container); context.refresh(); verify(connection).setExceptionListener(this.container); }
@Test public void getWithTransactionManager() throws Exception { try (GenericApplicationContext context = new GenericApplicationContext()) { context.getBeanFactory().registerScope("tx", new SimpleTransactionScope()); context.getBeanFactory().destroyScopedBean("txScopedObject2"); assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2")); assertTrue(bean2.wasDestroyed()); assertSame(bean2a, context.getBean(DerivedTestBean.class)); assertNotSame(bean2, bean2a); context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2"); assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2")); assertFalse(bean2a.wasDestroyed());
@Test public void testSimpleScanWithDefaultFiltersAndPrimaryLazyBean() { GenericApplicationContext context = new GenericApplicationContext(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); scanner.scan(BASE_PACKAGE); scanner.scan("org.springframework.context.annotation5"); assertTrue(context.containsBean("serviceInvocationCounter")); assertTrue(context.containsBean("fooServiceImpl")); assertTrue(context.containsBean("stubFooDao")); assertTrue(context.containsBean("myNamedComponent")); assertTrue(context.containsBean("myNamedDao")); assertTrue(context.containsBean("otherFooDao")); context.refresh(); assertFalse(context.getBeanFactory().containsSingleton("otherFooDao")); assertFalse(context.getBeanFactory().containsSingleton("fooServiceImpl")); FooServiceImpl fooService = context.getBean("fooServiceImpl", FooServiceImpl.class); assertTrue(context.getBeanFactory().containsSingleton("otherFooDao")); assertEquals("other", fooService.foo(123)); assertEquals("other", fooService.lookupFoo(123)); }
@Test public void individualBeanWithSupplierAndCustomizer() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)), bd -> bd.setLazyInit(true)); context.registerBean(BeanB.class, BeanB::new); context.registerBean(BeanC.class, BeanC::new); context.refresh(); assertFalse(context.getBeanFactory().containsSingleton(BeanA.class.getName())); assertSame(context.getBean(BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean(BeanC.class), context.getBean(BeanA.class).c); assertSame(context, context.getBean(BeanB.class).applicationContext); }
@Test public void individualNamedBeanWithSupplier() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean("a", BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class))); context.registerBean("b", BeanB.class, BeanB::new); context.registerBean("c", BeanC.class, BeanC::new); context.refresh(); assertTrue(context.getBeanFactory().containsSingleton("a")); assertSame(context.getBean("b", BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean("c"), context.getBean("a", BeanA.class).c); assertSame(context, context.getBean("b", BeanB.class).applicationContext); }
@Test public void individualNamedBeanWithSupplierAndCustomizer() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean("a", BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)), bd -> bd.setLazyInit(true)); context.registerBean("b", BeanB.class, BeanB::new); context.registerBean("c", BeanC.class, BeanC::new); context.refresh(); assertFalse(context.getBeanFactory().containsSingleton("a")); assertSame(context.getBean("b", BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean("c"), context.getBean("a", BeanA.class).c); assertSame(context, context.getBean("b", BeanB.class).applicationContext); }
AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); ac.getBeanFactory().registerScope("myScope", new Scope() { @Override public Object get(String name, ObjectFactory<?> objectFactory) { ac.getBeanFactory().setConversionService(new DefaultConversionService());
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); context.getBeanFactory().registerScope("request", new SimpleMapScope());
public void getFromScope() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerScope("tx", new SimpleTransactionScope()); context.getBeanFactory().destroyScopedBean("txScopedObject2"); assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2")); assertTrue(bean2.wasDestroyed()); assertSame(bean2a, context.getBean(DerivedTestBean.class)); assertNotSame(bean2, bean2a); context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2"); assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2")); assertFalse(bean2a.wasDestroyed());