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.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   * Test fixture for {@link ServerEndpointRegistration}.
35   *
36   * @author Rossen Stoyanchev
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  }