PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupportTests.java

https://gitlab.com/zouxc/spring-framework
Java | 316 lines | 227 code | 69 blank | 20 comment | 1 complexity | 54e3df0e3933ac6f9996145594525388 MD5 | raw file
  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. package org.springframework.web.socket.adapter.standard;
  17. import java.nio.ByteBuffer;
  18. import javax.websocket.DecodeException;
  19. import javax.websocket.Decoder;
  20. import javax.websocket.EncodeException;
  21. import javax.websocket.Encoder;
  22. import org.junit.After;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.context.annotation.Bean;
  29. import org.springframework.context.annotation.Configuration;
  30. import org.springframework.core.convert.ConversionService;
  31. import org.springframework.core.convert.ConverterNotFoundException;
  32. import org.springframework.core.convert.converter.Converter;
  33. import org.springframework.core.convert.support.DefaultConversionService;
  34. import org.springframework.core.convert.support.GenericConversionService;
  35. import org.springframework.web.context.WebApplicationContext;
  36. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  37. import org.springframework.web.socket.ContextLoaderTestUtils;
  38. import static org.hamcrest.Matchers.*;
  39. import static org.junit.Assert.*;
  40. /**
  41. * Test for {@link org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport}.
  42. *
  43. * @author Phillip Webb
  44. */
  45. public class ConvertingEncoderDecoderSupportTests {
  46. private static final String CONVERTED_TEXT = "_test";
  47. private static final ByteBuffer CONVERTED_BYTES = ByteBuffer.wrap("~test".getBytes());
  48. @Rule
  49. public ExpectedException thown = ExpectedException.none();
  50. private WebApplicationContext applicationContext;
  51. private MyType myType = new MyType("test");
  52. @Before
  53. public void setup() {
  54. setup(Config.class);
  55. }
  56. @After
  57. public void teardown() {
  58. ContextLoaderTestUtils.setCurrentWebApplicationContext(null);
  59. }
  60. private void setup(Class<?> configurationClass) {
  61. AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
  62. applicationContext.register(configurationClass);
  63. applicationContext.refresh();
  64. this.applicationContext = applicationContext;
  65. ContextLoaderTestUtils.setCurrentWebApplicationContext(this.applicationContext);
  66. }
  67. @Test
  68. public void encodeToText() throws Exception {
  69. assertThat(new MyTextEncoder().encode(myType), equalTo(CONVERTED_TEXT));
  70. }
  71. @Test
  72. public void encodeToTextCannotConvert() throws Exception {
  73. setup(NoConvertersConfig.class);
  74. thown.expect(EncodeException.class);
  75. thown.expectCause(isA(ConverterNotFoundException.class));
  76. new MyTextEncoder().encode(myType);
  77. }
  78. @Test
  79. public void encodeToBinary() throws Exception {
  80. assertThat(new MyBinaryEncoder().encode(myType).array(),
  81. equalTo(CONVERTED_BYTES.array()));
  82. }
  83. @Test
  84. public void encodeToBinaryCannotConvert() throws Exception {
  85. setup(NoConvertersConfig.class);
  86. thown.expect(EncodeException.class);
  87. thown.expectCause(isA(ConverterNotFoundException.class));
  88. new MyBinaryEncoder().encode(myType);
  89. }
  90. @Test
  91. public void decodeFromText() throws Exception {
  92. Decoder.Text<MyType> decoder = new MyTextDecoder();
  93. assertThat(decoder.willDecode(CONVERTED_TEXT), is(true));
  94. assertThat(decoder.decode(CONVERTED_TEXT), equalTo(myType));
  95. }
  96. @Test
  97. public void decodeFromTextCannotConvert() throws Exception {
  98. setup(NoConvertersConfig.class);
  99. Decoder.Text<MyType> decoder = new MyTextDecoder();
  100. assertThat(decoder.willDecode(CONVERTED_TEXT), is(false));
  101. thown.expect(DecodeException.class);
  102. thown.expectCause(isA(ConverterNotFoundException.class));
  103. decoder.decode(CONVERTED_TEXT);
  104. }
  105. @Test
  106. public void decodeFromBinary() throws Exception {
  107. Decoder.Binary<MyType> decoder = new MyBinaryDecoder();
  108. assertThat(decoder.willDecode(CONVERTED_BYTES), is(true));
  109. assertThat(decoder.decode(CONVERTED_BYTES), equalTo(myType));
  110. }
  111. @Test
  112. public void decodeFromBinaryCannotConvert() throws Exception {
  113. setup(NoConvertersConfig.class);
  114. Decoder.Binary<MyType> decoder = new MyBinaryDecoder();
  115. assertThat(decoder.willDecode(CONVERTED_BYTES), is(false));
  116. thown.expect(DecodeException.class);
  117. thown.expectCause(isA(ConverterNotFoundException.class));
  118. decoder.decode(CONVERTED_BYTES);
  119. }
  120. @Test
  121. public void encodeAndDecodeText() throws Exception {
  122. MyTextEncoderDecoder encoderDecoder = new MyTextEncoderDecoder();
  123. String encoded = encoderDecoder.encode(myType);
  124. assertThat(encoderDecoder.decode(encoded), equalTo(myType));
  125. }
  126. @Test
  127. public void encodeAndDecodeBytes() throws Exception {
  128. MyBinaryEncoderDecoder encoderDecoder = new MyBinaryEncoderDecoder();
  129. ByteBuffer encoded = encoderDecoder.encode(myType);
  130. assertThat(encoderDecoder.decode(encoded), equalTo(myType));
  131. }
  132. @Test
  133. public void autowiresIntoEncoder() throws Exception {
  134. WithAutowire withAutowire = new WithAutowire();
  135. withAutowire.init(null);
  136. assertThat(withAutowire.config, equalTo(applicationContext.getBean(Config.class)));
  137. }
  138. @Test
  139. public void cannotFindApplicationContext() throws Exception {
  140. ContextLoaderTestUtils.setCurrentWebApplicationContext(null);
  141. WithAutowire encoder = new WithAutowire();
  142. encoder.init(null);
  143. thown.expect(IllegalStateException.class);
  144. thown.expectMessage("Unable to locate the Spring ApplicationContext");
  145. encoder.encode(myType);
  146. }
  147. @Test
  148. public void cannotFindConversionService() throws Exception {
  149. setup(NoConfig.class);
  150. MyBinaryEncoder encoder = new MyBinaryEncoder();
  151. encoder.init(null);
  152. thown.expect(IllegalStateException.class);
  153. thown.expectMessage("Unable to find ConversionService");
  154. encoder.encode(myType);
  155. }
  156. @Configuration
  157. public static class Config {
  158. @Bean
  159. public ConversionService webSocketConversionService() {
  160. GenericConversionService conversionService = new DefaultConversionService();
  161. conversionService.addConverter(new MyTypeToStringConverter());
  162. conversionService.addConverter(new MyTypeToBytesConverter());
  163. conversionService.addConverter(new StringToMyTypeConverter());
  164. conversionService.addConverter(new BytesToMyTypeConverter());
  165. return conversionService;
  166. }
  167. }
  168. @Configuration
  169. public static class NoConvertersConfig {
  170. @Bean
  171. public ConversionService webSocketConversionService() {
  172. return new GenericConversionService();
  173. }
  174. }
  175. @Configuration
  176. public static class NoConfig {
  177. }
  178. public static class MyType {
  179. private String value;
  180. public MyType(String value) {
  181. this.value = value;
  182. }
  183. @Override
  184. public String toString() {
  185. return this.value;
  186. }
  187. @Override
  188. public int hashCode() {
  189. return value.hashCode();
  190. }
  191. @Override
  192. public boolean equals(Object obj) {
  193. if (obj instanceof MyType) {
  194. return ((MyType)obj).value.equals(value);
  195. }
  196. return false;
  197. }
  198. }
  199. private static class MyTypeToStringConverter implements Converter<MyType, String> {
  200. @Override
  201. public String convert(MyType source) {
  202. return "_" + source.toString();
  203. }
  204. }
  205. private static class MyTypeToBytesConverter implements Converter<MyType, byte[]> {
  206. @Override
  207. public byte[] convert(MyType source) {
  208. return ("~" + source.toString()).getBytes();
  209. }
  210. }
  211. private static class StringToMyTypeConverter implements Converter<String, MyType> {
  212. @Override
  213. public MyType convert(String source) {
  214. return new MyType(source.substring(1));
  215. }
  216. }
  217. private static class BytesToMyTypeConverter implements Converter<byte[], MyType> {
  218. @Override
  219. public MyType convert(byte[] source) {
  220. return new MyType(new String(source).substring(1));
  221. }
  222. }
  223. public static class MyTextEncoder extends
  224. ConvertingEncoderDecoderSupport.TextEncoder<MyType> {
  225. }
  226. public static class MyBinaryEncoder extends
  227. ConvertingEncoderDecoderSupport.BinaryEncoder<MyType> {
  228. }
  229. public static class MyTextDecoder extends
  230. ConvertingEncoderDecoderSupport.TextDecoder<MyType> {
  231. }
  232. public static class MyBinaryDecoder extends
  233. ConvertingEncoderDecoderSupport.BinaryDecoder<MyType> {
  234. }
  235. public static class MyTextEncoderDecoder extends
  236. ConvertingEncoderDecoderSupport<MyType, String> implements Encoder.Text<MyType>,
  237. Decoder.Text<MyType> {
  238. }
  239. public static class MyBinaryEncoderDecoder extends
  240. ConvertingEncoderDecoderSupport<MyType, ByteBuffer> implements Encoder.Binary<MyType>,
  241. Decoder.Binary<MyType> {
  242. }
  243. public static class WithAutowire extends ConvertingEncoderDecoderSupport.TextDecoder<MyType> {
  244. @Autowired
  245. private Config config;
  246. }
  247. }