From: Simon Brandhof Date: Sat, 27 Oct 2012 07:28:05 +0000 (+0200) Subject: Fix backward compatibility with org.sonar.batch.ServerMetadata X-Git-Tag: 3.4~434 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=73ae841b1d90ca0d51fc2d27ec3f9b235c75dd61;p=sonarqube.git Fix backward compatibility with org.sonar.batch.ServerMetadata --- 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 index 00000000000..5bba17f6905 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/ServerMetadata.java @@ -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(); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java index b8638c91453..bcb2e44d252 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java @@ -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); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java index 45aaf4180c1..ebe8342d221 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibility.java @@ -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)); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index 2634faec1ef..7b643b2c541 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -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 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 index 00000000000..9e7dc579532 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/WsConnector.java @@ -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(); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettings.java b/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettings.java index 0d54743e0f6..a2267e87cc7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettings.java +++ b/sonar-batch/src/main/java/org/sonar/batch/config/BootstrapSettings.java @@ -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 index 00000000000..0dbf698a057 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/ServerMetadataTest.java @@ -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"); + + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java index 6e9e1e4cade..bddab1a63ec 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseBatchCompatibilityTest.java @@ -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(); }