aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-04-06 19:39:28 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2013-04-06 19:39:28 +0200
commitc08690254c76d16e908724858b56c4bfa67b69c5 (patch)
tree6265775ec03c309cf1a3c11f5c94fa3c63816324
parent5322f84d9d3eafa31effdd561c77c7db5b02b04d (diff)
downloadsonar-scanner-cli-c08690254c76d16e908724858b56c4bfa67b69c5.tar.gz
sonar-scanner-cli-c08690254c76d16e908724858b56c4bfa67b69c5.zip
Add unit test
-rw-r--r--sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java118
-rw-r--r--sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServerInterceptor.java54
-rw-r--r--sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java64
3 files changed, 236 insertions, 0 deletions
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 <code>null</code>.
+ */
+ 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");
+ }
+}