View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.handler;
18  
19  import org.junit.Test;
20  
21  import org.springframework.beans.BeanInstantiationException;
22  import org.springframework.beans.factory.annotation.Autowired;
23  import org.springframework.context.ConfigurableApplicationContext;
24  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25  import org.springframework.context.annotation.Bean;
26  import org.springframework.context.annotation.Configuration;
27  
28  import static org.junit.Assert.*;
29  
30  /**
31   * Test fixture for {@link BeanCreatingHandlerProvider}.
32   *
33   * @author Rossen Stoyanchev
34   */
35  public class BeanCreatingHandlerProviderTests {
36  
37  
38  	@Test
39  	public void getHandlerSimpleInstantiation() {
40  
41  		BeanCreatingHandlerProvider<SimpleEchoHandler> provider =
42  				new BeanCreatingHandlerProvider<SimpleEchoHandler>(SimpleEchoHandler.class);
43  
44  		assertNotNull(provider.getHandler());
45  	}
46  
47  	@Test
48  	public void getHandlerWithBeanFactory() {
49  
50  		@SuppressWarnings("resource")
51  		ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
52  
53  		BeanCreatingHandlerProvider<EchoHandler> provider =
54  				new BeanCreatingHandlerProvider<EchoHandler>(EchoHandler.class);
55  		provider.setBeanFactory(context.getBeanFactory());
56  
57  		assertNotNull(provider.getHandler());
58  	}
59  
60  	@Test(expected=BeanInstantiationException.class)
61  	public void getHandlerNoBeanFactory() {
62  
63  		BeanCreatingHandlerProvider<EchoHandler> provider =
64  				new BeanCreatingHandlerProvider<EchoHandler>(EchoHandler.class);
65  
66  		provider.getHandler();
67  	}
68  
69  
70  	@Configuration
71  	static class Config {
72  
73  		@Bean
74  		public EchoService echoService() {
75  			return new EchoService();
76  		}
77  	}
78  
79  	public static class SimpleEchoHandler {
80  	}
81  
82  	private static class EchoHandler {
83  
84  		@SuppressWarnings("unused")
85  		private final EchoService service;
86  
87  		@Autowired
88  		public EchoHandler(EchoService service) {
89  			this.service = service;
90  		}
91  	}
92  
93  	private static class EchoService {	}
94  
95  }