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 );
@@ -423,16 +400,6 @@ void testPubSubDomainOverride() {
423
400
});
424
401
}
425
402
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
-
436
403
@ Test
437
404
void enableJmsAutomatically () {
438
405
this .contextRunner .withUserConfiguration (NoEnableJmsConfiguration .class )
@@ -444,7 +411,7 @@ void enableJmsAutomatically() {
444
411
@ Test
445
412
void runtimeHintsAreRegisteredForBindingOfAcknowledgeMode () {
446
413
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ()) {
447
- context .register (ArtemisAutoConfiguration .class , JmsAutoConfiguration .class );
414
+ context .register (TestConfiguration2 .class , JmsAutoConfiguration .class );
448
415
TestGenerationContext generationContext = new TestGenerationContext ();
449
416
new ApplicationContextAotGenerator ().processAheadOfTime (context , generationContext );
450
417
assertThat (RuntimeHintsPredicates .reflection ().onMethod (AcknowledgeMode .class , "of" ).invoke ())
@@ -462,7 +429,7 @@ static class TestConfiguration2 {
462
429
463
430
@ Bean
464
431
ConnectionFactory customConnectionFactory () {
465
- return new ActiveMQConnectionFactory ( );
432
+ return mock ( ConnectionFactory . class );
466
433
}
467
434
468
435
}
@@ -593,12 +560,12 @@ static class TestConfiguration10 {
593
560
594
561
@ Bean
595
562
ConnectionFactory connectionFactory1 () {
596
- return new ActiveMQConnectionFactory ( );
563
+ return mock ( ConnectionFactory . class );
597
564
}
598
565
599
566
@ Bean
600
567
ConnectionFactory connectionFactory2 () {
601
- return new ActiveMQConnectionFactory ( );
568
+ return mock ( ConnectionFactory . class );
602
569
}
603
570
604
571
}
0 commit comments