Move server testing infrastructure to test sources

This commit is contained in:
Brian Clozel
2016-07-12 17:36:30 +02:00
parent d736245f28
commit e906a78e0f
12 changed files with 18 additions and 18 deletions

View File

@@ -23,12 +23,12 @@ import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.http.server.reactive.boot.HttpServer;
import org.springframework.http.server.reactive.boot.JettyHttpServer;
import org.springframework.http.server.reactive.boot.ReactorHttpServer;
import org.springframework.http.server.reactive.boot.RxNettyHttpServer;
import org.springframework.http.server.reactive.boot.TomcatHttpServer;
import org.springframework.http.server.reactive.boot.UndertowHttpServer;
import org.springframework.http.server.reactive.bootstrap.HttpServer;
import org.springframework.http.server.reactive.bootstrap.JettyHttpServer;
import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer;
import org.springframework.http.server.reactive.bootstrap.RxNettyHttpServer;
import org.springframework.http.server.reactive.bootstrap.TomcatHttpServer;
import org.springframework.http.server.reactive.bootstrap.UndertowHttpServer;
import org.springframework.util.SocketUtils;

View File

@@ -25,7 +25,7 @@ import reactor.core.publisher.Mono;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.server.reactive.boot.ReactorHttpServer;
import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

View File

@@ -28,8 +28,8 @@ import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.boot.ReactorHttpServer;
import org.springframework.http.server.reactive.boot.UndertowHttpServer;
import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer;
import org.springframework.http.server.reactive.bootstrap.UndertowHttpServer;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* 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.
*/
package org.springframework.http.server.reactive.bootstrap;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.Lifecycle;
import org.springframework.http.server.reactive.HttpHandler;
/**
* @author Rossen Stoyanchev
*/
public interface HttpServer extends InitializingBean, Lifecycle {
void setHost(String host);
void setPort(int port);
void setHandler(HttpHandler handler);
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* 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.
*/
package org.springframework.http.server.reactive.bootstrap;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.util.SocketUtils;
/**
* @author Rossen Stoyanchev
*/
public class HttpServerSupport {
private String host = "0.0.0.0";
private int port = -1;
private HttpHandler httpHandler;
public void setHost(String host) {
this.host = host;
}
public String getHost() {
return host;
}
public void setPort(int port) {
this.port = port;
}
public int getPort() {
if(this.port == -1) {
this.port = SocketUtils.findAvailableTcpPort(8080);
}
return this.port;
}
public void setHandler(HttpHandler handler) {
this.httpHandler = handler;
}
public HttpHandler getHttpHandler() {
return this.httpHandler;
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* 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.
*/
package org.springframework.http.server.reactive.bootstrap;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
import org.springframework.util.Assert;
/**
* @author Rossen Stoyanchev
*/
public class JettyHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {
private Server jettyServer;
private boolean running;
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void afterPropertiesSet() throws Exception {
this.jettyServer = new Server();
Assert.notNull(getHttpHandler());
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter();
servlet.setHandler(getHttpHandler());
ServletHolder servletHolder = new ServletHolder(servlet);
ServletContextHandler contextHandler = new ServletContextHandler(this.jettyServer, "", false, false);
contextHandler.addServlet(servletHolder, "/");
ServerConnector connector = new ServerConnector(this.jettyServer);
connector.setHost(getHost());
connector.setPort(getPort());
this.jettyServer.addConnector(connector);
}
@Override
public void start() {
if (!this.running) {
try {
this.running = true;
this.jettyServer.start();
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
@Override
public void stop() {
if (this.running) {
try {
this.running = false;
jettyServer.stop();
jettyServer.destroy();
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
*
* 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.
*/
package org.springframework.http.server.reactive.bootstrap;
import reactor.core.flow.Loopback;
import reactor.core.state.Completable;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.util.Assert;
/**
* @author Stephane Maldini
*/
public class ReactorHttpServer extends HttpServerSupport
implements HttpServer, Loopback, Completable {
private ReactorHttpHandlerAdapter reactorHandler;
private reactor.io.netty.http.HttpServer reactorServer;
private boolean running;
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(getHttpHandler());
this.reactorHandler = new ReactorHttpHandlerAdapter(getHttpHandler());
this.reactorServer = reactor.io.netty.http.HttpServer.create(getHost(), getPort());
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public Object connectedInput() {
return reactorServer;
}
@Override
public Object connectedOutput() {
return reactorServer;
}
@Override
public boolean isStarted() {
return running;
}
@Override
public boolean isTerminated() {
return !running;
}
@Override
public void start() {
if (!this.running) {
try {
this.reactorServer.startAndAwait(reactorHandler);
this.running = true;
}
catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
}
}
@Override
public void stop() {
if (this.running) {
this.reactorServer.shutdown();
this.running = false;
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* 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.
*/
package org.springframework.http.server.reactive.bootstrap;
import java.net.InetSocketAddress;
import io.netty.buffer.ByteBuf;
import org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter;
import org.springframework.util.Assert;
/**
* @author Rossen Stoyanchev
*/
public class RxNettyHttpServer extends HttpServerSupport implements HttpServer {
private RxNettyHttpHandlerAdapter rxNettyHandler;
private io.reactivex.netty.protocol.http.server.HttpServer<ByteBuf, ByteBuf> rxNettyServer;
private boolean running;
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(getHttpHandler());
this.rxNettyHandler = new RxNettyHttpHandlerAdapter(getHttpHandler());
this.rxNettyServer = io.reactivex.netty.protocol.http.server.HttpServer
.newServer(new InetSocketAddress(getHost(), getPort()));
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void start() {
if (!this.running) {
this.running = true;
this.rxNettyServer.start(this.rxNettyHandler);
}
}
@Override
public void stop() {
if (this.running) {
this.running = false;
this.rxNettyServer.shutdown();
}
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* 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.
*/
package org.springframework.http.server.reactive.bootstrap;
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
import org.springframework.util.Assert;
/**
* @author Rossen Stoyanchev
*/
public class TomcatHttpServer extends HttpServerSupport implements InitializingBean, HttpServer {
private Tomcat tomcatServer;
private boolean running;
private String baseDir;
public TomcatHttpServer() {
}
public TomcatHttpServer(String baseDir) {
this.baseDir = baseDir;
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void afterPropertiesSet() throws Exception {
this.tomcatServer = new Tomcat();
if (this.baseDir != null) {
this.tomcatServer.setBaseDir(baseDir);
}
this.tomcatServer.setHostname(getHost());
this.tomcatServer.setPort(getPort());
Assert.notNull(getHttpHandler());
ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter();
servlet.setHandler(getHttpHandler());
File base = new File(System.getProperty("java.io.tmpdir"));
Context rootContext = tomcatServer.addContext("", base.getAbsolutePath());
Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
rootContext.addServletMapping("/", "httpHandlerServlet");
}
@Override
public void start() {
if (!this.running) {
try {
this.running = true;
this.tomcatServer.start();
}
catch (LifecycleException ex) {
throw new IllegalStateException(ex);
}
}
}
@Override
public void stop() {
if (this.running) {
try {
this.running = false;
this.tomcatServer.stop();
this.tomcatServer.destroy();
}
catch (LifecycleException ex) {
throw new IllegalStateException(ex);
}
}
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* 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.
*/
package org.springframework.http.server.reactive.bootstrap;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
import org.springframework.util.Assert;
/**
* @author Marek Hawrylczak
*/
public class UndertowHttpServer extends HttpServerSupport implements HttpServer {
private Undertow server;
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
private boolean running;
public void setDataBufferFactory(DataBufferFactory dataBufferFactory) {
this.dataBufferFactory = dataBufferFactory;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(getHttpHandler());
HttpHandler handler =
new UndertowHttpHandlerAdapter(getHttpHandler(), dataBufferFactory);
this.server = Undertow.builder().addHttpListener(getPort(), getHost())
.setHandler(handler).build();
}
@Override
public void start() {
if (!this.running) {
this.server.start();
this.running = true;
}
}
@Override
public void stop() {
if (this.running) {
this.server.stop();
this.running = false;
}
}
@Override
public boolean isRunning() {
return this.running;
}
}

View File

@@ -0,0 +1,5 @@
/**
* This package contains temporary interfaces and classes for running embedded servers.
* They are expected to be replaced by an upcoming Spring Boot support.
*/
package org.springframework.http.server.reactive.bootstrap;