--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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");
+ }
+}