From c08690254c76d16e908724858b56c4bfa67b69c5 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sat, 6 Apr 2013 19:39:28 +0200 Subject: [PATCH] Add unit test --- .../org/sonar/runner/impl/MockHttpServer.java | 118 ++++++++++++++++++ .../impl/MockHttpServerInterceptor.java | 54 ++++++++ .../runner/impl/ServerConnectionTest.java | 64 ++++++++++ 3 files changed, 236 insertions(+) create mode 100644 sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java create mode 100644 sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServerInterceptor.java create mode 100644 sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java new file mode 100644 index 0000000..49bd2fd --- /dev/null +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java @@ -0,0 +1,118 @@ +/* + * Sonar Runner - Implementation + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.runner.impl; + +import org.apache.commons.io.IOUtils; +import org.mortbay.jetty.Handler; +import org.mortbay.jetty.HttpConnection; +import org.mortbay.jetty.Request; +import org.mortbay.jetty.Server; +import org.mortbay.jetty.handler.AbstractHandler; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.io.IOException; + +import static javax.servlet.http.HttpServletResponse.SC_OK; +import static org.apache.commons.io.IOUtils.write; + +class MockHttpServer { + private Server server; + private String responseBody; + private String requestBody; + private String mockResponseData; + private int mockResponseStatus = SC_OK; + + public void start() throws Exception { + // 0 is random available port + server = new Server(0); + server.setHandler(getMockHandler()); + server.start(); + } + + /** + * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response. + * + * @return never null. + */ + public Handler getMockHandler() { + Handler handler = new AbstractHandler() { + + public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { + Request baseRequest = request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); + setResponseBody(getMockResponseData()); + setRequestBody(IOUtils.toString(baseRequest.getInputStream())); + response.setStatus(mockResponseStatus); + response.setContentType("text/xml;charset=utf-8"); + write(getResponseBody(), response.getOutputStream()); + baseRequest.setHandled(true); + } + }; + return handler; + } + + public void stop() { + try { + if (server != null) { + server.stop(); + } + } catch (Exception e) { + throw new IllegalStateException("Fail to stop HTTP server", e); + } + } + + public void setResponseBody(String responseBody) { + this.responseBody = responseBody; + } + + public String getResponseBody() { + return responseBody; + } + + public void setRequestBody(String requestBody) { + this.requestBody = requestBody; + } + + public String getRequestBody() { + return requestBody; + } + + public void setMockResponseData(String mockResponseData) { + this.mockResponseData = mockResponseData; + } + + public void setMockResponseStatus(int status) { + this.mockResponseStatus = status; + } + + public String getMockResponseData() { + return mockResponseData; + } + + public int getPort() { + return server.getConnectors()[0].getLocalPort(); + } + + public String getUrl() { + return "http://" + server.getConnectors()[0].getName(); + } +} diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServerInterceptor.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServerInterceptor.java new file mode 100644 index 0000000..a673b79 --- /dev/null +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServerInterceptor.java @@ -0,0 +1,54 @@ +/* + * Sonar Runner - Implementation + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.runner.impl; + +import org.junit.rules.ExternalResource; + +public class MockHttpServerInterceptor extends ExternalResource { + + private MockHttpServer server; + + @Override + protected final void before() throws Throwable { + server = new MockHttpServer(); + server.start(); + } + + @Override + protected void after() { + server.stop(); + } + + public void setMockResponseData(String data) { + server.setMockResponseData(data); + } + + public void setMockResponseStatus(int status) { + server.setMockResponseStatus(status); + } + + public String url() { + return server.getUrl(); + } + + public int getPort() { + return server.getPort(); + } +} \ No newline at end of file diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java new file mode 100644 index 0000000..ca722e3 --- /dev/null +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java @@ -0,0 +1,64 @@ +/* + * Sonar Runner - Implementation + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.runner.impl; + +import org.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.util.Properties; + +import static org.fest.assertions.Assertions.assertThat; + +public class ServerConnectionTest { + + @Rule + public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor(); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void should_download_to_string() throws Exception { + httpServer.setMockResponseData("abcde"); + Properties props = new Properties(); + props.setProperty("sonar.host.url", httpServer.url()); + + ServerConnection connection = ServerConnection.create(props); + String response = connection.downloadString("/batch/index.txt"); + + assertThat(response).isEqualTo("abcde"); + } + + @Test + public void should_download_to_file() throws Exception { + httpServer.setMockResponseData("abcde"); + Properties props = new Properties(); + props.setProperty("sonar.host.url", httpServer.url()); + + ServerConnection connection = ServerConnection.create(props); + File toFile = temp.newFile(); + connection.download("/batch/index.txt", toFile); + + assertThat(FileUtils.readFileToString(toFile)).isEqualTo("abcde"); + } +} -- 2.39.5