1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.socket.server.standard;
18
19 import javax.websocket.server.ServerEndpoint;
20
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.context.annotation.Bean;
27 import org.springframework.context.annotation.ComponentScan;
28 import org.springframework.context.annotation.Configuration;
29 import org.springframework.mock.web.test.MockServletContext;
30 import org.springframework.stereotype.Component;
31 import org.springframework.web.context.ContextLoader;
32 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
33
34 import static org.junit.Assert.*;
35
36 public class SpringConfiguratorTests {
37
38 private MockServletContext servletContext;
39
40 private ContextLoader contextLoader;
41
42 private AnnotationConfigWebApplicationContext webAppContext;
43
44 private SpringConfigurator configurator;
45
46
47 @Before
48 public void setup() {
49 this.servletContext = new MockServletContext();
50
51 this.webAppContext = new AnnotationConfigWebApplicationContext();
52 this.webAppContext.register(Config.class);
53
54 this.contextLoader = new ContextLoader(this.webAppContext);
55 this.contextLoader.initWebApplicationContext(this.servletContext);
56
57 this.configurator = new SpringConfigurator();
58 }
59
60 @After
61 public void destroy() {
62 this.contextLoader.closeWebApplicationContext(this.servletContext);
63 }
64
65
66 @Test
67 public void getEndpointPerConnection() throws Exception {
68 PerConnectionEchoEndpoint endpoint = this.configurator.getEndpointInstance(PerConnectionEchoEndpoint.class);
69 assertNotNull(endpoint);
70 }
71
72 @Test
73 public void getEndpointSingletonByType() throws Exception {
74 EchoEndpoint expected = this.webAppContext.getBean(EchoEndpoint.class);
75 EchoEndpoint actual = this.configurator.getEndpointInstance(EchoEndpoint.class);
76 assertSame(expected, actual);
77 }
78
79 @Test
80 public void getEndpointSingletonByComponentName() throws Exception {
81 ComponentEchoEndpoint expected = this.webAppContext.getBean(ComponentEchoEndpoint.class);
82 ComponentEchoEndpoint actual = this.configurator.getEndpointInstance(ComponentEchoEndpoint.class);
83 assertSame(expected, actual);
84 }
85
86
87 @Configuration
88 @ComponentScan(basePackageClasses=SpringConfiguratorTests.class)
89 static class Config {
90
91 @Bean
92 public EchoEndpoint javaConfigEndpoint() {
93 return new EchoEndpoint(echoService());
94 }
95
96 @Bean
97 public EchoService echoService() {
98 return new EchoService();
99 }
100 }
101
102 @ServerEndpoint("/echo")
103 private static class EchoEndpoint {
104
105 @SuppressWarnings("unused")
106 private final EchoService service;
107
108 @Autowired
109 public EchoEndpoint(EchoService service) {
110 this.service = service;
111 }
112 }
113
114 @Component("myComponentEchoEndpoint")
115 @ServerEndpoint("/echo")
116 private static class ComponentEchoEndpoint {
117
118 @SuppressWarnings("unused")
119 private final EchoService service;
120
121 @Autowired
122 public ComponentEchoEndpoint(EchoService service) {
123 this.service = service;
124 }
125 }
126
127 @ServerEndpoint("/echo")
128 private static class PerConnectionEchoEndpoint {
129
130 @SuppressWarnings("unused")
131 private final EchoService service;
132
133 @Autowired
134 public PerConnectionEchoEndpoint(EchoService service) {
135 this.service = service;
136 }
137 }
138
139 private static class EchoService { }
140
141 }