1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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 }