20
20
import java .lang .annotation .Retention ;
21
21
import java .lang .annotation .RetentionPolicy ;
22
22
import java .lang .annotation .Target ;
23
+ import java .lang .reflect .Method ;
23
24
25
+ import org .junit .Rule ;
24
26
import org .junit .Test ;
27
+ import org .junit .rules .ExpectedException ;
25
28
29
+ import org .springframework .beans .factory .BeanCreationException ;
26
30
import org .springframework .context .ConfigurableApplicationContext ;
27
31
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
28
32
import org .springframework .context .annotation .Bean ;
34
38
import org .springframework .jms .config .MessageListenerTestContainer ;
35
39
import org .springframework .jms .config .MethodJmsListenerEndpoint ;
36
40
import org .springframework .jms .listener .SimpleMessageListenerContainer ;
41
+ import org .springframework .messaging .handler .annotation .SendTo ;
37
42
import org .springframework .stereotype .Component ;
43
+ import org .springframework .transaction .PlatformTransactionManager ;
44
+ import org .springframework .transaction .annotation .EnableTransactionManagement ;
45
+ import org .springframework .transaction .annotation .Transactional ;
46
+ import org .springframework .util .ReflectionUtils ;
38
47
48
+ import static org .hamcrest .CoreMatchers .*;
39
49
import static org .junit .Assert .*;
50
+ import static org .mockito .Mockito .*;
40
51
41
52
/**
42
53
* @author Stephane Nicoll
43
54
* @author Juergen Hoeller
44
55
*/
45
56
public class JmsListenerAnnotationBeanPostProcessorTests {
46
57
58
+ @ Rule
59
+ public final ExpectedException thrown = ExpectedException .none ();
60
+
47
61
@ Test
48
62
public void simpleMessageListener () {
49
63
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (
@@ -73,10 +87,42 @@ public void metaAnnotationIsDiscovered() {
73
87
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (
74
88
Config .class , MetaAnnotationTestBean .class );
75
89
76
- JmsListenerContainerTestFactory factory = context .getBean (JmsListenerContainerTestFactory .class );
77
- assertEquals ("one container should have been registered" , 1 , factory .getListenerContainers ().size ());
78
- JmsListenerEndpoint endpoint = factory .getListenerContainers ().get (0 ).getEndpoint ();
79
- assertEquals ("metaTestQueue" , ((AbstractJmsListenerEndpoint ) endpoint ).getDestination ());
90
+ try {
91
+ JmsListenerContainerTestFactory factory = context .getBean (JmsListenerContainerTestFactory .class );
92
+ assertEquals ("one container should have been registered" , 1 , factory .getListenerContainers ().size ());
93
+ JmsListenerEndpoint endpoint = factory .getListenerContainers ().get (0 ).getEndpoint ();
94
+ assertEquals ("metaTestQueue" , ((AbstractJmsListenerEndpoint ) endpoint ).getDestination ());
95
+ }
96
+ finally {
97
+ context .close ();
98
+ }
99
+ }
100
+
101
+ @ Test
102
+ public void sendToAnnotationFoundOnProxy () {
103
+ ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (
104
+ Config .class , ProxyConfig .class , ProxyTestBean .class );
105
+ try {
106
+ JmsListenerContainerTestFactory factory = context .getBean (JmsListenerContainerTestFactory .class );
107
+ assertEquals ("one container should have been registered" , 1 , factory .getListenerContainers ().size ());
108
+ JmsListenerEndpoint endpoint = factory .getListenerContainers ().get (0 ).getEndpoint ();
109
+ Method m = ReflectionUtils .findMethod (endpoint .getClass (), "getDefaultResponseDestination" );
110
+ ReflectionUtils .makeAccessible (m );
111
+ Object destination = ReflectionUtils .invokeMethod (m , endpoint );
112
+ assertEquals ("SendTo annotation not found on proxy" , "foobar" , destination );
113
+ }
114
+ finally {
115
+ context .close ();
116
+ }
117
+ }
118
+
119
+ @ Test
120
+ public void invalidProxy () {
121
+ thrown .expect (BeanCreationException .class );
122
+ thrown .expectCause (is (instanceOf (IllegalStateException .class )));
123
+ thrown .expectMessage ("handleIt2" );
124
+ new AnnotationConfigApplicationContext (
125
+ Config .class , ProxyConfig .class , InvalidProxyTestBean .class );
80
126
}
81
127
82
128
@@ -102,7 +148,7 @@ public void handleIt(String body) {
102
148
@ JmsListener (destination = "metaTestQueue" )
103
149
@ Target (ElementType .METHOD )
104
150
@ Retention (RetentionPolicy .RUNTIME )
105
- static @interface FooListener {
151
+ @interface FooListener {
106
152
}
107
153
108
154
@@ -128,4 +174,47 @@ public JmsListenerContainerTestFactory testFactory() {
128
174
}
129
175
}
130
176
177
+ @ Configuration
178
+ @ EnableTransactionManagement
179
+ static class ProxyConfig {
180
+
181
+ @ Bean
182
+ public PlatformTransactionManager transactionManager () {
183
+ return mock (PlatformTransactionManager .class );
184
+ }
185
+
186
+ }
187
+
188
+ interface SimpleService {
189
+
190
+ void handleIt (String body );
191
+
192
+ }
193
+
194
+ @ Component
195
+ static class ProxyTestBean implements SimpleService {
196
+
197
+ @ Override
198
+ @ Transactional
199
+ @ JmsListener (destination = "testQueue" )
200
+ @ SendTo ("foobar" )
201
+ public void handleIt (String body ) {
202
+
203
+ }
204
+ }
205
+
206
+ @ Component
207
+ static class InvalidProxyTestBean implements SimpleService {
208
+
209
+ @ Override
210
+ public void handleIt (String body ) {
211
+ }
212
+
213
+ @ Transactional
214
+ @ JmsListener (destination = "testQueue" )
215
+ @ SendTo ("foobar" )
216
+ public void handleIt2 (String body ) {
217
+ }
218
+ }
219
+
131
220
}
0 commit comments