@Test public void metaAnnotationIsDiscovered() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, MetaAnnotationTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); RabbitListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); assertEquals("metaTestQueue", ((AbstractRabbitListenerEndpoint) endpoint).getQueueNames().iterator().next()); context.close(); }
@Override public MessageListenerTestContainer createListenerContainer(RabbitListenerEndpoint endpoint) { MessageListenerTestContainer container = new MessageListenerTestContainer(endpoint); // resolve the id if (endpoint.getId() == null && endpoint instanceof AbstractRabbitListenerEndpoint) { ((AbstractRabbitListenerEndpoint) endpoint).setId("endpoint#" + counter.getAndIncrement()); } String id = endpoint.getId(); assertNotNull(this.getClass().getSimpleName() + " does not support " + endpoint.getClass().getSimpleName() + " without an id", id); this.listenerContainers.put(id, container); return container; }
@Test public void lazyComponent() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( EnableRabbitDefaultContainerFactoryConfig.class, LazyBean.class); RabbitListenerContainerTestFactory defaultFactory = context.getBean("rabbitListenerContainerFactory", RabbitListenerContainerTestFactory.class); assertEquals(0, defaultFactory.getListenerContainers().size()); context.getBean(LazyBean.class); // trigger lazy resolution assertEquals(1, defaultFactory.getListenerContainers().size()); MessageListenerTestContainer container = defaultFactory.getListenerContainers().get(0); assertTrue("Should have been started " + container, container.isStarted()); context.close(); // Close and stop the listeners assertTrue("Should have been stopped " + container, container.isStopped()); }
@Test public void simpleMessageListener() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, SimpleMessageListenerTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertEquals("One container should have been registered", 1, factory.getListenerContainers().size()); MessageListenerTestContainer container = factory.getListenerContainers().get(0); RabbitListenerEndpoint endpoint = container.getEndpoint(); assertEquals("Wrong endpoint type", MethodRabbitListenerEndpoint.class, endpoint.getClass()); MethodRabbitListenerEndpoint methodEndpoint = (MethodRabbitListenerEndpoint) endpoint; assertNotNull(methodEndpoint.getBean()); assertNotNull(methodEndpoint.getMethod()); SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer(); methodEndpoint.setupListenerContainer(listenerContainer); assertNotNull(listenerContainer.getMessageListener()); assertTrue("Should have been started " + container, container.isStarted()); context.close(); // Close and stop the listeners assertTrue("Should have been stopped " + container, container.isStopped()); }
@Test public void simpleMessageListenerWithMixedAnnotations() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, SimpleMessageListenerWithMixedAnnotationsTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertEquals("One container should have been registered", 1, factory.getListenerContainers().size()); MessageListenerTestContainer container = factory.getListenerContainers().get(0); RabbitListenerEndpoint endpoint = container.getEndpoint(); assertEquals("Wrong endpoint type", MethodRabbitListenerEndpoint.class, endpoint.getClass()); MethodRabbitListenerEndpoint methodEndpoint = (MethodRabbitListenerEndpoint) endpoint; assertNotNull(methodEndpoint.getBean()); assertNotNull(methodEndpoint.getMethod()); Iterator<String> iterator = ((MethodRabbitListenerEndpoint) endpoint).getQueueNames().iterator(); assertEquals("testQueue", iterator.next()); assertEquals("secondQueue", iterator.next()); SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer(); methodEndpoint.setupListenerContainer(listenerContainer); assertNotNull(listenerContainer.getMessageListener()); assertTrue("Should have been started " + container, container.isStarted()); context.close(); // Close and stop the listeners assertTrue("Should have been stopped " + container, container.isStopped()); }
@Test public void multipleQueueNamesTestBean() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, MultipleQueueNamesTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); RabbitListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); final Iterator<String> iterator = ((AbstractRabbitListenerEndpoint) endpoint).getQueueNames().iterator(); assertEquals("metaTestQueue", iterator.next()); assertEquals("testQueue", iterator.next()); context.close(); }
@Test public void multipleQueuesTestBean() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, MultipleQueuesTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); RabbitListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); final Iterator<String> iterator = ((AbstractRabbitListenerEndpoint) endpoint).getQueueNames().iterator(); assertEquals("testQueue", iterator.next()); assertEquals("secondQueue", iterator.next()); context.close(); }
@Test public void propertyResolvingToExpressionTestBean() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, PropertyResolvingToExpressionTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); RabbitListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); final Iterator<String> iterator = ((AbstractRabbitListenerEndpoint) endpoint).getQueueNames().iterator(); assertEquals("testQueue", iterator.next()); assertEquals("secondQueue", iterator.next()); context.close(); }
@Test public void mixedQueuesAndQueueNamesTestBean() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, MixedQueuesAndQueueNamesTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); RabbitListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); final Iterator<String> iterator = ((AbstractRabbitListenerEndpoint) endpoint).getQueueNames().iterator(); assertEquals("metaTestQueue", iterator.next()); assertEquals("testQueue", iterator.next()); assertEquals("secondQueue", iterator.next()); context.close(); }
@Test public void multipleRoutingKeysTestBean() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MultipleRoutingKeysTestBean.class); RabbitListenerContainerTestFactory factory = context.getBean(RabbitListenerContainerTestFactory.class); assertThat("one container should have been registered", factory.getListenerContainers(), hasSize(1)); RabbitListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); assertEquals(Collections.singletonList("my_queue"), ((AbstractRabbitListenerEndpoint) endpoint).getQueueNames()); final List<Queue> queues = new ArrayList<>(context.getBeansOfType(Queue.class).values()); queues.sort(Comparator.comparing(Queue::getName)); assertThat(queues.stream().map(Queue::getName).collect(Collectors.toList()), contains("my_queue", "secondQueue", "testQueue")); assertEquals(Collections.singletonMap("foo", "bar"), queues.get(0).getArguments()); assertThat(context.getBeansOfType(org.springframework.amqp.core.Exchange.class).values(), hasSize(1)); final List<Binding> bindings = new ArrayList<>(context.getBeansOfType(Binding.class).values()); assertThat(bindings, hasSize(2)); bindings.sort(Comparator.comparing(Binding::getRoutingKey)); assertEquals("Binding [destination=my_queue, exchange=my_exchange, routingKey=red]", bindings.get(0).toString()); assertEquals("Binding [destination=my_queue, exchange=my_exchange, routingKey=yellow]", bindings.get(1).toString()); context.close(); }
/** * Test for {@link RabbitListenerRepeatableBean} that validates that the * {@code @RabbitListener} annotation is repeatable and generate one specific container per annotation. */ public void testRabbitListenerRepeatable(ApplicationContext context) { RabbitListenerContainerTestFactory simpleFactory = context.getBean("rabbitListenerContainerFactory", RabbitListenerContainerTestFactory.class); assertEquals(4, simpleFactory.getListenerContainers().size()); MethodRabbitListenerEndpoint first = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("first").getEndpoint(); assertEquals("first", first.getId()); assertEquals("myQueue", first.getQueueNames().iterator().next()); MethodRabbitListenerEndpoint second = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("second").getEndpoint(); assertEquals("second", second.getId()); assertEquals("anotherQueue", second.getQueueNames().iterator().next()); MethodRabbitListenerEndpoint third = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("third").getEndpoint(); assertEquals("third", third.getId()); assertEquals("class1", third.getQueueNames().iterator().next()); MethodRabbitListenerEndpoint fourth = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("fourth").getEndpoint(); assertEquals("fourth", fourth.getId()); assertEquals("class2", fourth.getQueueNames().iterator().next()); }
/** * Test for {@link RabbitListenersBean} that validates that the * {@code @RabbitListener} annotations generate one specific container per annotation. */ public void testRabbitListenerRepeatable(ApplicationContext context) { RabbitListenerContainerTestFactory simpleFactory = context.getBean("rabbitListenerContainerFactory", RabbitListenerContainerTestFactory.class); assertEquals(4, simpleFactory.getListenerContainers().size()); MethodRabbitListenerEndpoint first = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("first").getEndpoint(); assertEquals("first", first.getId()); assertEquals("myQueue", first.getQueueNames().iterator().next()); MethodRabbitListenerEndpoint second = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("second").getEndpoint(); assertEquals("second", second.getId()); assertEquals("anotherQueue", second.getQueueNames().iterator().next()); MethodRabbitListenerEndpoint third = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("third").getEndpoint(); assertEquals("third", third.getId()); assertEquals("class1", third.getQueueNames().iterator().next()); MethodRabbitListenerEndpoint fourth = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainer("fourth").getEndpoint(); assertEquals("fourth", fourth.getId()); assertEquals("class2", fourth.getQueueNames().iterator().next()); }
/** * Test for {@link ValidationBean} with a validator ({@link TestValidator}) specified * in a custom {@link org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory}. * * The test should throw a {@link org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException} */ public void testRabbitHandlerMethodFactoryConfiguration(ApplicationContext context) throws Exception { RabbitListenerContainerTestFactory simpleFactory = context.getBean("defaultFactory", RabbitListenerContainerTestFactory.class); assertEquals(1, simpleFactory.getListenerContainers().size()); MethodRabbitListenerEndpoint endpoint = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainers().get(0).getEndpoint(); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); endpoint.setupListenerContainer(container); MessagingMessageListenerAdapter listener = (MessagingMessageListenerAdapter) container.getMessageListener(); MessageProperties properties = new MessageProperties(); properties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN); Message amqpMessage = new Message("failValidation".getBytes(), properties); listener.onMessage(amqpMessage, mock(Channel.class)); }
/** * Test for {@link CustomBean} and an manually endpoint registered * with "myCustomEndpointId". The custom endpoint does not provide * any factory so it's registered with the default one */ public void testCustomConfiguration(ApplicationContext context) { RabbitListenerContainerTestFactory defaultFactory = context.getBean("rabbitListenerContainerFactory", RabbitListenerContainerTestFactory.class); RabbitListenerContainerTestFactory customFactory = context.getBean("customFactory", RabbitListenerContainerTestFactory.class); assertEquals(1, defaultFactory.getListenerContainers().size()); assertEquals(1, customFactory.getListenerContainers().size()); RabbitListenerEndpoint endpoint = defaultFactory.getListenerContainers().get(0).getEndpoint(); assertEquals("Wrong endpoint type", SimpleRabbitListenerEndpoint.class, endpoint.getClass()); assertEquals("Wrong listener set in custom endpoint", context.getBean("simpleMessageListener"), ((SimpleRabbitListenerEndpoint) endpoint).getMessageListener()); RabbitListenerEndpointRegistry customRegistry = context.getBean("customRegistry", RabbitListenerEndpointRegistry.class); assertEquals("Wrong number of containers in the registry", 2, customRegistry.getListenerContainerIds().size()); assertEquals("Wrong number of containers in the registry", 2, customRegistry.getListenerContainers().size()); assertNotNull("Container with custom id on the annotation should be found", customRegistry.getListenerContainer("listenerId")); assertNotNull("Container created with custom id should be found", customRegistry.getListenerContainer("myCustomEndpointId")); }
assertEquals(1, simpleFactory.getListenerContainers().size()); MethodRabbitListenerEndpoint endpoint = (MethodRabbitListenerEndpoint) simpleFactory.getListenerContainers().get(0).getEndpoint(); assertEquals("listener1", endpoint.getId()); assertQueues(endpoint, "queue1", "queue2");