11/*
2- * Copyright 2012-2024 the original author or authors.
2+ * Copyright 2012-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .boot .autoconfigure .jms ;
1818
19- import java .io .IOException ;
20-
2119import io .micrometer .observation .ObservationRegistry ;
2220import jakarta .jms .ConnectionFactory ;
2321import jakarta .jms .ExceptionListener ;
2422import jakarta .jms .Session ;
25- import org .apache .activemq .artemis .jms .client .ActiveMQConnectionFactory ;
2623import org .junit .jupiter .api .Test ;
2724
2825import org .springframework .aot .hint .predicate .RuntimeHintsPredicates ;
2926import org .springframework .aot .test .generate .TestGenerationContext ;
3027import org .springframework .beans .factory .config .BeanPostProcessor ;
3128import org .springframework .boot .autoconfigure .AutoConfigurations ;
32- import org .springframework .boot .autoconfigure .jms .artemis .ArtemisAutoConfiguration ;
3329import org .springframework .boot .test .context .assertj .AssertableApplicationContext ;
3430import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
3531import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
4541import org .springframework .jms .config .JmsListenerEndpoint ;
4642import org .springframework .jms .config .SimpleJmsListenerContainerFactory ;
4743import org .springframework .jms .config .SimpleJmsListenerEndpoint ;
48- import org .springframework .jms .connection .CachingConnectionFactory ;
4944import org .springframework .jms .core .JmsMessagingTemplate ;
5045import org .springframework .jms .core .JmsTemplate ;
5146import org .springframework .jms .listener .DefaultMessageListenerContainer ;
6964class JmsAutoConfigurationTests {
7065
7166 private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
72- .withConfiguration (AutoConfigurations .of (ArtemisAutoConfiguration .class , JmsAutoConfiguration .class ));
67+ .withBean (ConnectionFactory .class , () -> mock (ConnectionFactory .class ))
68+ .withConfiguration (AutoConfigurations .of (JmsAutoConfiguration .class ));
69+
70+ @ Test
71+ void testNoConnectionFactoryJmsConfiguration () {
72+ new ApplicationContextRunner ().withConfiguration (AutoConfigurations .of (JmsAutoConfiguration .class ))
73+ .run ((context ) -> assertThat (context ).doesNotHaveBean (JmsTemplate .class )
74+ .doesNotHaveBean (JmsMessagingTemplate .class )
75+ .doesNotHaveBean (DefaultJmsListenerContainerFactoryConfigurer .class )
76+ .doesNotHaveBean (DefaultJmsListenerContainerFactory .class ));
77+ }
7378
7479 @ Test
7580 void testDefaultJmsConfiguration () {
7681 this .contextRunner .withUserConfiguration (TestConfiguration .class ).run ((context ) -> {
77- assertThat (context ).hasSingleBean (ConnectionFactory .class );
78- assertThat (context ).hasSingleBean (CachingConnectionFactory .class );
79- CachingConnectionFactory factory = context .getBean (CachingConnectionFactory .class );
80- assertThat (factory .getTargetConnectionFactory ()).isInstanceOf (ActiveMQConnectionFactory .class );
82+ ConnectionFactory connectionFactory = context .getBean (ConnectionFactory .class );
8183 JmsTemplate jmsTemplate = context .getBean (JmsTemplate .class );
8284 JmsMessagingTemplate messagingTemplate = context .getBean (JmsMessagingTemplate .class );
83- assertThat (factory ). isEqualTo ( jmsTemplate .getConnectionFactory ());
85+ assertThat (jmsTemplate .getConnectionFactory ()). isEqualTo ( connectionFactory );
8486 assertThat (messagingTemplate .getJmsTemplate ()).isEqualTo (jmsTemplate );
85- assertThat (getBrokerUrl (factory )).startsWith ("vm://" );
8687 assertThat (context .containsBean ("jmsListenerContainerFactory" )).isTrue ();
8788 });
8889 }
8990
90- @ Test
91- void testConnectionFactoryBackOff () {
92- this .contextRunner .withUserConfiguration (TestConfiguration2 .class )
93- .run ((context ) -> assertThat (context .getBeansOfType (ActiveMQConnectionFactory .class ))
94- .containsOnlyKeys ("customConnectionFactory" ));
95- }
96-
9791 @ Test
9892 void testJmsTemplateBackOff () {
9993 this .contextRunner .withUserConfiguration (TestConfiguration3 .class )
@@ -107,27 +101,10 @@ void testJmsMessagingTemplateBackOff() {
107101 .isEqualTo ("fooBar" ));
108102 }
109103
110- @ Test
111- void testJmsTemplateBackOffEverything () {
112- this .contextRunner
113- .withUserConfiguration (TestConfiguration2 .class , TestConfiguration3 .class , TestConfiguration5 .class )
114- .run (this ::testJmsTemplateBackOffEverything );
115- }
116-
117- private void testJmsTemplateBackOffEverything (AssertableApplicationContext loaded ) {
118- JmsTemplate jmsTemplate = loaded .getBean (JmsTemplate .class );
119- assertThat (jmsTemplate .getPriority ()).isEqualTo (999 );
120- assertThat (loaded .getBeansOfType (ActiveMQConnectionFactory .class )).containsOnlyKeys ("customConnectionFactory" );
121- JmsMessagingTemplate messagingTemplate = loaded .getBean (JmsMessagingTemplate .class );
122- assertThat (messagingTemplate .getDefaultDestinationName ()).isEqualTo ("fooBar" );
123- assertThat (messagingTemplate .getJmsTemplate ()).isEqualTo (jmsTemplate );
124- }
125-
126104 @ Test
127105 void testDefaultJmsListenerConfiguration () {
128106 this .contextRunner .withUserConfiguration (TestConfiguration .class ).run ((loaded ) -> {
129- assertThat (loaded ).hasSingleBean (CachingConnectionFactory .class );
130- CachingConnectionFactory connectionFactory = loaded .getBean (CachingConnectionFactory .class );
107+ ConnectionFactory connectionFactory = loaded .getBean (ConnectionFactory .class );
131108 assertThat (loaded ).hasSingleBean (DefaultJmsListenerContainerFactory .class );
132109 DefaultJmsListenerContainerFactory containerFactory = loaded
133110 .getBean (DefaultJmsListenerContainerFactory .class );
@@ -423,16 +400,6 @@ void testPubSubDomainOverride() {
423400 });
424401 }
425402
426- private String getBrokerUrl (CachingConnectionFactory connectionFactory ) {
427- assertThat (connectionFactory .getTargetConnectionFactory ()).isInstanceOf (ActiveMQConnectionFactory .class );
428- try {
429- return ((ActiveMQConnectionFactory ) connectionFactory .getTargetConnectionFactory ()).toURI ().toString ();
430- }
431- catch (IOException ex ) {
432- throw new RuntimeException (ex );
433- }
434- }
435-
436403 @ Test
437404 void enableJmsAutomatically () {
438405 this .contextRunner .withUserConfiguration (NoEnableJmsConfiguration .class )
@@ -444,7 +411,7 @@ void enableJmsAutomatically() {
444411 @ Test
445412 void runtimeHintsAreRegisteredForBindingOfAcknowledgeMode () {
446413 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ()) {
447- context .register (ArtemisAutoConfiguration .class , JmsAutoConfiguration .class );
414+ context .register (TestConfiguration2 .class , JmsAutoConfiguration .class );
448415 TestGenerationContext generationContext = new TestGenerationContext ();
449416 new ApplicationContextAotGenerator ().processAheadOfTime (context , generationContext );
450417 assertThat (RuntimeHintsPredicates .reflection ().onMethod (AcknowledgeMode .class , "of" ).invoke ())
@@ -462,7 +429,7 @@ static class TestConfiguration2 {
462429
463430 @ Bean
464431 ConnectionFactory customConnectionFactory () {
465- return new ActiveMQConnectionFactory ( );
432+ return mock ( ConnectionFactory . class );
466433 }
467434
468435 }
@@ -593,12 +560,12 @@ static class TestConfiguration10 {
593560
594561 @ Bean
595562 ConnectionFactory connectionFactory1 () {
596- return new ActiveMQConnectionFactory ( );
563+ return mock ( ConnectionFactory . class );
597564 }
598565
599566 @ Bean
600567 ConnectionFactory connectionFactory2 () {
601- return new ActiveMQConnectionFactory ( );
568+ return mock ( ConnectionFactory . class );
602569 }
603570
604571 }
0 commit comments