spring-boot/spring-boot-samples/spring-boot-sample-websocke.../src/test/java/samples/websocket/tomcat/SampleWebSocketsApplication...

140 lines
5.0 KiB
Java
Raw Normal View History

2013-08-06 00:50:24 +08:00
/*
* Copyright 2012-2016 the original author or authors.
2013-08-06 00:50:24 +08:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2015-06-23 15:47:12 +08:00
package samples.websocket.tomcat;
2013-08-06 00:50:24 +08:00
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
2013-08-06 00:50:24 +08:00
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import samples.websocket.tomcat.client.GreetingService;
import samples.websocket.tomcat.client.SimpleClientWebSocketHandler;
import samples.websocket.tomcat.client.SimpleGreetingService;
import org.springframework.beans.factory.annotation.Value;
2013-08-06 00:50:24 +08:00
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
2016-03-14 21:19:55 +08:00
import org.springframework.boot.context.web.LocalServerPort;
Migrate existing tests from deprecated package Update the existing tests to use the relocated `spring-boot-test` classes. Restructuring was achieved using the following command: find . -type f -name '*.java' -exec sed -i '' \ -e s/org.springframework.boot.test.ConfigFileApplicationContextInitializer/\ org.springframework.boot.test.context.ConfigFileApplicationContextInitializer/g \ -e s/org.springframework.boot.test.EnvironmentTestUtils/\ org.springframework.boot.test.util.EnvironmentTestUtils/g \ -e s/org.springframework.boot.test.IntegrationTest/\ org.springframework.boot.test.context.IntegrationTest/g \ -e s/org.springframework.boot.test.IntegrationTestPropertiesListener/\ org.springframework.boot.test.context.IntegrationTestPropertiesListener/g \ -e s/org.springframework.boot.test.OutputCapture/\ org.springframework.boot.test.rule.OutputCapture/g \ -e s/org.springframework.boot.test.SpringApplicationConfiguration/\ org.springframework.boot.test.context.SpringApplicationConfiguration/g \ -e s/org.springframework.boot.test.SpringApplicationContextLoader/\ org.springframework.boot.test.context.SpringApplicationContextLoader/g \ -e s/org.springframework.boot.test.SpringBootMockServletContext/\ org.springframework.boot.test.mock.web.SpringBootMockServletContext/g \ -e s/org.springframework.boot.test.TestRestTemplate/\ org.springframework.boot.test.web.client.TestRestTemplate/g \ -e s/org.springframework.boot.test.WebIntegrationTest/\ org.springframework.boot.test.context.web.WebIntegrationTest/g {} \; See gh-5293
2016-03-12 13:59:37 +08:00
import org.springframework.boot.test.context.SpringApplicationConfiguration;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.context.ConfigurableApplicationContext;
2013-08-06 00:50:24 +08:00
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2013-08-06 00:50:24 +08:00
import org.springframework.web.socket.client.WebSocketConnectionManager;
2013-12-04 03:51:51 +08:00
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
2013-08-06 00:50:24 +08:00
import static org.assertj.core.api.Assertions.assertThat;
2014-04-23 16:42:10 +08:00
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(SampleTomcatWebSocketApplication.class)
@WebIntegrationTest(randomPort = true)
@DirtiesContext
public class SampleWebSocketsApplicationTests {
2013-08-06 00:50:24 +08:00
private static Log logger = LogFactory.getLog(SampleWebSocketsApplicationTests.class);
2013-08-06 00:50:24 +08:00
2016-03-14 21:19:55 +08:00
@LocalServerPort
private int port = 1234;
2014-04-23 16:42:10 +08:00
@Test
public void echoEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
2015-10-08 14:32:31 +08:00
.properties("websocket.uri:ws://localhost:" + this.port
+ "/echo/websocket")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context
.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(messagePayloadReference.get())
.isEqualTo("Did you say \"Hello world!\"?");
}
2013-08-06 00:50:24 +08:00
@Test
public void reverseEndpoint() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class)
2015-10-08 14:32:31 +08:00
.properties(
"websocket.uri:ws://localhost:" + this.port + "/reverse")
.run("--spring.main.web_environment=false");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context
.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(messagePayloadReference.get()).isEqualTo("Reversed: !dlrow olleH");
2013-08-06 00:50:24 +08:00
}
@Configuration
static class ClientConfiguration implements CommandLineRunner {
@Value("${websocket.uri}")
private String webSocketUri;
2014-01-22 08:21:00 +08:00
private final CountDownLatch latch = new CountDownLatch(1);
2013-08-06 00:50:24 +08:00
private final AtomicReference<String> messagePayload = new AtomicReference<String>();
2013-08-06 00:50:24 +08:00
@Override
public void run(String... args) throws Exception {
2013-10-09 11:25:10 +08:00
logger.info("Waiting for response: latch=" + this.latch.getCount());
if (this.latch.await(10, TimeUnit.SECONDS)) {
logger.info("Got response: " + this.messagePayload.get());
}
else {
logger.info("Response not received: latch=" + this.latch.getCount());
}
2013-08-06 00:50:24 +08:00
}
@Bean
public WebSocketConnectionManager wsConnectionManager() {
2013-10-09 11:25:10 +08:00
WebSocketConnectionManager manager = new WebSocketConnectionManager(client(),
handler(), this.webSocketUri);
2013-08-06 00:50:24 +08:00
manager.setAutoStartup(true);
return manager;
}
@Bean
public StandardWebSocketClient client() {
return new StandardWebSocketClient();
}
@Bean
public SimpleClientWebSocketHandler handler() {
return new SimpleClientWebSocketHandler(greetingService(), this.latch,
this.messagePayload);
2013-08-06 00:50:24 +08:00
}
@Bean
public GreetingService greetingService() {
return new SimpleGreetingService();
}
2014-12-04 07:46:52 +08:00
2013-08-06 00:50:24 +08:00
}
}