1
1
/*
2
- * Copyright 2012-2024 the original author or authors.
2
+ * Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .boot .autoconfigure .jms ;
18
18
19
- import java .io .IOException ;
20
-
21
19
import io .micrometer .observation .ObservationRegistry ;
22
20
import jakarta .jms .ConnectionFactory ;
23
21
import jakarta .jms .ExceptionListener ;
24
22
import jakarta .jms .Session ;
25
- import org .apache .activemq .artemis .jms .client .ActiveMQConnectionFactory ;
26
23
import org .junit .jupiter .api .Test ;
27
24
28
25
import org .springframework .aot .hint .predicate .RuntimeHintsPredicates ;
29
26
import org .springframework .aot .test .generate .TestGenerationContext ;
30
27
import org .springframework .beans .factory .config .BeanPostProcessor ;
31
28
import org .springframework .boot .autoconfigure .AutoConfigurations ;
32
- import org .springframework .boot .autoconfigure .jms .artemis .ArtemisAutoConfiguration ;
33
29
import org .springframework .boot .test .context .assertj .AssertableApplicationContext ;
34
30
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
35
31
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
45
41
import org .springframework .jms .config .JmsListenerEndpoint ;
46
42
import org .springframework .jms .config .SimpleJmsListenerContainerFactory ;
47
43
import org .springframework .jms .config .SimpleJmsListenerEndpoint ;
48
- import org .springframework .jms .connection .CachingConnectionFactory ;
49
44
import org .springframework .jms .core .JmsMessagingTemplate ;
50
45
import org .springframework .jms .core .JmsTemplate ;
51
46
import org .springframework .jms .listener .DefaultMessageListenerContainer ;
69
64
class JmsAutoConfigurationTests {
70
65
71
66
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
+ }
73
78
74
79
@ Test
75
80
void testDefaultJmsConfiguration () {
76
81
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 );
81
83
JmsTemplate jmsTemplate = context .getBean (JmsTemplate .class );
82
84
JmsMessagingTemplate messagingTemplate = context .getBean (JmsMessagingTemplate .class );
83
- assertThat (factory ). isEqualTo ( jmsTemplate .getConnectionFactory ());
85
+ assertThat (jmsTemplate .getConnectionFactory ()). isEqualTo ( connectionFactory );
84
86
assertThat (messagingTemplate .getJmsTemplate ()).isEqualTo (jmsTemplate );
85
- assertThat (getBrokerUrl (factory )).startsWith ("vm://" );
86
87
assertThat (context .containsBean ("jmsListenerContainerFactory" )).isTrue ();
87
88
});
88
89
}
89
90
90
- @ Test
91
- void testConnectionFactoryBackOff () {
92
- this .contextRunner .withUserConfiguration (TestConfiguration2 .class )
93
- .run ((context ) -> assertThat (context .getBeansOfType (ActiveMQConnectionFactory .class ))
94
- .containsOnlyKeys ("customConnectionFactory" ));
95
- }
96
-
97
91
@ Test
98
92
void testJmsTemplateBackOff () {
99
93
this .contextRunner .withUserConfiguration (TestConfiguration3 .class )
@@ -107,27 +101,10 @@ void testJmsMessagingTemplateBackOff() {
107
101
.isEqualTo ("fooBar" ));
108
102
}
109
103
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
-
126
104
@ Test
127
105
void testDefaultJmsListenerConfiguration () {
128
106
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 );
131
108
assertThat (loaded ).hasSingleBean (DefaultJmsListenerContainerFactory .class );
132
109
DefaultJmsListenerContainerFactory containerFactory = loaded
133
110
.getBean (DefaultJmsListenerContainerFactory .class );
@@ -425,16 +402,6 @@ void testPubSubDomainOverride() {
425
402
});
426
403
}
427
404
428
- private String getBrokerUrl (CachingConnectionFactory connectionFactory ) {
429
- assertThat (connectionFactory .getTargetConnectionFactory ()).isInstanceOf (ActiveMQConnectionFactory .class );
430
- try {
431
- return ((ActiveMQConnectionFactory ) connectionFactory .getTargetConnectionFactory ()).toURI ().toString ();
432
- }
433
- catch (IOException ex ) {
434
- throw new RuntimeException (ex );
435
- }
436
- }
437
-
438
405
@ Test
439
406
void enableJmsAutomatically () {
440
407
this .contextRunner .withUserConfiguration (NoEnableJmsConfiguration .class )
@@ -446,7 +413,7 @@ void enableJmsAutomatically() {
446
413
@ Test
447
414
void runtimeHintsAreRegisteredForBindingOfAcknowledgeMode () {
448
415
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ()) {
449
- context .register (ArtemisAutoConfiguration .class , JmsAutoConfiguration .class );
416
+ context .register (TestConfiguration2 .class , JmsAutoConfiguration .class );
450
417
TestGenerationContext generationContext = new TestGenerationContext ();
451
418
new ApplicationContextAotGenerator ().processAheadOfTime (context , generationContext );
452
419
assertThat (RuntimeHintsPredicates .reflection ().onMethod (AcknowledgeMode .class , "of" ).invoke ())
@@ -464,7 +431,7 @@ static class TestConfiguration2 {
464
431
465
432
@ Bean
466
433
ConnectionFactory customConnectionFactory () {
467
- return new ActiveMQConnectionFactory ( );
434
+ return mock ( ConnectionFactory . class );
468
435
}
469
436
470
437
}
@@ -595,12 +562,12 @@ static class TestConfiguration10 {
595
562
596
563
@ Bean
597
564
ConnectionFactory connectionFactory1 () {
598
- return new ActiveMQConnectionFactory ( );
565
+ return mock ( ConnectionFactory . class );
599
566
}
600
567
601
568
@ Bean
602
569
ConnectionFactory connectionFactory2 () {
603
- return new ActiveMQConnectionFactory ( );
570
+ return mock ( ConnectionFactory . class );
604
571
}
605
572
606
573
}
0 commit comments