]> source.dussan.org Git - sonarqube.git/commitdiff
Fix backward compatibility with org.sonar.batch.ServerMetadata
authorSimon Brandhof <simon.brandhof@gmail.com>
Sat, 27 Oct 2012 07:28:05 +0000 (09:28 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Sat, 27 Oct 2012 07:28:05 +0000 (09:28 +0200)
sonar-batch/src/main/java/org/sonar/batch/ServerMetadata.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsConnector.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettings.java
sonar-batch/src/test/java/org/sonar/batch/ServerMetadataTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java

diff --git a/sonar-batch/src/main/java/org/sonar/batch/ServerMetadata.java b/sonar-batch/src/main/java/org/sonar/batch/ServerMetadata.java
new file mode 100644 (file)
index 0000000..5bba17f
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.batch;
+
+import org.sonar.api.BatchComponent;
+import org.sonar.api.platform.Server;
+import org.sonar.batch.bootstrap.ServerClient;
+
+import java.util.Date;
+
+/**
+ * @deprecated replaced by ServerClient since version 3.4. Plugins should use org.sonar.api.platform.Server
+ */
+@Deprecated
+public class ServerMetadata extends Server implements BatchComponent {
+  private ServerClient server;
+
+  public ServerMetadata(ServerClient server) {
+    this.server = server;
+  }
+
+  @Override
+  public String getId() {
+    return server.getId();
+  }
+
+  @Override
+  public String getVersion() {
+    return server.getVersion();
+  }
+
+  @Override
+  public Date getStartedAt() {
+    return server.getStartedAt();
+  }
+
+  /**
+   * @return the server URL when executed from batch, else null.
+   * @since 2.4
+   */
+  @Override
+  public String getURL() {
+    return server.getURL();
+  }
+
+  /**
+   * @since 2.10
+   */
+  @Override
+  public String getPermanentServerId() {
+    return server.getPermanentServerId();
+  }
+}
index b8638c914535d41a241174969c8d70bb13efc24e..bcb2e44d252156726392cfc770f86684df2322a6 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.utils.HttpDownloader;
 import org.sonar.api.utils.UriReader;
 import org.sonar.batch.FakeMavenPluginExecutor;
 import org.sonar.batch.MavenPluginExecutor;
+import org.sonar.batch.ServerMetadata;
 import org.sonar.batch.config.BatchDatabaseSettingsLoader;
 import org.sonar.batch.config.BootstrapSettings;
 import org.sonar.batch.local.DryRunDatabase;
@@ -39,6 +40,7 @@ import org.sonar.core.persistence.MyBatis;
 import org.sonar.jpa.session.DatabaseSessionProvider;
 import org.sonar.jpa.session.DefaultDatabaseConnector;
 import org.sonar.jpa.session.ThreadLocalDatabaseSessionFactory;
+import org.sonar.wsclient.Sonar;
 
 /**
  * Level 1 components
@@ -64,6 +66,9 @@ public class BootstrapModule extends Module {
     container.addSingleton(DryRunExporter.class);
     container.addSingleton(Logback.class);
     container.addSingleton(ServerClient.class);
+    container.addSingleton(ServerMetadata.class);
+    container.addSingleton(WsConnector.class);
+    container.addSingleton(Sonar.class);
     container.addSingleton(TempDirectories.class);
     container.addSingleton(HttpDownloader.class);
     container.addSingleton(UriReader.class);
index 45aaf4180c1e7bfa0ef7002d75633f3172ed0895..ebe8342d2216e1d550c49bd66845f194cdf4e61a 100644 (file)
@@ -49,14 +49,7 @@ public class DatabaseBatchCompatibility implements BatchComponent {
   }
 
   private void checkCorrectServerId() {
-    String remoteServerId;
-    try {
-      remoteServerId = server.getServerId();
-    } catch (IOException e) {
-      throw new SonarException("Impossible to get the ID of the remote server: " + server.getURL(), e);
-    }
-
-    if (!version.getSonarCoreId().equals(remoteServerId)) {
+    if (!version.getSonarCoreId().equals(server.getServerId())) {
       StringBuilder message = new StringBuilder("The current batch process and the configured remote server do not share the same DB configuration.\n");
       message.append("\t- Batch side: ");
       message.append(settings.getString(DatabaseProperties.PROP_URL));
index 2634faec1ef53c840db74b810e9f33274965d53b..7b643b2c5415d696cf1fd6ae9b38994f6e6e3611 100644 (file)
@@ -48,11 +48,11 @@ import java.util.Date;
  */
 public class ServerClient extends Server implements BatchComponent {
   private Settings settings;
-  private EnvironmentInformation env;
+  private HttpDownloader.BaseHttpDownloader downloader;
 
   public ServerClient(Settings settings, EnvironmentInformation env) {
     this.settings = settings;
-    this.env = env;
+    this.downloader = new HttpDownloader.BaseHttpDownloader(settings, env.toString());
   }
 
   @Override
@@ -89,7 +89,7 @@ public class ServerClient extends Server implements BatchComponent {
     return settings.getString(CoreProperties.PERMANENT_SERVER_ID);
   }
 
-  public String getServerId() throws IOException {
+  public String getServerId() {
     String remoteServerInfo = request("/api/server");
     // don't use JSON utilities to extract ID from such a small string
     return extractServerId(remoteServerInfo);
@@ -126,7 +126,6 @@ public class ServerClient extends Server implements BatchComponent {
     String login = settings.getString(CoreProperties.LOGIN);
 
     try {
-      HttpDownloader.BaseHttpDownloader downloader = new HttpDownloader.BaseHttpDownloader(settings, env.toString());
       InputSupplier<InputStream> inputSupplier;
       if (Strings.isNullOrEmpty(login)) {
         inputSupplier = downloader.newInputSupplier(uri);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsConnector.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsConnector.java
new file mode 100644 (file)
index 0000000..9e7dc57
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.batch.bootstrap;
+
+import org.sonar.wsclient.connectors.Connector;
+import org.sonar.wsclient.services.CreateQuery;
+import org.sonar.wsclient.services.DeleteQuery;
+import org.sonar.wsclient.services.Query;
+import org.sonar.wsclient.services.UpdateQuery;
+
+/**
+ * @since 3.4
+ */
+public class WsConnector extends Connector {
+
+  private ServerClient server;
+
+  public WsConnector(ServerClient server) {
+    this.server = server;
+  }
+
+  /**
+   * @return JSON response or null if 404 NOT FOUND error
+   * @throws org.sonar.wsclient.connectors.ConnectionException
+   *          if connection error or HTTP status not in (200, 404)
+   */
+  @Override
+  public String execute(Query<?> query) {
+    return server.request(query.getUrl());
+  }
+
+  /**
+   * @return JSON response or null if 404 NOT FOUND error
+   * @since 2.2
+   */
+  @Override
+  public String execute(CreateQuery<?> query) {
+    throw new UnsupportedOperationException();
+  }
+
+  /**
+   * @return JSON response or null if 404 NOT FOUND error
+   * @since 2.2
+   */
+  @Override
+  public String execute(DeleteQuery query) {
+    throw new UnsupportedOperationException();
+  }
+
+  /**
+   * @return JSON response or null if 404 NOT FOUND error
+   * @since 2.6
+   */
+  @Override
+  public String execute(UpdateQuery<?> query) {
+    throw new UnsupportedOperationException();
+  }
+}
index 0d54743e0f6f5c01996138f389d13f62969d083c..a2267e87cc7bc098c2ef855e7c06c23361c08e00 100644 (file)
@@ -39,7 +39,7 @@ public final class BootstrapSettings extends Settings {
     load();
   }
 
-  public BootstrapSettings load() {
+  private BootstrapSettings load() {
     clear();
 
     // order is important -> bottom-up. The last one overrides all the others.
diff --git a/sonar-batch/src/test/java/org/sonar/batch/ServerMetadataTest.java b/sonar-batch/src/test/java/org/sonar/batch/ServerMetadataTest.java
new file mode 100644 (file)
index 0000000..0dbf698
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.batch;
+
+import org.junit.Test;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.batch.bootstrap.ServerClient;
+
+import java.util.Date;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ServerMetadataTest {
+  @Test
+  public void should_proxy_server_client() {
+    ServerClient client = mock(ServerClient.class);
+    when(client.getId()).thenReturn("id1");
+    when(client.getPermanentServerId()).thenReturn("pid1");
+    Date startedAt = DateUtils.parseDate("2012-05-18");
+    when(client.getStartedAt()).thenReturn(startedAt);
+    when(client.getURL()).thenReturn("http://foo");
+    when(client.getVersion()).thenReturn("v1");
+
+    ServerMetadata metadata = new ServerMetadata(client);
+
+    assertThat(metadata.getId()).isEqualTo("id1");
+    assertThat(metadata.getPermanentServerId()).isEqualTo("pid1");
+    assertThat(metadata.getStartedAt()).isEqualTo(startedAt);
+    assertThat(metadata.getURL()).isEqualTo("http://foo");
+    assertThat(metadata.getVersion()).isEqualTo("v1");
+
+  }
+}
index 6e9e1e4cade5749537457fc608e9548184057379..bddab1a63ec9e438eb0ba9f36ad3dfe737d64f6d 100644 (file)
@@ -105,10 +105,9 @@ public class DatabaseBatchCompatibilityTest {
 
   @Test
   public void shouldFailIfCantGetServerId() throws Exception {
-    when(server.getServerId()).thenThrow(new IOException());
+    when(server.getServerId()).thenThrow(new IllegalStateException());
 
-    thrown.expect(SonarException.class);
-    thrown.expectMessage("Impossible to get the ID of the remote server: http://localhost:9000");
+    thrown.expect(IllegalStateException.class);
 
     new DatabaseBatchCompatibility(mock(DatabaseVersion.class), server, settings).start();
   }