From: Jean-Baptiste Lievremont Date: Mon, 7 Apr 2014 10:06:59 +0000 (+0200) Subject: SONAR-5105 Verify config using DB connection instead of settings that come from WS X-Git-Tag: 4.3~119 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f03d90c5d72b1f258d1a4ceb594b261a1204cbf7;p=sonarqube.git SONAR-5105 Verify config using DB connection instead of settings that come from WS --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseCompatibility.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseCompatibility.java index 8f2fcbbaed3..e2e9ccc1ada 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseCompatibility.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DatabaseCompatibility.java @@ -25,6 +25,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.database.DatabaseProperties; import org.sonar.api.utils.MessageException; import org.sonar.core.persistence.DatabaseVersion; +import org.sonar.core.properties.PropertiesDao; /** * Detects if database is not up-to-date with the version required by the batch. @@ -33,13 +34,15 @@ public class DatabaseCompatibility implements BatchComponent { private DatabaseVersion version; private Settings settings; + private PropertiesDao propertiesDao; private ServerMetadata server; private AnalysisMode analysisMode; - public DatabaseCompatibility(DatabaseVersion version, ServerMetadata server, Settings settings, AnalysisMode mode) { + public DatabaseCompatibility(DatabaseVersion version, ServerMetadata server, Settings settings, PropertiesDao propertiesDao, AnalysisMode mode) { this.version = version; this.server = server; this.settings = settings; + this.propertiesDao = propertiesDao; this.analysisMode = mode; } @@ -51,7 +54,7 @@ public class DatabaseCompatibility implements BatchComponent { } private void checkCorrectServerId() { - if (!settings.getString(CoreProperties.SERVER_ID).equals(server.getServerId())) { + if (!propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID).getValue().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/test/java/org/sonar/batch/bootstrap/DatabaseCompatibilityTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseCompatibilityTest.java index 1d0c20675de..98c9391d5be 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseCompatibilityTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DatabaseCompatibilityTest.java @@ -28,6 +28,8 @@ import org.sonar.api.config.Settings; import org.sonar.api.database.DatabaseProperties; import org.sonar.api.utils.MessageException; import org.sonar.core.persistence.DatabaseVersion; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -40,6 +42,7 @@ public class DatabaseCompatibilityTest { DatabaseVersion databaseVersion; ServerMetadata server; Settings settings; + PropertiesDao propertiesDao; private AnalysisMode mode; @@ -52,7 +55,9 @@ public class DatabaseCompatibilityTest { settings = new Settings(); settings.setProperty(DatabaseProperties.PROP_URL, "jdbc:postgresql://localhost/foo"); settings.setProperty(DatabaseProperties.PROP_USER, "bar"); - settings.setProperty(CoreProperties.SERVER_ID, "123456"); + + propertiesDao = mock(PropertiesDao.class); + when(propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID)).thenReturn(new PropertyDto().setValue("123456")); mode = mock(AnalysisMode.class); @@ -66,7 +71,7 @@ public class DatabaseCompatibilityTest { thrown.expect(MessageException.class); thrown.expectMessage("Database relates to a more recent version of SonarQube. Please check your settings (JDBC settings, version of Maven plugin)"); - new DatabaseCompatibility(databaseVersion, server, settings, mode).start(); + new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start(); } @Test @@ -76,31 +81,31 @@ public class DatabaseCompatibilityTest { thrown.expect(MessageException.class); thrown.expectMessage("Database must be upgraded."); - new DatabaseCompatibility(databaseVersion, server, settings, mode).start(); + new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start(); } @Test public void shouldFailIfNotSameServerId() throws Exception { - settings.setProperty(CoreProperties.SERVER_ID, "11111111"); + when(propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID)).thenReturn(new PropertyDto().setValue("11111111")); thrown.expect(MessageException.class); thrown.expectMessage("The current batch process and the configured remote server do not share the same DB configuration."); thrown.expectMessage("- Batch side: jdbc:postgresql://localhost/foo (bar / *****)"); thrown.expectMessage("- Server side: check the configuration at http://localhost:9000/system"); - new DatabaseCompatibility(databaseVersion, server, settings, mode).start(); + new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start(); } @Test public void shouldUseDefaultUserNameWhenFaillingIfNotSameServerIdAndNoUserNameFound() throws Exception { - settings.setProperty(CoreProperties.SERVER_ID, "11111111"); + when(propertiesDao.selectGlobalProperty(CoreProperties.SERVER_ID)).thenReturn(new PropertyDto().setValue("11111111")); settings.removeProperty(DatabaseProperties.PROP_USER); thrown.expect(MessageException.class); thrown.expectMessage("- Batch side: jdbc:postgresql://localhost/foo (sonar / *****)"); - new DatabaseCompatibility(databaseVersion, server, settings, mode).start(); + new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start(); } @Test @@ -109,13 +114,13 @@ public class DatabaseCompatibilityTest { thrown.expect(IllegalStateException.class); - new DatabaseCompatibility(mock(DatabaseVersion.class), server, settings, mode).start(); + new DatabaseCompatibility(mock(DatabaseVersion.class), server, settings, propertiesDao, mode).start(); } @Test public void shouldDoNothingIfUpToDate() { when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE); - new DatabaseCompatibility(databaseVersion, server, settings, mode).start(); + new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start(); // no error } @@ -124,7 +129,7 @@ public class DatabaseCompatibilityTest { settings.setProperty(CoreProperties.SERVER_ID, "11111111"); when(mode.isPreview()).thenReturn(true); - new DatabaseCompatibility(databaseVersion, server, settings, mode).start(); + new DatabaseCompatibility(databaseVersion, server, settings, propertiesDao, mode).start(); // no failure }