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.Endpoint;
20 import javax.websocket.EndpointConfig;
21 import javax.websocket.Session;
22
23 import org.junit.Test;
24
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.context.ConfigurableApplicationContext;
27 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
28 import org.springframework.context.annotation.Bean;
29 import org.springframework.context.annotation.Configuration;
30
31 import static org.junit.Assert.*;
32
33
34
35
36
37
38 public class ServerEndpointRegistrationTests {
39
40
41 @Test
42 public void endpointPerConnection() throws Exception {
43
44 @SuppressWarnings("resource")
45 ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
46
47 ServerEndpointRegistration registration = new ServerEndpointRegistration("/path", EchoEndpoint.class);
48 registration.setBeanFactory(context.getBeanFactory());
49
50 EchoEndpoint endpoint = registration.getConfigurator().getEndpointInstance(EchoEndpoint.class);
51
52 assertNotNull(endpoint);
53 }
54
55 @Test
56 public void endpointSingleton() throws Exception {
57
58 EchoEndpoint endpoint = new EchoEndpoint(new EchoService());
59 ServerEndpointRegistration registration = new ServerEndpointRegistration("/path", endpoint);
60
61 EchoEndpoint actual = registration.getConfigurator().getEndpointInstance(EchoEndpoint.class);
62
63 assertSame(endpoint, actual);
64 }
65
66
67 @Configuration
68 static class Config {
69
70 @Bean
71 public EchoService echoService() {
72 return new EchoService();
73 }
74 }
75
76 private static class EchoEndpoint extends Endpoint {
77
78 @SuppressWarnings("unused")
79 private final EchoService service;
80
81 @Autowired
82 public EchoEndpoint(EchoService service) {
83 this.service = service;
84 }
85
86 @Override
87 public void onOpen(Session session, EndpointConfig config) {
88 }
89 }
90
91 private static class EchoService { }
92
93 }