Java Code Examples for org.springframework.web.socket.handler.TestWebSocketSession#setOpen()

The following examples show how to use org.springframework.web.socket.handler.TestWebSocketSession#setOpen() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketMessageBrokerConfigurationSupportTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void clientInboundChannelSendMessage() throws Exception {
	ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
	TestChannel channel = config.getBean("clientInboundChannel", TestChannel.class);
	SubProtocolWebSocketHandler webSocketHandler = config.getBean(SubProtocolWebSocketHandler.class);

	List<ChannelInterceptor> interceptors = channel.getInterceptors();
	assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size()-1).getClass());

	TestWebSocketSession session = new TestWebSocketSession("s1");
	session.setOpen(true);
	webSocketHandler.afterConnectionEstablished(session);

	webSocketHandler.handleMessage(session,
			StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build());

	Message<?> message = channel.messages.get(0);
	StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
	assertNotNull(accessor);
	assertFalse(accessor.isMutable());
	assertEquals(SimpMessageType.MESSAGE, accessor.getMessageType());
	assertEquals("/foo", accessor.getDestination());
}
 
Example 2
Source File: WebSocketMessageBrokerConfigurationSupportTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void clientInboundChannelSendMessage() throws Exception {
	ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
	TestChannel channel = config.getBean("clientInboundChannel", TestChannel.class);
	SubProtocolWebSocketHandler webSocketHandler = config.getBean(SubProtocolWebSocketHandler.class);

	List<ChannelInterceptor> interceptors = channel.getInterceptors();
	assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size()-1).getClass());

	TestWebSocketSession session = new TestWebSocketSession("s1");
	session.setOpen(true);
	webSocketHandler.afterConnectionEstablished(session);

	webSocketHandler.handleMessage(session,
			StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build());

	Message<?> message = channel.messages.get(0);
	StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
	assertNotNull(accessor);
	assertFalse(accessor.isMutable());
	assertEquals(SimpMessageType.MESSAGE, accessor.getMessageType());
	assertEquals("/foo", accessor.getDestination());
}
 
Example 3
Source File: WebSocketMessageBrokerConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void clientInboundChannelSendMessage() throws Exception {
	ApplicationContext config = createConfig(TestChannelConfig.class, TestConfigurer.class);
	TestChannel channel = config.getBean("clientInboundChannel", TestChannel.class);
	SubProtocolWebSocketHandler webSocketHandler = config.getBean(SubProtocolWebSocketHandler.class);

	List<ChannelInterceptor> interceptors = channel.getInterceptors();
	assertEquals(ImmutableMessageChannelInterceptor.class, interceptors.get(interceptors.size()-1).getClass());

	TestWebSocketSession session = new TestWebSocketSession("s1");
	session.setOpen(true);
	webSocketHandler.afterConnectionEstablished(session);

	TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.SEND).headers("destination:/foo").build();
	webSocketHandler.handleMessage(session, textMessage);

	Message<?> message = channel.messages.get(0);
	StompHeaderAccessor accessor = StompHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
	assertNotNull(accessor);
	assertFalse(accessor.isMutable());
	assertEquals(SimpMessageType.MESSAGE, accessor.getMessageType());
	assertEquals("/foo", accessor.getDestination());
}
 
Example 4
Source File: SubProtocolWebSocketHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
	TestWebSocketSession session1 = new TestWebSocketSession("id1");
	TestWebSocketSession session2 = new TestWebSocketSession("id2");
	session1.setOpen(true);
	session2.setOpen(true);
	session1.setAcceptedProtocol("v12.stomp");
	session2.setAcceptedProtocol("v12.stomp");

	this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session1);
	this.webSocketHandler.afterConnectionEstablished(session2);

	DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
	Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
	DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
	DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));

	long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
	handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
	session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
	session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);

	this.webSocketHandler.start();
	this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));

	assertTrue(session1.isOpen());
	assertNull(session1.getCloseStatus());

	assertFalse(session2.isOpen());
	assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus());

	assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo,
			handlerAccessor.getPropertyValue("lastSessionCheckTime"));
}
 
Example 5
Source File: SubProtocolWebSocketHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
	TestWebSocketSession session1 = new TestWebSocketSession("id1");
	TestWebSocketSession session2 = new TestWebSocketSession("id2");
	session1.setOpen(true);
	session2.setOpen(true);
	session1.setAcceptedProtocol("v12.stomp");
	session2.setAcceptedProtocol("v12.stomp");

	this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session1);
	this.webSocketHandler.afterConnectionEstablished(session2);

	DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
	Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
	DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
	DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));

	long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
	handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
	session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
	session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);

	this.webSocketHandler.start();
	this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));

	assertTrue(session1.isOpen());
	assertNull(session1.getCloseStatus());

	assertFalse(session2.isOpen());
	assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus());

	assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo,
			handlerAccessor.getPropertyValue("lastSessionCheckTime"));
}
 
Example 6
Source File: SubProtocolWebSocketHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
	TestWebSocketSession session1 = new TestWebSocketSession("id1");
	TestWebSocketSession session2 = new TestWebSocketSession("id2");
	session1.setOpen(true);
	session2.setOpen(true);
	session1.setAcceptedProtocol("v12.stomp");
	session2.setAcceptedProtocol("v12.stomp");

	this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session1);
	this.webSocketHandler.afterConnectionEstablished(session2);

	DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
	Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
	DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
	DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));

	long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
	handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
	session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
	session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);

	this.webSocketHandler.start();
	this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));

	assertTrue(session1.isOpen());
	assertNull(session1.getCloseStatus());

	assertFalse(session2.isOpen());
	assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus());

	assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo,
			handlerAccessor.getPropertyValue("lastSessionCheckTime"));
}