2020import  java .lang .annotation .Retention ;
2121import  java .lang .annotation .RetentionPolicy ;
2222import  java .lang .annotation .Target ;
23+ import  java .lang .reflect .Method ;
2324
25+ import  org .junit .Rule ;
2426import  org .junit .Test ;
27+ import  org .junit .rules .ExpectedException ;
2528
29+ import  org .springframework .beans .factory .BeanCreationException ;
2630import  org .springframework .context .ConfigurableApplicationContext ;
2731import  org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2832import  org .springframework .context .annotation .Bean ;
3438import  org .springframework .jms .config .MessageListenerTestContainer ;
3539import  org .springframework .jms .config .MethodJmsListenerEndpoint ;
3640import  org .springframework .jms .listener .SimpleMessageListenerContainer ;
41+ import  org .springframework .messaging .handler .annotation .SendTo ;
3742import  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 ;
3847
48+ import  static  org .hamcrest .CoreMatchers .*;
3949import  static  org .junit .Assert .*;
50+ import  static  org .mockito .Mockito .*;
4051
4152/** 
4253 * @author Stephane Nicoll 
4354 * @author Juergen Hoeller 
4455 */ 
4556public  class  JmsListenerAnnotationBeanPostProcessorTests  {
4657
58+ 	@ Rule 
59+ 	public  final  ExpectedException  thrown  = ExpectedException .none ();
60+ 
4761	@ Test 
4862	public  void  simpleMessageListener () {
4963		ConfigurableApplicationContext  context  = new  AnnotationConfigApplicationContext (
@@ -73,10 +87,42 @@ public void metaAnnotationIsDiscovered() {
7387		ConfigurableApplicationContext  context  = new  AnnotationConfigApplicationContext (
7488				Config .class , MetaAnnotationTestBean .class );
7589
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 );
80126	}
81127
82128
@@ -102,7 +148,7 @@ public void handleIt(String body) {
102148	@ JmsListener (destination  = "metaTestQueue" )
103149	@ Target (ElementType .METHOD )
104150	@ Retention (RetentionPolicy .RUNTIME )
105- 	static   @interface FooListener  {
151+ 	@interface FooListener  {
106152	}
107153
108154
@@ -128,4 +174,47 @@ public JmsListenerContainerTestFactory testFactory() {
128174		}
129175	}
130176
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+ 
131220}
0 commit comments