Java Code Examples for org.springframework.context.support.StaticApplicationContext

The following examples show how to use org.springframework.context.support.StaticApplicationContext. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: java-technology-stack   Source File: AutoProxyCreatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("singleton", "false");
	sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(prototypeFactory));
	TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));

	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 2
Source Project: rqueue   Source File: RqueueMessageHandlerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void concurrencyResolverSingleValue() {
  StaticApplicationContext applicationContext = new StaticApplicationContext();
  applicationContext.registerSingleton("messageHandler", MessageHandlerWithConcurrency.class);
  applicationContext.registerSingleton("rqueueMessageHandler", DummyMessageHandler.class);
  Map<String, Object> map = new HashMap<>();
  map.put("queue.name", slowQueue + "," + smartQueue);
  map.put("queue.concurrency", "5");
  applicationContext
      .getEnvironment()
      .getPropertySources()
      .addLast(new MapPropertySource("test", map));
  applicationContext.refresh();
  DummyMessageHandler messageHandler = applicationContext.getBean(DummyMessageHandler.class);
  assertEquals(new Concurrency(1, 5), messageHandler.mappingInformation.getConcurrency());
}
 
Example 3
@Test
public void taskExecutorByBeanType() {
	StaticApplicationContext context = new StaticApplicationContext();

	BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);

	BeanDefinition executorDefinition = new RootBeanDefinition(ThreadPoolTaskExecutor.class);
	executorDefinition.getPropertyValues().add("threadNamePrefix", "testExecutor");
	context.registerBeanDefinition("myExecutor", executorDefinition);

	BeanDefinition targetDefinition =
			new RootBeanDefinition(AsyncAnnotationBeanPostProcessorTests.TestBean.class);
	context.registerBeanDefinition("target", targetDefinition);

	context.refresh();

	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));
	context.close();
}
 
Example 4
Source Project: spring-cloud-aws   Source File: QueueMessageHandlerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_withHeaderAnnotationAsArgument_shouldReceiveRequestedHeader() {
	// Arrange
	StaticApplicationContext applicationContext = new StaticApplicationContext();
	applicationContext.registerSingleton("messageHandlerWithHeaderAnnotation",
			MessageReceiverWithHeaderAnnotation.class);
	applicationContext.registerSingleton("queueMessageHandler",
			QueueMessageHandler.class);
	applicationContext.refresh();

	QueueMessageHandler queueMessageHandler = applicationContext
			.getBean(QueueMessageHandler.class);
	MessageReceiverWithHeaderAnnotation messageReceiver = applicationContext
			.getBean(MessageReceiverWithHeaderAnnotation.class);

	// Act
	queueMessageHandler.handleMessage(MessageBuilder
			.withPayload("Hello from a sender").setHeader("SenderId", "elsUnitTest")
			.setHeader(QueueMessageHandler.LOGICAL_RESOURCE_ID, "testQueue").build());

	// Assert
	assertThat(messageReceiver.getPayload()).isEqualTo("Hello from a sender");
	assertThat(messageReceiver.getSenderId()).isEqualTo("elsUnitTest");
}
 
Example 5
Source Project: rqueue   Source File: RqueueMessageHandlerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void priorityResolverMultiLevelQueue() {
  StaticApplicationContext applicationContext = new StaticApplicationContext();
  applicationContext.registerSingleton("messageHandler", MessageHandlerWithPriority.class);
  applicationContext.registerSingleton("rqueueMessageHandler", DummyMessageHandler.class);
  Map<String, Object> map = new HashMap<>();
  map.put("queue.name", slowQueue + "," + smartQueue);
  map.put("queue.priority", "critical=10,high=5,low=2");
  applicationContext
      .getEnvironment()
      .getPropertySources()
      .addLast(new MapPropertySource("test", map));
  applicationContext.refresh();
  DummyMessageHandler messageHandler = applicationContext.getBean(DummyMessageHandler.class);
  Map<String, Integer> priority = new HashMap<>();
  priority.put("critical", 10);
  priority.put("high", 5);
  priority.put("low", 2);
  assertEquals(priority, messageHandler.mappingInformation.getPriority());
}
 
Example 6
@Test
public void anonymousClassAsListener() {
	final Set<MyEvent> seenEvents = new HashSet<>();
	StaticApplicationContext context = new StaticApplicationContext();
	context.addApplicationListener(new ApplicationListener<MyEvent>() {
		@Override
		public void onApplicationEvent(MyEvent event) {
			seenEvents.add(event);
		}
	});
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	context.publishEvent(new MyOtherEvent(context));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertSame(2, seenEvents.size());
	assertTrue(seenEvents.contains(event1));
	assertTrue(seenEvents.contains(event2));

	context.close();
}
 
Example 7
@Test
public void detectHandlerMethodsInAncestorContexts() {
	StaticApplicationContext cxt = new StaticApplicationContext();
	cxt.registerSingleton("myHandler", MyHandler.class);

	AbstractHandlerMethodMapping<String> mapping1 = new MyHandlerMethodMapping();
	mapping1.setApplicationContext(new StaticApplicationContext(cxt));
	mapping1.afterPropertiesSet();

	assertEquals(0, mapping1.getHandlerMethods().size());

	AbstractHandlerMethodMapping<String> mapping2 = new MyHandlerMethodMapping();
	mapping2.setDetectHandlerMethodsInAncestorContexts(true);
	mapping2.setApplicationContext(new StaticApplicationContext(cxt));
	mapping2.afterPropertiesSet();

	assertEquals(2, mapping2.getHandlerMethods().size());
}
 
Example 8
@Test
public void simpleApplicationEventMulticasterWithTaskExecutor() {
	@SuppressWarnings("unchecked")
	ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
	ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());

	SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
	smc.setTaskExecutor(new Executor() {
		@Override
		public void execute(Runnable command) {
			command.run();
			command.run();
		}
	});
	smc.addApplicationListener(listener);

	smc.multicastEvent(evt);
	verify(listener, times(2)).onApplicationEvent(evt);
}
 
Example 9
@Test
public void listenersInApplicationContextWithNestedChild() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition nestedChild = new RootBeanDefinition(StaticApplicationContext.class);
	nestedChild.getPropertyValues().add("parent", context);
	nestedChild.setInitMethodName("refresh");
	context.registerBeanDefinition("nestedChild", nestedChild);
	RootBeanDefinition listener1Def = new RootBeanDefinition(MyOrderedListener1.class);
	listener1Def.setDependsOn("nestedChild");
	context.registerBeanDefinition("listener1", listener1Def);
	context.refresh();

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	assertTrue(listener1.seenEvents.contains(event1));

	SimpleApplicationEventMulticaster multicaster = context.getBean(
			AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			SimpleApplicationEventMulticaster.class);
	assertFalse(multicaster.getApplicationListeners().isEmpty());

	context.close();
	assertTrue(multicaster.getApplicationListeners().isEmpty());
}
 
Example 10
@Test
public void listenersInApplicationContextWithPayloadEvents() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(MyPayloadListener.class));
	context.refresh();

	MyPayloadListener listener = context.getBean("listener", MyPayloadListener.class);
	context.publishEvent("event1");
	context.publishEvent("event2");
	context.publishEvent("event3");
	context.publishEvent("event4");
	assertTrue(listener.seenPayloads.contains("event1"));
	assertTrue(listener.seenPayloads.contains("event2"));
	assertTrue(listener.seenPayloads.contains("event3"));
	assertTrue(listener.seenPayloads.contains("event4"));

	AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
	assertEquals(2, multicaster.retrieverCache.size());

	context.close();
}
 
Example 11
@Test
public void testQualifiedByAttributesWithCustomQualifierRegistered() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver)
			context.getDefaultListableBeanFactory().getAutowireCandidateResolver();
	resolver.addQualifierType(MultipleAttributeQualifier.class);
	context.registerSingleton("testBean", MultiQualifierClient.class);
	context.refresh();

	MultiQualifierClient testBean = (MultiQualifierClient) context.getBean("testBean");

	assertNotNull( testBean.factoryTheta);
	assertNotNull( testBean.implTheta);
}
 
Example 12
Source Project: eclair   Source File: LoggerBeanNamesResolverTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void resolveManyLoggers() {
    // given
    String beanName = "simpleLogger";
    String alias = "logger";
    String beanName2 = "simpleLogger2";
    String alias2 = "logger2";

    GenericApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerBeanDefinition(beanName, givenLoggerBeanDefinition());
    applicationContext.getBeanFactory().registerAlias(beanName, alias);
    applicationContext.registerBeanDefinition(beanName2, givenLoggerBeanDefinition());
    applicationContext.getBeanFactory().registerAlias(beanName2, alias2);
    // when
    Set<String> namesByBeanName = loggerBeanNamesResolver.resolve(applicationContext, beanName);
    // then
    assertThat(namesByBeanName, hasSize(2));
    assertThat(namesByBeanName, containsInAnyOrder(beanName, alias));
}
 
Example 13
@Test
public void resolveView() throws Exception {
	StaticApplicationContext ctx = new StaticApplicationContext();

	String prefix = ClassUtils.classPackageAsResourcePath(getClass());
	String suffix = ".xsl";
	String viewName = "products";

	XsltViewResolver viewResolver = new XsltViewResolver();
	viewResolver.setPrefix(prefix);
	viewResolver.setSuffix(suffix);
	viewResolver.setApplicationContext(ctx);

	XsltView view = (XsltView) viewResolver.resolveViewName(viewName, Locale.ENGLISH);
	assertNotNull("View should not be null", view);
	assertEquals("Incorrect URL", prefix + viewName + suffix, view.getUrl());
}
 
Example 14
Source Project: spring-analysis-note   Source File: LifecycleEventTests.java    License: MIT License 6 votes vote down vote up
@Test
public void contextStoppedEvent() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerSingleton("lifecycle", LifecycleTestBean.class);
	context.registerSingleton("listener", LifecycleListener.class);
	context.refresh();
	LifecycleTestBean lifecycleBean = (LifecycleTestBean) context.getBean("lifecycle");
	LifecycleListener listener = (LifecycleListener) context.getBean("listener");
	assertFalse(lifecycleBean.isRunning());
	context.start();
	assertTrue(lifecycleBean.isRunning());
	assertEquals(0, listener.getStoppedCount());
	context.stop();
	assertFalse(lifecycleBean.isRunning());
	assertEquals(1, listener.getStoppedCount());
	assertSame(context, listener.getApplicationContext());
}
 
Example 15
@Test  // SPR-15291
public void contentNegotiationWithRedirect() {
	HandlerResult handlerResult = new HandlerResult(new Object(), "redirect:/",
			on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(String.class),
			this.bindingContext);

	UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
	viewResolver.setApplicationContext(new StaticApplicationContext());
	ViewResolutionResultHandler resultHandler = resultHandler(viewResolver);

	MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON));
	resultHandler.handleResult(exchange, handlerResult).block(Duration.ZERO);

	MockServerHttpResponse response = exchange.getResponse();
	assertEquals(303, response.getStatusCode().value());
	assertEquals("/", response.getHeaders().getLocation().toString());
}
 
Example 16
@Test
public void listenersInApplicationContextWithNestedChild() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition nestedChild = new RootBeanDefinition(StaticApplicationContext.class);
	nestedChild.getPropertyValues().add("parent", context);
	nestedChild.setInitMethodName("refresh");
	context.registerBeanDefinition("nestedChild", nestedChild);
	RootBeanDefinition listener1Def = new RootBeanDefinition(MyOrderedListener1.class);
	listener1Def.setDependsOn("nestedChild");
	context.registerBeanDefinition("listener1", listener1Def);
	context.refresh();

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	assertTrue(listener1.seenEvents.contains(event1));

	SimpleApplicationEventMulticaster multicaster = context.getBean(
			AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			SimpleApplicationEventMulticaster.class);
	assertFalse(multicaster.getApplicationListeners().isEmpty());

	context.close();
	assertTrue(multicaster.getApplicationListeners().isEmpty());
}
 
Example 17
@Test
void postProcessBeanFactory_withConfigurableEnvironment_registersPropertySource()
		throws Exception {
	// Arrange
	StaticApplicationContext staticApplicationContext = new StaticApplicationContext();
	staticApplicationContext.registerSingleton("process",
			AmazonEc2InstanceDataPropertySourcePostProcessor.class);

	// Act
	staticApplicationContext.refresh();

	// Assert
	assertThat(staticApplicationContext.getEnvironment().getPropertySources().get(
			AmazonEc2InstanceDataPropertySourcePostProcessor.INSTANCE_DATA_PROPERTY_SOURCE_NAME))
					.isNotNull();
}
 
Example 18
Source Project: spring-analysis-note   Source File: QualifierAnnotationTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testQualifiedByAttributesWithCustomQualifierRegistered() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver)
			context.getDefaultListableBeanFactory().getAutowireCandidateResolver();
	resolver.addQualifierType(MultipleAttributeQualifier.class);
	context.registerSingleton("testBean", MultiQualifierClient.class);
	context.refresh();

	MultiQualifierClient testBean = (MultiQualifierClient) context.getBean("testBean");

	assertNotNull( testBean.factoryTheta);
	assertNotNull( testBean.implTheta);
}
 
Example 19
Source Project: java-technology-stack   Source File: LifecycleEventTests.java    License: MIT License 6 votes vote down vote up
@Test
public void contextStoppedEvent() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerSingleton("lifecycle", LifecycleTestBean.class);
	context.registerSingleton("listener", LifecycleListener.class);
	context.refresh();
	LifecycleTestBean lifecycleBean = (LifecycleTestBean) context.getBean("lifecycle");
	LifecycleListener listener = (LifecycleListener) context.getBean("listener");
	assertFalse(lifecycleBean.isRunning());
	context.start();
	assertTrue(lifecycleBean.isRunning());
	assertEquals(0, listener.getStoppedCount());
	context.stop();
	assertFalse(lifecycleBean.isRunning());
	assertEquals(1, listener.getStoppedCount());
	assertSame(context, listener.getApplicationContext());
}
 
Example 20
Source Project: spring-analysis-note   Source File: AutoProxyCreatorTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 21
@Test
public void taskExecutorByBeanType() {
	StaticApplicationContext context = new StaticApplicationContext();

	BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);

	BeanDefinition executorDefinition = new RootBeanDefinition(ThreadPoolTaskExecutor.class);
	executorDefinition.getPropertyValues().add("threadNamePrefix", "testExecutor");
	context.registerBeanDefinition("myExecutor", executorDefinition);

	BeanDefinition targetDefinition =
			new RootBeanDefinition(AsyncAnnotationBeanPostProcessorTests.TestBean.class);
	context.registerBeanDefinition("target", targetDefinition);

	context.refresh();

	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));
	context.close();
}
 
Example 22
Source Project: java-technology-stack   Source File: HandlerMethodMappingTests.java    License: MIT License 6 votes vote down vote up
@Test
public void detectHandlerMethodsInAncestorContexts() {
	StaticApplicationContext cxt = new StaticApplicationContext();
	cxt.registerSingleton("myHandler", MyHandler.class);

	AbstractHandlerMethodMapping<String> mapping1 = new MyHandlerMethodMapping();
	mapping1.setApplicationContext(new StaticApplicationContext(cxt));
	mapping1.afterPropertiesSet();

	assertEquals(0, mapping1.getHandlerMethods().size());

	AbstractHandlerMethodMapping<String> mapping2 = new MyHandlerMethodMapping();
	mapping2.setDetectHandlerMethodsInAncestorContexts(true);
	mapping2.setApplicationContext(new StaticApplicationContext(cxt));
	mapping2.afterPropertiesSet();

	assertEquals(2, mapping2.getHandlerMethods().size());
}
 
Example 23
@Test
public void testQualifiedByAnnotation() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByAnnotationTestBean.class);
	context.refresh();
	QualifiedByAnnotationTestBean testBean = (QualifiedByAnnotationTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarrySpecial", person.getName());
}
 
Example 24
@Test
public void listenerAndBroadcasterWithCircularReference() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerBeanDefinition("broadcaster", new RootBeanDefinition(BeanThatBroadcasts.class));
	RootBeanDefinition listenerDef = new RootBeanDefinition(BeanThatListens.class);
	listenerDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("broadcaster"));
	context.registerBeanDefinition("listener", listenerDef);
	context.refresh();

	BeanThatBroadcasts broadcaster = context.getBean("broadcaster", BeanThatBroadcasts.class);
	context.publishEvent(new MyEvent(context));
	assertEquals("The event was not received by the listener", 2, broadcaster.receivedCount);

	context.close();
}
 
Example 25
Source Project: spring-cloud-aws   Source File: QueueMessageHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void receiveMessage_withHeadersAsArgumentAnnotation_shouldReceiveAllHeaders() {
	// Arrange
	StaticApplicationContext applicationContext = new StaticApplicationContext();
	applicationContext.registerSingleton("messageHandlerWithHeadersAnnotation",
			MessageReceiverWithHeadersAnnotation.class);
	applicationContext.registerSingleton("queueMessageHandler",
			QueueMessageHandler.class);
	applicationContext.refresh();

	QueueMessageHandler queueMessageHandler = applicationContext
			.getBean(QueueMessageHandler.class);
	MessageReceiverWithHeadersAnnotation messageReceiver = applicationContext
			.getBean(MessageReceiverWithHeadersAnnotation.class);

	// Act
	queueMessageHandler
			.handleMessage(MessageBuilder.withPayload("Hello from a sender")
					.setHeader(QueueMessageHandler.LOGICAL_RESOURCE_ID, "testQueue")
					.setHeader("SenderId", "ID").setHeader("SentTimestamp", "1000")
					.setHeader("ApproximateFirstReceiveTimestamp", "2000").build());

	// Assert
	assertThat(messageReceiver.getHeaders()).isNotNull();
	assertThat(messageReceiver.getHeaders().get("SenderId")).isEqualTo("ID");
	assertThat(
			messageReceiver.getHeaders().get(QueueMessageHandler.LOGICAL_RESOURCE_ID))
					.isEqualTo("testQueue");
}
 
Example 26
@Test
void start_withAQueueNameThatIsAlreadyRunning_shouldNotStartTheQueueAgainAndIgnoreTheCall()
		throws Exception {
	// Arrange
	StaticApplicationContext applicationContext = new StaticApplicationContext();
	applicationContext.registerSingleton("testMessageListener",
			TestMessageListener.class);

	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	AmazonSQSAsync sqs = mock(AmazonSQSAsync.class, withSettings().stubOnly());

	when(sqs.receiveMessage(any(ReceiveMessageRequest.class)))
			.thenReturn(new ReceiveMessageResult());

	container.setAmazonSqs(sqs);
	container.setBackOffTime(0);

	QueueMessageHandler messageHandler = new QueueMessageHandler();
	messageHandler.setApplicationContext(applicationContext);
	container.setMessageHandler(messageHandler);

	mockGetQueueUrl(sqs, "testQueue",
			"https://start_withAQueueNameThatIsAlreadyRunning_shouldNotStartTheQueueAgainAndIgnoreTheCall.amazonaws.com");
	mockGetQueueAttributesWithEmptyResult(sqs,
			"https://start_withAQueueNameThatIsAlreadyRunning_shouldNotStartTheQueueAgainAndIgnoreTheCall.amazonaws.com");

	messageHandler.afterPropertiesSet();
	container.afterPropertiesSet();
	container.start();

	assertThat(container.isRunning("testQueue")).isTrue();

	// Act
	container.start("testQueue");

	// Assert
	assertThat(container.isRunning("testQueue")).isTrue();

	container.stop();
}
 
Example 27
Source Project: java-technology-stack   Source File: MethodValidationTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testMethodValidationPostProcessor() {
	StaticApplicationContext ac = new StaticApplicationContext();
	ac.registerSingleton("mvpp", MethodValidationPostProcessor.class);
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("beforeExistingAdvisors", false);
	ac.registerSingleton("aapp", AsyncAnnotationBeanPostProcessor.class, pvs);
	ac.registerSingleton("bean", MyValidBean.class);
	ac.refresh();
	doTestProxyValidation(ac.getBean("bean", MyValidInterface.class));
	ac.close();
}
 
Example 28
@Test
public void testWebApplicationObjectSupportWithWrongContext() {
	StaticApplicationContext ac = new StaticApplicationContext();
	ac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class));
	WebApplicationObjectSupport wao = (WebApplicationObjectSupport) ac.getBean("test");
	try {
		wao.getWebApplicationContext();
		fail("Should have thrown IllegalStateException");
	}
	catch (IllegalStateException ex) {
		// expected
	}
}
 
Example 29
@Test
public void simpValidatorMvc() {
	StaticApplicationContext appCxt = new StaticApplicationContext();
	appCxt.registerSingleton("mvcValidator", TestValidator.class);
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {};
	config.setApplicationContext(appCxt);

	assertThat(config.simpValidator(), Matchers.notNullValue());
	assertThat(config.simpValidator(), Matchers.instanceOf(TestValidator.class));
}
 
Example 30
Source Project: rqueue   Source File: RqueueMessageHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = BeanCreationException.class)
public void concurrencyResolverInvalidValue() {
  StaticApplicationContext applicationContext = new StaticApplicationContext();
  applicationContext.registerSingleton("messageHandler", MessageHandlerWithConcurrency.class);
  applicationContext.registerSingleton("rqueueMessageHandler", DummyMessageHandler.class);
  Map<String, Object> map = new HashMap<>();
  map.put("queue.name", slowQueue + "," + smartQueue);
  map.put("queue.concurrency", slowQueue);
  applicationContext
      .getEnvironment()
      .getPropertySources()
      .addLast(new MapPropertySource("test", map));
  applicationContext.refresh();
}